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/replica.zig | 578 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 578 insertions(+) create mode 100644 src/replica.zig (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig new file mode 100644 index 00000000..42d94503 --- /dev/null +++ b/src/replica.zig @@ -0,0 +1,578 @@ +//! A session's replicas (docs/next-daemon.md, phase 5 part A): the authority +//! orders every input and records what its core consumed (src/tape.zig); +//! each replica runs a core of its own, applies its own inputs at once, and +//! takes the authority's stream as the truth. +//! +//! A replica's state is the confirmed position plus its own pending inputs, +//! applied on top (a speculation, recorded as the authority would record +//! it). For each unit of the authority's stream, in order: +//! - its own pending head, recorded the same (inputs, host calls and +//! answers): the head goes, nothing is redone (the fast path); +//! - else the rollback copy is put back (src/rollback.zig), the confirmed +//! units since it are replayed, then this one, and the pending inputs +//! are applied again. +//! A speculation that reaches a host call or a syscall (a Host function, +//! a `tape.site`) or an emulator (the authority's) stops: the copy is put +//! back and the input waits for the authority's answer, one RTT late but +//! never wrong. Clock and environment reads (`tape.value`) are made here, +//! and the comparison catches a guess the authority did not share. +//! +//! No transport: messages are bytes in and out. A client's input is its +//! id, a sequence number and the event as detached/wire.zig frames it; a +//! unit is the input's origin (0 for the authority's own: a pty's output, a +//! tick), its sequence number and the authority's tape of it. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const tape = @import("tape.zig"); +const rollback = @import("rollback.zig"); +const walk = @import("walk.zig"); +const wire = @import("detached/wire.zig"); +const Allocator = std.mem.Allocator; +const Pardes = pardes.Pardes; + +const checked = builtin.mode == .debug or builtin.mode == .safe; +const head_len = 6; // origin or client: u16le, seq: u32le +/// Confirmed units fast-pathed under one copy before it is taken afresh: +/// a rollback replays them all. +const rebase_after = 32; + +fn header(buf: []u8, who: u16, seq: u32) void { + std.mem.writeInt(u16, buf[0..2], who, .little); + std.mem.writeInt(u32, buf[2..6], seq, .little); +} + +pub const Unit = struct { origin: u16, seq: u32, 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..] }; +} + +pub const Input = struct { client: u16, seq: u32, event: pardes.Event }; + +/// A client's input message; its event borrows `msg`. +pub fn parseInput(msg: []const u8) ?Input { + if (msg.len < head_len) return null; + const f = (wire.framed(msg[head_len..]) catch return null) orelse return null; + const ev = switch (wire.decodeClient(f.tag, f.payload) catch return null) { + .event => |ev| ev, + else => return null, + }; + return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev }; +} + +/// The core whose tape is `t`, stepped: tape.on is process-wide, and so +/// are syntax.zig's held trees, so one process can step several. +const Stepping = struct { + tape: ?*tape.Tape, + trees: ?*pardes.syntax.HeldTrees, + + fn begin(t: *tape.Tape, epoch: ?*rollback.Epoch) Stepping { + const was: Stepping = .{ .tape = tape.on, .trees = pardes.syntax.held_trees }; + tape.on = t; + rollback.Epoch.select(epoch); + return was; + } + + fn end(was: Stepping) void { + tape.on = was.tape; + pardes.syntax.held_trees = was.trees; + } +}; + +fn nowNs() u64 { + return pardes.FrameStats.nowNs(); +} + +// ---- the authority ---- + +pub const Authority = struct { + gpa: Allocator, + t: tape.Tape, + core: *Pardes, + host: pardes.Host, + out: std.ArrayListUnmanaged(u8) = .empty, + seq: u32 = 0, + /// Each client's view (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), + + /// 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 { + // A replay gives its core every allocator (tape.zig), and a digest + // tells an allocator from none. + var opts = options; + opts.image_allocator = opts.image_allocator orelse gpa; + opts.pdf_allocator = opts.pdf_allocator orelse gpa; + opts.tree_sitter_allocator = opts.tree_sitter_allocator orelse gpa; + opts.frame_allocator = opts.frame_allocator orelse gpa; + const a = try gpa.create(Authority); + errdefer gpa.destroy(a); + a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + const was = Stepping.begin(&a.t, null); + defer was.end(); + a.core = try Pardes.init(gpa, opts); + a.core.host = host; + a.core.pump(host) catch {}; + return a; + } + + pub fn deinit(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.deinit(); + was.end(); + tape.release(&a.t); + a.out.deinit(a.gpa); + a.gpa.destroy(a); + } + + /// What was recorded since the last unit, as one (the boot's, a setup's + /// done straight on the core). + pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { + a.out.clearRetainingCapacity(); + try a.out.appendNTimes(a.gpa, 0, head_len); + header(a.out.items, origin, seq); + try a.out.appendSlice(a.gpa, tape.taken(&a.t)); + tape.clearTaken(&a.t); + return a.out.items; + } + + /// An input of the authority's own (a pty's output, a tick, a setup's + /// command), recorded into the unit being made. + pub fn apply(a: *Authority, ev: pardes.Event) void { + const was = Stepping.begin(&a.t, null); + a.core.update(ev); + a.core.pump(a.host) catch {}; + was.end(); + } + + /// One of the authority's own inputs, as its unit. + pub fn local(a: *Authority, ev: pardes.Event) ![]const u8 { + a.apply(ev); + a.seq += 1; + return 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 } { + const was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse { + was.end(); + return error.TooManyViews; + }; + a.core.pump(a.host) catch {}; + was.end(); + a.view_of[client] = k; + a.seq += 1; + return .{ .view = k, .unit = try a.unit(0, a.seq) }; + } + + /// 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 { + const in = parseInput(msg) orelse return error.BadInput; + 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 {}; + was.end(); + return a.unit(in.client, in.seq); + } +}; + +// ---- a replica ---- + +pub const Stats = struct { + inputs: u32 = 0, + /// Applied at once and clean. + speculated: u32 = 0, + /// Stopped at a host call, a syscall or an emulator: waits one RTT. + stopped: u32 = 0, + /// Not tried: an event of the view's (a resize, the pointer), phase 4's. + unspeculated: u32 = 0, + /// The authority's unit was the pending head, recorded the same. + fast: u32 = 0, + /// The copy put back: the head recorded otherwise (a guess of the clock + /// the authority did not share), or another's unit first. + mispredicted: u32 = 0, + foreign: u32 = 0, + rebases: u32 = 0, + units: u32 = 0, + /// Nanoseconds: one speculative apply, a copy taken, a rollback (the + /// copy put back, the confirmed units replayed, the pending reapplied). + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_bytes: u64 = 0, + copies: u32 = 0, + + pub fn deinit(s: *Stats, gpa: Allocator) void { + s.spec_ns.deinit(gpa); + s.copy_ns.deinit(gpa); + s.rollback_ns.deinit(gpa); + } +}; + +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null }; + +pub const Replica = struct { + gpa: Allocator, + id: u16, + /// Its own view of the session (Authority.openView's). + view: u8 = 0, + /// The core's allocator; its tapes and the engine's own lists are not. + epoch: rollback.Epoch = .{}, + t: tape.Tape, + copy: rollback.Copy = .{}, + pending: std.ArrayListUnmanaged(Pending) = .empty, + /// pending[0..applied] are applied on top of the confirmed position. + applied: usize = 0, + /// The pending input that waits for the authority (it stopped at a host + /// call, or is the view's), and those after it with it. + blocked: ?u32 = null, + /// Confirmed units fast-pathed since the copy was taken. + since: std.ArrayListUnmanaged([]u8) = .empty, + /// Pending inputs to apply again (after a rollback). + dirty: bool = false, + next_seq: u32 = 1, + stats: Stats = .{}, + diverged: ?[]const u8 = null, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds of the rollback being made, until its reapply. + recovering: ?u64 = null, + + /// A replica of the session whose first unit is `first`. + pub fn init(gpa: Allocator, id: u16, first: []const u8) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa) }; + r.t.core_gpa = r.epoch.allocator(); + r.confirm(first); + if (r.t.core == null) { + r.deinit(); + return error.NoCore; + } + return r; + } + + pub fn deinit(r: *Replica) void { + if (r.t.core) |c| { + const was = Stepping.begin(&r.t, &r.epoch); + if (r.copy.held) r.copy.restore(); + c.deinit(); + was.end(); + } + r.copy.deinit(); + for (r.pending.items) |p| r.freePending(p); + r.pending.deinit(r.gpa); + for (r.since.items) |u| r.gpa.free(u); + r.since.deinit(r.gpa); + r.out.deinit(r.gpa); + r.stats.deinit(r.gpa); + tape.release(&r.t); + if (r.diverged) |why| r.gpa.free(why); + r.epoch.deinit(); + r.gpa.destroy(r); + } + + pub fn core(r: *const Replica) *Pardes { + return r.t.core.?; + } + + fn freePending(r: *Replica, p: Pending) void { + r.gpa.free(p.msg); + if (p.spec) |s| r.gpa.free(s); + } + + /// The replica's own input: applied at once when it can be, and the + /// message for the authority returned (valid until the next call). + pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 { + r.stats.inputs += 1; + const seq = r.next_seq; + r.next_seq += 1; + const room = try r.gpa.alloc(u8, head_len + wire.clientBound(.{ .event = ev })); + defer r.gpa.free(room); + 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 }); + r.dirty = true; + r.settle(); + r.out.clearRetainingCapacity(); + try r.out.appendSlice(r.gpa, msg); + return r.out.items; + } + + /// One unit of the authority's stream, in its order. + pub fn confirm(r: *Replica, msg: []const u8) void { + if (r.diverged != null) return; + const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); + r.stats.units += 1; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and + tape.sameRecords(r.pending.items[0].spec.?, u.tape)) + { + r.stats.fast += 1; + r.freePending(r.pending.orderedRemove(0)); + r.applied -= 1; + if (r.applied == 0) { + // Nothing speculative on top any more: the state is the + // confirmed one. + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + return; + } + 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) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }) catch {}; + tape.describe(r.pending.items[0].spec.?, &w.writer); + w.writer.print("and the authority recorded:\n", .{}) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + r.recovering = nowNs(); + r.toConfirmed(); + } + 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); + defer w.deinit(); + w.writer.print("replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + return r.fail(why); + } + if (u.origin == r.id) { + if (r.pending.items.len == 0 or r.pending.items[0].seq != u.seq) return r.fail("the authority confirmed an input this replica did not send next"); + if (r.blocked == u.seq) r.blocked = null; + r.freePending(r.pending.orderedRemove(0)); + } + r.dirty = true; + } + + fn fail(r: *Replica, why: []const u8) void { + if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return; + } + + fn clearSince(r: *Replica) void { + for (r.since.items) |u| r.gpa.free(u); + r.since.clearRetainingCapacity(); + } + + /// Back to the confirmed position: the copy put back, the units fast- + /// pathed since replayed. + fn toConfirmed(r: *Replica) void { + if (r.copy.held) { + r.copy.restore(); + rollback.Epoch.select(null); + } + r.applied = 0; + for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); + r.clearSince(); + for (r.pending.items) |*p| if (p.spec) |s| { + r.gpa.free(s); + p.spec = null; + }; + } + + /// The pending inputs applied again, as far as they go. + pub fn settle(r: *Replica) void { + if (!r.dirty or r.diverged != null) return; + r.dirty = false; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + while (r.applied < r.pending.items.len and r.blocked != r.pending.items[r.applied].seq) if (!r.speculate()) break; + if (r.recovering) |t0| { + r.stats.rollback_ns.append(r.gpa, nowNs() - t0) catch {}; + r.recovering = null; + } + } + + /// pending[applied] applied on top: whether it was. + fn speculate(r: *Replica) bool { + const p = &r.pending.items[r.applied]; + 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(); + r.copy.take(&r.epoch, c) catch return false; + rollback.Epoch.select(&r.epoch); + r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + var terminals: [pardes.MAX_PANES]u64 = undefined; + if (checked) terminalDigests(c, &terminals); + const t0 = nowNs(); + tape.beginSpeculation(&r.t); + c.toView(r.view); + c.update(in.event); + c.pump(c.host) catch {}; + const clean = tape.endSpeculation(&r.t); + const dt = nowNs() - t0; + // Where a blob was written, should one be: the input that did. + if (checked and std.c.getenv("PARDES_REPLICA_TRACE") != null) r.copy.check(); + if (!clean) { + // Undone, and the earlier ones applied again on the confirmed + // position: this one waits for the authority. + r.stats.stopped += 1; + r.blocked = p.seq; + const upto = r.applied; + r.toConfirmed(); + while (r.applied < upto) if (!r.speculate()) break; + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + } + return false; + } + if (checked) { + var after: [pardes.MAX_PANES]u64 = undefined; + terminalDigests(c, &after); + if (!std.mem.eql(u64, &terminals, &after)) @panic("replica: a speculation wrote a terminal's emulator"); + } + r.stats.speculated += 1; + r.stats.spec_ns.append(r.gpa, dt) catch {}; + p.spec = r.gpa.dupe(u8, tape.taken(&r.t)) catch return false; + tape.clearTaken(&r.t); + r.applied += 1; + return true; + } + + /// Nothing pending and nothing applied: the replica is the authority's + /// confirmed state, which `digest` says. + pub fn quiet(r: *const Replica) bool { + return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held; + } + + pub fn digest(r: *Replica) u64 { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + return tape.digestOf(r.core()); + } +}; + +fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { + for (p.panes, out) |slot, *d| { + d.* = 0; + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + var h = walk.Hasher.init(0); + state.digest(&h); + d.* = h.final(); + } +} + +pub fn digestOf(a: *Authority) u64 { + const was = Stepping.begin(&a.t, null); + defer was.end(); + return tape.digestOf(a.core); +} + +const TestHost = struct { + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { + const text = [_]u8{@intCast(cp & 0x7f)}; + const msg = try r.input(.{ .key = .{ .cp = cp, .text = if (cp < 0x80 and cp >= 0x20) &text else "" } }); + try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); +} + +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()); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + for ("oabc") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + try std.testing.expectEqual(@as(usize, 5), r.applied); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(@as(u32, 5), r.stats.fast); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "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()); + 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); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first); + defer r2.deinit(); + var s1: std.ArrayListUnmanaged([]u8) = .empty; + var s2: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (s1.items) |m| gpa.free(m); + for (s2.items) |m| gpa.free(m); + s1.deinit(gpa); + s2.deinit(gpa); + } + for ("oone") |k| try key(r1, k, &s1); + for ("Otwo") |k| try key(r2, k, &s2); + // The authority hears r2 first, then r1; both hear the units in that + // order. + for ([_]*std.ArrayListUnmanaged([]u8){ &s2, &s1 }) |list| for (list.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + r2.confirm(u); + r2.settle(); + }; + for ([_]*Replica{ r1, r2 }) |r| { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + } + try std.testing.expect(r1.stats.foreign >= 1); + try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); +} -- 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/replica.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 411ac49dc8b7d901385caa05d1aefd4843de2a97 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:09:59 -0300 Subject: screen-state replicas: a flood's rows renumbered in place, not grown and trimmed by the thousand (a yes delta applies in 97 us, was 2.4 ms), holes blanked when they come into view, and input into a terminal left to the authority screen_state.zig: when a delta's scroll is more than the replica's screen and history hold, nothing it holds is in the new state; its rows stay where they are, renumbered, the active ones and the tail written from the delta, the rest holes, and only as many more rows grown as the authority's history is longer. A hole has whatever that row held before, so `blank` clears the holes in view before they are asked for. The bench's floods run under the C allocator, as a replica does. replica.zig: a replica's input whose live pane is a terminal is not speculated (its step reads the emulator, the last delta's here and the pty's newer one there): it waits for the authority, as a key in tty mode already did at its pty write. ReleaseFast, 200x60 yes at 16 MB/s: apply p50 97 us, p99 284 us; encode a frame 41 us at one peer and at three; 64 KB/s a peer. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 6 +++++ src/screen_state.zig | 76 +++++++++++++++++++++++++++++++++++++++------------- test/replicas.zig | 8 +++--- 3 files changed, 69 insertions(+), 21 deletions(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index e229bad5..a18391d4 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -622,6 +622,11 @@ pub const Replica = struct { const t0 = nowNs(); tape.beginSpeculation(&r.t); 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 + // there what the pty has said since. It waits; phase 6 predicts the + // 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 {}; const clean = tape.endSpeculation(&r.t); @@ -728,6 +733,7 @@ pub const Replica = struct { 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.rep.blank(&state.vt, want[0], want[1]); sc.asked = want; var range: [16]u8 = undefined; std.mem.writeInt(u64, range[0..8], want[0], .little); diff --git a/src/screen_state.zig b/src/screen_state.zig index 20ed7ad7..e91f1e14 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -518,13 +518,27 @@ pub const Replica = struct { 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)); const held = &r.held[0]; - if (k >= held.items.len) held.clearRetainingCapacity() else { - std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); - held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + if (k >= @as(u64, t.rows) + history) { + // A flood: nothing held here is in this state. The rows stay + // where they are, renumbered: the active ones written below, + // the tail too, the rest holes (blanked when they come into + // view, `blank`), not grown and trimmed by the thousand. As + // many more as the history is longer there, grown blank. + const have = primary.pages.total_rows - primary.pages.rows; + if (have < history) try scrollUp(primary, history - have); + held.clearRetainingCapacity(); + try held.appendNTimes(gpa, unknown, t.rows); + r.holes.clearRetainingCapacity(); + if (history > tail) try r.holes.append(gpa, .{ top - history, top - tail }); + } else { + try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); + if (k >= held.items.len) held.clearRetainingCapacity() else { + std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); + held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + } + if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); } - 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 @@ -542,8 +556,8 @@ pub const Replica = struct { const screen = t.screens.active; const held = &r.held[@intFromEnum(t.screens.active_key)]; - const blank = blankHash(t.cols); - while (held.items.len < nrows) try held.append(gpa, blank); + const blank_row = blankHash(t.cols); + while (held.items.len < nrows) try held.append(gpa, blank_row); held.shrinkRetainingCapacity(nrows); for (0..nrows) |y| { const h = std.mem.readInt(u64, hashes[y * 8 ..][0..8], .little); @@ -581,6 +595,19 @@ pub const Replica = struct { return .{ .seq = seq, .extra = extra }; } + /// The holes in `[lo, hi)` blank: until their rows come, a hole shows + /// nothing rather than what that row held before a flood renumbered it. + pub fn blank(r: *const Replica, t: *Terminal, lo: u64, hi: u64) void { + const primary = t.screens.get(.primary).?; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + for (r.holes.items) |hole| { + const a = @max(lo, hole[0], oldest); + const b = @min(hi, hole[1], r.top); + if (a >= b) continue; + primary.clearRows(.{ .screen = .{ .y = @intCast(a - oldest) } }, .{ .screen = .{ .y = @intCast(b - 1 - oldest) } }, false); + } + } + /// The holes in `[lo, hi)`: the rows to fetch before showing them. pub fn missing(r: *const Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void { for (r.holes.items) |hole| { @@ -679,6 +706,9 @@ fn scrollUp(screen: *Screen, k: u64) !void { screen.cursorReload(); } +/// No row's address: what a row whose content is not known is held as. +const unknown: Hash = std.math.maxInt(Hash); + /// `rowHash` of a row never written. fn blankHash(cols: u16) Hash { var h = std.hash.Wyhash.init(0); @@ -955,6 +985,7 @@ pub const Soak = struct { const hole = s.rep.holes.items[random.uintLessThan(usize, s.rep.holes.items.len)]; const lo = hole[0] + random.uintLessThan(u64, hole[1] - hole[0]); const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); + s.rep.blank(s.b, lo, hi); var out: std.Io.Writer.Allocating = .init(s.gpa); defer out.deinit(); try encodeRows(&s.enc, &s.track, s.a, lo, hi, &out.writer); @@ -1077,27 +1108,36 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { // 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. + // at the same base), each replica applying its own at once. Under the C + // allocator, as a replica's (the checking one costs an apply 4x). + try floods(gpa, std.heap.c_allocator, io, w); +} + +fn floods(gpa: std.mem.Allocator, ca: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { + var enc: Encoder = .init(ca); + defer enc.deinit(); + var out: std.Io.Writer.Allocating = .init(ca); + defer out.deinit(); 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| 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); + const a = try ca.create(Terminal); + defer ca.destroy(a); + a.* = try Terminal.init(io, ca, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer a.deinit(ca); var as = a.vtStream(); defer as.deinit(); - var tr: Track = .init(gpa); - defer tr.deinit(gpa, a); + var tr: Track = .init(ca); + defer tr.deinit(ca, 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 }); + 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| { - b.deinit(gpa); - r.deinit(gpa); + b.deinit(ca); + r.deinit(ca); }; var applies: std.ArrayList(u64) = .empty; defer applies.deinit(gpa); @@ -1124,7 +1164,7 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { 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); + peer.ack((try rep.apply(ca, io, b, out.written())).seq); try applies.append(gpa, @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0)); deltas += 1; wire += out.written().len; diff --git a/test/replicas.zig b/test/replicas.zig index f3618cf1..1acd1c11 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -383,8 +383,10 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { 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; + if (m.at >= flood_at and (flood_end == 0 or m.at <= flood_end)) { + totals.flood_delta_bytes += totals.delta_bytes - before; + try totals.frame_ns.append(gpa, screens.encode_ns - t0); + } // 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; @@ -537,7 +539,7 @@ pub fn main(init: std.process.Init) !void { }); 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("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}); if (failed) std.process.exit(1); } -- cgit v1.3 From 64184a16e58094f6b861cd1027920ddf21d9df0d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:20:35 -0300 Subject: screen-state replicas: a rollback never brings a terminal's past back: the rows the core keeps read of a terminal marked stale after a delta and after a copy is put back, and a test that a delta applied under a speculation outlives its rollback replica.zig: the core's cache of a terminal's rows (shell_rows) is invalidated by feedOutput, which a replica no longer calls; a delta and fetched rows now mark it, and so does a rollback, whose copy holds whatever rows were read before it was taken (a replica draws between steps). The test types into the text pane, lets the shell print under the held copy, lands its delta, reads the rows, and rolls back: the terminal is the authority's newest, the history as long, the rows the screen's. Without the stale mark it reads an older grid's rows; without the epoch's pause the rollback frees the emulator's pages and it crashes. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index a18391d4..92ee7b37 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -571,6 +571,9 @@ 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; } r.applied = 0; for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); @@ -703,6 +706,8 @@ pub const Replica = struct { r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; sc.refused = false; terminal.takeMarks(state, applied.extra); + // Its rows as the core keeps them read are another grid's. + c.shell_rows.markStale(pane); var seq: [4]u8 = undefined; std.mem.writeInt(u32, &seq, applied.seq, .little); return r.screenMessage(m.pane, m.serial, kind_ack, &seq); @@ -710,6 +715,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"); + c.shell_rows.markStale(pane); return null; }, else => return null, @@ -855,3 +861,105 @@ test "another client's input first: a replica rolls back, replays it and applies try std.testing.expect(r1.stats.foreign >= 1); try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); } + +fn screensTo(a: *Authority, screens: *Screens, peer: u16, r: *Replica) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try screens.flush(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + // Typing goes to the text pane under the shell (the host's write of + // `active`, taped). + a.core.active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Typed into the file, at once: a copy is held. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var line: [64]u8 = undefined; + for (0..3) |round| { + // The shell prints: its unit rolls the replica back and applies the + // key again; then its delta lands, under the new copy. + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); + r.confirm((try a.pending()).?); + r.settle(); + try std.testing.expect(r.copy.held); + try screensTo(a, &screens, peer, r); + // The rows the core keeps, read once under the copy. + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + } + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + gpa.free(m); + } + sent.clearRetainingCapacity(); + try screensTo(a, &screens, peer, r); + // Drawn (its rows read, between steps), then typed into: the copy keeps + // those rows. The shell prints, its delta lands under the copy, and then + // another's unit puts the copy back: the rows it kept are of an older + // grid and must not be read. + r.confirm(try a.local(.{ .focus = false })); + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + try key(r, 'x', &sent); + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try screensTo(a, &screens, peer, r); + r.confirm(try a.local(.{ .focus = true })); + r.settle(); + const n = a.core.panes[shell].?.rows; + const back = try terminal.shellRows(r.core(), r.core().panes[shell].?); + const now = try terminal.shellRows(a.core, a.core.panes[shell].?); + for (now[now.len - n ..], back[back.len - n ..]) |x, y| try std.testing.expectEqualStrings(x, y); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try screensTo(a, &screens, peer, r); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + var dump_a: std.Io.Writer.Allocating = .init(gpa); + defer dump_a.deinit(); + var dump_r: std.Io.Writer.Allocating = .init(gpa); + defer dump_r.deinit(); + try screen_state.dumpScreen(&a.core.panes[shell].?.terminal.?.vt, &dump_a.writer); + try screen_state.dumpScreen(&r.core().panes[shell].?.terminal.?.vt, &dump_r.writer); + try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); + const rows_a = try terminal.shellRows(a.core, a.core.panes[shell].?); + const rows_r = try terminal.shellRows(r.core(), r.core().panes[shell].?); + // As many rows (the history is the authority's to the row), the screen's + // the same; above it, rows scrolled past more than a screen at once are + // holes until fetched. + try std.testing.expectEqual(rows_a.len, rows_r.len); + const shown = a.core.panes[shell].?.rows; + for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y); +} -- 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/replica.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/replica.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 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/replica.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/replica.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 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/replica.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 6118ad214bb74744e7b38004b34e4487a6635be1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 01:51:44 -0300 Subject: replica clients' terminals as screen state: the session's Peers keep a Screens (rterm's), a peer each: the rows a pump's steps read before its unit, the deltas after it, and before each client input's step, the rows it read before its unit; a joiner's terminals afresh after its dump; acks, rows asked and resets from the client (0x04) taken at the boundary; units carry no pty bytes and a word of digest The client applies a delta or rows (0x06) in stream order and asks for the rows it shows and lacks after each frame; its dump's emulators are loaded on the C heap (walk.zig `.alloc` of a pane's `terminal`, dump.zig `terminal_gpa`), as `terminal.create` makes a screen replica's. Its load checked under its own tape (no emulator in the digest). tty.zig: with replica clients, a wait takes one step of a person's input and none after a pty's output (`split`): the next is held for the next wait, so a step reads the terminals as the deltas after each unit bring them. Screens: `dropPeer` (clients come and go), its numbers reused. Co-Authored-By: Claude Opus 5.5 --- src/dump.zig | 6 ++- src/panes.zig | 2 +- src/replica.zig | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++----- src/tty/tty.zig | 87 ++++++++++++++++++++++++++++++++++-------- src/walk.zig | 1 + 5 files changed, 185 insertions(+), 26 deletions(-) (limited to 'src/replica.zig') diff --git a/src/dump.zig b/src/dump.zig index cb639568..152495e2 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -129,6 +129,10 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { return null; } +/// What a load makes a terminal's emulator with, when not the loader's +/// own: a screen-state replica's are the C heap's (src/replica.zig). +pub var terminal_gpa: ?std.mem.Allocator = null; + // ---- a whole session to and from a dump (acme keeps this in rows.c) ---- // // A dump (src/walk.zig) is everything the session is but what is the @@ -231,7 +235,7 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: p.fs.deinit(gpa); gpa.destroy(p); } - l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa }; + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = terminal_gpa }; try l.into(Pardes, p); const read = l.shared.items.len; const mounts = try l.get([]const filesystem.Mount); diff --git a/src/panes.zig b/src/panes.zig index 64851787..20523de6 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -34,7 +34,7 @@ pub const Pane = struct { .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's and a PDF's memory is their own allocators' (a dump // loaded makes it there). - .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" } }, + .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" }, .{ "terminal", "terminal" } }, }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; diff --git a/src/replica.zig b/src/replica.zig index 5c965b2b..7bf0574f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -292,6 +292,7 @@ pub const Screens = struct { enc: screen_state.Encoder, tracks: [pardes.MAX_PANES]?Tracked = @splat(null), peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, + free_peers: std.ArrayListUnmanaged(u16) = .empty, out: std.ArrayListUnmanaged(u8) = .empty, /// Nanoseconds: what each flush spent encoding, for every peer. encode_ns: u64 = 0, @@ -311,6 +312,7 @@ pub const Screens = struct { s.gpa.destroy(p); } s.peers.deinit(s.gpa); + s.free_peers.deinit(s.gpa); s.out.deinit(s.gpa); s.enc.deinit(); } @@ -323,12 +325,21 @@ pub const Screens = struct { /// A new peer, with nothing yet: its first delta of each terminal /// starts it afresh. pub fn addPeer(s: *Screens) !u16 { + if (s.free_peers.pop()) |again| return again; const p = try s.gpa.create([pardes.MAX_PANES]PeerOf); p.* = @splat(.{}); try s.peers.append(s.gpa, p); return @intCast(s.peers.items.len - 1); } + /// Peer `peer` gone: what it was sent forgotten, its number free for + /// the next `addPeer` (a socket's clients come and go). + pub fn dropPeer(s: *Screens, peer: u16) void { + for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa); + s.peers.items[peer].* = @splat(.{}); + s.free_peers.append(s.gpa, peer) catch {}; + } + /// What `peer` is owed of every terminal, each a message onto `out` /// (owned): nothing for one it has as it is, or with `bound` in flight. pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void { @@ -558,6 +569,8 @@ pub const Replica = struct { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; r.t.headless = true; + // Its terminals are the session's deltas (Peers sends them). + r.t.screen_state = true; errdefer { tape.release(&r.t); r.epoch.deinit(); @@ -572,6 +585,10 @@ pub const Replica = struct { 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; + // Its emulators off the epoch, as `terminal.create` makes a screen + // replica's: deltas write them while a copy is held. + pardes.dump.terminal_gpa = terminal.emulator_gpa; + defer pardes.dump.terminal_gpa = 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 @@ -878,7 +895,7 @@ pub const Replica = struct { /// 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; + return r.digest() == want; } /// Its own view drawn and presented through `present`, between steps @@ -1109,6 +1126,8 @@ pub const Peer = struct { fd: c_int, id: u16, view: u8, + /// Its number in the session's `Screens`. + sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). seq: u32 = 0, reader: ?std.Thread = null, @@ -1181,9 +1200,13 @@ pub const Peer = struct { 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, + // Its word about a terminal (an ack, rows asked, a reset), to + // the session's Screens at the next boundary. + .screen_ack => { + const msg = peer.peers.gpa.dupe(u8, payload) catch return false; + peer.peers.post(.{ .screen = .{ .peer = peer, .msg = msg } }); + return true; + }, .check => { peer.peers.post(.{ .check = peer }); return true; @@ -1241,8 +1264,13 @@ pub const Peers = struct { boundary_ns: std.ArrayListUnmanaged(u64) = .empty, cut: u32 = 0, head: [unit_head]u8 = undefined, + /// Its clients' terminals as screen state (src/screen_state.zig), while + /// it records: what each was sent. + screens: Screens = undefined, + msgs: std.ArrayListUnmanaged([]u8) = .empty, - pub const Note = union(enum) { join: c_int, input: struct { peer: *Peer, msg: []u8 }, gone: *Peer, check: *Peer }; + pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer }; + pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { var n: usize = 0; @@ -1256,7 +1284,7 @@ pub const Peers = struct { ps.lock.unlock(ps.io); switch (note) { .join => |fd| _ = libc.close(fd), - .input => |in| ps.gpa.free(in.msg), + .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check => {}, } return; @@ -1298,8 +1326,12 @@ pub const Peers = struct { const t0 = nowNs(); defer if (ps.recording) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; if (ps.recording) { + // The rows the pump's steps read, then its unit, then what the + // terminals came to (the next steps read that). + ps.screensOut(core, .fill); ps.seq += 1; ps.unit(0, ps.seq); + ps.screensOut(core, .flush); } if (!ps.mail.load(.acquire)) return; ps.lock.lockUncancelable(ps.io); @@ -1312,6 +1344,13 @@ pub const Peers = struct { ps.input(core, in.peer, in.msg); ps.gpa.free(in.msg); }, + .screen => |in| { + defer ps.gpa.free(in.msg); + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + defer ps.gpa.free(reply); + const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); + in.peer.send(&.{ &head, reply }); + }, .gone => |peer| ps.leave(core, peer), .check => |peer| { var out: [16]u8 = undefined; @@ -1325,12 +1364,41 @@ pub const Peers = struct { const in = parseInput(msg) orelse return; const t0 = nowNs(); defer ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; + // The terminals as this step reads them, then the step, the rows + // it read, its unit (part B terminal notes' order). + ps.screensOut(core, .flush); core.toView(peer.view); core.update(in.event); core.step(); + ps.screensOut(core, .fill); ps.unit(peer.id, in.seq); } + const ScreensOut = enum { flush, fill }; + + /// What each client is owed of the terminals (`Screens.flush`), or the + /// rows a step read that it lacks (`Screens.fill`, then `filled`), each + /// a screen message queued for it. + fn screensOut(ps: *Peers, core: *Pardes, comptime what: ScreensOut) void { + for (ps.list) |slot| if (slot) |peer| ps.screensTo(core, peer, what); + if (what == .fill) ps.screens.filled(core); + } + + fn screensTo(ps: *Peers, core: *Pardes, peer: *Peer, comptime what: ScreensOut) void { + defer { + for (ps.msgs.items) |m| ps.gpa.free(m); + ps.msgs.clearRetainingCapacity(); + } + (switch (what) { + .flush => ps.screens.flush(core, peer.sid, &ps.msgs), + .fill => ps.screens.fill(core, peer.sid, &ps.msgs), + }) catch return; + for (ps.msgs.items) |m| { + const head = wire.header(@backingInt(wire.ServerTag.screen), m.len); + peer.send(&.{ &head, m }); + } + } + fn refuse(fd: c_int, why: []const u8) void { var out: [600]u8 = undefined; if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} @@ -1348,6 +1416,11 @@ pub const Peers = struct { 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; + // Terminals cross as screen state: a unit carries no pty bytes, + // and its digest is a word (PARDES_REPLICA_TRACE: one a field). + ps.t.screen_state = true; + ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; + ps.screens = .init(ps.gpa); tape.on = &ps.t; ps.recording = true; } @@ -1402,6 +1475,11 @@ pub const Peers = struct { return; } slot.* = peer; + // Its terminals afresh, after its dump and before any unit. + if (ps.screens.addPeer()) |sid| { + peer.sid = sid; + ps.screensTo(core, peer, .flush); + } else |_| _ = libc.shutdown(fd, 2); log.info("client {d} joined '{s}' as view {d}: a {d} KB dump 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 }); } @@ -1411,6 +1489,7 @@ pub const Peers = struct { slot.* = null; }; 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); if (ps.count() == 0) core.unattended = ps.alone_unattended; @@ -1438,7 +1517,7 @@ pub const Peers = struct { var i: usize = 0; while (i < ps.inbox.items.len) { const mine = switch (ps.inbox.items[i]) { - .input => |in| in.peer == peer, + .input, .screen => |in| in.peer == peer, .gone, .check => |p| p == peer, .join => false, }; @@ -1446,7 +1525,10 @@ pub const Peers = struct { i += 1; continue; } - if (ps.inbox.items[i] == .input) ps.gpa.free(ps.inbox.items[i].input.msg); + switch (ps.inbox.items[i]) { + .input, .screen => |in| ps.gpa.free(in.msg), + else => {}, + } _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(io); @@ -1465,6 +1547,7 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; + ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; tape.release(&ps.t); @@ -1478,13 +1561,14 @@ pub const Peers = struct { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); core.closeView(peer.view); + ps.screens.dropPeer(peer.sid); 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), + .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check => {}, }; ps.inbox.clearRetainingCapacity(); @@ -1498,6 +1582,7 @@ pub const Peers = struct { ps.taken.deinit(ps.gpa); ps.input_ns.deinit(ps.gpa); ps.boundary_ns.deinit(ps.gpa); + ps.msgs.deinit(ps.gpa); } fn writeStats(ps: *Peers, path: []const u8) void { @@ -1627,6 +1712,18 @@ pub fn sendInput(fd: c_int, msg: []const u8) bool { return writeAll(fd, msg[head_len..]); } +/// A message `Replica.screen` or `screenRequest` made, to the session. +pub fn sendScreen(fd: c_int, msg: []const u8) bool { + var buf: [64]u8 = undefined; + if (wire.header_len + msg.len > buf.len) { + const head = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + return writeAll(fd, &head) and writeAll(fd, msg); + } + buf[0..wire.header_len].* = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + @memcpy(buf[wire.header_len..][0..msg.len], msg); + return writeAll(fd, buf[0 .. wire.header_len + msg.len]); +} + /// Asks the session's digest, answered in stream order. pub fn askCheck(fd: c_int) bool { var out: [16]u8 = undefined; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 43994645..2eafc785 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -158,6 +158,8 @@ pub const Command = struct { replica_mail, /// A replica client's: what the session streamed (replica.Client). unit: []u8, + /// ...a terminal's delta or rows, before the units after it. + screen: []u8, /// The session's digest, asked by PARDES_REPLICA_CHECK. checked: u64, link_gone, @@ -1353,6 +1355,12 @@ const Remote = struct { core.present_skipped = skipped; core.needs_frame = needs; c.dirty = false; + // The history rows it shows and does not hold (a scroll back), + // asked for: blank until they come. + while (c.r.screenRequest()) |req| if (!replica.sendScreen(c.fd, req)) { + c.quit = true; + break; + }; } }; @@ -1371,10 +1379,12 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo const d = (wire.decodeServer(tag, payload) catch return false).check; l.postEvent(.{ .checked = d }) catch {}; }, + .screen => { + const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; + l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); + }, .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 => {}, + .pong, .welcome, .dump => {}, } return true; } @@ -1517,7 +1527,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se _ = libc.shutdown(joined.fd, 2); while (true) { while (loop.tryEvent() catch null) |ev| switch (ev) { - .unit => |b| std.heap.c_allocator.free(b), + .unit, .screen => |b| std.heap.c_allocator.free(b), .paste => |b| gpa.free(@constCast(b)), else => {}, }; @@ -2024,6 +2034,10 @@ const Shell = struct { /// speculations stand on (a replica takes such a unit as is). mail_only: bool = false, waited: bool = false, + /// This wait's steps so far, and the input held for the next (`split`). + took_input: bool = false, + took_output: bool = false, + held: ?@TypeOf(Command.value) = null, /// This process a replica client of a session elsewhere (`remote`): /// its inputs go to the replica, never straight to the core. remote: ?*Remote = null, @@ -2468,6 +2482,8 @@ const Shell = struct { } s.input_seen = false; s.output_seen = false; + s.took_input = false; + s.took_output = false; s.mail_only = true; defer s.waited = true; // Paced, what is not an input (a frame's ack, a pty's output) is @@ -2500,25 +2516,32 @@ const Shell = struct { fn waitOnce(s: *Shell, wait: u32) void { var batch: usize = 0; - if (wait != 0) { - // A virtual clock moves a frame a wake while anything moves - // continuously (the glide), as the real one would. - s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; - requestWake(s.io, wait); - } - pardes.turn.rest(); - const first = s.loop.nextEvent() catch { + const first = if (s.held) |held| held: { + s.held = null; + break :held held; + } else next: { + if (wait != 0) { + // A virtual clock moves a frame a wake while anything moves + // continuously (the glide), as the real one would. + s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; + requestWake(s.io, wait); + } + pardes.turn.rest(); + const got = s.loop.nextEvent() catch { + pardes.turn.wake(); + s.core.quit = true; + return s.reloadWatched(); + }; pardes.turn.wake(); - s.core.quit = true; - return s.reloadWatched(); + break :next got; }; - pardes.turn.wake(); batch = 1; // A run of queued motions is one move to its last cell: each would // otherwise redo hover and selection work the next one undoes. var motion: ?@TypeOf(Command.value) = null; var ev = first; while (true) { + if (s.split(ev)) break; if (ev == .mouse and (ev.mouse.type == .motion or ev.mouse.type == .drag)) { if (motion) |m| if (m.mouse.button != ev.mouse.button or m.mouse.type != ev.mouse.type or @as(u3, @bitCast(m.mouse.mods)) != @as(u3, @bitCast(ev.mouse.mods))) @@ -2542,6 +2565,31 @@ const Shell = struct { s.reloadWatched(); } + /// With replica clients, a wait (one pump, one unit) takes one step of + /// a person's input, and none after a pty's output: a step reads the + /// terminals as the clients' deltas bring them, sent after each unit + /// (docs/next-daemon.md, part B terminal notes). One that must wait is + /// held for the next. Whether `ev` is. + fn split(s: *Shell, ev: @TypeOf(Command.value)) bool { + if (!s.peers.recording) return false; + switch (ev) { + .pty_read => { + s.took_output = true; + return false; + }, + .key_press => if (s.in_paste) return false, + .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {}, + else => return false, + } + if (s.took_output or s.took_input) { + s.held = ev; + s.input_seen = true; + return true; + } + s.took_input = true; + return false; + } + fn apply(s: *Shell, event: @TypeOf(Command.value)) bool { const core = s.core; const tz_event = tracy.zone(@src(), "event"); @@ -2626,6 +2674,15 @@ const Shell = struct { } std.heap.c_allocator.free(bytes); }, + .screen => |bytes| { + if (s.remote) |c| { + if (c.r.screen(bytes)) |reply| if (!replica.sendScreen(c.fd, reply)) { + c.quit = true; + }; + c.dirty = 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; diff --git a/src/walk.zig b/src/walk.zig index 33247d42..2dc0a721 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -966,6 +966,7 @@ pub const DecodeError = error{ EndOfDump, BadDump, Incompatible, OutOfMemory }; pub const Allocators = struct { image: ?std.mem.Allocator = null, pdf: ?std.mem.Allocator = null, + terminal: ?std.mem.Allocator = null, }; fn allocOf(comptime T: type, comptime name: []const u8) ?[]const u8 { -- cgit v1.3 From f8a56c962d5103b84453d4305cd27973a71d4aa3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 02:01:30 -0300 Subject: replica clients join again: one that diverged (a digest the session's unit did not end on) and one the session let go to upgrade dial it again, the same join as the first, and say why in a line over their frame until a key; a client of another build is told "session upgraded to a newer pardes: upgrade this client too" tty.zig `remote` is a loop of joins over one terminal: the link ended, its reader joined, the replica let go, the session dialled again (an upgrade's exec and load, and its daemon's, retried for 20 s), its dump loaded, its view's size sent again. `Remote.notify` draws the line (`noticeLine`, the client's own, never the session's core). The welcome carries the session's semantics: the daemon judged the hello by the hash it last heard, which an upgrade under way may not have said yet. replica.zig `Peers.refuseAll`: before an Upgrade's dump each client is sent `refused` after what it was sent (up to 200 ms) and let go, its view with it. "A newer pardes is installed" is a Host call (`newer_build`) the core makes in its focus step, so replica clients take the line from the tape; the GUI still says it itself. PARDES_TEST_DIVERGE= puts a client's core off the session's once quiet after its n-th unit. test/daemon.py: the clients test has one rejoin and agree; the upgrade test has a client of the old build told to upgrade and one of the build running join again and agree. Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 6 +- src/host_io.zig | 3 + src/pardes.zig | 8 +- src/replica.zig | 36 +++++- src/tty/tty.zig | 305 ++++++++++++++++++++++++++++++++++++-------------- src/upgrade.zig | 15 ++- test/daemon.py | 56 ++++++++- 7 files changed, 332 insertions(+), 97 deletions(-) (limited to 'src/replica.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 347d45d1..78f6e76e 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -134,7 +134,7 @@ pub const ServerMsg = union(ServerTag) { /// 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 }, + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0 }, refused: []const u8, pong: u64, dump: []const u8, @@ -164,6 +164,7 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .dump_len = try r.takeInt(u64, .little), .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), + .semantics = try r.takeInt(u64, .little), } }, .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, .pong => .{ .pong = try r.takeInt(u64, .little) }, @@ -188,6 +189,7 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(u64, h.dump_len, .little); try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); + try w.writeInt(u64, h.semantics, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), @@ -620,7 +622,7 @@ 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', 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); + 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, 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); diff --git a/src/host_io.zig b/src/host_io.zig index c627d4ab..b1ed8b4b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -32,6 +32,9 @@ pub const Host = struct { /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + /// Whether a newer pardes is installed at the path this process + /// started from, to be said once (upgrade.zig): asked at a focus-in. + newer_build: ?*const fn (ctx: ?*anyopaque) bool = null, spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null, pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null, diff --git a/src/pardes.zig b/src/pardes.zig index cc55d695..6e01f8cd 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3670,6 +3670,9 @@ pub const CellPixels = if (pdf_enabled) struct { h: u16 = 16, } else struct {}; +/// Said at a focus-in when the Host's `newer_build` answers yes (upgrade.zig). +pub const newer_build_words = "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"; + pub const Event = union(enum) { key: Key, mouse: Mouse, @@ -6338,7 +6341,10 @@ pub const Pardes = struct { .command => |line| _ = p.executeBuiltinLine(p.active, line), .pinch => |scale| p.ov_pinch_scale = scale, .touch_scroll => |delta| p.ov_touch_scroll_delta = delta, - .focus => |focused| p.window_focused = focused, + .focus => |focused| { + p.window_focused = focused; + if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words); + }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), .pointer_leave => { p.pointer_inside = false; diff --git a/src/replica.zig b/src/replica.zig index 7bf0574f..95135307 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1140,6 +1140,8 @@ pub const Peer = struct { limit: usize = 64 << 20, closing: bool = false, cut: bool = false, + /// Its writer has ended (what it was sent, written). + done: std.atomic.Value(bool) = .init(false), /// `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 @@ -1169,6 +1171,7 @@ pub const Peer = struct { fn writeLoop(peer: *Peer) void { const io = peer.peers.io; + defer peer.done.store(true, .release); while (true) { peer.lock.lockUncancelable(io); while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); @@ -1457,7 +1460,7 @@ pub const Peers = struct { 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; + 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, .semantics = wire.semantics } }) catch unreachable; peer.send(&.{welcome}); var at: usize = 0; while (at < bytes.len) : (at += dump_piece) { @@ -1535,6 +1538,33 @@ pub const Peers = struct { ps.gpa.destroy(peer); } + /// Every client told `why` and let go, its view with it: an Upgrade's + /// exec is next (tty.zig `upgradeNow`). What each was sent goes first, + /// for up to 200 ms; a client then dials the session again. + pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void { + if (ps.count() == 0) return; + var out: [600]u8 = undefined; + const words = wire.encodeServer(&out, .{ .refused = why }) catch return; + const io = ps.io; + for (ps.list) |slot| if (slot) |peer| { + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + }; + const until = nowNs() + 200 * std.time.ns_per_ms; + while (nowNs() < until) { + const busy = for (ps.list) |slot| { + if (slot) |peer| if (!peer.done.load(.acquire)) break true; + } else false; + if (!busy) break; + const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + ps.dropAll(core); + } + /// 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 { @@ -1612,6 +1642,9 @@ pub const Joined = struct { dump: []u8, /// The session's digest as dumped (`Replica.loaded`). digest: u64 = 0, + /// Its build's (wire.semantics): the daemon judged the hello by what it + /// last heard, which an upgrade under way may not have said yet. + semantics: u64 = 0, /// What was read past the dump: the stream's (`readStream`). rest: Framer = .{}, }; @@ -1657,6 +1690,7 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J r.j.view = w.view; r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; + r.j.semantics = w.semantics; r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch { r.failed = "out of memory for the session's dump"; return false; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 2eafc785..e62da55c 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -160,6 +160,8 @@ pub const Command = struct { unit: []u8, /// ...a terminal's delta or rows, before the units after it. screen: []u8, + /// ...why it let this client go (an Upgrade's exec), the stream's end. + refused: []u8, /// The session's digest, asked by PARDES_REPLICA_CHECK. checked: u64, link_gone, @@ -1295,6 +1297,59 @@ const Remote = struct { /// frame shows the inputs after it too). mispredicted: u32 = 0, visible: u32 = 0, + /// What the session said as it let this client go (owned, the C heap's). + refused: ?[]u8 = null, + /// A line over its frame, until its next key (`notify`): why it joined + /// again. The client's own, never the session's core. + notice_buf: [240]u8 = undefined, + notice_len: usize = 0, + /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core + /// is put off the session's (a digested counter moved), the next unit's + /// digest finds it, and it joins again (test/daemon.py). + test_diverge: u32 = 0, + /// Put off so in this join: its checks are not written (they differ). + put_off: bool = false, + + /// The replica the dial's dump makes, checked against the session as + /// it was dumped; null, and why in `why`. The dump is let go. + fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8) ?*replica.Replica { + defer { + gpa.free(joined.dump); + joined.dump = &.{}; + } + const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { + _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; + return null; + }; + if (!r.loaded(joined.digest)) { + r.deinit(); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + return null; + } + return r; + } + + /// A join's link, from its start. + fn reset(c: *Remote, r: *replica.Replica, fd: posix.fd_t) void { + c.r = r; + c.fd = fd; + c.quit = false; + c.gone = false; + c.dirty = true; + c.owed = false; + c.changed = false; + c.asked = false; + c.mispredicted = 0; + c.put_off = false; + } + + fn notify(c: *Remote, comptime fmt: []const u8, args: anytype) void { + const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; + for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { + b.* = '?'; + }; + c.notice_len = text.len; + } /// PARDES_REPLICA_STATS=: how its inputs and the session's units /// went, a line at its end (test/daemon.py, the gates). @@ -1324,7 +1379,7 @@ const Remote = struct { fn checked(c: *Remote, theirs: u64) void { c.asked = false; const path = c.check orelse return; - if (!c.r.quiet()) return; + if (!c.r.quiet() or c.put_off) 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; @@ -1383,7 +1438,12 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); }, - .refused => return false, + .refused => { + const words = (wire.decodeServer(tag, payload) catch return false).refused; + const bytes = std.heap.c_allocator.dupe(u8, words) catch return false; + l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes); + return false; + }, .pong, .welcome, .dump => {}, } return true; @@ -1402,14 +1462,16 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo 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); + const Said = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var said: [800]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&said, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } }; - defer _ = libc.close(joined.fd); - defer joined.rest.deinit(gpa); + var why: [600]u8 = @splat(0); + var joined = replica.dial(gpa, path, session, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words}); const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); @@ -1424,19 +1486,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se 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 r = Remote.load(gpa, &joined, o, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); @@ -1449,7 +1499,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se 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); @@ -1458,6 +1507,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se 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); + if (init.environ_map.get("PARDES_TEST_DIVERGE")) |n| link.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; var sh: Shell = .{ .io = io, .gpa = gpa, @@ -1471,77 +1521,130 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se .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; + // A join a pass: the first, and one after the replica diverged or the + // session let it go to upgrade. The terminal stays as it is. + while (true) { + r.gmtoff = joined.gmtoff; + sh.core = r.core(); + sh.from = joined.view; + sh.terms = @splat(null); + sh.terms[joined.view] = &screen; + screen.feed.view = joined.view; + link.reset(r, joined.fd); + var reader_done: std.atomic.Value(bool) = .init(false); + const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); + // Its size is its view's first input. + screen.size = null; + sh.winched(); + screen.vx.queueRefresh(); + 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 != null) link.quit = true; } - 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 (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, .screen, .refused => |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(); + _ = libc.close(joined.fd); + joined.rest.deinit(gpa); + // Why it joins again, in a line over its frame until a key. 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); + link.notify("joined again: this client diverged from the session ({s})", .{reason}); + } else if (link.refused) |words| { + link.notify("joined again: {s}", .{words}); + std.heap.c_allocator.free(words); + link.refused = null; + } else { + r.deinit(); + if (link.gone) { + screen.leave(); + Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); + } + return; } - } - 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, .screen => |b| std.heap.c_allocator.free(b), - .paste => |b| gpa.free(@constCast(b)), - else => {}, + r.deinit(); + // The session again: an upgrade's takes the time its exec and load + // take, and its daemon's. + var tries: u32 = 0; + joined = while (true) : (tries += 1) { + why = @splat(0); + if (replica.dial(gpa, path, session, &why)) |j| { + if (j.semantics == wire.semantics) break j; + replica.bye(j.fd); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + screen.leave(); + Said.no(io, "{s}", .{upgrade_words}); + } + const words = std.mem.sliceTo(&why, 0); + if (std.mem.indexOf(u8, words, "runs another build") != null) { + screen.leave(); + Said.no(io, "{s}", .{upgrade_words}); + } + if (tries > 200) { + screen.leave(); + Said.no(io, "{s}", .{words}); + } + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + }; + r = Remote.load(gpa, &joined, o, &why) orelse { + screen.leave(); + Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; - 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 {}; } } +const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; + /// 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 @@ -2048,6 +2151,8 @@ const Shell = struct { /// (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); + // Its notice read: a key of its own puts it away. + if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; const msg = c.r.input(ev) catch return; if (!replica.sendInput(c.fd, msg)) c.quit = true; c.dirty = true; @@ -2093,6 +2198,7 @@ const Shell = struct { .detach = detach, .adopt = adopt, .upgrade = upgradeAsked, + .newer_build = newerBuild, .spawn = spawn, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -2268,6 +2374,12 @@ const Shell = struct { daemon.adopt(); } + /// A client's own build is not the session's to say (its core's host + /// is the session's, through the tape). + fn newerBuild(ctx: ?*anyopaque) bool { + return of(ctx).remote == null and upgrade.newer(); + } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { const s = of(ctx); if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); @@ -2301,6 +2413,9 @@ const Shell = struct { for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread); while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); core.settle(); + // Its replica clients told, their views gone before the dump: each + // dials the session again once it is back. + s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ @@ -2668,6 +2783,11 @@ const Shell = struct { .unit => |bytes| { if (s.remote) |c| { c.r.confirm(bytes); + if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { + c.test_diverge = 0; + c.put_off = true; + c.r.core().next_serial +%= 1000; + } if (c.r.moved) c.dirty = true; c.r.moved = false; c.changed = true; @@ -2684,17 +2804,18 @@ const Shell = struct { std.heap.c_allocator.free(bytes); }, .checked => |d| if (s.remote) |c| c.checked(d), + .refused => |words| if (s.remote) |c| { + if (c.refused) |old| std.heap.c_allocator.free(old); + c.refused = words; + c.quit = true; + return true; + } else std.heap.c_allocator.free(words), .link_gone => if (s.remote) |c| { c.quit = true; c.gone = true; return true; }, - .focus_in => { - 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_in => s.feed(.{ .focus = true }), .focus_out => { s.feed(.pointer_leave); s.feed(.{ .focus = false }); @@ -2887,6 +3008,7 @@ const Shell = struct { const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; paintCells(win, trailed.cells, trailed.cols, trailed.rows); + if (s.remote) |c| if (c.notice_len > 0) noticeLine(win, c.notice_buf[0..c.notice_len], trailed.cols, trailed.rows); tz_cells.end(); for (surface.images[0..surface.nimages]) |maybe| { const place = maybe orelse continue; @@ -3641,6 +3763,17 @@ test "a mouse's side buttons, xterm's 8 and 9, are Back and Forward, as SDL's X1 } } +/// A replica client's own line over its frame's last row (`Remote.notify`), +/// reversed. `text` is printable ASCII and outlives the render (vaxis keeps +/// the slices). +fn noticeLine(win: vaxis.Window, text: []const u8, cols: u16, rows: u16) void { + if (rows == 0) return; + for (0..cols) |x| win.writeCell(@intCast(x), rows - 1, .{ + .char = .{ .grapheme = if (x < text.len) text[x..][0..1] else " " }, + .style = .{ .reverse = true }, + }); +} + /// A blank whose ink alone changed is not sent: vaxis's render skips it /// (pardes's patch to Vaxis.zig in build.zig), since a blank's ink shows /// nowhere. Deciding that here cost every cell of every frame a look at the diff --git a/src/upgrade.zig b/src/upgrade.zig index 49e8aff5..37c79991 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -74,11 +74,18 @@ fn inodeOf(path: [:0]const u8) ?u64 { /// rebuilding (a stat). ponytail: explicit only, no watch and no automatic /// upgrade. pub fn noticeNewer(core: *pardes.Pardes) void { - const was = started_inode orelse return; - const now = inodeOf(exe() orelse return) orelse return; - if (now == was or told_inode == now) return; + if (newer()) core.setMessage(core.active, pardes.newer_build_words); +} + +/// Whether to say a newer build is installed now (`noticeNewer`): the +/// tty's Host answers the core's focus step with it, so its replica +/// clients take the line from the tape. +pub fn newer() bool { + const was = started_inode orelse return false; + const now = inodeOf(exe() orelse return false) orelse return false; + if (now == was or told_inode == now) return false; told_inode = now; - core.setMessage(core.active, "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"); + return true; } pub fn exe() ?[:0]const u8 { diff --git a/test/daemon.py b/test/daemon.py index b54b2b54..273c29e0 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -337,6 +337,26 @@ def clients(binary): 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)) + # One put off the session's state (PARDES_TEST_DIVERGE): the next + # unit's digest finds it, it says so in a line and joins again, and + # its keys after go in and it agrees. + td = Term(24, 90) + terms.append(td) + d = td.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'd.check'), PARDES_TEST_DIVERGE='1')) + started.append(d.pid) + wait_for('client D\'s first frame', lambda: b'line 3 of the notes' in td.text(), timeout=20) + os.write(td.master, b'jj') + time.sleep(.5) + # Another's unit, replayed on D's core: its digest finds it. + os.write(ta.master, b'jj') + wait_for('client D diverged and joined again', lambda: b'joined again' in td.text(), timeout=20) + assert d.poll() is None + seen = len(checks(root / 'd.check')) + os.write(td.master, b'Oafter-rejoin-D\x1b') + wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{pane}/body')) + wait_for('client D agrees after its rejoin', lambda: agreed(root / 'd.check', seen)) + os.write(td.master, b' sD') + wait_for('client D let go', lambda: d.poll() is not None, timeout=10) # Killed outright: the session and the other client carry on. os.kill(b.pid, signal.SIGKILL) b.wait(timeout=10) @@ -373,6 +393,7 @@ def clients(binary): 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 put off the session\'s state joined again and agreed {len(checks(root / "d.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: @@ -767,6 +788,12 @@ def upgrades(old, new): old_build = semantics(old) replica = Replica(root / 'pardes-replica.sock') assert replica.hello('work', old_build)[0] == 'welcome' + # A replica client of the old build: let go at the upgrade, it dials + # again and is told to upgrade too. + term3 = Term(24, 90) + stale = term3.spawn([str(old), f'--attach={root / "pardes-replica.sock"}:work'], env) + started.append(stale.pid) + wait_for('the old build\'s client drawn', lambda: b'\x1b[?1049h' in term3.text() and b'one' in term3.text()) before = len(term.text()), len(term2.text()) install(new, binary) @@ -794,16 +821,23 @@ def upgrades(old, new): else: wait_for('the daemon exec\'d again', lambda: any(a.startswith(b'--upgraded=') for a in cmdline(daemon))) assert alive(daemon) and stub.poll() is None and stub2.poll() is None - # A replica of the old build is told why it is let go. + # A replica of the old build is told why it is let go, after what it + # was sent (units, terminals). tag, body = replica.read() + while tag in (0x05, 0x06, 0x07): + tag, body = replica.read() assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) replica.close() + stale.wait(timeout=30) + assert b'upgrade this client too' in term3.text(), term3.text()[-400:] + term3.close() + print('upgrade: a replica client of the old build was let go, dialled again and was told to upgrade too') # The old image's prompt files swept, the new one's made. wait_for('the prompt files level', lambda: len(list(Path('/tmp').glob(f'pardes-osc133-*-{session}-*'))) == len(rcs)) assert sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) != rcs or not rcs # Nothing kept that was not: the old binary let go, every fd back. - # (The replica's connection is the one fewer.) - level = fds_before[0] - 1, fds_before[1] + # (The replicas' connections are two fewer.) + level = fds_before[0] - 2, fds_before[1] try: wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level, timeout=5) except TimeoutError: @@ -842,10 +876,26 @@ def upgrades(old, new): install(refuser, binary) if procfs: running = os.stat(f'/proc/{session}/exe').st_ino + # A replica client of the build running: let go at the upgrade, + # it joins again once the session is back, says so, and agrees. + term4 = Term(24, 90) + again = term4.spawn([str(new), f'--attach={root / "pardes-replica.sock"}:work'], + dict(env, PARDES_REPLICA_CHECK=str(root / 'e.check'))) + started.append(again.pid) + wait_for('the client of the build running drawn', lambda: b'\x1b[?1049h' in term4.text() and b'one' in term4.text()) refusing('1') client.write(f'/pane/{shell}/exec', b'Upgrade\n') client.socket.close() client = saying(sock, 'it goes on as') + wait_for('the client joined again', lambda: b'joined again: the session is upgrading' in term4.text(), timeout=30) + assert again.poll() is None + seen = len(checks(root / 'e.check')) + os.write(term4.master, b'gg') + wait_for('the client agrees after it joined again', lambda: agreed(root / 'e.check', seen), timeout=20) + again.send_signal(signal.SIGHUP) + again.wait(timeout=10) + term4.close() + print('upgrade: a replica client of the build running joined again after the upgrade, said so and agreed') assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' -- cgit v1.3 From 44587446c2478de276b7c5c5a12063e6c7946b4e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 02:17:56 -0300 Subject: remote client: predicted echo (src/echo.zig) over its own view, judged a frame at a time and woken while predictions are out; PARDES_NO_ECHO turns it off; a session's step that reads whether a terminal is at its prompt after its output (the first prompt's greeting, a command waiting for the prompt) is taped (`.prompt`), as a screen replica's emulator is the last delta's until the next Found by a client opening a shell of its own (` nt`): the session's layout after the shell's first output saw the prompt and typed its greeting (`ls`, after a pty resize), and the client's replay of it, its emulator a delta behind, did not: it diverged at once. At 207 ms through the relay (Debug): a key into a shell on the client's screen p50 9.1 ms with prediction, 213.6 ms without. Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 2 +- src/replica.zig | 2 ++ src/tape.zig | 4 ++++ src/terminal.zig | 2 +- src/tty/tty.zig | 10 ++++++++-- 5 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src/replica.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 6e01f8cd..2b1fae61 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7873,7 +7873,7 @@ pub const Pardes = struct { // its shell's FIRST prompt, or never: typed later it would land // in whatever runs then, or in the middle of a line someone // typed (panes.terminal.greeting). - if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (panes.terminal.greeting(pane)) { + if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (tape.value(.prompt, panes.terminal.greeting, .{pane})) { .wait => {}, .never => pane.greet = false, .now => { diff --git a/src/replica.zig b/src/replica.zig index 95135307..8d8d47c5 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -598,6 +598,8 @@ pub const Replica = struct { c.in_len = 0; c.in_head = 0; tape.adoptCore(&r.t, c); + // Its keys into a terminal drawn at once (src/echo.zig). + c.echo = &r.echo; return r; } diff --git a/src/tape.zig b/src/tape.zig index b30096f5..f0766bd1 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -91,6 +91,10 @@ pub const Site = enum(u16) { pointer, /// What a terminal's ingest told the positions pardes keeps over it. positions, + /// Whether a terminal is at its prompt, as a step reads it after its + /// output (a greeting, a command waiting for the prompt): a + /// screen-state replica's emulator is the last delta's until the next. + prompt, }; /// Fields a host writes straight into the core between its calls. diff --git a/src/terminal.zig b/src/terminal.zig index 3a589659..2bc96a6e 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -463,7 +463,7 @@ pub fn shellSpawned(p: *Pardes, id: usize, prompt_marks: bool) void { pub fn releasePendingCommandIfReady(p: *Pardes, id: usize, pane: *Pane) void { if (pane.pending_command.wait == .input) - releasePendingCommand(p, id, pane, promptInputReady(pane)); + releasePendingCommand(p, id, pane, pardes.tape.value(.prompt, promptInputReady, .{pane})); } fn releasePendingCommand(p: *Pardes, id: usize, pane: *Pane, ready: bool) void { diff --git a/src/tty/tty.zig b/src/tty/tty.zig index e62da55c..b4eb6be9 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1321,6 +1321,8 @@ const Remote = struct { _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; return null; }; + // PARDES_NO_ECHO: no predicted echo (the gates' baseline). + if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; if (!r.loaded(joined.digest)) { r.deinit(); _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; @@ -1525,7 +1527,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se try startInput(&loop, &screen); defer stopInput(io, &screen.thread); (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach(); - if (link.check != null) (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); + (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); try screen.vx.queryTerminalSend(screen.tty.writer()); // A join a pass: the first, and one after the replica diverged or the // session let it go to upgrade. The terminal stays as it is. @@ -1544,6 +1546,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se sh.winched(); screen.vx.queueRefresh(); while (!link.quit) { + // Its predicted echo judged and caught up, once a frame. + if (r.echoFrame()) link.dirty = true; 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. @@ -1565,7 +1569,9 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se screen.vx.queueRefresh(); link.dirty = true; } - if (link.owed) link.dirty = true; + if (link.owed or r.echo.changed) link.dirty = true; + // Predictions out: judged a frame at a time while they are. + if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) requestWake(io, @intCast(pardes.animation.frame_ns / std.time.ns_per_ms)); if (link.check != null) { // Quiet a while after a change: asked once (a wake, then the // next quiet one). -- cgit v1.3 From c892cf0c3246b6c41f8f67810d360c5ea0ff66ef Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 02:34:25 -0300 Subject: a replica client's join costs the session's step its mutable state alone: the dump leaves the blobs out (walk.zig `Dump.deferred`: a `.blob`'s texts of 4 KiB and up, noted where they go) and its terminals' emulators (a size: the first delta brings each), the client's writer thread writes the welcome and the dump with the blobs put back in place, and each blob is pinned until written (memory.zig `pin`: its free put off); the texts the session never digested are hashed first on a thread of their own (walk.zig `unhashed`, `makeWords`), the join waiting a boundary for them The client is unchanged: what it reads is the dump `dumpBytes` makes, byte for byte (a test splices one and compares). ReleaseFast, the session's worst boundary while three clients join one after another (`t2/join2.py`), before and after: typical (4.1 MB of files, 1 MB of scrollback, an 8.1 MB dump) 12.4 to 1.6 ms; big (38.6 MB, 8 MB, 76 MB) 51.3 to 1.5 ms; a 100 MB file open beside typical's (254 MB) 172.5 to 1.6 ms. Of the dump the step makes 113 to 122 KB. PARDES_REPLICA_STATS says each join's time and the state's size. Co-Authored-By: Claude Opus 5.5 --- src/dump.zig | 19 +++++ src/memory.zig | 79 +++++++++++++++++++++ src/replica.zig | 207 ++++++++++++++++++++++++++++++++++++++++++++++++------- src/terminal.zig | 49 ++++++++----- src/walk.zig | 55 +++++++++++++++ 5 files changed, 365 insertions(+), 44 deletions(-) (limited to 'src/replica.zig') diff --git a/src/dump.zig b/src/dump.zig index 152495e2..c64f83c4 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -174,8 +174,27 @@ pub fn dumpState(p: *Pardes) !void { /// The session as a dump, gpa-owned. pub fn dumpBytes(p: *Pardes) ![]u8 { + return dumpWith(p, null); +} + +/// A dump for a screen-state replica's join (src/replica.zig): its blobs' +/// bytes left out and noted in `deferred` (they are the session's, alive +/// while pinned: memory.zig `pin`), its terminals' emulators left out (the +/// first delta brings each afresh). What is left is the session's mutable +/// state, made in its step. +pub fn dumpJoin(p: *Pardes, deferred: *std.ArrayListUnmanaged(walk.Deferred)) ![]u8 { + slim_terminals = true; + defer slim_terminals = false; + return dumpWith(p, deferred); +} + +/// `dumpJoin`'s: a terminal dumps its size, not its emulator. +pub var slim_terminals = false; + +fn dumpWith(p: *Pardes, deferred: ?*std.ArrayListUnmanaged(walk.Deferred)) ![]u8 { var d = try Session.init(p.gpa); errdefer d.deinit(); + d.deferred = deferred; try d.put(Pardes, p); const mounts: []const filesystem.Mount = p.fs.mounts.items; try d.put([]const filesystem.Mount, &mounts); diff --git a/src/memory.zig b/src/memory.zig index 7adcf914..cd29308f 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -422,6 +422,7 @@ pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { if (bytes.len == 0) return; // A blob's digest word goes with it (walk.zig). walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(u8))) return; allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); } @@ -433,9 +434,72 @@ pub fn freeRaw(allocator: Allocator, items: anytype) void { const bytes = std.mem.sliceAsBytes(items); if (bytes.len == 0) return; walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(E))) return; allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); } +// ---- a join's pins (src/replica.zig) ---- +// +// A replica client's join dumps the session's mutable state in its step and +// leaves its blobs out (walk.Deferred); its writer thread streams them after. +// A blob is never written in place and is freed raw (above), so until the +// writer has sent it, its free is put off: `pin` it, `unpin` it after. +var pins_lock: std.atomic.Value(bool) = .init(false); +var pins: std.AutoHashMapUnmanaged(usize, u32) = .empty; +var pin_count: std.atomic.Value(usize) = .init(0); +const PutOff = struct { allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment }; +var put_off: std.ArrayListUnmanaged(PutOff) = .empty; + +fn lockPins() void { + while (pins_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); +} + +/// The blob at `bytes.ptr` kept alive until it is unpinned as many times. +pub fn pin(bytes: []const u8) void { + lockPins(); + defer pins_lock.store(false, .release); + const got = pins.getOrPut(std.heap.page_allocator, @intFromPtr(bytes.ptr)) catch return; + got.value_ptr.* = if (got.found_existing) got.value_ptr.* + 1 else 1; + pin_count.store(pins.count(), .release); +} + +/// One pin of the blob at `bytes.ptr` let go: its free, put off, made now +/// the last one goes. +pub fn unpin(bytes: []const u8) void { + const addr = @intFromPtr(bytes.ptr); + lockPins(); + const n = pins.getPtr(addr) orelse { + pins_lock.store(false, .release); + return; + }; + n.* -= 1; + var due: ?PutOff = null; + if (n.* == 0) { + _ = pins.remove(addr); + pin_count.store(pins.count(), .release); + for (put_off.items, 0..) |f, i| if (@intFromPtr(f.bytes.ptr) == addr) { + due = put_off.swapRemove(i); + break; + }; + } + pins_lock.store(false, .release); + if (due) |f| { + // A word made for it while it was pinned (walk.makeWords) goes too. + walk.forget(addr); + f.allocator.rawFree(@constCast(f.bytes), f.alignment, @returnAddress()); + } +} + +/// Whether the free of `bytes` is put off (it is pinned). +fn pinned(allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment) bool { + if (pin_count.load(.acquire) == 0) return false; + lockPins(); + defer pins_lock.store(false, .release); + if (!pins.contains(@intFromPtr(bytes.ptr))) return false; + put_off.append(std.heap.page_allocator, .{ .allocator = allocator, .bytes = bytes, .alignment = alignment }) catch return false; + return true; +} + /// Gives free memory back to the system, the arenas' and libc's, inside its /// heap too: after a big result is formatted or a big pane torn down, the /// tree-sitter arena kept 73 MiB of emptied buckets resident and glibc what @@ -568,3 +632,18 @@ test "memory limits preserve desktop capacities" { try std.testing.expectEqual(256 * MiB, limits.arena.image); try std.testing.expectEqual(if (config.mupdf) 512 * MiB else 64 * KiB, limits.arena.pdf); } + +test "a pinned blob's free is put off until its last pin goes" { + const gpa = std.testing.allocator; + const b = try gpa.alloc(u8, 8192); + @memset(b, 7); + pin(b); + pin(b); + freeUnfilled(gpa, b); + // Still the blob's: a join's writer reads it. + try std.testing.expectEqual(@as(u8, 7), b[100]); + unpin(b); + try std.testing.expectEqual(@as(u8, 7), b[8000]); + // The last pin: freed now (the testing allocator says if not). + unpin(b); +} diff --git a/src/replica.zig b/src/replica.zig index 8d8d47c5..2e14cb6f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1071,6 +1071,17 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } +/// `bytes` as dump pieces (1 MiB at most each). +fn writePieces(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + const piece = bytes[at..@min(bytes.len, at + dump_piece)]; + const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len); + if (!writeAll(fd, &head) or !writeAll(fd, piece)) return false; + } + return true; +} + fn readSome(fd: c_int, buf: []u8) usize { while (true) { const n = libc.read(fd, buf.ptr, buf.len); @@ -1144,6 +1155,24 @@ pub const Peer = struct { cut: bool = false, /// Its writer has ended (what it was sent, written). done: std.atomic.Value(bool) = .init(false), + /// Its join, which its writer writes before what it is sent: the + /// welcome, then the dump with the session's blobs put back where they + /// go, pinned until written (memory.zig `pin`). The core's allocator's. + welcome: [wire.hello_max + 64]u8 = undefined, + welcome_len: usize = 0, + dump: []u8 = &.{}, + blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, + dump_gpa: Allocator = undefined, + + fn writeJoin(peer: *Peer) bool { + if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + var pos: usize = 0; + for (peer.blobs.items) |b| { + if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; + pos = b.at; + } + return writePieces(peer.fd, peer.dump[pos..]); + } /// `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 @@ -1174,6 +1203,12 @@ pub const Peer = struct { fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); + if (!peer.writeJoin()) { + _ = libc.shutdown(peer.fd, 2); + return; + } + // Its pins let go at the next boundary. + peer.peers.post(.{ .joined = peer }); while (true) { peer.lock.lockUncancelable(io); while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); @@ -1261,7 +1296,10 @@ pub const Peers = struct { alone_unattended: bool = false, /// The worst a join cost the session's step, and the last one's dump. join_ns: u64 = 0, + each_join: std.ArrayListUnmanaged(u64) = .empty, dump_bytes: usize = 0, + /// ...of it the state the step dumped (the rest, blobs, the writer's). + state_bytes: usize = 0, /// What each client input cost the session (its step, its unit sent), /// and each boundary (its pump's unit sent, what came taken in), ns: /// PARDES_REPLICA_STATS writes them at the session's end. @@ -1273,8 +1311,14 @@ pub const Peers = struct { /// it records: what each was sent. screens: Screens = undefined, msgs: std.ArrayListUnmanaged([]u8) = .empty, - - pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer }; + /// Clients waiting to join while the session's texts are hashed + /// (`joins`), the texts and the thread hashing them. + held: std.ArrayListUnmanaged(c_int) = .empty, + texts: std.ArrayListUnmanaged([]const u8) = .empty, + hasher: ?std.Thread = null, + rounds: u8 = 0, + + pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { @@ -1290,7 +1334,7 @@ pub const Peers = struct { switch (note) { .join => |fd| _ = libc.close(fd), .input, .screen => |in| ps.gpa.free(in.msg), - .gone, .check => {}, + .gone, .check, .joined, .hashed => {}, } return; }; @@ -1344,7 +1388,17 @@ pub const Peers = struct { ps.mail.store(false, .release); ps.lock.unlock(ps.io); for (ps.taken.items) |note| switch (note) { - .join => |fd| ps.join(core, fd), + .join => |fd| { + ps.held.append(ps.gpa, fd) catch { + refuse(fd, "out of memory"); + continue; + }; + ps.joins(core); + }, + .hashed => { + ps.unhashedDone(); + ps.joins(core); + }, .input => |in| { ps.input(core, in.peer, in.msg); ps.gpa.free(in.msg); @@ -1357,6 +1411,7 @@ pub const Peers = struct { in.peer.send(&.{ &head, reply }); }, .gone => |peer| ps.leave(core, peer), + .joined => |peer| released(peer), .check => |peer| { var out: [16]u8 = undefined; peer.send(&.{wire.encodeServer(&out, .{ .check = tape.digestOf(core) }) catch unreachable}); @@ -1410,9 +1465,48 @@ pub const Peers = struct { _ = libc.close(fd); } + /// The clients waiting to join, joined, once the texts the session + /// never digested have their words (`walk.unhashed`): those are hashed + /// on a thread of their own, pinned meanwhile, and the joins wait a + /// boundary or two for them, the session's step never. A second round + /// joins whatever is left. + fn joins(ps: *Peers, core: *Pardes) void { + if (ps.hasher != null or ps.held.items.len == 0) return; + if (ps.rounds < 2) { + walk.unhashed(Pardes, core, &ps.texts); + if (ps.texts.items.len > 0) { + for (ps.texts.items) |b| pardes.memory.pin(b); + if (std.Thread.spawn(.{}, hashTexts, .{ps})) |t| { + ps.hasher = t; + ps.rounds += 1; + return; + } else |_| ps.unhashedDone(); + } + } + ps.rounds = 0; + for (ps.held.items) |fd| ps.join(core, fd); + ps.held.clearRetainingCapacity(); + } + + fn hashTexts(ps: *Peers) void { + walk.makeWords(ps.texts.items); + ps.post(.hashed); + } + + /// The hashing thread joined, its texts unpinned. + fn unhashedDone(ps: *Peers) void { + if (ps.hasher) |t| t.join(); + ps.hasher = null; + for (ps.texts.items) |b| pardes.memory.unpin(b); + ps.texts.clearRetainingCapacity(); + } + fn join(ps: *Peers, core: *Pardes, fd: c_int) void { const t0 = nowNs(); - defer ps.join_ns = @max(ps.join_ns, nowNs() - t0); + defer { + ps.join_ns = @max(ps.join_ns, nowNs() - t0); + ps.each_join.append(ps.gpa, nowNs() - t0) catch {}; + } const slot = for (&ps.list) |*s| { if (s.* == null) break s; } else return refuse(fd, "that session has as many clients as it takes"); @@ -1440,7 +1534,12 @@ pub const Peers = struct { core.undrawn |= @as(u8, 1) << @intCast(k); ps.seq += 1; ps.unit(0, ps.seq); - const bytes = pardes.dump.dumpBytes(core) catch |err| { + // The session's mutable state, dumped now; its blobs (file texts, + // undo, registers) left out, pinned, and streamed by the client's + // writer thread: the step pays for the state, not for the text. + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const bytes = pardes.dump.dumpJoin(core, &blobs) catch |err| { + blobs.deinit(core.gpa); core.undrawn &= ~(@as(u8, 1) << @intCast(k)); core.closeView(k); ps.unit(0, ps.seq); @@ -1448,28 +1547,29 @@ pub const Peers = struct { 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; + var whole = bytes.len; + for (blobs.items) |b| { + pardes.memory.pin(b.bytes); + whole += b.bytes.len; + } + ps.dump_bytes = whole; + ps.state_bytes = bytes.len; // The joiner's core is the dump's: the host this tape wraps, and // what the host wrote in the core, are said again at the next input. tape.resync(&ps.t); - const peer = ps.gpa.create(Peer) catch return refuse(fd, "out of memory"); + const peer = ps.gpa.create(Peer) catch { + for (blobs.items) |b| pardes.memory.unpin(b.bytes); + blobs.deinit(core.gpa); + core.gpa.free(bytes); + return 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 }; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; ps.next_id +%= 1; if (ps.next_id == 0) ps.next_id = 1; - 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, .semantics = wire.semantics } }) 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.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable).len; peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; if (peer.writer == null or peer.reader == null) { @@ -1485,7 +1585,7 @@ pub const Peers = struct { peer.sid = sid; ps.screensTo(core, peer, .flush); } else |_| _ = libc.shutdown(fd, 2); - log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, bytes.len >> 10, (nowNs() - t0) / 1000 }); + log.info("client {d} joined '{s}' as view {d}: a {d} KB dump ({d} KB of state) in {d} us", .{ peer.id, ps.name, k, whole >> 10, bytes.len >> 10, (nowNs() - t0) / 1000 }); if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq }); } @@ -1514,6 +1614,7 @@ pub const Peers = struct { _ = libc.shutdown(peer.fd, 2); if (peer.writer) |w| w.join(); if (peer.reader) |r| r.join(); + released(peer); _ = libc.close(peer.fd); peer.out.deinit(ps.gpa); peer.sending.deinit(ps.gpa); @@ -1523,8 +1624,8 @@ pub const Peers = struct { while (i < ps.inbox.items.len) { const mine = switch (ps.inbox.items[i]) { .input, .screen => |in| in.peer == peer, - .gone, .check => |p| p == peer, - .join => false, + .gone, .check, .joined => |p| p == peer, + .join, .hashed => false, }; if (!mine) { i += 1; @@ -1567,6 +1668,15 @@ pub const Peers = struct { ps.dropAll(core); } + /// A join's dump and pins let go: written, or its client gone. + fn released(peer: *Peer) void { + for (peer.blobs.items) |b| pardes.memory.unpin(b.bytes); + peer.blobs.deinit(peer.dump_gpa); + peer.blobs = .empty; + if (peer.dump.len > 0) peer.dump_gpa.free(peer.dump); + peer.dump = &.{}; + } + /// 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 { @@ -1589,6 +1699,10 @@ pub const Peers = struct { /// Every client let go (the session's end, a Restore): each sees its /// link end. pub fn dropAll(ps: *Peers, core: *Pardes) void { + ps.unhashedDone(); + for (ps.held.items) |fd| _ = libc.close(fd); + ps.held.clearRetainingCapacity(); + ps.rounds = 0; for (&ps.list) |*slot| if (slot.*) |peer| { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); @@ -1601,7 +1715,7 @@ pub const Peers = struct { for (ps.inbox.items) |note| switch (note) { .join => |fd| _ = libc.close(fd), .input, .screen => |in| ps.gpa.free(in.msg), - .gone, .check => {}, + .gone, .check, .joined, .hashed => {}, }; ps.inbox.clearRetainingCapacity(); ps.lock.unlock(ps.io); @@ -1615,20 +1729,29 @@ pub const Peers = struct { ps.input_ns.deinit(ps.gpa); ps.boundary_ns.deinit(ps.gpa); ps.msgs.deinit(ps.gpa); + ps.held.deinit(ps.gpa); + ps.texts.deinit(std.heap.c_allocator); + ps.each_join.deinit(ps.gpa); } fn writeStats(ps: *Peers, path: []const u8) void { var buf: [400]u8 = undefined; - const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} cut {d}\n", .{ + const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} state_kb {d} cut {d}\n", .{ ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000, ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000, - ps.join_ns / 1000, ps.dump_bytes >> 10, ps.cut, + ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut, }) catch return; var z: [4096]u8 = undefined; const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; const fd = libc.open(pz, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); if (fd < 0) return; _ = libc.write(fd, line.ptr, line.len); + var each: [400]u8 = undefined; + var w: std.Io.Writer = .fixed(&each); + w.writeAll("session joins_us") catch {}; + for (ps.each_join.items) |ns| w.print(" {d}", .{ns / 1000}) catch {}; + w.writeAll("\n") catch {}; + _ = libc.write(fd, w.buffered().ptr, w.buffered().len); _ = libc.close(fd); } }; @@ -2052,3 +2175,35 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expect(r.quiet()); try std.testing.expectEqual(digestOf(a), r.digest()); } + +test "a join's dump, its blobs put back where they go, is the session's dump" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 20000); + defer gpa.free(text); + @memset(text, 'x'); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + try std.testing.expect(blobs.items.len > 0); + try std.testing.expect(state.len + 20000 <= whole.len); + var joined: std.ArrayListUnmanaged(u8) = .empty; + defer joined.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try joined.appendSlice(gpa, state[pos..b.at]); + try joined.appendSlice(gpa, b.bytes); + pos = b.at; + } + try joined.appendSlice(gpa, state[pos..]); + try std.testing.expectEqualSlices(u8, whole, joined.items); +} diff --git a/src/terminal.zig b/src/terminal.zig index 2bc96a6e..42268f0a 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -259,6 +259,9 @@ pub const State = struct { defer gpa.free(bytes); try w.writeAll(std.mem.asBytes(&@as(u64, bytes.len))); try w.writeAll(bytes); + // A join's (dump.zig `dumpJoin`): its size alone, an emulator made + // afresh there, which its first delta replaces. + if (pardes.dump.slim_terminals) return w.writeAll(std.mem.asBytes(&[2]u16{ s.vt.cols, s.vt.rows })); try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = .ground }); } @@ -283,12 +286,17 @@ pub const State = struct { defer arena.deinit(); const parts = try pardes.walk.load(Parts, arena.allocator(), bytes[8..][0..n], null); var reader: std.Io.Reader = .fixed(bytes[8 + n ..]); - var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ - .max_continuation_bytes = 0, - .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, - }); - defer decoded.deinit(gpa); - out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; + if (reader.buffer.len == 4) { + const size = std.mem.bytesToValue([2]u16, bytes[8 + n ..][0..4]); + out.* = .{ .gpa = gpa, .vt = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .stream = undefined }; + } else { + var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ + .max_continuation_bytes = 0, + .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, + }); + defer decoded.deinit(gpa); + 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()); }; @@ -846,18 +854,9 @@ pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { pane.terminal = null; } -/// Allocate the live emulator half of a terminal pane. Slot ownership, serial -/// assignment, and spawn effects remain core lifecycle invariants. -pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (authorityOnly()) return error.OutOfMemory; - const pane = try createDoc(gpa, cols, rows); - errdefer gpa.destroy(pane); - if (comptime !enabled) return pane; - 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, .{ +/// A terminal pane's emulator, `cols` by `rows`, as `create` makes one. +fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { + return ghostty_vt.Terminal.init(terminalIo(), gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = 16 * 1024 * 1024, @@ -870,6 +869,20 @@ 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, }); +} + +/// Allocate the live emulator half of a terminal pane. Slot ownership, serial +/// assignment, and spawn effects remain core lifecycle invariants. +pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { + if (authorityOnly()) return error.OutOfMemory; + const pane = try createDoc(gpa, cols, rows); + errdefer gpa.destroy(pane); + if (comptime !enabled) return pane; + 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 emulator(own, cols, rows); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = own }); state.reply_len = 0; state.changes = 0; diff --git a/src/walk.zig b/src/walk.zig index 2dc0a721..d0d20e8a 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -112,6 +112,10 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { break :kept if (b.len == items.len) b.word else null; } else null; if (!rehash) if (kept) |w| break :word w; + if (E == u8) if (unhashed_out) |list| if (kept == null) { + list.append(std.heap.c_allocator, items) catch {}; + break :word 0; + }; in_blob = false; defer in_blob = true; var one = Hasher.init(0); @@ -130,6 +134,37 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { h.update(std.mem.asBytes(&word)); } +/// Where `unhashed` notes the texts a digest would hash for want of a word. +threadlocal var unhashed_out: ?*std.ArrayListUnmanaged([]const u8) = null; + +/// The blob texts a digest of `v` would hash now, having no word yet, onto +/// `out` (the C heap's): a walk with no blob hashed, which `makeWords` then +/// makes on another thread. A replica's join (src/replica.zig) does that +/// first: the session's step never hashes a text it never digested before. +pub fn unhashed(comptime T: type, v: *const T, out: *std.ArrayListUnmanaged([]const u8)) void { + unhashed_out = out; + defer unhashed_out = null; + _ = digest(T, v); +} + +/// The words of `texts` (`unhashed`'s), made on any thread: they must stay +/// as they are meanwhile (pinned, memory.zig), and the next digest takes +/// them. +pub fn makeWords(texts: []const []const u8) void { + for (texts) |items| { + // As hashBlob makes it (hashSlice's length, then the bytes). + var one = Hasher.init(0); + one.update(std.mem.asBytes(&items.len)); + one.update(items); + const w = one.final(); + lockBlobs(); + defer unlockBlobs(); + const got = blob_words.getOrPut(std.heap.page_allocator, @intFromPtr(items.ptr)) catch continue; + if (!got.found_existing) got.value_ptr.* = .{ .len = items.len, .word = w }; + blob_count.store(blob_words.count(), .release); + } +} + pub fn digest(comptime T: type, v: *const T) u64 { var h = Hasher.init(0); hash(T, v, &h); @@ -801,6 +836,9 @@ fn tableOf(comptime types: []const type) []const u8 { return Once.bytes; } +/// A blob's bytes a dump left out (`Dump.deferred`): they go at `at`. +pub const Deferred = struct { at: usize, bytes: []const u8 }; + /// Writes a dump of `roots`' values, one `put` each in order. pub fn Dump(comptime roots: []const type) type { return struct { @@ -811,6 +849,13 @@ pub fn Dump(comptime roots: []const type) type { out: std.Io.Writer.Allocating, memo: std.AutoHashMapUnmanaged(Key, u32) = .empty, shared: u32 = 0, + /// Set: a `.blob`'s bytes (of `blob_min` and up) are not written but + /// noted, where they go and what they are; the dump with them put + /// back in place is the dump. A replica's join streams them from + /// its writer thread (src/replica.zig), the session's step paying + /// for the rest alone. + deferred: ?*std.ArrayListUnmanaged(Deferred) = null, + in_blob: bool = false, const Key = struct { addr: usize, len: usize, ty: u32 }; @@ -893,6 +938,13 @@ pub fn Dump(comptime roots: []const type) type { .slice => { const E = @typeInfo(T).pointer.child; if (try d.seen(T, @intFromPtr(v.ptr), v.len)) return; + if (comptime flat(E)) if (d.in_blob) if (d.deferred) |later| { + const bytes = std.mem.sliceAsBytes(v.*); + if (bytes.len >= blob_min) { + try w.writeAll(std.mem.asBytes(&@as(u64, v.len))); + return later.append(d.gpa, .{ .at = d.out.writer.end, .bytes = bytes }); + } + }; try d.items(E, v.*); }, .array => { @@ -905,6 +957,9 @@ pub fn Dump(comptime roots: []const type) type { const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed"; inline for (comptime dumpFields(T)) |name| { const F = @FieldType(T, name); + const was = d.in_blob; + d.in_blob = was or comptime blob(T, name); + defer d.in_blob = was; if (comptime cutArray(T, name)) { const all = &@field(v, name); try d.items(@typeInfo(F).array.child, all[0..@min(all.len, lengthIn(v, comptime lengthOf(T, name).?))]); -- cgit v1.3 From ddb2554fff073eed0b73a43825b2c007c895bc4d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 05:58:13 -0300 Subject: replica: a far client's content cache and zstd: the session lists its join's blobs by BLAKE3 hash first (wire 16 `blobs`), the client finds each in the core it leaves or on disk ($XDG_CACHE_HOME/pardes/blobs, hashed again on load, a bad file a miss, the least recently used evicted past 1 GiB) and asks for the rest (`want`), and everything after the welcome goes in zstd envelopes (level 6 over 8 MiB for the join, an undo snapshot being a near copy of its text, then level 3), so `ssh -C` leaves the default command; a far client (through ssh, or PARDES_REPLICA_FAR) keeps what it was sent and, at its end, what it made; a hangup ends a remote client the normal way; through the box a client holding the blobs is drawn in 2.4 s where main took 7.0 to 11.9, and a first join of a session with undo in 7.1 s where main took 11.9; libzstd 1.5.7 vendored, its allocations on c_heap, a second copy refused by shared-libs-check; tests for the cache's verify-on-load, its eviction and a far join's blobs sent once; test/daemon.py's far clients Co-Authored-By: Claude Opus 5.5 --- build.zig | 38 ++ build.zig.zon | 6 + docs/shared-libs.md | 1 + src/detached/wire.zig | 56 ++- src/main.zig | 14 +- src/replica.zig | 857 ++++++++++++++++++++++++++++++++++++++++++-- src/tty/tty.zig | 69 +++- src/walk.zig | 15 + test/daemon.py | 190 +++++++++- tools/shared_libs_check.zig | 2 + 10 files changed, 1178 insertions(+), 70 deletions(-) (limited to 'src/replica.zig') diff --git a/build.zig b/build.zig index c0235e41..5e88ef68 100644 --- a/build.zig +++ b/build.zig @@ -964,6 +964,44 @@ pub fn build(b: *std.Build) void { if (sources != 1) @panic("zstbi's build no longer compiles one C file: redo its patch in build.zig"); }; + // One zstd (docs/shared-libs.md): a far replica's stream, compressed by + // its session and read by its client (src/replica.zig). The library's + // compressor and decompressor alone, single-threaded and without its + // x86 assembly, each context made with ZSTD_customMem over c_heap; of + // its block compressors only those of levels 3 (dfast) and 6 (lazy), + // the two replica.zig asks for, and its unused functions dropped at + // link. Zig's std has a decompressor, but no compressor. + if (!freestanding_core) { + const dep = b.dependency("zstd", .{}); + const zstd = b.addLibrary(.{ + .name = "zstd", + .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }), + .linkage = .static, + }); + zstd.root_module.addCSourceFiles(.{ + .root = dep.path("lib"), + .files = &.{ + "common/debug.c", "common/entropy_common.c", "common/error_private.c", + "common/fse_decompress.c", "common/pool.c", "common/threading.c", + "common/xxhash.c", "common/zstd_common.c", "compress/fse_compress.c", + "compress/hist.c", "compress/huf_compress.c", "compress/zstd_compress.c", + "compress/zstd_compress_literals.c", "compress/zstd_compress_sequences.c", "compress/zstd_compress_superblock.c", + "compress/zstd_double_fast.c", "compress/zstd_fast.c", "compress/zstd_lazy.c", + "compress/zstd_ldm.c", "compress/zstd_opt.c", "compress/zstd_preSplit.c", + "decompress/huf_decompress.c", "decompress/zstd_ddict.c", "decompress/zstd_decompress.c", + "decompress/zstd_decompress_block.c", + }, + .flags = &.{ + "-DXXH_NAMESPACE=ZSTD_", "-DZSTD_DISABLE_ASM", "-DZSTD_LEGACY_SUPPORT=0", + "-DDEBUGLEVEL=0", "-DZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", "-DZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", + "-DZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", "-DZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", "-DZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", + }, + }); + zstd.link_function_sections = true; + zstd.link_data_sections = true; + for (core_mods) |m| m.linkLibrary(zstd); + } + // One call per module instead of one line per module per dependency, and // BEFORE the fold below, which reads what these imports put in the table. for (core_mods) |m| wireCore(m, .{ diff --git a/build.zig.zon b/build.zig.zon index b7313981..09860fb1 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -191,6 +191,12 @@ .hash = "N-V-__8AAG02ugUcWec-Ndp-i7JTsJ0dgF8nnJRUInkGLG7G", .lazy = true, }, + // zstd for a far replica client's stream (src/replica.zig): the + // upstream release tarball, its lib/ compiled by build.zig. + .zstd = .{ + .url = "https://github.com/facebook/zstd/releases/download/v1.5.7/zstd-1.5.7.tar.gz", + .hash = "N-V-__8AAPZ7fwBg4JoCzM_0o2A8wxH2hsUUeiU1iuZv53L5", + }, }, .minimum_zig_version = "0.17.0", .paths = .{""}, diff --git a/docs/shared-libs.md b/docs/shared-libs.md index 79f3cdb9..ce0c7d3f 100644 --- a/docs/shared-libs.md +++ b/docs/shared-libs.md @@ -49,6 +49,7 @@ and gui columns are the two binaries. | Regex | mvzr | pardes | 1 → 1 | 17 KiB | Zig | | Parsing | tree-sitter | pardes | 1 → 1 | | `ts_set_allocator` → c_heap | | Window, input, GPU | SDL 3.4.18 | gui | 1 → 1 | 1009 KiB | `SDL_SetMemoryFunctions` | +| Compress and decompress a far replica's stream (2026-10-30) | zstd 1.5.7 (the upstream tarball; build.zig compiles its lib/: no assembly, no threads, the dfast and lazy block compressors alone) | src/replica.zig | 0 → 1 | 380 KiB tty (516 with every block compressor) | `ZSTD_customMem` → c_heap | Text section: tty 61,369,343 → 61,294,855 bytes (**−74,488**); gui 67,336,392 → 67,182,000 bytes (**−154,392**). diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 78f6e76e..2f2b95c8 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,11 +9,13 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); -/// 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 +/// 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); 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; +pub const version: u16 = 16; /// 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 @@ -60,6 +62,11 @@ pub const ClientTag = enum(u8) { /// What is the session's digest, in stream order (a replica checks /// itself against it once quiet). check = 0x05, + /// A client's first word after its hello: whether it is far (through + /// ssh), so that it holds blobs by their hash and reads zstd. + far = 0x06, + /// The join's blobs a far client lacks, a bit each in `blobs` order. + want = 0x07, key = 0x10, mouse = 0x11, @@ -128,19 +135,28 @@ pub const ServerTag = enum(u8) { screen = 0x06, /// The session's digest (u64le), after every unit before it. check = 0x07, + /// Part of the join's list of blobs, to a far client: each one's place + /// in the state, its length and its BLAKE3 hash (src/replica.zig). + blobs = 0x08, + /// A piece of a far client's stream from here on, zstd-compressed: the + /// messages in it are framed as these are. Opaque here. + zstd = 0x09, }; pub const ServerMsg = union(ServerTag) { /// 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, semantics: u64 = 0 }, + /// guesses the session's time of day). A far client is told how many + /// blobs the dump holds (`blobs`). + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, blobs: u32 = 0 }, refused: []const u8, pong: u64, dump: []const u8, unit: []const u8, screen: []const u8, check: u64, + blobs: []const u8, + zstd: []const u8, }; /// A message's header, for a payload sent on its own (a unit, a dump's @@ -165,6 +181,7 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), .semantics = try r.takeInt(u64, .little), + .blobs = try r.takeInt(u32, .little), } }, .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, .pong => .{ .pong = try r.takeInt(u64, .little) }, @@ -172,6 +189,8 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .unit => return .{ .unit = payload }, .screen => return .{ .screen = payload }, .check => .{ .check = try r.takeInt(u64, .little) }, + .blobs => return .{ .blobs = payload }, + .zstd => return .{ .zstd = payload }, }; try end(&r); return msg; @@ -190,10 +209,11 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); try w.writeInt(u64, h.semantics, .little); + try w.writeInt(u32, h.blobs, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), - .dump, .unit, .screen => |bytes| try w.writeAll(bytes), + .dump, .unit, .screen, .blobs, .zstd => |bytes| try w.writeAll(bytes), .check => |v| try w.writeInt(u64, v, .little), } try finishMessage(&w, at); @@ -227,6 +247,8 @@ pub const ClientMsg = union(enum) { ping: u64, screen_ack: []const u8, check, + far: bool, + want: []const u8, event: pardes.Event, }; @@ -436,8 +458,9 @@ 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), + .screen_ack, .want => |bytes| try w.writeAll(bytes), .check => {}, + .far => |far| try w.writeByte(@intFromBool(far)), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -506,6 +529,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { .ping => .{ .ping = try r.takeInt(u64, .little) }, .screen_ack => return .{ .screen_ack = payload }, .check => .check, + .far => .{ .far = try getBool(&r) }, + .want => return .{ .want = payload }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -566,8 +591,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, .check => 0, - .screen_ack => |bytes| bytes.len, + .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 => 0, .key => |k| k.text.len, @@ -622,7 +647,7 @@ 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', 0, 0, 0, 0, 0, 0, 0, 0, 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); + 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, 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); @@ -703,12 +728,13 @@ test "detached wire: a session is never told to do a frontend's remembering" { // 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", + "hello", "bye", "ping", "screen_ack", "check", + // ...and how far it is, and which of the join's blobs it lacks. + "far", "want", // ...and everything a person can do to a window. - "key", "mouse", "resize", - "paste", "command", "pdf_scroll", - "pinch", "touch_scroll", "pointer_leave", - "focus", + "key", "mouse", "resize", + "paste", "command", "pdf_scroll", "pinch", "touch_scroll", + "pointer_leave", "focus", }; // One: the tag set is exactly that, named rather than counted, so diff --git a/src/main.zig b/src/main.zig index ce1c5bba..fafafc08 100644 --- a/src/main.zig +++ b/src/main.zig @@ -884,7 +884,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, } }; if (std.mem.indexOfScalar(u8, where, '/') != null) { - @import("tty/tty.zig").remote(init, opts, where, name) catch |err| Say.no(io, "{t}", .{err}); + @import("tty/tty.zig").remote(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); std.process.exit(0); } // ssh -L : , until the socket here answers. @@ -902,10 +902,12 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, var argv: [48:null]?[*:0]const u8 = @splat(null); var n: usize = 0; var words_buf: [1024]u8 = undefined; - // Compressed by default: the join's dump is text, and the FreeBSD box's - // link carries 170 to 400 KB/s (a 3.9 MB join: 7.8 s plain, 5.4 with - // -C). $PARDES_SSH, when set, is the whole command. - var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh -C"), ' '); + // Not `ssh -C`: what the session sends a far client is zstd already + // (replica.zig), and zlib over it costs both ends' time for nothing. + // ponytail: what the client sends goes as it is, keys and a paste + // alike; a big paste through a slow link is the ceiling, and would be + // an envelope of its own. $PARDES_SSH, when set, is the whole command. + 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", .{}); @@ -953,7 +955,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, 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| { + @import("tty/tty.zig").remote(init, opts, here, name, true) catch |err| { Ssh.end(); Say.no(io, "{t}", .{err}); }; diff --git a/src/replica.zig b/src/replica.zig index 2e14cb6f..9d12de70 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -29,6 +29,7 @@ const rollback = @import("rollback.zig"); const walk = @import("walk.zig"); const wire = @import("detached/wire.zig"); const screen_state = @import("screen_state.zig"); +const c_heap = @import("c_heap"); const terminal = pardes.panes.terminal; const Allocator = std.mem.Allocator; const Pardes = pardes.Pardes; @@ -1047,6 +1048,9 @@ const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; /// PARDES_REPLICA_LAG_KB says another (a test's). var lag_max: usize = 64 << 20; const dump_piece = 1 << 20; +/// A blob in a far join's list (wire.zig `blobs`): its place in the state +/// (u64le), its length (u64le) and its BLAKE3 hash. +const blob_entry = 48; const Tm = extern struct { sec: c_int, min: c_int, hour: c_int, mday: c_int, mon: c_int, year: c_int, wday: c_int, yday: c_int, isdst: c_int, gmtoff: c_long, zone: ?[*:0]const u8 }; extern "c" fn localtime_r(t: *const libc.time_t, tm: *Tm) ?*Tm; @@ -1082,6 +1086,60 @@ fn writePieces(fd: c_int, bytes: []const u8) bool { return true; } +// ---- zstd (libzstd, vendored: build.zig) ---- + +/// A far client's stream from its welcome on (`Peer.farJoin`): its join in +/// one frame at level 6 over an 8 MiB window, since an undo snapshot is a +/// near copy of its file's text, a file's length before or after it (the +/// join's numbers: Phase 6's content cache and zstd notes); what follows in +/// a frame at level 3 over 1 MiB. Each envelope (wire.zig `zstd`) is a +/// flush of at most `piece` of input, so that a reader unpacks one whole. +const zstd = struct { + const CCtx = opaque {}; + const DCtx = opaque {}; + const In = extern struct { src: ?*const anyopaque, size: usize, pos: usize }; + const Out = extern struct { dst: ?*anyopaque, size: usize, pos: usize }; + const Mem = extern struct { + alloc: *const fn (?*anyopaque, usize) callconv(.c) ?*anyopaque, + free: *const fn (?*anyopaque, ?*anyopaque) callconv(.c) void, + ctx: ?*anyopaque = null, + }; + extern fn ZSTD_createCCtx_advanced(mem: Mem) ?*CCtx; + extern fn ZSTD_freeCCtx(c: ?*CCtx) usize; + extern fn ZSTD_CCtx_setParameter(c: *CCtx, param: c_int, value: c_int) usize; + extern fn ZSTD_compressStream2(c: *CCtx, out: *Out, in: *In, op: c_int) usize; + extern fn ZSTD_createDCtx_advanced(mem: Mem) ?*DCtx; + extern fn ZSTD_freeDCtx(d: ?*DCtx) usize; + extern fn ZSTD_decompressStream(d: *DCtx, out: *Out, in: *In) usize; + extern fn ZSTD_isError(code: usize) c_uint; + const level_param = 100; + const window_param = 101; + const Op = enum(c_int) { more = 0, flush = 1, end = 2 }; + const piece = 1 << 20; + + /// What zstd allocates is a Zig allocator's (c_heap): a test's own. + var gpa: Allocator = std.heap.c_allocator; + const heap = c_heap.Heap(&gpa); + fn alloc(_: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + return heap.malloc(n); + } + fn free(_: ?*anyopaque, p: ?*anyopaque) callconv(.c) void { + heap.free(p); + } + const mem: Mem = .{ .alloc = alloc, .free = free }; + + fn compressor(level: c_int, window_log: c_int) ?*CCtx { + const c = ZSTD_createCCtx_advanced(mem) orelse return null; + if (ZSTD_isError(ZSTD_CCtx_setParameter(c, level_param, level)) != 0 or + ZSTD_isError(ZSTD_CCtx_setParameter(c, window_param, window_log)) != 0) + { + _ = ZSTD_freeCCtx(c); + return null; + } + return c; + } +}; + fn readSome(fd: c_int, buf: []u8) usize { while (true) { const n = libc.read(fd, buf.ptr, buf.len); @@ -1095,21 +1153,34 @@ fn readSome(fd: c_int, buf: []u8) usize { pub const Framer = struct { buf: std.ArrayListUnmanaged(u8) = .empty, len: usize = 0, + /// What zstd envelopes (wire.zig `zstd`) unpacked to, its messages not + /// taken yet, and the stream's decompressor: a far client's. + inner: std.ArrayListUnmanaged(u8) = .empty, + z: ?*zstd.DCtx = null, + bad: bool = false, pub fn deinit(f: *Framer, gpa: Allocator) void { f.buf.deinit(gpa); + f.inner.deinit(gpa); + if (f.z) |z| _ = zstd.ZSTD_freeDCtx(z); + f.z = null; } /// Framed messages off `fd` (detached/wire.zig) handed to `take` until - /// the stream ends, `take` says stop, or a message is malformed. + /// the stream ends, `take` says stop, or a message is malformed. An + /// envelope's are handed on as if they had come bare. fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + if (fr.bad or !fr.drain(ctx, take)) return; while (true) { var at: usize = 0; var stop = false; while (!stop) { const f = (wire.framed(fr.buf.items[at..fr.len]) catch break) orelse break; at += f.total; - stop = !take(ctx, f.tag, f.payload); + if (f.tag == @backingInt(wire.ServerTag.zstd)) { + fr.bad = !fr.unpack(gpa, f.payload); + stop = fr.bad or !fr.drain(ctx, take); + } else stop = !take(ctx, f.tag, f.payload); } if (at > 0) { std.mem.copyForwards(u8, fr.buf.items[0 .. fr.len - at], fr.buf.items[at..fr.len]); @@ -1126,6 +1197,43 @@ pub const Framer = struct { fr.len += n; } } + + /// An envelope's bytes onto `inner`. Its writer flushed at most + /// `zstd.piece` of input into it, so it unpacks to no more than that. + fn unpack(fr: *Framer, gpa: Allocator, payload: []const u8) bool { + if (fr.z == null) fr.z = zstd.ZSTD_createDCtx_advanced(zstd.mem); + const z = fr.z orelse return false; + var in: zstd.In = .{ .src = payload.ptr, .size = payload.len, .pos = 0 }; + while (true) { + fr.inner.ensureUnusedCapacity(gpa, 256 << 10) catch return false; + const spare = fr.inner.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + if (zstd.ZSTD_isError(zstd.ZSTD_decompressStream(z, &out, &in)) != 0) return false; + fr.inner.items.len += out.pos; + if (in.pos == in.size and out.pos < out.size) return true; + } + } + + /// The whole messages in `inner` handed to `take`; false when it says + /// stop (what is left waits for the next run) or one is malformed. + fn drain(fr: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) bool { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.inner.items[at..]) catch { + fr.bad = true; + return false; + }) orelse break; + at += f.total; + stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + const rest = fr.inner.items.len - at; + std.mem.copyForwards(u8, fr.inner.items[0..rest], fr.inner.items[at..]); + fr.inner.items.len = rest; + } + return !stop; + } }; fn readFramed(gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { @@ -1163,9 +1271,25 @@ pub const Peer = struct { dump: []u8 = &.{}, blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, dump_gpa: Allocator = undefined, + /// Its first word after its hello: whether it is far (through ssh), its + /// blobs asked for by hash and its stream in zstd (`farJoin`). + far: ?bool = null, + /// A far client's answer to its join's list: the blobs it lacks. + want: ?[]u8 = null, + /// A far client's stream's compressor, and an envelope's worth of its + /// output; the input since the last envelope. + z: ?*zstd.CCtx = null, + zout: std.ArrayListUnmanaged(u8) = .empty, + zin: usize = 0, fn writeJoin(peer: *Peer) bool { if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const far = peer.far; + peer.lock.unlock(io); + if (far orelse return false) return peer.farJoin(); var pos: usize = 0; for (peer.blobs.items) |b| { if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; @@ -1174,6 +1298,95 @@ pub const Peer = struct { return writePieces(peer.fd, peer.dump[pos..]); } + /// A far client's join, in one zstd frame: the list of the dump's blobs + /// (each one's place in the state, its length and its BLAKE3 hash), the + /// state, then the blobs it says it lacks; what follows the join goes + /// in a cheaper frame. The hashing is this thread's, never the step's. + /// ponytail: a blob is hashed again at every far join, 3.6 GB/s; a + /// session's join after its first would keep them, beside its words. + fn farJoin(peer: *Peer) bool { + const gpa = peer.peers.gpa; + peer.z = zstd.compressor(6, 23) orelse return false; + var list: std.ArrayListUnmanaged(u8) = .empty; + defer list.deinit(gpa); + for (peer.blobs.items) |b| { + var entry: [blob_entry]u8 = undefined; + std.mem.writeInt(u64, entry[0..8], b.at, .little); + std.mem.writeInt(u64, entry[8..16], b.bytes.len, .little); + std.crypto.hash.Blake3.hash(b.bytes, entry[16..48], .{}); + list.appendSlice(gpa, &entry) catch return false; + if (list.items.len + blob_entry > dump_piece) { + if (!peer.zMessage(.blobs, list.items)) return false; + list.clearRetainingCapacity(); + } + } + if (list.items.len > 0 and !peer.zMessage(.blobs, list.items)) return false; + if (!peer.zPieces(peer.dump) or !peer.zWrite(&.{}, .flush)) return false; + if (peer.blobs.items.len > 0) { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.want == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const want = peer.want; + peer.lock.unlock(io); + const bits = want orelse return false; + if (bits.len != (peer.blobs.items.len + 7) / 8) return false; + for (peer.blobs.items, 0..) |b, i| if (bits[i / 8] >> @intCast(i % 8) & 1 != 0) { + if (!peer.zPieces(b.bytes)) return false; + }; + } + if (!peer.zWrite(&.{}, .end)) return false; + _ = zstd.ZSTD_freeCCtx(peer.z); + peer.z = zstd.compressor(3, 20); + return peer.z != null; + } + + /// One message into its stream's compressor. + fn zMessage(peer: *Peer, tag: wire.ServerTag, payload: []const u8) bool { + const head = wire.header(@backingInt(tag), payload.len); + return peer.zWrite(&head, .more) and peer.zWrite(payload, .more); + } + + /// `bytes` as dump pieces (1 MiB at most each), into the compressor. + fn zPieces(peer: *Peer, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + if (!peer.zMessage(.dump, bytes[at..@min(bytes.len, at + dump_piece)])) return false; + } + return true; + } + + /// `bytes` into its stream's compressor, and what comes out written as + /// envelopes: one at each `zstd.piece` of input, and one now if `op` + /// says flush or end (the frame's). + fn zWrite(peer: *Peer, bytes: []const u8, op: zstd.Op) bool { + const z = peer.z orelse return false; + var rest = bytes; + while (true) { + const n = @min(rest.len, zstd.piece - peer.zin); + const full = peer.zin + n == zstd.piece; + const now: zstd.Op = if (n < rest.len or (full and op == .more)) .flush else op; + var in: zstd.In = .{ .src = rest.ptr, .size = n, .pos = 0 }; + while (true) { + peer.zout.ensureUnusedCapacity(peer.peers.gpa, 128 << 10) catch return false; + const spare = peer.zout.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + const left = zstd.ZSTD_compressStream2(z, &out, &in, @backingInt(now)); + if (zstd.ZSTD_isError(left) != 0) return false; + peer.zout.items.len += out.pos; + if (if (now == .more) in.pos == in.size else left == 0) break; + } + peer.zin += n; + if (now != .more) { + const head = wire.header(@backingInt(wire.ServerTag.zstd), peer.zout.items.len); + if (!writeAll(peer.fd, &head) or !writeAll(peer.fd, peer.zout.items)) return false; + peer.zout.clearRetainingCapacity(); + peer.zin = 0; + } + rest = rest[n..]; + if (rest.len == 0) return true; + } + } + /// `parts` queued for it, in order; never waits. A client whose queue /// passes its limit is cut off: its link is shut down, and its reader /// says it is gone. @@ -1220,7 +1433,7 @@ pub const Peer = struct { // 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); + const ok = if (peer.z != null) peer.zWrite(peer.sending.items, .flush) else writeAll(peer.fd, peer.sending.items); peer.sending.clearRetainingCapacity(); if (!ok) { _ = libc.shutdown(peer.fd, 2); @@ -1251,6 +1464,23 @@ pub const Peer = struct { peer.peers.post(.{ .check = peer }); return true; }, + // What its join's writer waits for (`writeJoin`, `farJoin`). + .far, .want => { + const io = peer.peers.io; + const msg = wire.decodeClient(tag, payload) catch return false; + const bits = if (msg == .want) peer.peers.gpa.dupe(u8, msg.want) catch return false else null; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (bits) |b| { + if (peer.want != null) { + peer.peers.gpa.free(b); + return false; + } + peer.want = b; + } else peer.far = msg.far; + peer.cond.signal(io); + return true; + }, else => {}, } peer.seq += 1; @@ -1569,7 +1799,7 @@ pub const Peers = struct { if (ps.next_id == 0) ps.next_id = 1; // The session's digest as dumped, for the joiner to check its load by. const digest = tape.digestOf(core); - peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable).len; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; if (peer.writer == null or peer.reader == null) { @@ -1618,6 +1848,9 @@ pub const Peers = struct { _ = libc.close(peer.fd); peer.out.deinit(ps.gpa); peer.sending.deinit(ps.gpa); + if (peer.want) |b| ps.gpa.free(b); + if (peer.z) |z| _ = zstd.ZSTD_freeCCtx(z); + peer.zout.deinit(ps.gpa); // What its reader posted after it was let go (its end) is nobody's. ps.lock.lockUncancelable(io); var i: usize = 0; @@ -1772,12 +2005,24 @@ pub const Joined = struct { semantics: u64 = 0, /// What was read past the dump: the stream's (`readStream`). rest: Framer = .{}, + /// A far join's blobs that came over the link, for `Have.store` (the C + /// heap's), and how it went: its blobs, those it held already (from the + /// core it left, from disk) and the bytes of the ones it was sent. + got: []Got = &.{}, + blobs: u32 = 0, + from_memory: u32 = 0, + from_disk: u32 = 0, + sent_bytes: u64 = 0, }; +/// A blob a far join was sent: where it is in the dump, and its hash. +pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; + /// Connected to the replica socket at `path`, its hello said for /// `session`, welcomed, and the dump read whole: what a client joins with. -/// Errors are said in `why`. -pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?Joined { +/// A far client (`have`) is sent its dump's blobs by hash first, and asks +/// for those it holds nowhere. Errors are said in `why`. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have) ?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) }; @@ -1790,24 +2035,51 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J _ = libc.close(fd); return said(why, "nothing answers at {s}", .{path}); } - var out: [wire.hello_max]u8 = undefined; + return joinOn(gpa, fd, session, why, have); +} + +/// `dial`'s, on a connection made: the hello, then the join. The +/// connection is closed if the join fails. +fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have) ?Joined { + var out: [wire.hello_max + 16]u8 = undefined; const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); }; + // Its first word, in the same write: whether it is far. + const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; const Read = struct { j: *Joined, gpa: Allocator, why: []u8, - got: usize = 0, + have: ?*Have, failed: ?[]const u8 = null, welcomed: bool = false, + done: bool = false, + /// The join's list of blobs (`blob_entry` each), as it comes. + list: std.ArrayListUnmanaged(u8) = .empty, + /// Where what comes fills the dump, in order (each a place and a + /// length): the state's runs between the blobs, then each blob + /// asked for. A blob listed twice is copied once the dump is whole. + runs: std.ArrayListUnmanaged([2]usize) = .empty, + run: usize = 0, + run_at: usize = 0, + dups: std.ArrayListUnmanaged([3]usize) = .empty, + + fn deinit(r: *@This()) void { + r.list.deinit(r.gpa); + r.runs.deinit(r.gpa); + r.dups.deinit(r.gpa); + } + + fn fail(r: *@This(), words: []const u8) bool { + r.failed = words; + return false; + } + fn take(r: *@This(), tag: u8, payload: []const u8) bool { - const msg = wire.decodeServer(tag, payload) catch { - r.failed = "the session's answer was not understood"; - return false; - }; + const msg = wire.decodeServer(tag, payload) catch return r.fail("the session's answer was not understood"); switch (msg) { .welcome => |w| { r.j.pid = w.pid; @@ -1816,42 +2088,109 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; r.j.semantics = w.semantics; - r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch { - r.failed = "out of memory for the session's dump"; - return false; - }; + r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch return r.fail("out of memory for the session's dump"); r.welcomed = true; - return w.dump_len > 0; + r.j.blobs = if (r.have != null) w.blobs else 0; + // A near client's dump comes whole, and a far one's + // without blobs is its state. + if (r.j.blobs == 0) r.runs.append(r.gpa, .{ 0, r.j.dump.len }) catch return r.fail("out of memory for the session's dump"); + r.done = w.dump_len == 0; + return !r.done; }, .refused => |words| { r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused"; return false; }, + .blobs => |entries| { + const whole = @as(usize, r.j.blobs) * blob_entry; + if (r.runs.items.len > 0 or r.list.items.len + entries.len > whole) return r.fail("the session sent more of its blobs' list than it said"); + r.list.appendSlice(r.gpa, entries) catch return r.fail("out of memory for the session's blobs' list"); + if (r.list.items.len == whole) return r.lacking(); + return true; + }, .dump => |piece| { - if (!r.welcomed or r.got + piece.len > r.j.dump.len) { - r.failed = "the session sent more dump than it said"; - return false; + if (r.runs.items.len == 0) return r.fail("the session sent its dump before its blobs' list"); + var p = piece; + while (p.len > 0) { + if (r.run == r.runs.items.len) return r.fail("the session sent more dump than it said"); + const run = r.runs.items[r.run]; + const n = @min(p.len, run[1] - r.run_at); + @memcpy(r.j.dump[run[0] + r.run_at ..][0..n], p[0..n]); + r.run_at += n; + p = p[n..]; + if (r.run_at == run[1]) { + r.run += 1; + r.run_at = 0; + } } - @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"; + if (r.run < r.runs.items.len) return true; + for (r.dups.items) |d| @memcpy(r.j.dump[d[0]..][0..d[2]], r.j.dump[d[1]..][0..d[2]]); + r.done = true; return false; }, + else => return r.fail("the session sent a unit before its dump"), } } + + /// The list whole: each blob put in place from what the client + /// holds (`Have.find`), or asked for, and the runs to fill made. + fn lacking(r: *@This()) bool { + const h = r.have.?; + const dump = r.j.dump; + const n: usize = r.j.blobs; + const bits = r.gpa.alloc(u8, (n + 7) / 8) catch return r.fail("out of memory for the session's blobs' list"); + defer r.gpa.free(bits); + @memset(bits, 0); + var got: std.ArrayListUnmanaged(Got) = .empty; + defer got.deinit(std.heap.c_allocator); + var firsts: std.AutoHashMapUnmanaged([32]u8, [2]usize) = .empty; + defer firsts.deinit(r.gpa); + var shift: usize = 0; + var end: usize = 0; + for (0..n) |i| { + const e = r.list.items[i * blob_entry ..][0..blob_entry]; + const len = std.math.cast(usize, std.mem.readInt(u64, e[8..16], .little)) orelse return r.fail("the session's list of blobs is malformed"); + const pos = std.math.add(usize, std.math.cast(usize, std.mem.readInt(u64, e[0..8], .little)) orelse maxInt, shift) catch maxInt; + if (pos < end or pos > dump.len or len > dump.len - pos) return r.fail("the session's list of blobs is malformed"); + shift += len; + if (pos > end) r.runs.append(r.gpa, .{ end, pos - end }) catch return r.fail("out of memory for the session's blobs' list"); + end = pos + len; + const hash = e[16..48]; + const first = firsts.getOrPut(r.gpa, hash.*) catch return r.fail("out of memory for the session's blobs' list"); + if (first.found_existing and first.value_ptr[1] == len) { + r.dups.append(r.gpa, .{ pos, first.value_ptr[0], len }) catch return r.fail("out of memory for the session's blobs' list"); + continue; + } + first.value_ptr.* = .{ pos, len }; + if (h.find(hash, dump[pos..][0..len])) |where| switch (where) { + .memory => r.j.from_memory += 1, + .disk => r.j.from_disk += 1, + } else { + bits[i / 8] |= @as(u8, 1) << @intCast(i % 8); + got.append(std.heap.c_allocator, .{ .pos = pos, .len = len, .hash = hash.* }) catch return r.fail("out of memory for the session's blobs' list"); + r.j.sent_bytes += len; + } + } + if (dump.len > end) r.runs.append(r.gpa, .{ end, dump.len - end }) catch return r.fail("out of memory for the session's blobs' list"); + for (got.items) |g| r.runs.append(r.gpa, .{ g.pos, g.len }) catch return r.fail("out of memory for the session's blobs' list"); + const head = wire.header(@backingInt(wire.ClientTag.want), bits.len); + if (!writeAll(r.j.fd, &head) or !writeAll(r.j.fd, bits)) return r.fail("the session hung up"); + r.j.got = got.toOwnedSlice(std.heap.c_allocator) catch return r.fail("out of memory for the session's blobs' list"); + return true; + } + const maxInt = std.math.maxInt(usize); }; - if (!writeAll(fd, hello)) { + if (!writeAll(fd, out[0 .. hello.len + far.len])) { _ = libc.close(fd); return said(why, "the session hung up", .{}); } - var r: Read = .{ .j = &j, .gpa = gpa, .why = why }; + var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have }; + defer r.deinit(); j.rest.run(gpa, fd, &r, Read.take); - if (r.failed != null or !r.welcomed or r.got < j.dump.len) { + if (r.failed != null or !r.done) { j.rest.deinit(gpa); if (j.dump.len > 0) gpa.free(j.dump); + std.heap.c_allocator.free(j.got); _ = libc.close(fd); const text = r.failed orelse "the session hung up before the dump was whole"; if (text.ptr == why.ptr) return null; @@ -1860,6 +2199,251 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J return j; } +// ---- a far client's blobs ---- + +/// What a far client holds of a join's blobs already, by content (`dial`): +/// the cache on disk, and on a rejoin the blobs of the core it leaves. +pub const Have = struct { + io: std.Io, + cache: ?Cache = null, + /// The core it leaves (`leaving`), each blob hashed once when one of its + /// length is looked for. ponytail: a scan a lookup, fine to some + /// thousands of blobs; a map by length would take over past that. + old: std.ArrayListUnmanaged([]const u8) = .empty, + old_hash: std.ArrayListUnmanaged(?[32]u8) = .empty, + /// The thread writing the last join's blobs to disk (`store`). + storing: ?std.Thread = null, + + pub fn init(io: std.Io) Have { + return .{ .io = io, .cache = Cache.open(io) }; + } + + pub fn deinit(h: *Have) void { + h.wait(); + h.left(); + h.old.deinit(std.heap.c_allocator); + h.old_hash.deinit(std.heap.c_allocator); + if (h.cache) |c| c.dir.close(h.io); + } + + fn wait(h: *Have) void { + if (h.storing) |t| t.join(); + h.storing = null; + } + + /// `core`'s blobs, looked at by the next join until `left`: a rejoin + /// takes what it held, by content, before the core goes. + pub fn leaving(h: *Have, core: *const Pardes) void { + h.left(); + walk.blobs(Pardes, core, &h.old); + h.old_hash.appendNTimes(std.heap.c_allocator, null, h.old.items.len) catch h.old.clearRetainingCapacity(); + } + + pub fn left(h: *Have) void { + h.old.clearRetainingCapacity(); + h.old_hash.clearRetainingCapacity(); + } + + pub const Where = enum { memory, disk }; + + /// The blob of `hash` put in `out` (its length), from the core it + /// leaves or the cache; null when it holds it nowhere. + fn find(h: *Have, hash: *const [32]u8, out: []u8) ?Where { + for (h.old.items, h.old_hash.items) |b, *word| { + if (b.len != out.len) continue; + if (word.* == null) { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + word.* = d; + } + if (!std.mem.eql(u8, &word.*.?, hash)) continue; + @memcpy(out, b); + return .memory; + } + if (h.cache) |c| if (c.get(h.io, hash, out)) return .disk; + return null; + } + + /// The blobs a join was sent, written to the cache on a thread of their + /// own, which frees the dump (`gpa`'s) and the list after: the first + /// frame waits for none of it. + pub fn store(h: *Have, gpa: Allocator, j: *Joined) void { + const dump = j.dump; + const got = j.got; + j.dump = &.{}; + j.got = &.{}; + h.wait(); + const Store = struct { + fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got) void { + defer { + a.free(bytes); + std.heap.c_allocator.free(list); + } + var incoming: u64 = 0; + for (list) |g| incoming += g.len; + c.trim(io, @min(incoming, c.cap)); + var put: u64 = 0; + for (list) |g| { + if (put + g.len > c.cap) continue; + put += g.len; + c.put(io, &g.hash, bytes[g.pos..][0..g.len]); + } + } + }; + const c = h.cache orelse { + gpa.free(dump); + std.heap.c_allocator.free(got); + return; + }; + h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got }) catch { + Store.run(c, h.io, gpa, dump, got); + return; + }; + } + + /// `core`'s blobs the cache lacks, written there: what this client made + /// since it joined (an edit's text, its undo), for its next start. + pub fn keep(h: *Have, core: *const Pardes) void { + h.wait(); + const c = h.cache orelse return; + var all: std.ArrayListUnmanaged([]const u8) = .empty; + defer all.deinit(std.heap.c_allocator); + walk.blobs(Pardes, core, &all); + var missing: std.ArrayListUnmanaged(struct { bytes: []const u8, hash: [32]u8 }) = .empty; + defer missing.deinit(std.heap.c_allocator); + var incoming: u64 = 0; + for (all.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + if (c.has(h.io, &d, b.len)) continue; + missing.append(std.heap.c_allocator, .{ .bytes = b, .hash = d }) catch return; + incoming += b.len; + } + if (missing.items.len == 0) return; + c.trim(h.io, @min(incoming, c.cap)); + var put: u64 = 0; + for (missing.items) |m| { + if (put + m.bytes.len > c.cap) continue; + put += m.bytes.len; + c.put(h.io, &m.hash, m.bytes); + } + } +}; + +/// A far client's blobs on disk, a file each named by its BLAKE3 hash in +/// hex, in $XDG_CACHE_HOME/pardes/blobs (else ~/.cache/pardes/blobs): what +/// a client started again, or a second one on this machine, is not sent +/// again. The cache directory and not the runtime one: the blobs are worth +/// keeping across a logout or a reboot and safe to lose, and they can be +/// hundreds of megabytes, which the runtime directory (a tmpfs, often +/// capped at a tenth of memory and emptied at logout) is not for. A file +/// read back is hashed again: one cut short, corrupted or planted is a +/// miss and goes, never wrong content. +pub const Cache = struct { + dir: std.Io.Dir, + /// Bytes it keeps at most: PARDES_BLOB_CACHE_MB, else 1 GiB. + cap: u64, + + pub fn open(io: std.Io) ?Cache { + var buf: [4096]u8 = undefined; + const base = if (std.c.getenv("XDG_CACHE_HOME")) |x| std.mem.span(x) else ""; + const path = if (base.len > 0 and base[0] == '/') + std.fmt.bufPrint(&buf, "{s}/pardes/blobs", .{base}) catch return null + else if (std.c.getenv("HOME")) |home| + std.fmt.bufPrint(&buf, "{s}/.cache/pardes/blobs", .{std.mem.span(home)}) catch return null + else + return null; + const cwd = std.Io.Dir.cwd(); + cwd.createDirPath(io, std.fs.path.dirname(path).?) catch return null; + cwd.createDir(io, path, .fromMode(0o700)) catch |err| if (err != error.PathAlreadyExists) return null; + const mb = if (std.c.getenv("PARDES_BLOB_CACHE_MB")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 1024 else 1024; + return .{ .dir = cwd.openDir(io, path, .{ .iterate = true }) catch return null, .cap = mb << 20 }; + } + + fn name(hash: *const [32]u8) [64]u8 { + return std.fmt.bytesToHex(hash.*, .lower); + } + + /// The blob of `hash` read into `out` (its length) and hashed again. + /// A file of another length or content is a miss, and goes; a hit's + /// time is made now, so the least recently used go first (`trim`). + pub fn get(c: Cache, io: std.Io, hash: *const [32]u8, out: []u8) bool { + const file = name(hash); + var f = c.dir.openFile(io, &file, .{}) catch return false; + const ok = ok: { + defer f.close(io); + const st = f.stat(io) catch break :ok false; + if (st.size != out.len) break :ok false; + var reader = f.reader(io, &.{}); + const n = reader.interface.readSliceShort(out) catch break :ok false; + if (n != out.len) break :ok false; + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(out, &d, .{}); + break :ok std.mem.eql(u8, &d, hash); + }; + if (!ok) { + c.dir.deleteFile(io, &file) catch {}; + return false; + } + c.dir.setTimestampsNow(io, &file, .{}) catch {}; + return true; + } + + /// Whether a file of `len` bytes is there under `hash` (not read). + pub fn has(c: Cache, io: std.Io, hash: *const [32]u8, len: usize) bool { + const file = name(hash); + const st = c.dir.statFile(io, &file, .{}) catch return false; + return st.size == len; + } + + var tmp_serial: std.atomic.Value(u32) = .init(0); + + /// `bytes` kept under `hash`: written aside and renamed into place, so + /// that a reader never meets half a file (and would hash it wrong). + pub fn put(c: Cache, io: std.Io, hash: *const [32]u8, bytes: []const u8) void { + const file = name(hash); + if (c.has(io, hash, bytes.len)) return; + var buf: [96]u8 = undefined; + const tmp = std.fmt.bufPrint(&buf, ".tmp-{d}-{d}-{s}", .{ std.c.getpid(), tmp_serial.fetchAdd(1, .monotonic), file[0..16] }) catch return; + c.dir.writeFile(io, .{ .sub_path = tmp, .data = bytes }) catch { + c.dir.deleteFile(io, tmp) catch {}; + return; + }; + c.dir.rename(tmp, c.dir, &file, io) catch c.dir.deleteFile(io, tmp) catch {}; + } + + /// Room for `incoming` more bytes under the cap, the least recently + /// used files deleted first (a hit makes its file's time now). + /// ponytail: the directory scanned and sorted at each join's store; + /// fine to tens of thousands of blobs, where an index would take over. + pub fn trim(c: Cache, io: std.Io, incoming: u64) void { + const File = struct { name: [80]u8, len: u8, size: u64, mtime: i96 }; + var files: std.ArrayListUnmanaged(File) = .empty; + defer files.deinit(std.heap.c_allocator); + var total: u64 = 0; + var it = c.dir.iterate(); + while (it.next(io) catch null) |entry| { + if (entry.kind != .file or entry.name.len > 80) continue; + const st = c.dir.statFile(io, entry.name, .{}) catch continue; + var f: File = .{ .name = undefined, .len = @intCast(entry.name.len), .size = st.size, .mtime = st.mtime.nanoseconds }; + @memcpy(f.name[0..f.len], entry.name); + files.append(std.heap.c_allocator, f) catch return; + total += st.size; + } + if (total + incoming <= c.cap) return; + std.mem.sort(File, files.items, {}, struct { + fn older(_: void, a: File, b: File) bool { + return a.mtime < b.mtime; + } + }.older); + for (files.items) |f| { + if (total + incoming <= c.cap) break; + c.dir.deleteFile(io, f.name[0..f.len]) catch continue; + total -= f.size; + } + } +}; + fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined { _ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {}; return null; @@ -2207,3 +2791,218 @@ test "a join's dump, its blobs put back where they go, is the session's dump" { try joined.appendSlice(gpa, state[pos..]); try std.testing.expectEqualSlices(u8, whole, joined.items); } + +test "a cached blob is hashed again as it is read: one cut short, corrupted, planted or longer is a miss, and goes" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 1 << 20 }; + var text: [6000]u8 = undefined; + for (&text, 0..) |*b, i| b.* = @truncate(i *% 31 +% i / 7); + var hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&text, &hash, .{}); + const file = Cache.name(&hash); + var out: [6000]u8 = undefined; + try std.testing.expect(!c.get(io, &hash, &out)); + c.put(io, &hash, &text); + try std.testing.expect(c.has(io, &hash, text.len)); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); + var corrupted = text; + corrupted[4000] ^= 0x20; + const planted: [6000]u8 = @splat('x'); + var longer: [6001]u8 = undefined; + @memcpy(longer[0..6000], &text); + longer[6000] = '\n'; + for ([_][]const u8{ text[0..3000], &corrupted, &planted, &longer }) |bad| { + try tmp.dir.writeFile(io, .{ .sub_path = &file, .data = bad }); + @memset(&out, 0); + try std.testing.expect(!c.get(io, &hash, &out)); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, &file, .{})); + } + // ...and put back, it is read again. + c.put(io, &hash, &text); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); +} + +test "the blob cache stays under its cap, the least recently used going first" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 3 * 5000 }; + var blobs: [3][5000]u8 = undefined; + var hashes: [3][32]u8 = undefined; + for (&blobs, &hashes, 0..) |*b, *h, k| { + @memset(b, @intCast('a' + k)); + std.crypto.hash.Blake3.hash(b, h, .{}); + c.put(io, h, b); + // Each older than the next, a thousand seconds apart. + const name = Cache.name(h); + try tmp.dir.setTimestamps(io, &name, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, @intCast(k + 1)) * 1000 * std.time.ns_per_s } } }); + } + // The oldest read: it is the newest now. + var out: [5000]u8 = undefined; + try std.testing.expect(c.get(io, &hashes[0], &out)); + c.trim(io, 5000); + try std.testing.expect(c.has(io, &hashes[0], 5000)); + try std.testing.expect(!c.has(io, &hashes[1], 5000)); + try std.testing.expect(c.has(io, &hashes[2], 5000)); + // A file not a blob's counts, and goes by its time as well. + const junk: [9000]u8 = @splat(0); + try tmp.dir.writeFile(io, .{ .sub_path = ".tmp-1-1-0123456789abcdef", .data = &junk }); + try tmp.dir.setTimestamps(io, ".tmp-1-1-0123456789abcdef", .{ .modify_timestamp = .{ .new = .{ .nanoseconds = 500 * std.time.ns_per_s } } }); + c.trim(io, 5000); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".tmp-1-1-0123456789abcdef", .{})); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Under the cap with room to spare: nothing goes. + c.trim(io, 0); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Room for the whole cap: everything goes, the older first. + c.trim(io, c.cap); + try std.testing.expect(!c.has(io, &hashes[0], 5000) and !c.has(io, &hashes[2], 5000)); +} + +/// A far join over a socket pair, the session's writer and reader threads +/// against the client's `joinOn`, as the daemon hands a connection over: +/// its hello read off first. +fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const state = pardes.dump.dumpJoin(core, &blobs) catch |err| { + _ = libc.close(fds[0]); + _ = libc.close(fds[1]); + return err; + }; + var whole = state.len; + for (blobs.items) |b| whole += b.bytes.len; + const peer = try ps.gpa.create(Peer); + peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa }; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + const Serve = struct { + fn run(p: *Peer) void { + var hello: [wire.header_len + 13]u8 = undefined; + var got: usize = 0; + while (got < hello.len) { + const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); + if (n <= 0) return p.peers.post(.{ .gone = p }); + got += @intCast(n); + } + p.readLoop(); + } + }; + peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer}); + peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); + defer ps.letGo(peer); + var why: [300]u8 = @splat(0); + return joinOn(std.testing.allocator, fds[1], "t", &why, have) orelse { + std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); + return error.JoinFailed; + }; +} + +fn endJoin(j: *Joined, have: ?*Have) void { + _ = libc.close(j.fd); + j.rest.deinit(std.testing.allocator); + if (have) |h| { + h.store(std.testing.allocator, j); + h.wait(); + } else { + std.testing.allocator.free(j.dump); + std.heap.c_allocator.free(j.got); + } +} + +test "a far join is sent each blob once: joined again with them on disk or in the core it leaves, the state alone; a cached file gone bad, that blob again" { + const gpa = std.testing.allocator; + const io = std.testing.io; + zstd.gpa = gpa; + defer zstd.gpa = std.heap.c_allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 30000); + defer gpa.free(text); + for (text, 0..) |*b, i| b.* = if (i % 61 == 60) '\n' else @intCast('a' + (i * 7 + i / 13) % 26); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + _ = try a.local(.{ .key = .{ .cp = 'o', .text = "o" } }); + _ = try a.local(.{ .paste = text[0..9000] }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + // What the session's list will say: its blobs, those of one text once. + var listed: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer listed.deinit(a.core.gpa); + a.core.gpa.free(try pardes.dump.dumpJoin(a.core, &listed)); + var distinct: std.AutoHashMapUnmanaged([32]u8, usize) = .empty; + defer distinct.deinit(gpa); + var distinct_bytes: u64 = 0; + var biggest: [32]u8 = undefined; + var biggest_len: usize = 0; + for (listed.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b.bytes, &d, .{}); + if ((try distinct.getOrPut(gpa, d)).found_existing) continue; + distinct_bytes += b.bytes.len; + if (b.bytes.len > biggest_len) { + biggest = d; + biggest_len = b.bytes.len; + } + } + try std.testing.expect(distinct.count() >= 3); + + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + var have: Have = .{ .io = io, .cache = .{ .dir = tmp.dir, .cap = 64 << 20 } }; + defer { + have.cache = null; + have.deinit(); + } + var ps: Peers = .{ .gpa = gpa, .io = io }; + defer ps.deinit(); + + // A near client: the dump whole, as before. + var j = try farJoinOnce(&ps, a.core, null); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(@as(u32, 0), j.blobs); + endJoin(&j, null); + // The first far join: every blob sent, once. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(listed.items.len, j.blobs); + try std.testing.expectEqual(@as(u32, 0), j.from_disk + j.from_memory); + try std.testing.expectEqual(distinct_bytes, j.sent_bytes); + endJoin(&j, &have); + // Again: all of them on disk. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_disk); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); + // The biggest's file corrupted: it alone is sent again. + const name = Cache.name(&biggest); + const junk = try gpa.alloc(u8, biggest_len); + defer gpa.free(junk); + @memset(junk, 'z'); + try tmp.dir.writeFile(io, .{ .sub_path = &name, .data = junk }); + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count() - 1, j.from_disk); + try std.testing.expectEqual(@as(u64, biggest_len), j.sent_bytes); + endJoin(&j, &have); + // A rejoin, no disk: what the core it leaves holds. + const cache = have.cache; + have.cache = null; + have.leaving(a.core); + j = try farJoinOnce(&ps, a.core, &have); + have.left(); + have.cache = cache; + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_memory); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); +} diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 994ebd92..2007c6f7 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1309,12 +1309,17 @@ const Remote = struct { test_diverge: u32 = 0, /// Put off so in this join: its checks are not written (they differ). put_off: bool = false, + /// How its last join went (replica.Joined's counts), and how long its + /// dial and load took (PARDES_REPLICA_STATS). + join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, + join_ms: u64 = 0, /// The replica the dial's dump makes, checked against the session as - /// it was dumped; null, and why in `why`. The dump is let go. - fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8) ?*replica.Replica { + /// it was dumped; null, and why in `why`. The dump is let go, a far + /// join's to the thread that keeps the blobs it was sent (`Have.store`). + fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8, have: ?*replica.Have) ?*replica.Replica { defer { - gpa.free(joined.dump); + if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); joined.dump = &.{}; } const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { @@ -1358,10 +1363,12 @@ const Remote = struct { 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, + 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} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {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, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, + c.join.sent_bytes >> 10, }) catch return; var z: [4096]u8 = undefined; const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; @@ -1446,7 +1453,7 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes); return false; }, - .pong, .welcome, .dump => {}, + .pong, .welcome, .dump, .blobs, .zstd => {}, } return true; } @@ -1461,7 +1468,7 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo /// 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 { +pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far_link: bool) !void { const io = init.io; const gpa = init.gpa; const Said = struct { @@ -1472,7 +1479,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se } }; var why: [600]u8 = @splat(0); - var joined = replica.dial(gpa, path, session, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash, + // those it holds kept from the link, and its stream in zstd. A client + // on this machine's own socket is sent the dump whole, as before: a + // hash and a compression there cost more than the bytes. + const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else far_link; + var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; + defer have_store.deinit(); + const have: ?*replica.Have = if (far) &have_store else null; + var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + var joined = replica.dial(gpa, path, session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words}); const allocs = pardes.memory.init(gpa); @@ -1488,10 +1504,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var o = opts; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - var r = Remote.load(gpa, &joined, o, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var r = Remote.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + // Its join's time, the dial and the load (PARDES_REPLICA_STATS). + var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); + // A hangup ends it the normal way (`quit`), what it made kept on disk + // (`Have.keep`) and its ssh forward's socket unlinked (main.zig). + posix.sigaddset(&winch_set, posix.SIG.HUP); + hangup_quits = true; 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); @@ -1539,6 +1561,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se sh.terms[joined.view] = &screen; screen.feed.view = joined.view; link.reset(r, joined.fd); + link.join = .{ .blobs = joined.blobs, .from_memory = joined.from_memory, .from_disk = joined.from_disk, .sent_bytes = joined.sent_bytes }; + link.join_ms = dial_ms; var reader_done: std.atomic.Value(bool) = .init(false); const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); // Its size is its view's first input. @@ -1610,6 +1634,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se std.heap.c_allocator.free(words); link.refused = null; } else { + // What it made since it joined, kept for its next start. + if (have) |h| h.keep(r.core()); r.deinit(); if (link.gone) { screen.leave(); @@ -1617,13 +1643,15 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se } return; } - r.deinit(); - // The session again: an upgrade's takes the time its exec and load + // The session again, the core it leaves held meanwhile: its blobs + // are not sent again. An upgrade's takes the time its exec and load // take, and its daemon's. + if (have) |h| h.leaving(r.core()); var tries: u32 = 0; + dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; joined = while (true) : (tries += 1) { why = @splat(0); - if (replica.dial(gpa, path, session, &why)) |j| { + if (replica.dial(gpa, path, session, &why, have)) |j| { if (j.semantics == wire.semantics) break j; replica.bye(j.fd); _ = libc.close(j.fd); @@ -1645,10 +1673,13 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); }; - r = Remote.load(gpa, &joined, o, &why) orelse { + if (have) |h| h.left(); + r.deinit(); + r = Remote.load(gpa, &joined, o, &why, have) orelse { screen.leave(); Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; + dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); } } @@ -3453,16 +3484,20 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { fn keepSignal(_: posix.SIG) callconv(.c) void {} +/// A replica client's (`remote`): a hangup is its end, whoever sent it. +var hangup_quits = false; + fn winchWatch(loop: *Loop) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); - if (daemon.link >= 0) posix.sigaddset(&set, posix.SIG.HUP); + if (daemon.link >= 0 or hangup_quits) posix.sigaddset(&set, posix.SIG.HUP); while (true) { var sig: c_int = 0; if (libc.sigwait(&set, &sig) != 0) continue; // The terminals are the loop's: it asks each its size, and takes a // hangup as the end of the one that hung up. - loop.postEvent(if (sig == @backingInt(posix.SIG.HUP)) .hangup else .winched) catch {}; + const ev: @TypeOf(Command.value) = if (sig != @backingInt(posix.SIG.HUP)) .winched else if (hangup_quits) .quit else .hangup; + loop.postEvent(ev) catch {}; } } diff --git a/src/walk.zig b/src/walk.zig index 3556ce88..1c5bbbfc 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -111,6 +111,10 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { const b = blob_words.get(addr) orelse break :kept null; break :kept if (b.len == items.len) b.word else null; } else null; + if (blobs_out) |list| { + list.append(std.heap.c_allocator, std.mem.sliceAsBytes(items)) catch {}; + break :word 0; + } if (!rehash) if (kept) |w| break :word w; if (E == u8) if (unhashed_out) |list| if (kept == null) { list.append(std.heap.c_allocator, items) catch {}; @@ -136,6 +140,17 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { /// Where `unhashed` notes the texts a digest would hash for want of a word. threadlocal var unhashed_out: ?*std.ArrayListUnmanaged([]const u8) = null; +/// Where `blobs` notes every blob, hashing none. +threadlocal var blobs_out: ?*std.ArrayListUnmanaged([]const u8) = null; + +/// Every blob of `v` a digest would take as one (`blob_min` bytes and up), +/// as its bytes, onto `out` (the C heap's), hashed by none: a replica +/// client's, which knows its blobs by content (src/replica.zig `Have`). +pub fn blobs(comptime T: type, v: *const T, out: *std.ArrayListUnmanaged([]const u8)) void { + blobs_out = out; + defer blobs_out = null; + _ = digest(T, v); +} /// The blob texts a digest of `v` would hash now, having no word yet, onto /// `out` (the C heap's): a walk with no blob hashed, which `makeWords` then diff --git a/test/daemon.py b/test/daemon.py index 8b0a20bd..fb4f3013 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -23,6 +23,8 @@ private HOME and runtime directory: `ssh -L` forwards, say), timed python3 -B test/daemon.py --clients the replica clients alone + python3 -B test/daemon.py --far + the far clients and their blob cache alone """ import fcntl import os @@ -65,6 +67,7 @@ class Term: self.out = bytearray() self.lock = threading.Lock() self.closing = False + self.tail = b'' self.reader = threading.Thread(target=self.drain, daemon=True) self.reader.start() @@ -83,7 +86,9 @@ class Term: return with self.lock: self.out += got - if b'\x1b[5n' in got: + # A request split across two reads is answered too. + seen, self.tail = self.tail + got, got[-3:] + if b'\x1b[5n' in seen: try: os.write(self.master, b'\x1b[0n') except OSError: @@ -183,8 +188,9 @@ 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 = 15 + 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 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -212,6 +218,7 @@ class Replica: is read, whole.""" name = session.encode() self.send(0x01, struct.pack('= 2) + time.sleep(.5) + # B: a second client on this machine, and put off the session's + # state once quiet: its first join from disk, its rejoin from the + # core it left. + tb, b = client('b', {'PARDES_TEST_DIVERGE': '1'}) + os.write(tb.master, b'jj') + time.sleep(.5) + os.write(ta.master, b'jj') + wait_for('client B diverged and joined again', lambda: b'joined again' in tb.text(), timeout=30) + typed('b', tb, b'from-B') + typed('a', ta, b'again-A') + detach('b', tb, b) + detach('a', ta, a) + a_joins, b_joins = joins(root / 'a.stats'), joins(root / 'b.stats') + assert len(a_joins) == 1 and len(b_joins) == 2, (a_joins, b_joins) + assert a_joins[0]['from_disk'] == 0 and a_joins[0]['from_memory'] == 0 and a_joins[0]['sent_kb'] > 100, a_joins + assert b_joins[0]['from_disk'] >= b_joins[0]['blobs'] - 2 and b_joins[0]['sent_kb'] < a_joins[0]['sent_kb'], b_joins + assert b_joins[1]['from_memory'] >= b_joins[1]['blobs'] - 1 and b_joins[1]['sent_kb'] < 64, b_joins + # C: a client started again after them: what they made since they + # joined is on disk too (Have.keep at their end). + tc, c = client('c') + typed('c', tc, b'from-C') + detach('c', tc, c) + c_join = joins(root / 'c.stats')[0] + assert c_join['from_disk'] >= c_join['blobs'] - 1 and c_join['sent_kb'] < 64, c_join + # D: the cache corrupted and cut short by hand: a miss each, sent again. + files = sorted(cached(cache).items(), key=lambda f: -f[1]) + for k, (name, size) in enumerate(files): + p = cache / 'pardes' / 'blobs' / name + if k % 2: + p.write_bytes(p.read_bytes()[:size // 2]) + else: + data = bytearray(p.read_bytes()) + data[size // 2] ^= 0x55 + p.write_bytes(bytes(data)) + td, d = client('d') + typed('d', td, b'from-D') + detach('d', td, d) + d_join = joins(root / 'd.stats')[0] + assert d_join['from_disk'] == 0 and d_join['sent_kb'] > 100, d_join + # E: the cache full (a 1 MiB cap, held by older files): they go first, + # and the cache stays under its cap. + for k in range(2): + p = cache / 'pardes' / 'blobs' / f'junk-{k}' + p.write_bytes(b'j' * (512 << 10)) + os.utime(p, (1000 + k, 1000 + k)) + te, e = client('e', {'PARDES_BLOB_CACHE_MB': '1'}) + typed('e', te, b'from-E') + detach('e', te, e) + left = cached(cache) + assert not [n for n in left if n.startswith('junk')] and sum(left.values()) <= 1 << 20, left + tf, f = client('f', {'PARDES_BLOB_CACHE_MB': '1'}) + typed('f', tf, b'from-F') + 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)) + 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 + print('daemon: far clients: the first sent {} KB; a second from disk ({} of {} blobs), its rejoin from memory ' + '({} of {}); one started again from disk ({} of {}); after the cache was spoilt by hand sent {} KB; with ' + 'the cache full it stayed under its cap; every digest agreed ({} checks)'.format( + a_joins[0]['sent_kb'], b_joins[0]['from_disk'], b_joins[0]['blobs'], b_joins[1]['from_memory'], + b_joins[1]['blobs'], c_join['from_disk'], c_join['blobs'], d_join['sent_kb'], + sum(len(checks(root / f'{n}.check')) for n in 'abcdef'))) + try: + nine.write(f'/pane/{pane}/exec', b'Exit\n') + except OSError as err: + assert 'Exit again' in str(err), err + 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) + 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'] @@ -478,6 +659,7 @@ def main(binary, cycles): started.append(daemon) replicas(root, binary, session, daemon) clients(binary) + far(binary) shell = terminals(client)[0] shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) started.append(shell_pid) @@ -1078,6 +1260,8 @@ if __name__ == '__main__': 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] == ['--far']: + far(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/tools/shared_libs_check.zig b/tools/shared_libs_check.zig index d18c6850..7b3174e4 100644 --- a/tools/shared_libs_check.zig +++ b/tools/shared_libs_check.zig @@ -45,6 +45,8 @@ const always = [_]Family{ .{ .what = "a second zlib", .symbols = &.{ "inflate_table", "inflate_fast", "deflate_slow" }, .most = 1 }, // One FreeType: the build's, which MuPDF is compiled against. .{ .what = "a second FreeType", .symbols = &.{ "FT_Init_FreeType", "FT_New_Library" }, .most = 1 }, + // One zstd: the build's, a far replica's stream (src/replica.zig). + .{ .what = "a second zstd", .symbols = &.{ "ZSTD_compressStream2", "ZSTD_decompressStream", "ZSTD_createCCtx_advanced" }, .most = 1 }, }; pub fn main(init: std.process.Init) !void { -- cgit v1.3 From cb2688602b3f668790a9c35890973af80143697f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 06:25:57 -0300 Subject: gui: the window a replica client (pardes-gui --attach=: and :, as the tty's, far through ssh as amort's: its blobs by hash and its stream in zstd) and a window session serving replicas (a terminal's client or a window's): the client feeds its replica what its window does, read over its own view (views.within) and applied after, draws its view between steps, takes the stream on its own reader and rejoins as the tty's does (replica.Link, the client's half shared with it, the far path's Have with it); the session takes clients at its step boundary, one person's input a wait, a client's view no window of its own, its font and opacity answers taped; a client says its frontend first (Event.frontend, in wire 16 beside the far word) and the tty and window builds step alike (one desktop semantics: each has the other's words, which a view takes by its own frontend, builtins.registry.here); a check asked 300 ms after the last input and check, not per wake; test/daemon.py --window-clients Co-Authored-By: Claude Opus 5.5 --- src/Output.zig | 22 +- src/builtins.zig | 58 ++++- src/builtins_json.zig | 13 +- src/config.zig | 2 +- src/detached/wire.zig | 35 ++- src/draw.zig | 26 +-- src/exec.zig | 8 +- src/gui/gui.zig | 573 +++++++++++++++++++++++++++++++++++++++++++++----- src/main.zig | 27 ++- src/ninep/ctl.zig | 38 ++-- src/ninep/tree.zig | 2 +- src/pardes.zig | 50 ++++- src/replica.zig | 310 +++++++++++++++++++++++++++ src/tape.zig | 15 ++ src/tty/tty.zig | 311 +++++---------------------- src/views.zig | 54 +++-- test/daemon.py | 188 +++++++++++++++++ test/output.zig | 9 +- 18 files changed, 1351 insertions(+), 390 deletions(-) (limited to 'src/replica.zig') diff --git a/src/Output.zig b/src/Output.zig index 29e49180..359db1cf 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -905,7 +905,7 @@ fn openStepped(p: *Pardes, id: usize, from: Origin, content: []u8) !void { look.armLookWalk(p, free); } -fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { +fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { const full_header = "pardes builtins, and how to run each:\nSPC and its keys, a chord, a button, the\ntopbar - or the name, executed anywhere.\n\n"; const group_header = "pardes builtins under SPC"; const calls_help = "\nCallstack: static caller chains,\nwith signatures and clickable call sites.\nCallers / Callees: one direction at a time.\n"; @@ -934,8 +934,10 @@ fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { } for (pardes.builtin_rows) |row| { // a path-less builtin filters as the empty path: in the full listing - // (which starts with nothing) and out of every group + // (which starts with nothing) and out of every group; a window's own + // word is none in a terminal's view if (!std.mem.startsWith(u8, row.path orelse "", prefix)) continue; + if (!builtins.registry.here(p, row.cmd)) continue; try w.print("{s}\n", .{row.line}); } if (show_calls_help) try w.writeAll(calls_help); @@ -966,21 +968,23 @@ fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { } pub fn openHelp(p: *Pardes, id: usize, prefix: []const u8) !void { - const content = try helpContent(p.gpa, prefix); + const content = try helpContent(p, p.gpa, prefix); // content is handed off unfreed on purpose: openRead adopts it or frees // it, and nothing between the alloc above and this line can fail. return openRead(p, id, .{ .cmd = .Help }, prefix, content); } test "full Help renders builtin rows, a call hierarchy guide, then language keys" { - const content = try helpContent(std.testing.allocator, ""); + const core = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + const content = try helpContent(core, std.testing.allocator, ""); defer std.testing.allocator.free(content); // FIRST blank line: the end of the header (the footer opens with one too) const body = content[(std.mem.indexOf(u8, content, "\n\n") orelse return error.MissingHelpHeader) + 2 ..]; var lines = std.mem.splitScalar(u8, body, '\n'); - for (pardes.builtin_rows) |row| + for (pardes.builtin_rows) |row| if (builtins.registry.here(core, row.cmd)) try std.testing.expectEqualStrings(row.line, lines.next() orelse return error.MissingBuiltinHelpRow); // The short call guide disambiguates the three related commands. @@ -995,7 +999,7 @@ test "full Help renders builtin rows, a call hierarchy guide, then language keys return error.MissingLanguageKeys); try std.testing.expect(std.mem.indexOf(u8, body, "\ngd gD gy gi gr goto: definition,\n") != null); // The language group also explains call commands, without the motion footer. - const group = try helpContent(std.testing.allocator, "l"); + const group = try helpContent(core, std.testing.allocator, "l"); defer std.testing.allocator.free(group); try std.testing.expect(std.mem.indexOf(u8, group, "language keys") == null); try std.testing.expect(std.mem.indexOf(u8, group, "Callstack: static caller chains") != null); @@ -1013,13 +1017,13 @@ pub fn openConfig(p: *Pardes, id: usize) !void { .locations = located.written(), .default_dump_dir = if (host_fs) (pardes.dump.defaultDirectory(&dir_buf) orelse "") else "", .startup_config_path = p.opts.startup_config_path, - .platform = @tagName(pardes.platform), + .platform = builtins.viewPlatform(p), .theme_name = p.theme().name, .theme_file = pardes.colors.loadedThemeFile(p), .compiled_default_shell = config.defaultShell(), .hover_delay_frames = config.look_preview_delay_frames, .native_images = p.native_images, - .capabilities = builtins.capabilities, + .capabilities = builtins.viewCapabilities(p), .state = &p.settings, }); const content = try out.toOwnedSlice(); @@ -1061,7 +1065,7 @@ pub fn openEffectCode(p: *Pardes, id: usize, argument: []const u8) !void { .transition, .scene => {}, else => return error.NotAnEffect, } - if (!setting.enabled(builtins.capabilities)) return error.EffectUnavailable; + if (!setting.enabled(builtins.viewCapabilities(p))) return error.EffectUnavailable; // Without -Dembed-sources the paths would name nothing this build can open. if (!limits.embedded_sources) return error.EffectUnavailable; const paths = effect_sources.forSetting(setting) orelse diff --git a/src/builtins.zig b/src/builtins.zig index 239edeaf..69ac3113 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -16,7 +16,8 @@ const builtin = @import("builtin"); const Header = @import("esp32p4_gpio.zig").Header; const limits = @import("memory.zig").limits; -pub const capabilities: config.Runtime.Capabilities = .{ +/// What this build's own frontend draws and its host does: the render's. +pub const frontend: config.Runtime.Capabilities = .{ .font_picker = pardes.font_picker, .panel_transitions = pardes.hosted, .scene_shaders = pardes.platform == .gui or pardes.platform == .macos, @@ -32,6 +33,30 @@ pub const capabilities: config.Runtime.Capabilities = .{ .lift = pardes.platform == .gui, }; +/// The terminal's build and the window's (docs/next-daemon.md, "Phase 6: +/// GUI replica notes"): each has the other's words, which a view takes as +/// its own frontend does (`viewCapabilities`), so a terminal's replica of a +/// window's session steps as the window's own does, and back. +pub const desktop = pardes.platform == .tty or pardes.platform == .gui; +const window_capabilities: config.Runtime.Capabilities = .{ .font_picker = true, .panel_transitions = true, .scene_shaders = true, .tagline_font_size = true, .window_opacity = true, .window_blur = false, .ligatures = true, .lift = true }; +const terminal_capabilities: config.Runtime.Capabilities = .{ .font_picker = false, .panel_transitions = true, .scene_shaders = false, .tagline_font_size = false, .window_opacity = false, .window_blur = false, .ligatures = false, .lift = false }; + +/// The words this build has: a desktop build's are the window's. +pub const capabilities: config.Runtime.Capabilities = if (desktop) window_capabilities else frontend; + +/// What the live view's frontend takes: a window's, or a terminal's +/// (`Pardes.windowed`, which its client says, Event.frontend). +pub fn viewCapabilities(p: *const Pardes) config.Runtime.Capabilities { + if (comptime !desktop) return frontend; + return if (p.windowed) window_capabilities else terminal_capabilities; +} + +/// The live view's frontend, by name (the Settings report's platform). +pub fn viewPlatform(p: *const Pardes) []const u8 { + if (comptime !desktop) return @tagName(pardes.platform); + return if (p.windowed) "gui" else "tty"; +} + /// Where a builtin acts: at the pane it runs from, or on the editor as a /// whole. acme keeps its window verbs on a window's ctl and its session /// commands elsewhere, as webfs and upas/fs keep session settings on a root @@ -93,6 +118,7 @@ fn isEnabled(comptime T: type) bool { return !@hasDecl(T, "enabled") or T.enabled; } + /// Why `word`, a builtin some build has, is none in this one: `GUI-only` /// for the GUI's own (`gui_only`), else `not in this build`; null for a /// word that is one here, or in no build. @@ -155,6 +181,21 @@ pub const OutputTraits = struct { // Keep enum-dependent signatures out of the outer declaration walk. pub const registry = struct { + /// Whether builtin `b` is one the live view's frontend takes: a window's + /// own words (a builtin that says `in_window`, the settings a terminal + /// cannot show) in a window's view alone. The rest of a build's words are + /// every view's. + pub fn here(p: *const Pardes, b: Builtin()) bool { + return hereFor(b, viewCapabilities(p), !desktop or p.windowed); + } + + /// `here` for a frontend of these capabilities, windowed or not. + pub fn hereFor(b: Builtin(), caps: config.Runtime.Capabilities, windowed: bool) bool { + inline for (comptime manualBuiltinList(), 0..) |T, i| if (@backingInt(b) == i) return !@hasDecl(T, "in_window") or windowed; + inline for (comptime settingList(), comptime manualBuiltinList().len..) |setting, i| if (@backingInt(b) == i) return setting.enabled(caps); + unreachable; + } + pub fn Builtin() type { // Duplicate-name validation compares every pair. @setEvalBranchQuota(200_000); @@ -453,11 +494,14 @@ test "Look does not announce itself on the message row; Del still does" { } test "Ligatures is a setting only in the shell that shapes text" { - const exists = @hasField(registry.Builtin(), "Ligatures"); - try std.testing.expectEqual(pardes.platform == .gui, exists); const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); - try std.testing.expectEqual(exists, p.executeBuiltinLine(p.active, "Ligatures off")); + // A desktop build has the word, which a window's view takes alone. + try std.testing.expectEqual(desktop, @hasField(registry.Builtin(), "Ligatures")); + const exists = registry.here(p, .Ligatures); + try std.testing.expectEqual(pardes.platform == .gui, exists); + // Taken either way: run, or said to be GUI-only. + try std.testing.expect(p.executeBuiltinLine(p.active, "Ligatures off")); try std.testing.expectEqual(!exists, p.settings.ligatures); var storage: [8192]u8 = undefined; var out: std.Io.Writer = .fixed(&storage); @@ -468,7 +512,7 @@ test "Ligatures is a setting only in the shell that shapes text" { .compiled_default_shell = config.defaultShell(), .hover_delay_frames = null, .native_images = false, - .capabilities = capabilities, + .capabilities = viewCapabilities(p), .state = &p.settings, }); try std.testing.expectEqual(exists, std.mem.indexOf(u8, storage[0..out.end], "Ligatures off\n") != null); @@ -912,6 +956,7 @@ pub const Fonts = struct { pub const output: OutputTraits = .{ .name = config.fonts_buffer, .steps = true, .commands = true }; pub const enabled = capabilities.font_picker; pub const gui_only = true; + pub const in_window = true; pub fn run(c: Ctx) void { if (comptime enabled) apply(c) else unreachable; } @@ -1665,8 +1710,9 @@ pub const Pet = struct { pub const scope: Scope = .session; pub const takes_arg = true; pub const requires_arg = true; - pub const enabled = pardes.platform == .gui; + pub const enabled = desktop; pub const gui_only = true; + pub const in_window = true; pub fn run(c: Ctx) void { const name = std.mem.trim(u8, c.arg orelse return, " \t\r\n"); c.p.settings.pet = std.meta.stringToEnum(@TypeOf(c.p.settings.pet), name) orelse return; diff --git a/src/builtins_json.zig b/src/builtins_json.zig index 3598a3e2..489f1e22 100644 --- a/src/builtins_json.zig +++ b/src/builtins_json.zig @@ -20,9 +20,16 @@ fn lessName(_: void, a: Entry, b: Entry) bool { } pub fn write(w: *std.Io.Writer) !void { - var order: [pardes.builtin_rows.len]Entry = undefined; - for (pardes.builtin_rows, 0..) |row, i| order[i] = .{ .name = @tagName(row.cmd), .row = i }; - std.mem.sort(Entry, &order, {}, lessName); + var all: [pardes.builtin_rows.len]Entry = undefined; + var len: usize = 0; + // This build's own frontend's words (a desktop build has the other's + // too, which a view of its kind takes: builtins.zig `here`). + for (pardes.builtin_rows, 0..) |row, i| if (pardes.builtins.registry.hereFor(row.cmd, pardes.builtins.frontend, pardes.platform != .tty)) { + all[len] = .{ .name = @tagName(row.cmd), .row = i }; + len += 1; + }; + const order = all[0..len]; + std.mem.sort(Entry, order, {}, lessName); try w.writeAll("[\n"); for (order, 0..) |e, n| { const row = pardes.builtin_rows[e.row]; diff --git a/src/config.zig b/src/config.zig index 742e9010..ebca14ea 100644 --- a/src/config.zig +++ b/src/config.zig @@ -164,7 +164,7 @@ pub const leader_path = paths: { table.set(.Font, null); table.set(.TaglineSize, null); } - if (pardes.platform == .gui) table.set(.Pet, null); + if (builtins.Pet.enabled) table.set(.Pet, null); table.set(.MessageAnimation, null); table.set(.MessageLinger, null); table.set(.MessageFall, null); diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 2f2b95c8..119601fc 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -11,28 +11,40 @@ const config = @import("pardes_config"); /// 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); 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 +/// content cache and zstd notes), and a client says its frontend (a window +/// or a terminal) as its first input, a window replicating a terminal's +/// session and back (phase 6's GUI replica notes); 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 = 16; +/// The terminal's build and the window's step alike (`semantics`). +const family = switch (config.platform) { + .tty, .gui => "desktop", + else => @tagName(config.platform), +}; + /// 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 /// and commit, the platform and build options, every dependency's content /// hash (grammars, ghostty, Unicode) and the walker codec's assumptions /// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD /// session is the main case, and what differs there is the host's, taped. +/// Nor the terminal's build against the window's: their cores step alike, +/// the GUI's own words by the view's frontend (builtins.zig `here`), so a +/// window replicates a terminal's session and a terminal a window's. /// ponytail: an unstamped build (Debug with -Dplatform, every test build) /// has no commit, so two dev trees of one release are told apart only by /// their options and dependencies; -Dstamp-commit stamps one. pub const semantics: u64 = blk: { @setEvalBranchQuota(1_000_000); break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( - "wire {d}\npardes {s} {s}\n{t} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", + "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", .{ version, config.version, config.commit orelse "unstamped", - config.platform, config.syntax_highlighting, config.syntax_zig_grammar, + family, config.syntax_highlighting, config.syntax_zig_grammar, config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf, config.theme_animation, config.workspace_tag, config.embed_sources, config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize), @@ -78,6 +90,8 @@ pub const ClientTag = enum(u8) { touch_scroll = 0x1c, pointer_leave = 0x1d, focus = 0x1e, + /// Which frontend the client is: a window, or a terminal. + frontend = 0x20, }; const ButtonTag = enum(u8) { @@ -505,6 +519,11 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { .touch_scroll => |v| try w.writeInt(u32, @bitCast(v), .little), .pointer_leave => {}, .focus => |focused| try w.writeByte(@intFromBool(focused)), + .frontend => |f| { + try w.writeByte(@intFromBool(f.windowed)); + try w.writeByte(@intFromBool(f.native_images)); + try w.writeByte(@intFromBool(f.native_image_patches)); + }, // See `clientTag`: no tag, so nothing to encode. .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .tick => unreachable, }, @@ -577,6 +596,7 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { .touch_scroll => .{ .event = .{ .touch_scroll = try getF32(&r) } }, .pointer_leave => .{ .event = .pointer_leave }, .focus => .{ .event = .{ .focus = try getBool(&r) } }, + .frontend => .{ .event = .{ .frontend = .{ .windowed = try getBool(&r), .native_images = try getBool(&r), .native_image_patches = try getBool(&r) } } }, }; try end(&r); return msg; @@ -594,7 +614,7 @@ 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 => 0, + .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus, .frontend => 0, .key => |k| k.text.len, .paste => |b| b.len, .command => |line| line.len, @@ -732,9 +752,12 @@ test "detached wire: a session is never told to do a frontend's remembering" { // ...and how far it is, and which of the join's blobs it lacks. "far", "want", // ...and everything a person can do to a window. - "key", "mouse", "resize", + "key", "mouse", "resize", "paste", "command", "pdf_scroll", "pinch", "touch_scroll", "pointer_leave", "focus", + // ...and which of its frontends the person sits at: a window, or a + // terminal (what the view draws, and the words it takes). + "frontend", }; // One: the tag set is exactly that, named rather than counted, so diff --git a/src/draw.zig b/src/draw.zig index 03bc0cf9..8503197c 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -133,7 +133,7 @@ pub fn place(p: *Pardes, s: *Surface) void { // frame's own time, not the stepped animations' 16 ms grid. A grid // snaps (it has no in-between), and so does nothing that shows it. const shown = lift != .off or inactiveDim(p) != 0; - const motion = animation.Motion.of(if (shown and builtins.capabilities.lift) p.settings.motion else .off); + const motion = animation.Motion.of(if (shown and builtins.frontend.lift) p.settings.motion else .off); // A flavour exaggerates what is only a few pixels (Motion.gain). spring.retarget(if (id == p.active) animation.Motion.of(p.settings.motion).gain else 0, p.now_ns, motion); _ = spring.step(p.now_ns); @@ -224,7 +224,7 @@ fn glideCursor(p: *Pardes, s: *Surface) void { s.chrome.cursor_glide = 0; s.chrome.cursor_alpha = 1; s.chrome.cursor_idle_ms = std.math.maxInt(u32); - if (!builtins.capabilities.lift) return; + if (!builtins.frontend.lift) return; const box = focusedCursorBox(p, s) orelse { // No cursor drawn (a PDF or picture took focus, a program hid its // own): a glide under way has nowhere to land, and its springs, @@ -1737,7 +1737,7 @@ test "a lift is sampled at each frame's own time, and asks for frames only while const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); // A grid has no in-between: there a lift snaps. - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; _ = try p.setTestFile("text\n"); p.newScratchBelow(0); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -1912,7 +1912,7 @@ test "with no lift, no dim and no motion a focus switch asks for no frames" { test "each Motion flavour reaches the lift as its own trajectory" { // A grid snaps; the flavours are a pixel shell's. - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); _ = try p.setTestFile("text\n"); @@ -1951,7 +1951,7 @@ test "each Motion flavour reaches the lift as its own trajectory" { } test "the caret is its own body: a page leaves it still under the moving text, a wheel scroll lands its springs on its line's new row" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); var text: std.ArrayList(u8) = .empty; @@ -1995,7 +1995,7 @@ test "the caret is its own body: a page leaves it still under the moving text, a } test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([20][20]u8, @splat("some text on a line\n".*)))); @@ -2078,7 +2078,7 @@ test "the focused cursor glides a jump, eases a step, the thin caret too, and is } test "a tag caret past its row's end glides to the tag's right edge, where the layer's is" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 16 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 60, .rows = 16, .row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 } } }); @@ -2128,7 +2128,7 @@ test "a tag caret past its row's end glides to the tag's right edge, where the l } test "a terminal's cursor lands with scrolling output, never gliding against it" { - if (!builtins.capabilities.lift or !pardes.terminal_panes) return error.SkipZigTest; + if (!builtins.frontend.lift or !pardes.terminal_panes) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 12 }); defer p.deinit(); while (p.nextEffect()) |_| {} @@ -2323,7 +2323,7 @@ fn twoColumns(p: *Pardes, arena: *std.heap.ArenaAllocator) !void { } test "while its pane slides the cursor rides with it, at the same cell of the pane every frame" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2376,7 +2376,7 @@ test "while its pane slides the cursor rides with it, at the same cell of the pa } test "focus moved to another column glides from where the cursor was drawn" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2412,7 +2412,7 @@ test "focus moved to another column glides from where the cursor was drawn" { } test "a click that takes focus glides the cursor once, to where it lands on release" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2446,7 +2446,7 @@ test "a click that takes focus glides the cursor once, to where it lands on rele } test "focus moving to a pane with no cursor while the cursor glides lets the session go idle" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2475,7 +2475,7 @@ test "focus moving to a pane with no cursor while the cursor glides lets the ses } test "a pane focus reaches brightens on the same spring the one it leaves dims on, and Motion off snaps" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([4][20]u8, @splat("some text on a line\n".*)))); diff --git a/src/exec.zig b/src/exec.zig index df397b37..e1c13daa 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -1033,7 +1033,7 @@ fn applyInitText(p: *Pardes, text: []const u8, settings_only: bool) void { if (settings_only and setting == null and std.meta.stringToEnum(Builtin, word[0 .. std.mem.indexOfAny(u8, word, " \t+") orelse word.len]) != null) continue; // One init file serves the window and the terminal: a setting only // the other has (Font, GripWidth in a terminal) is no failure here. - if (setting) |s| if (!s.enabled(builtins.capabilities)) continue; + if (setting) |s| if (!s.enabled(builtins.viewCapabilities(p))) continue; const failures = p.fs.failures; const known = executeBuiltinLine(p, p.active, line); if (known and p.fs.failures == failures) continue; @@ -1057,6 +1057,12 @@ pub fn runBuiltin(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]co /// `runBuiltin` saying whether a gesture on the pane's body made it. pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]const u8, from_body: bool) void { if (!normal.multiOnce(p)) return; // a builtin is per-keystroke, never per-cursor + // A window's own word in a terminal's view (Fonts, Lift): said, as a + // word a build lacks is, and never run. + if (!builtins.registry.here(p, b)) { + var said: [96]u8 = undefined; + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is GUI-only, invalid here", .{@tagName(b)}) catch "GUI-only, invalid here"); + } // No pane at all (the window Delcol emptied): a word runs all the same, // with a stand-in that is no pane (New, Newcol, Exit are what make sense). const pane = p.panes[id] orelse (if (builtins.registry.scope(b) == .session or p.anyPane() == null) p.standIn() orelse return else return); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a88a7335..f62871c4 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -24,6 +24,8 @@ const ninep_io = @import("../9p_io.zig"); const daemon = @import("../daemon.zig"); const upgrade = @import("../upgrade.zig"); +const replica = @import("../replica.zig"); +const wire = @import("../detached/wire.zig"); const tracy = @import("../tracy.zig"); pub const c = @import("gui_c"); // SDL3 and font.h, translated by build.zig @@ -671,24 +673,29 @@ test "closing tombstone overlays but never owns canonical cells" { } fn updateCoreResize(core: *pardes.Pardes, cols: u16, rows: u16, cell_w: u32, cell_h: u32, tag_w: u32, tag_h: u32) bool { + core.update(resizeOf(core, cols, rows, cell_w, cell_h, tag_w, tag_h) orelse return false); + return true; +} + +/// The resize a window of `cols` by `rows` cells of these sizes is to the +/// live view, or null when it is that already. +fn resizeOf(core: anytype, cols: u16, rows: u16, cell_w: u32, cell_h: u32, tag_w: u32, tag_h: u32) ?pardes.Event { const metrics: pardes.RowMetrics = .{ .body_w = @intCast(cell_w), .body_h = @intCast(cell_h), .tagline_w = @intCast(tag_w), .tagline_h = @intCast(tag_h) }; const same_metrics = std.meta.eql(metrics, core.row_metrics); if (comptime pardes.pdf_enabled) { const cell_px_w: u16 = @intCast(@min(@max(1, cell_w), std.math.maxInt(u16))); const cell_px_h: u16 = @intCast(@min(@max(1, cell_h), std.math.maxInt(u16))); if (cols == core.screen_w and rows == core.screen_h and - cell_px_w == core.cell_pixels.w and cell_px_h == core.cell_pixels.h and same_metrics) return false; - core.update(.{ .resize = .{ + cell_px_w == core.cell_pixels.w and cell_px_h == core.cell_pixels.h and same_metrics) return null; + return .{ .resize = .{ .cols = cols, .rows = rows, .cell_pixels = .{ .w = cell_px_w, .h = cell_px_h }, .row_metrics = metrics, - } }); - } else { - if (cols == core.screen_w and rows == core.screen_h and same_metrics) return false; - core.update(.{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = metrics } }); + } }; } - return true; + if (cols == core.screen_w and rows == core.screen_h and same_metrics) return null; + return .{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = metrics } }; } const TouchSample = struct { x: f32 = 0, y: f32 = 0, pressure: f32 = 1 }; @@ -1963,12 +1970,23 @@ fn setTransitionFields( } } -pub const run = runNative; +pub fn run(init: std.process.Init, opts: pardes.Options, headless: bool) !void { + return runNative(init, opts, headless, null); +} + +/// `pardes-gui --attach=:` (and `:`, +/// through ssh, main.zig): this window a replica client of a session +/// elsewhere, as the tty's `remote` is a terminal's (`remoteSession`). +pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far: bool) !void { + return runNative(init, opts, false, .{ .path = path, .session = session, .far = far }); +} -fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool) !void { +const Dial = struct { path: []const u8, session: []const u8, far: bool }; + +fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, dial: ?Dial) !void { const gpa = init.gpa; const env = init.environ_map; - if (env.get("PARDES_TEST_GRID") != null) return runGrid(init, opts_in); + if (dial == null and env.get("PARDES_TEST_GRID") != null) return runGrid(init, opts_in); const test_mode = env.get("PARDES_TEST") != null; const capture_dir = env.get("PARDES_TEST_CAPTURE_DIR"); @@ -2294,6 +2312,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool) !v // compositor moves it, as it does a title bar. if (!test_mode and !c.SDL_SetWindowHitTest(window, hitTest, &g)) log.warn("SDL_SetWindowHitTest: {s}", .{c.SDL_GetError()}); + if (dial) |to| return remoteSession(init, &g, opts_in, test_mode, to); try localSession(init, &g, opts_in, test_mode, headless); } @@ -2458,6 +2477,10 @@ fn localSession( frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null; const host = shell.host(); core.host = host; + shell.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake = sdlWake, .alone_unattended = headless }; + defer shell.peers.deinit(); + // Before the core goes: each client sees its link end. + defer shell.peers.dropAll(shell.core); while (core.nextEffect()) |e| core.perform(e); // An upgraded session's shells, once a fresh session's own has a @@ -2496,20 +2519,25 @@ fn localSession( if (headless) { shell.closeTheWindow(); // Nothing else may wake the first wait. - daemon.watch(&queue, deliverNote); + daemon.watch(&shell, deliverNote); } while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Its unit to each replica client, and theirs taken in. + shell.peers.boundary(core); upgrade.locate(); // Asked in the step (the word), done between steps: the session whole. if (shell.upgrade_asked) |pane| shell.upgradeNow(pane); // A window asked for again, or an adopted shell, from the daemon's // thread through the queue; started after the first frame. - daemon.watch(&queue, deliverNote); + daemon.watch(&shell, deliverNote); if (core.quit) break; // a session that ended does not restore into one if (core.takeRestore()) |rp| blk: { + // ponytail: a Restore replaces the core its clients replicate; + // they are let go, and attach again (as the tty's). + shell.peers.dropAll(core); const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| { core.reportError(core.active, "Restore", err); break :blk; @@ -2525,6 +2553,7 @@ fn localSession( // the others close. shell.closeOtherWindows(); shell.gview = 0; + shell.windowed = 1; shell.stopPtys(); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( inotify_fd, @@ -2561,6 +2590,314 @@ fn localSession( } } +/// A window's link to a session elsewhere (`remoteSession`): what the +/// window's input made, fed to the replica once its own view is put back, +/// and what the link's reader took off the stream. +const Client = struct { + link: replica.Link, + io: std.Io, + gpa: std.mem.Allocator, + /// The window's inputs since the last feed, their texts in `texts`: + /// read over its own view (`Replica.within`), applied after it. + pending: std.ArrayListUnmanaged(pardes.Event) = .empty, + texts: std.heap.ArenaAllocator, + /// The reader's, under `lock`: what it took, and whether the link ended. + lock: std.Io.Mutex = .init, + inbox: std.ArrayListUnmanaged(replica.Said) = .empty, + taken: std.ArrayListUnmanaged(replica.Said) = .empty, + ended: bool = false, + + fn deinit(cl: *Client) void { + cl.pending.deinit(cl.gpa); + cl.texts.deinit(); + cl.inbox.deinit(cl.gpa); + cl.taken.deinit(cl.gpa); + } + + fn post(cl: *Client, ev: pardes.Event) void { + var e = ev; + const a = cl.texts.allocator(); + switch (e) { + .key => |*k| k.text = a.dupe(u8, k.text) catch "", + .paste, .command => |*b| b.* = a.dupe(u8, b.*) catch return, + else => {}, + } + cl.pending.append(cl.gpa, e) catch {}; + } + + /// What the window's input made, applied at once and sent. + fn feed(cl: *Client) void { + for (cl.pending.items) |ev| cl.link.input(ev); + cl.pending.clearRetainingCapacity(); + _ = cl.texts.reset(.retain_capacity); + } + + /// The reader's: a message of the stream, for the loop, which it wakes. + fn said(cl: *Client, msg: replica.Said) bool { + cl.lock.lockUncancelable(cl.io); + defer cl.lock.unlock(cl.io); + cl.inbox.append(cl.gpa, msg) catch return false; + sdlWake(null); + return true; + } + + /// What the reader took, in its order, and the link's end after it. + fn takeSaid(cl: *Client) void { + cl.lock.lockUncancelable(cl.io); + std.mem.swap(std.ArrayListUnmanaged(replica.Said), &cl.inbox, &cl.taken); + const ended = cl.ended; + cl.lock.unlock(cl.io); + for (cl.taken.items) |msg| { + // PARDES_TEST_LATENCY: when a terminal's delta came (an echo's). + if (msg == .screen) latencyNote("S {d}\n", .{monoNs()}); + cl.link.take(msg); + } + cl.taken.clearRetainingCapacity(); + if (ended) { + cl.link.quit = true; + cl.link.gone = true; + } + } + + /// Its own view drawn in the window, outside any step, as the tty's + /// client draws its terminal (tty.zig `Remote.draw`): what presenting + /// writes in the core (a frame skipped, one owed) is put back. + fn draw(cl: *Client, s: *Shell) void { + const r = cl.link.r; + const core = 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; + r.draw(.{ .ctx = s, .f = present }); + core.frames.mark_render = core.frames.mark_update; + cl.link.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, cl.link.owed); + core.present_skipped = skipped; + core.needs_frame = needs; + cl.link.drawn(s.presented); + } +}; + +/// The session's stream, onto the client's inbox: its units, and its end. +fn clientReader(cl: *Client, fd: c_int, rest: *replica.Framer) void { + replica.readLink(cl.gpa, fd, rest, cl, Client.said); + cl.lock.lockUncancelable(cl.io); + cl.ended = true; + cl.lock.unlock(cl.io); + sdlWake(null); +} + +/// What the window does over its own view (`Replica.within`), its inputs +/// posted to the client (`Input.remote`): an SDL event dispatched, its size +/// (the tagline's face too), the pointer under a frame that moved, the +/// timedemo's and the test feed's steps. +const OwnView = struct { + g: *Gui, + in: *Input, + s: *Shell, + sev: ?*const c.SDL_Event = null, + pointer: bool = false, + eof: bool = false, + + fn run(v: *OwnView, core: *pardes.Pardes) anyerror!void { + if (v.sev) |sev| return dispatch(v.g, v.in, sev); + const g = v.g; + if (fitTaglineFont(g, core.settings.font.tagline_percent)) v.s.remote.?.link.dirty = true; + const geom = windowCells(g); + if (resizeOf(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) |ev| v.in.post(ev); + if (v.pointer) refreshPresentedPointer(g, v.in); + runDemo(g, v.in, core); + if (v.s.test_mode) { + const r = v.s.feed.pump(v.s.gpa, v.in, g) catch return error.TestFeed; + if (r.eof) v.eof = true; + } + } +}; + +/// `pardes-gui --attach=:`: the window, made as a session's +/// is, a replica client of that session (docs/next-daemon.md, "Phase 6: GUI +/// replica notes"). Its own core, loaded from the session's dump, applies +/// its input at once (src/replica.zig) and draws its own view; the +/// session's units are the truth it rolls back to. The tty's `remote`, with +/// a window for a terminal. +fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_mode: bool, to: Dial) !void { + const io = init.io; + const gpa = init.gpa; + const Said = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [800]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes-gui: --attach: " ++ fmt ++ "\n", args) catch "pardes-gui: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + var why: [600]u8 = @splat(0); + // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash + // and its stream in zstd, as the tty's client (replica.zig `Have`). + const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else to.far; + var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; + defer have_store.deinit(); + const have: ?*replica.Have = if (far) &have_store else null; + var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + var joined = replica.dial(gpa, to.path, to.session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); + + 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_in; + o.image_allocator = allocs.image; + o.pdf_allocator = allocs.pdf; + var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + + var client: Client = .{ .link = .{ .r = r, .fd = joined.fd }, .io = io, .gpa = gpa, .texts = .init(gpa) }; + defer client.deinit(); + const link = &client.link; + try link.fromEnv(gpa, init.environ_map); + defer link.deinit(gpa); + // A client spawns, watches and serves nothing: the Shell's tables are + // empty ones, there for what draws. + var ptys: [pardes.MAX_PANES]?Pty = @splat(null); + var gens: [pardes.MAX_PANES]u32 = @splat(0); + var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp }; + defer queue.deinit(); + var pipe_tasks: PipeTasks = .{}; + var watches: file_watch.Table = @splat(null); + const prompt_rcs: host_io.Shell.PromptFiles = .{}; + var shell: Shell = .{ + .core = r.core(), + .gui = g, + .io = io, + .gpa = gpa, + .lsp_allocator = allocs.lsp, + .prompt_rcs = &prompt_rcs, + .ptys = &ptys, + .gens = &gens, + .queue = &queue, + .pipe_tasks = &pipe_tasks, + .inotify_fd = -1, + .watches = &watches, + .test_mode = test_mode, + .remote = &client, + }; + defer shell.shaders.deinit(gpa, -1, &watches); + g.no_blink = test_mode; + defer g.hit_core = null; + _ = c.SDL_StartTextInput(g.window); + if (test_mode) setStdinRaw() catch {}; + // A join a pass: the first, and one after the replica diverged or the + // session let it go to upgrade. The window stays as it is. + while (true) { + r.gmtoff = joined.gmtoff; + const core = r.core(); + shell.core = core; + shell.gview = joined.view; + shell.surface = null; + g.hit_core = core; + frame_probe = &core.frames; + link.reset(r, joined.fd); + link.joinedAs(&joined, dial_ms); + client.ended = false; + // ponytail: why it joined again is the window's title (and its + // log) until a key, where a terminal's client draws it over its + // frame: a window's frame is the core's cells, which it cannot write. + var titled = link.notice_len > 0; + if (titled) { + var title: [260]u8 = undefined; + _ = c.SDL_SetWindowTitle(g.window, std.fmt.bufPrintSentinel(&title, "pardes: {s}", .{link.notice()}, 0) catch "pardes"); + log.warn("{s}", .{link.notice()}); + } + const reader = try std.Thread.spawn(.{}, clientReader, .{ &client, joined.fd, &joined.rest }); + var in: Input = .{ .core = core, .shell = &shell, .remote = &client }; + // What it is, then its size, are its view's first inputs. + link.input(.{ .frontend = .{ .windowed = true, .native_images = true, .native_image_patches = true } }); + var drew = false; + while (!link.quit) { + core.frames.period_ns = refreshNs(g); + syncLigatures(g, core); + { + var passes: [Post.max]ShaderBuild.Pass = undefined; + g.post.sync(gpa, g.device, g.swapchain_format, shell.shaders.view(&core.settings.post, &passes), shell.shaders.revision, core); + } + var own: OwnView = .{ .g = g, .in = &in, .s = &shell, .pointer = drew }; + r.within(&own, OwnView.run); + client.feed(); + if (own.eof) break; + // Its predicted echo judged and caught up, once a frame. + if (r.echoFrame()) link.dirty = true; + drew = false; + if (link.dirty) { + client.draw(&shell); + drew = shell.presented; + } + const predicting = r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0; + var ms: c_int = if (test_mode) 16 else -1; + if (demoDue(g)) |due| ms = if (ms < 0) due else @min(ms, due); + if (predicting or link.owed) ms = if (ms < 0) frame_ms else @min(ms, frame_ms); + if (link.quietCheck()) |wait| ms = if (ms < 0) @intCast(wait) else @min(ms, @as(c_int, @intCast(wait))); + // Idle, its frame drawn: the next input needs no copy taken. + if (ms != 0) r.ready(); + var sev = std.mem.zeroes(c.SDL_Event); + g.input_seen = false; + var got = c.SDL_WaitEventTimeout(&sev, ms); + while (got) : (got = c.SDL_PollEvent(&sev)) { + if (sev.type == c.SDL_EVENT_USER) continue; + var one: OwnView = .{ .g = g, .in = &in, .s = &shell, .sev = &sev }; + r.within(&one, OwnView.run); + client.feed(); + if (link.quit) break; + } + client.takeSaid(); + r.settle(); + if (titled and link.notice_len == 0) { + titled = false; + _ = c.SDL_SetWindowTitle(g.window, "pardes"); + } + if (link.owed or r.echo.changed) link.dirty = true; + if (r.diverged != null) link.quit = true; + } + if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); + if (!link.gone) replica.bye(joined.fd); + // The reader ends with the link: what it took meanwhile is let go. + _ = libc.shutdown(joined.fd, 2); + reader.join(); + client.lock.lockUncancelable(io); + for (client.inbox.items) |msg| switch (msg) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }; + client.inbox.clearRetainingCapacity(); + client.lock.unlock(io); + _ = libc.close(joined.fd); + joined.rest.deinit(gpa); + if (!link.rejoins()) { + // What it made since it joined, kept for its next start. + if (have) |h| h.keep(r.core()); + r.deinit(); + if (link.gone) Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}); + return; + } + // 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)}); + 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)}); + dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + } +} + +const frame_ms: c_int = @intCast(pardes.animation.frame_ns / std.time.ns_per_ms); + fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { const io = init.io; const gpa = init.gpa; @@ -3090,14 +3427,33 @@ const Input = struct { /// The shell whose windows these are: a window closed while others /// stay takes its view with it (views.zig). shell: ?*Shell = null, + /// A replica client's (`remote`): its inputs go to its link, never + /// straight to the core, which only reads here. + remote: ?*Client = null, quit: bool = false, fn post(in: *Input, ev: pardes.Event) void { + if (in.remote) |r| return r.post(ev); + // The window's input is its own view's, a replica client's input + // the session's last or not. + if (in.shell) |s| if (s.core.view != s.gview) { + _ = s.windowView(); + }; if (in.core) |core| return core.update(ev); } fn close(in: *Input) void { - if (in.shell) |s| if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); + if (in.remote) |r| { + r.link.quit = true; + in.quit = true; + return; + } + if (in.shell) |s| { + if (s.windowsOpen() > 1) return s.closeView(s.core.view); + // The last window, with replica clients: they keep the session, + // as a Detach would, the daemon holding it. + if (s.peers.count() > 0 and daemon.kept()) return s.closeTheWindow(); + } if (in.core) |core| core.quit = true; in.quit = true; } @@ -3127,7 +3483,9 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { updateDisplayScale(g.window); // Underlines are drawn into the glyphs a logical pixel thick. resetGlyphAtlas(g); - if (in.core) |core| core.needs_frame = true; + if (in.remote) |r| { + r.link.dirty = true; + } else if (in.core) |core| core.needs_frame = true; }, c.SDL_EVENT_WINDOW_FOCUS_GAINED => g.post.focus(true), c.SDL_EVENT_WINDOW_FOCUS_LOST => g.post.focus(false), @@ -3141,7 +3499,7 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), c.SDL_EVENT_WINDOW_FOCUS_GAINED => { in.post(.{ .focus = true }); - if (in.core) |core| upgrade.noticeNewer(core); + if (in.remote == null) if (in.core) |core| upgrade.noticeNewer(core); }, c.SDL_EVENT_WINDOW_FOCUS_LOST => in.post(.{ .focus = false }), c.SDL_EVENT_WINDOW_MOUSE_LEAVE => { @@ -3161,8 +3519,14 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { const want = std.math.clamp(g.px + step, font_px_min, font_px_max); if (want != g.px) { g.px = want; - refitFont(g, in.core); - if (in.core) |core| observeGuiFont(g, core); + // A client's view is sized by its next loop, as its input. + if (in.remote) |r| { + refitFont(g, null); + r.link.dirty = true; + } else { + refitFont(g, in.core); + if (in.core) |core| observeGuiFont(g, core); + } } return; } @@ -3590,6 +3954,18 @@ fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { in.post(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mc.col, .row = mc.row, .tag_hit = mc.tag_hit, .body_hit = mc.body_hit, .ctrl = w.mods.ctrl, .shift = w.mods.shift, .alt = w.mods.alt } }); while (g.program_wheel <= -1) : (g.program_wheel += 1) in.post(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = mc.col, .row = mc.row, .tag_hit = mc.tag_hit, .body_hit = mc.body_hit, .ctrl = w.mods.ctrl, .shift = w.mods.shift, .alt = w.mods.alt } }); + } else if (in.remote != null) { + // ponytail: a replica client's wheel moves the view a + // whole row a turn, with no glide and no touchpad's + // fraction: its core's clock is the session's, which + // moves only as its units come. + if (notchOf(w.y, w.integer_y)) |lines| { + g.program_wheel = @floatFromInt(lines); + } else g.program_wheel = accumulateWheelDelta(g.program_wheel, w.y); + while (g.program_wheel >= 1) : (g.program_wheel -= 1) + in.post(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mc.col, .row = mc.row, .shift = true } }); + while (g.program_wheel <= -1) : (g.program_wheel += 1) + in.post(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = mc.col, .row = mc.row, .shift = true } }); } else { g.scroll_pane = id; g.scroll_col = mc.col; @@ -3680,10 +4056,11 @@ fn heldPointerButton(g: *const Gui) ?pardes.Mouse.Button { return null; } -fn refreshPresentedPointer(g: *Gui, core: *pardes.Pardes) void { +fn refreshPresentedPointer(g: *Gui, in: *Input) void { if (!g.pointer_present) return; + const core = in.core orelse return; const mc = mouseCell(g, core, g.pad_x, g.pad_y) orelse { - if (g.pointer_mapped) core.update(.pointer_leave); + if (g.pointer_mapped) in.post(.pointer_leave); g.pointer_mapped = false; return; }; @@ -3693,7 +4070,7 @@ fn refreshPresentedPointer(g: *Gui, core: *pardes.Pardes) void { g.pointer_cell = mc; if (!moved) return; const held = heldPointerButton(g); - core.update(.{ .mouse = .{ + in.post(.{ .mouse = .{ .button = held orelse .none, .kind = if (held != null) .drag else .motion, .col = mc.col, @@ -3806,6 +4183,22 @@ const Shell = struct { gview: u8 = 0, /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). upgrade_asked: ?u8 = null, + /// This window a replica client of a session elsewhere (`remote`). + remote: ?*Client = null, + /// The session's replica clients (src/replica.zig), on sockets: views + /// of their own with no window here (`windowed`). + peers: replica.Peers = .{}, + /// The views that are this process's windows (the rest are clients'). + windowed: u8 = 1, + /// 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 (tty.zig's `mail_only`). + mail_only: bool = false, + waited: bool = false, + /// With replica clients, a wait takes one person's input (tty.zig + /// `split`): the next is held for the next wait. + took_input: bool = false, + held: ?c.SDL_Event = null, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3894,9 +4287,11 @@ const Shell = struct { /// window, for `pardes-gui --attach` to open one again. fn detachWindow(ctx: ?*anyopaque) void { const s = shellOf(ctx); + // A replica client's: its link ends. + if (s.peers.detach(s.core.view)) return; if (s.gui == null) return s.core.reportFailure(s.core.active, "Detach: no window is open"); // One of several: its window goes, and the session stays in the rest. - if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); + if (s.windowsOpen() > 1) return s.closeView(s.core.view); if (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session; closing its window ends it"); s.closeTheWindow(); } @@ -3906,6 +4301,9 @@ const Shell = struct { const g = s.gui orelse return; const k = s.core.view; if (s.gview == k) return; + // A replica client's view has no window here: the Gui keeps the one + // it holds (`windowView` puts that view back when it acts). + if (s.windowed & (@as(u8, 1) << @intCast(k)) == 0) return; if (s.gview < s.windows.len) copyWindow(&s.windows[s.gview], g); copyWindow(g, &s.windows[k]); s.gview = k; @@ -3916,7 +4314,11 @@ const Shell = struct { if (!pardes.views.several(s.core)) return; const g = s.gui orelse return; const w = c.SDL_GetWindowFromEvent(sev) orelse return; - if (w == g.window) return; + // The Gui's window's, while a client's view is live: its own again. + if (w == g.window) { + _ = s.windowView(); + return; + } for (s.windows, 0..) |win, k| if (k != s.gview and pardes.views.isOpen(s.core, @intCast(k)) and win.window == w) { s.core.toView(@intCast(k)); return s.follow(); @@ -3950,10 +4352,20 @@ const Shell = struct { if (g.capture_dir.len > 0) std.Io.Dir.cwd().createDirPath(s.io, g.capture_dir) catch {}; } s.gview = k; + s.windowed |= @as(u8, 1) << @intCast(k); s.core.toView(k); s.core.needs_frame = true; } + /// The Gui's window's view live, for what the window does to its own + /// (its size, its scroll): a replica client's input may have been the + /// last. True when it was not, and is now. + fn windowView(s: *Shell) bool { + if (s.core.view == s.gview or !pardes.views.isOpen(s.core, s.gview)) return false; + s.core.toView(s.gview); + return true; + } + /// View `k`'s window closed while others stay: the view goes with it. fn closeView(s: *Shell, k: u8) void { const g = s.gui orelse return; @@ -3962,11 +4374,40 @@ const Shell = struct { closeWindow(g); releaseWindowParts(g, s.gpa); s.gview = std.math.maxInt(u8); + s.windowed &= ~(@as(u8, 1) << @intCast(k)); s.core.closeView(k); + // Into another window's view, if a client's was the one left live. + for (0..pardes.views.max) |i| if (s.windowed & (@as(u8, 1) << @intCast(i)) != 0 and pardes.views.isOpen(s.core, @intCast(i))) { + if (s.core.view != i) s.core.toView(@intCast(i)); + break; + }; s.follow(); s.core.needs_frame = true; } + /// With replica clients, a wait takes one person's input and its pump + /// makes one unit of it (tty.zig `split`): whether `sev`, a second, is + /// held for the next wait. Any event but a wake is no replica's post. + fn split(s: *Shell, sev: *const c.SDL_Event) bool { + if (sev.type != c.SDL_EVENT_USER) s.mail_only = false; + if (!s.peers.recording) return false; + switch (sev.type) { + c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP, c.SDL_EVENT_MOUSE_MOTION, c.SDL_EVENT_MOUSE_WHEEL, c.SDL_EVENT_WINDOW_FOCUS_GAINED, c.SDL_EVENT_WINDOW_FOCUS_LOST, c.SDL_EVENT_WINDOW_MOUSE_LEAVE, c.SDL_EVENT_FINGER_DOWN, c.SDL_EVENT_FINGER_MOTION, c.SDL_EVENT_FINGER_UP, c.SDL_EVENT_PINCH_BEGIN, c.SDL_EVENT_PINCH_UPDATE => {}, + else => return false, + } + if (s.took_input) { + s.held = sev.*; + return true; + } + s.took_input = true; + return false; + } + + /// How many of the session's views are windows here. + fn windowsOpen(s: *const Shell) usize { + return @popCount(s.windowed & s.core.views_open); + } + /// The session's end: every window but the live one closed. fn closeOtherWindows(s: *Shell) void { const g = s.gui orelse return; @@ -3989,7 +4430,9 @@ const Shell = struct { s.gui = null; s.presented = false; s.surface = null; - s.core.unattended = true; + // Its clients look at it still (Peers keeps it attended). + s.peers.alone_unattended = true; + s.core.unattended = s.peers.count() == 0; daemon.setState(true); } @@ -4001,6 +4444,7 @@ const Shell = struct { } s.gui = g; s.parked = null; + s.peers.alone_unattended = false; s.core.unattended = false; s.core.needs_frame = true; resetScroll(g); @@ -4051,6 +4495,9 @@ const Shell = struct { }; s.drainQueue(); core.settle(); + // Its replica clients told, their views gone before the dump: each + // dials the session again once it is back. + s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); var at_buf: [1024:0]u8 = undefined; @@ -4100,6 +4547,7 @@ const Shell = struct { for (0..pardes.views.max) |i| { const k: u8 = @intCast(i); if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + s.windowed |= @as(u8, 1) << @intCast(live); } s.core.toView(live); s.follow(); @@ -4200,6 +4648,7 @@ const Shell = struct { defer s.takeExits(); s.queue.takeInto(&s.batch); const msgs = &s.batch; + if (msgs.len > 0) s.mail_only = false; var check_files = false; for (msgs.slice()) |m| switch (m) { .output => |o| { @@ -4311,6 +4760,9 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const core = s.core; var in: Input = .{ .core = core, .shell = s }; s.follow(); + s.mail_only = true; + s.took_input = false; + defer s.waited = true; if (s.gui) |g| { var sev = std.mem.zeroes(c.SDL_Event); // Idle is asleep until an event: SDL's own, and the user events the @@ -4372,9 +4824,13 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { ms = 0; } g.input_seen = false; + // An input held from the last wait (`split`) is this one's, at once. + const held = s.held; + s.held = null; + if (held) |h| sev = h; // The wait is the 9P connections' turn with the core. pardes.turn.rest(); - const got = c.SDL_WaitEventTimeout(&sev, ms); + const got = held != null or c.SDL_WaitEventTimeout(&sev, ms); pardes.turn.wake(); // A virtual clock: while a render-time animation moves, each loop // is a presented frame and moves it by one display frame @@ -4402,6 +4858,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // each would repeat hover and selection work the next undoes. var motion: ?c.SDL_Event = null; while (true) { + if (s.split(&sev)) break; if (sev.type == c.SDL_EVENT_MOUSE_MOTION and (motion == null or motion.?.motion.windowID == sev.motion.windowID)) { motion = sev; } else { @@ -4440,17 +4897,20 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // 1.9 ms spinning against 7-8 ms asleep (docs/next-anim.md). // ponytail: a core while something moves (~250 ms a scroll); a // sleep to a millisecond short of the tick if the power matters more. - while (paced and !g.input_seen) { + while (paced and !g.input_seen and s.held == null) { if (tick -| c.SDL_GetTicksNS() < 50 * std.time.ns_per_us) break; pardes.turn.rest(); const more = c.SDL_PollEvent(&sev); pardes.turn.wake(); if (more) { - s.route(&sev); - dispatch(g, &in, &sev); - while (c.SDL_PollEvent(&sev)) { + if (!s.split(&sev)) { s.route(&sev); dispatch(g, &in, &sev); + while (c.SDL_PollEvent(&sev)) { + if (s.split(&sev)) break; + s.route(&sev); + dispatch(g, &in, &sev); + } } } else { // The events asked for every 100 µs, the core busy between. @@ -4510,6 +4970,16 @@ fn pollFrame(ctx: ?*anyopaque) void { } return; }; + // A replica client's view live: the window's own view back before what + // the window does to it (its size, its scroll, its font). + if (core.view != s.gview) { + const geom = windowCells(g); + const own = if (core.parked[s.gview]) |slot| &slot.view else null; + const due = g.scroll_pane != null or g.scroll_notch != 0 or g.glide.moving() or + core.settings.font.tagline_percent != g.tagline_percent or core.settings.font.pending or + (own != null and resizeOf(own.?, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height) != null); + if (due) _ = s.windowView(); + } syncWindowOpacity(g, core); core.frames.period_ns = refreshNs(g); syncTaglineFont(g, core); @@ -4680,16 +5150,8 @@ fn refreshNs(g: *const Gui) u64 { } fn finishWindowOpacity(core: *pardes.Pardes, applied: *u8, failure: ?[]const u8) void { - core.settings.window_opacity_pending = false; - if (failure) |reason| { - // Do not claim a value the compositor refused; an explicit retry of - // the same command will arm a new request. - core.settings.window_opacity = applied.*; - var buf: [256]u8 = undefined; - core.setMessage(core.active, std.fmt.bufPrint(&buf, "WindowOpacity: {s}", .{reason[0..@min(reason.len, 240)]}) catch "WindowOpacity: compositor rejected the request"); - } else { - applied.* = core.settings.window_opacity; - } + core.windowOpacityDone(applied.*, failure); + applied.* = core.settings.window_opacity; } fn syncWindowOpacity(g: *Gui, core: *pardes.Pardes) void { @@ -4758,7 +5220,9 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { g.scene_failures +|= 1; log.err("scene target unavailable (attempt {d}/3)", .{g.scene_failures}); if (g.scene_failures >= 3) { - core.disableSceneEffects(); + // ponytail: a client's window keeps asking (its core is the + // session's to change). + if (s.remote == null) core.disableSceneEffects(); g.scene_failures = 0; } } else if (scene_requested and s.presented) g.scene_failures = 0; @@ -4772,12 +5236,20 @@ fn postPresent(ctx: ?*anyopaque) void { // the pump advances it all the same. if (!s.presented) return; const frame = s.surface orelse return; - refreshPresentedPointer(g, s.core); + // A client's view live: the window's frame is acknowledged when its + // own view is live and drawn. + if (s.core.view != s.gview) return; + var in: Input = .{ .core = s.core }; + refreshPresentedPointer(g, &in); s.core.acknowledgePanelPresentation(frame.panelTracks()); } fn guiNow(ctx: ?*anyopaque) u64 { const s = shellOf(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 s.paced_ns orelse c.SDL_GetTicksNS(); } @@ -4871,7 +5343,8 @@ fn takeShell(s: *Shell, pane: u8, a: Adoption) void { /// The daemon's thread: what it says, onto the queue (drainQueue). fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { - const q: *Queue = @ptrCast(@alignCast(ctx)); + const s: *Shell = @ptrCast(@alignCast(ctx)); + const q = s.queue; switch (note) { .attach => |a| { // A terminal for a window session: the daemon refuses that kind @@ -4884,9 +5357,8 @@ 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"), + // Joined at the next step boundary (Peers, any thread). + .replica => |fd| s.peers.handed(fd), } } @@ -7974,8 +8446,14 @@ fn syncLigatures(g: *Gui, core: *pardes.Pardes) void { } fn syncTaglineFont(g: *Gui, core: *pardes.Pardes) void { - const percent = core.settings.font.tagline_percent; - if (percent == g.tagline_percent) return; + if (!fitTaglineFont(g, core.settings.font.tagline_percent)) return; + const geom = windowCells(g); + _ = updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height); +} + +/// The tagline's face at `percent` of the body's: whether it changed. +fn fitTaglineFont(g: *Gui, percent: u8) bool { + if (percent == g.tagline_percent) return false; g.tagline_percent = percent; const tagline = taglineRaster(g.font, g.px, g.cell_w, g.cell_h, percent); g.tagline_px = tagline.px; @@ -7983,8 +8461,7 @@ fn syncTaglineFont(g: *Gui, core: *pardes.Pardes) void { g.tagline_baseline = tagline.baseline; g.tagline_height = tagline.height; resetGlyphAtlas(g); - const geom = windowCells(g); - _ = updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height); + return true; } /// What text draws, so a frame does not work it out again. `cells` is the diff --git a/src/main.zig b/src/main.zig index d4edc74d..ada5f3b9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -109,7 +109,8 @@ const help_text = \\ --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 + \\ a terminal's or a window's (either kind's + \\ session), over `ssh -L` to its pardes-replica.sock \\ ($PARDES_SSH: the ssh command, words, default \\ `ssh`; and \\ $PARDES_REMOTE_RUNTIME: its runtime directory, @@ -802,7 +803,6 @@ fn nativeMain(std_init: std.process.Init) !void { 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); @@ -981,12 +981,23 @@ fn commOf(pid: []const u8, buf: *[64]u8) []const u8 { return std.mem.trimEnd(u8, buf[0..@intCast(got)], "\n"); } -/// `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. +/// The replica client of this build's frontend: a terminal's (tty.zig) or a +/// window's (gui.zig). +fn clientOf(init: std.process.Init, opts: pardes.Options, path: []const u8, name: []const u8, far: bool) !void { + switch (pardes.platform) { + .tty => try @import("tty/tty.zig").remote(init, opts, path, name, far), + .gui => try @import("gui/gui.zig").remote(init, opts, path, name, far), + else => unreachable, + } +} + +/// `pardes --attach=:` (`pardes-gui` too): a replica client +/// (tty.zig and gui.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; + if (comptime pardes.platform != .tty and pardes.platform != .gui) unreachable; const Say = struct { fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { var buf: [700]u8 = undefined; @@ -995,7 +1006,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, } }; if (std.mem.indexOfScalar(u8, where, '/') != null) { - @import("tty/tty.zig").remote(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); + clientOf(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); std.process.exit(0); } // ssh -L : , until the socket here answers. @@ -1066,7 +1077,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, 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, true) catch |err| { + clientOf(init, opts, here, name, true) catch |err| { Ssh.end(); Say.no(io, "{t}", .{err}); }; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 1b9d73f5..2859304a 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -524,10 +524,12 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ? break now; } else typed; const arg = if (sp) |s| std.mem.trim(u8, line[s + 1 ..], " \t") else ""; - const b = std.meta.stringToEnum(Builtin, word) orelse { + const here = if (std.meta.stringToEnum(Builtin, word)) |b| builtins.registry.here(p, b) else false; + const b = (if (here) std.meta.stringToEnum(Builtin, word) else null) orelse { // A setting this build's frontend cannot show (Lift, GripWidth on a - // terminal) is known, and says so rather than sound misspelt. - if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null) @as([]const u8, "GUI-only") else null) |absent| { + // terminal, the window's own words in a terminal's view) is known, + // and says so rather than sound misspelt. + if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null or std.meta.stringToEnum(Builtin, word) != null) @as([]const u8, "GUI-only") else null) |absent| { var why: [64]u8 = undefined; // EINVAL, a request this build cannot take, in words 9ns reads // as that: `invalid`. @@ -618,18 +620,24 @@ const e_prompt = "control message needs its argument"; /// What is not a setting -- platform, themes, where the config came from -- /// is /status's or the DumpConfig window's. fn writeSettings(p: *Pardes, w: *std.Io.Writer) !void { - try writeSettingsOf(p, &p.settings, w); + try writeSettingsFor(p, &p.settings, w, builtins.viewCapabilities(p)); try w.writeAll("LocationsConfig "); try p.locations_config.write(w); try w.writeByte('\n'); - if (comptime builtins.Pet.enabled) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)}); + if (comptime builtins.Pet.enabled) if (builtins.registry.here(p, .Pet)) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)}); } /// The setting words of `s`, one a line, as the root ctl reads them; a Dump /// keeps those that differ from a fresh session's (dump.zig). pub fn writeSettingsOf(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer) !void { - inline for (config.Runtime.settings) |setting| { + return writeSettingsFor(p, s, w, builtins.capabilities); +} + +/// ...those a frontend of `caps` takes (the root ctl's: the live view's). +fn writeSettingsFor(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer, caps: config.Runtime.Capabilities) !void { + inline for (config.Runtime.settings) |setting| skip: { if (comptime !setting.enabled(builtins.capabilities)) continue; + if (!setting.enabled(caps)) break :skip; const word = setting.word; switch (setting.action) { .toggle => |field| try w.print("{s} {s}\n", .{ word, if (@field(s, @tagName(field))) "on" else "off" }), @@ -909,8 +917,10 @@ fn stripCitations(text: []u8) []u8 { return text[0..out]; } -fn writeCommands(w: *std.Io.Writer) !void { +fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void { for (std.enums.values(Builtin)) |b| { + // A window's own word is none in a terminal's view. + if (!builtins.registry.here(p, b)) continue; // Edit is a pane's word the root's ctl takes too, at the active pane. const scope: []const u8 = if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane"; try w.print("{s}{s} {s}", .{ @@ -938,16 +948,16 @@ fn writeCommands(w: *std.Io.Writer) !void { } } -pub fn commandsLen() u64 { +pub fn commandsLen(p: *const Pardes) u64 { var buf: [256]u8 = undefined; var counting: std.Io.Writer.Discarding = .init(&buf); - writeCommands(&counting.writer) catch return 0; + writeCommands(p, &counting.writer) catch return 0; return counting.fullCount(); } pub fn readCommands(p: *Pardes, req: Req) Reply { var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); - writeCommands(&text.writer) catch return Reply.fail(req.tag, E.NOMEM); + writeCommands(p, &text.writer) catch return Reply.fail(req.tag, E.NOMEM); p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch return Reply.fail(req.tag, E.NOMEM); return tree.stagedReply(p, req); } @@ -1362,7 +1372,9 @@ test "commands lists every builtin with its argument and the ctl that takes it" // Then, where the source has one, ` -- ` and what it does. if (words.next()) |dashes| try testing.expectEqualStrings("--", dashes); } - try testing.expectEqual(std.enums.values(Builtin).len, lines); + var here: usize = 0; + for (std.enums.values(Builtin)) |b| here += @intFromBool(builtins.registry.here(p, b)); + try testing.expectEqual(here, lines); for ([_][]const u8{ "\nDel arg pane -- ", "\nNewcol root -- ", "\nSave arg pane", "\nVerbose arg root on,off", "\nPlacement arg root acme,pardes", "\nUndo pane -- ", "\nEdit arg both -- acme's Edit" }) |row| try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null); } @@ -1738,6 +1750,8 @@ test "status reads the editor, and look and exec validate whole batches" { const gpa = testing.allocator; const p = try withFile(gpa, "Msg root\n"); defer p.deinit(); + // A terminal's view first (a window's build starts windowed). + p.windowed = false; const status = rd(p, root_status, 0, 4096); try testing.expectEqual(Status.ok, status.reply.status); var lines = std.mem.tokenizeScalar(u8, status.bytes, '\n'); @@ -2855,7 +2869,7 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); const root_ctl = @backingInt(tree.TopFile.ctl); - if (!builtins.capabilities.lift) { + if (!builtins.viewCapabilities(p).lift) { try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", wr(p, root_ctl, "Lift on\n").reply.ename); try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename); // A builtin the GUI alone has, written to a ctl or an exec, never diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bda2c1d1..bd87f5bc 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -678,7 +678,7 @@ fn topSize(p: *Pardes, f: TopFile) u64 { // one (a 9ns mount's splice), and a follower's reads go past it. .log => 0, .ctl => ctl.rootLen(p), - .commands => ctl.commandsLen(), + .commands => ctl.commandsLen(p), .focus => focus: { if (p.header_focus) break :focus 0; const pn = p.panes[p.active] orelse break :focus 0; diff --git a/src/pardes.zig b/src/pardes.zig index 2b1fae61..d7f35ed9 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -2868,7 +2868,9 @@ test "Pet command is SDL-only optional and validates its argument" { defer p.deinit(); try std.testing.expectEqual(.off, p.settings.pet); if (comptime platform != .gui) { - try std.testing.expect(std.meta.stringToEnum(builtins.registry.Builtin(), "Pet") == null); + // A terminal's view has the word and takes it not. + try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); + try std.testing.expectEqual(.off, p.settings.pet); return; } try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); @@ -3717,9 +3719,16 @@ pub const Event = union(enum) { /// How the host's handover `seq` went (Handover.finish): taken by `to`, /// or `failure` says why not. Borrowed for the step. handed: struct { seq: u32, to: []const u8 = "", failure: ?[]const u8 = null }, + /// The live view's frontend, which its replica client says first + /// (docs/next-daemon.md, "Phase 6: GUI replica notes"): a window, which + /// draws pictures itself and takes the GUI's own words, or a terminal. + frontend: Frontend, tick, }; +/// A view's frontend: what a client of a session elsewhere says it is. +pub const Frontend = struct { windowed: bool, native_images: bool = false, native_image_patches: bool = false }; + pub const PendingWrite = struct { bytes: []u8, off: usize = 0 }; /// `command` is Kill's: SIGTERM to the terminal's foreground job, and never @@ -4550,7 +4559,10 @@ pub const Pardes = struct { /// A pixel shell's window, which hears the pointer leave it: only then /// does /status say whether the pointer is over it (a terminal's mouse /// reports never say it left, so a tty would claim it for ever). - windowed: bool = false, + /// The live view's frontend is a window (Event.frontend): what it draws, + /// and the words it takes (builtins.zig `here`). A window's build + /// starts so, a terminal's not. + windowed: bool = platform == .gui, /// The last plain B1 click, for acme's double-click (text.c:881, /// textselect: a second click at the same point within 500 ms). last_click: ?struct { id: usize, on_tag: bool, row: i32, col: i32, ms: i64 } = null, @@ -5526,6 +5538,8 @@ pub const Pardes = struct { pub fn takeFontRequest(p: *Pardes) ?[]const u8 { if (comptime !font_picker) return null; + const taped = tape.enter(p, .take_font_request); + defer tape.leave(p, taped); if (!p.settings.font.pending or p.font_request_taken) return null; const path = p.settings.font.requested_path.get(); if (path.len == 0) return null; @@ -5540,6 +5554,8 @@ pub const Pardes = struct { unit: config.Runtime.FontSizeUnit, ) bool { if (comptime !font_picker) return false; + const taped = tape.enter(p, .{ .observe_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer tape.leave(p, taped); if (!p.settings.font.effective_name.set(effective_name)) return false; p.settings.font.effective_size_hundredths = effective_size_hundredths; p.settings.font.effective_size_unit = unit; @@ -5555,6 +5571,8 @@ pub const Pardes = struct { unit: config.Runtime.FontSizeUnit, ) bool { if (comptime !font_picker) return false; + const taped = tape.enter(p, .{ .acknowledge_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return false; const changed = !std.mem.eql( u8, @@ -5571,6 +5589,8 @@ pub const Pardes = struct { pub fn rejectFont(p: *Pardes) void { if (comptime !font_picker) return; + const taped = tape.enter(p, .reject_font); + defer tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return; p.settings.font.pending = false; p.font_request_taken = false; @@ -6346,6 +6366,11 @@ pub const Pardes = struct { if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words); }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), + .frontend => |f| { + p.windowed = f.windowed; + p.native_images = f.native_images; + p.native_image_patches = f.native_image_patches; + }, .pointer_leave => { p.pointer_inside = false; p.pointer_body_hit = null; @@ -6818,11 +6843,14 @@ pub const Pardes = struct { p.leader_on = true; }; // a path-less builtin is a row here too (it is the same index Help - // reads), and SPC cannot reach one — so both scans skip it + // reads), and SPC cannot reach one — so both scans skip it; and a + // window's own word is no path in a terminal's view for (builtin_rows) |row| { + if (!builtins.registry.here(p, row.cmd)) continue; if (std.mem.eql(u8, row.path orelse continue, pfx)) return exec.runBuiltin(p, row.cmd, p.active, "", null); } for (builtin_rows) |row| { + if (!builtins.registry.here(p, row.cmd)) continue; if (std.mem.startsWith(u8, row.path orelse continue, pfx)) { p.leader_on = true; return; @@ -7398,9 +7426,25 @@ pub const Pardes = struct { /// A native post pass proved unavailable: the chain is emptied, which /// DumpConfig then reports. pub fn disableSceneEffects(p: *Pardes) void { + const taped = tape.enter(p, .disable_scene_effects); + defer tape.leave(p, taped); p.settings.post.clear(); } + /// The window's answer to a WindowOpacity: done, or `failure` says why + /// not, `applied` the opacity it keeps. + pub fn windowOpacityDone(p: *Pardes, applied: u8, failure: ?[]const u8) void { + const taped = tape.enter(p, .{ .window_opacity_done = .{ .applied = applied, .failure = failure } }); + defer tape.leave(p, taped); + p.settings.window_opacity_pending = false; + const reason = failure orelse return; + // Do not claim a value the compositor refused; an explicit retry of + // the same command will arm a new request. + p.settings.window_opacity = applied; + var buf: [256]u8 = undefined; + p.setMessage(p.active, std.fmt.bufPrint(&buf, "WindowOpacity: {s}", .{reason[0..@min(reason.len, 240)]}) catch "WindowOpacity: compositor rejected the request"); + } + // ---- doc panes ---- pub fn openTutorView(p: *Pardes, id: usize) !*Pane { diff --git a/src/replica.zig b/src/replica.zig index 9d12de70..32fd4fbe 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -910,6 +910,16 @@ pub const Replica = struct { pardes.views.drawOne(r.core(), r.view, present) catch {}; } + /// `f(ctx, core)` with its own view live, outside any step, as `draw` + /// draws it (views.within): what its frontend reads of its own view + /// (its pointer's cells, its size) while another client's input was the + /// session's last. + pub fn within(r: *Replica, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) void { + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + pardes.views.within(r.core(), r.view, ctx, f) catch {}; + } + /// One of the authority's terminal messages (`Screens.flush`, or an /// answer to `screenRequests`), taken between units in stream order: a /// delta written into its pane's emulator, or fetched rows. The @@ -2486,6 +2496,306 @@ pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, compti rest.run(gpa, fd, ctx, take); } +/// What the session streams a client after its dump, as its frontend's loop +/// takes it: a unit's and a terminal message's bytes and a refusal's words +/// on the C heap, the loop's to free. +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8 }; + +/// The session's stream off `fd`, each message handed to `post` until the +/// link ends (a refusal ends it too); `post` says whether it took it (one +/// it did not is freed here). +pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime post: fn (@TypeOf(ctx), Said) bool) void { + const Take = struct { + fn take(x: @TypeOf(ctx), tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false; + const msg: Said = switch (t) { + .unit => .{ .unit = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .screen => .{ .screen = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, + .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .pong, .welcome, .dump, .blobs, .zstd => return true, + }; + if (!post(x, msg)) switch (msg) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }; + return msg != .refused; + } + }; + rest.run(gpa, fd, ctx, Take.take); +} + +/// A replica client's link to its session, the terminal's (tty.zig +/// `remote`) and the window's (gui.zig `remote`): its replica, its socket, +/// what the session said, and what it checks. Drawing is the frontend's. +pub const Link = struct { + r: *Replica, + fd: c_int, + quit: bool = false, + /// The session hung up. + gone: bool = false, + /// Something to draw: an input applied, a unit taken. + dirty: bool = true, + /// The last frame was held for the frontend's answer (a terminal's + /// FrameAck, a window's skipped image). + owed: bool = false, + /// 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, + /// When its own last input was, and its last check asked (`quietCheck` + /// waits a while after each). + input_ns: u64 = 0, + asked_ns: u64 = 0, + /// How many of its own inputs the session recorded otherwise when it + /// last drew: a frame after one that draws something is a rollback that + /// may have been seen (counted so: an upper bound, the frame shows the + /// inputs after it too). + mispredicted: u32 = 0, + visible: u32 = 0, + /// What the session said as it let this client go (the C heap's). + refused: ?[]u8 = null, + /// A line over its frame, until its next key (`notify`): why it joined + /// again. The client's own, never the session's core. + notice_buf: [240]u8 = undefined, + notice_len: usize = 0, + /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core + /// is put off the session's (a digested counter moved), the next unit's + /// digest finds it, and it joins again (test/daemon.py). + test_diverge: u32 = 0, + /// Put off so in this join: its checks are not written (they differ). + put_off: bool = false, + /// How its last join went (Joined's counts), and how long its dial and + /// load took (PARDES_REPLICA_STATS). + join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, + join_ms: u64 = 0, + + pub const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; + + /// The replica the dial's dump makes, checked against the session as + /// it was dumped; null, and why in `why`. The dump is let go, a far + /// join's to the thread that keeps the blobs it was sent (`Have.store`). + pub fn load(gpa: Allocator, joined: *Joined, o: pardes.Options, why: []u8, have: ?*Have) ?*Replica { + defer { + if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); + joined.dump = &.{}; + } + const r = Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { + _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; + return null; + }; + // PARDES_NO_ECHO: no predicted echo (the gates' baseline). + if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; + if (!r.loaded(joined.digest)) { + r.deinit(); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + return null; + } + return r; + } + + /// The test knobs the environment sets (PARDES_REPLICA_CHECK, + /// PARDES_TEST_DIVERGE). + pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void { + if (env.get("PARDES_REPLICA_CHECK")) |file| c.check = try gpa.dupeSentinel(u8, file, 0); + if (env.get("PARDES_TEST_DIVERGE")) |n| c.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; + } + + pub fn deinit(c: *Link, gpa: Allocator) void { + if (c.check) |file| gpa.free(file); + if (c.refused) |words| std.heap.c_allocator.free(words); + } + + /// How its join went, its dial and load `ms`. + pub fn joinedAs(c: *Link, j: *const Joined, ms: u64) void { + c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes }; + c.join_ms = ms; + } + + /// A join's link, from its start. + pub fn reset(c: *Link, r: *Replica, fd: c_int) void { + c.r = r; + c.fd = fd; + c.quit = false; + c.gone = false; + c.dirty = true; + c.owed = false; + c.changed = false; + c.asked = false; + c.mispredicted = 0; + c.put_off = false; + } + + pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void { + const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; + for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { + b.* = '?'; + }; + c.notice_len = text.len; + } + + pub fn notice(c: *const Link) []const u8 { + return c.notice_buf[0..c.notice_len]; + } + + /// The frontend's input: applied at once, and sent to the session. + pub fn input(c: *Link, ev: pardes.Event) void { + // Its notice read: a key of its own puts it away. + if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; + c.input_ns = nowNs(); + const msg = c.r.input(ev) catch return; + if (!sendInput(c.fd, msg)) c.quit = true; + c.dirty = true; + } + + /// What the session streamed, in its order; the bytes are let go. + pub fn take(c: *Link, msg: Said) void { + switch (msg) { + .unit => |bytes| { + defer std.heap.c_allocator.free(bytes); + c.r.confirm(bytes); + if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { + c.test_diverge = 0; + c.put_off = true; + c.r.core().next_serial +%= 1000; + } + if (c.r.moved) c.dirty = true; + c.r.moved = false; + c.changed = true; + }, + .screen => |bytes| { + defer std.heap.c_allocator.free(bytes); + if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) { + c.quit = true; + }; + c.dirty = true; + }, + .checked => |d| c.checked(d), + .refused => |words| { + if (c.refused) |old| std.heap.c_allocator.free(old); + c.refused = words; + c.quit = true; + }, + } + if (c.r.diverged != null) c.quit = true; + } + + /// Its own view drawn, by the frontend (`drew` is whether anything was + /// written to it): a rollback it may have shown counted, and the + /// history rows it shows and does not hold (a scroll back) asked for, + /// blank until they come. + pub fn drawn(c: *Link, drew: bool) void { + if (c.r.stats.mispredicted > c.mispredicted) { + c.mispredicted = c.r.stats.mispredicted; + if (drew) c.visible += 1; + } + c.dirty = false; + while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) { + c.quit = true; + break; + }; + } + + /// Each pass of the frontend's loop: quiet (nothing of its own + /// pending) 300 ms after its last input and its last check, and + /// changed since, the session's digest is asked once, answered in + /// stream order after the units before it (a session that pumps on, a + /// window's test feed, sends a unit a frame; the check's own wakes it + /// once). The wake it wants, ms. + pub fn quietCheck(c: *Link) ?u32 { + if (c.check == null or !c.changed or c.asked or !c.r.quiet()) return null; + const quiet_ns = 300 * std.time.ns_per_ms; + const since = nowNs() -| @max(c.input_ns, c.asked_ns); + if (since < quiet_ns) return @intCast((quiet_ns - since) / std.time.ns_per_ms + 1); + c.changed = false; + c.asked = askCheck(c.fd); + c.asked_ns = nowNs(); + return null; + } + + /// The session's digest against this replica's, if it is quiet still. + fn checked(c: *Link, theirs: u64) void { + c.asked = false; + const path = c.check orelse return; + if (!c.r.quiet() or c.put_off) 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; + appendTo(path, line); + } + + /// PARDES_REPLICA_STATS=: how its inputs and the session's units + /// went, a line at its end (test/daemon.py, the gates). + pub fn writeStats(c: *Link, path: []const u8) void { + const st = &c.r.stats; + var buf: [600]u8 = undefined; + const e = &c.r.echo.stats; + 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} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {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, + pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, + pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, + c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, + }) catch return; + var z: [4096]u8 = undefined; + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, line); + } + + /// The link ended: whether the client joins again, and why in its + /// notice (it diverged, or the session let it go to upgrade). + pub fn rejoins(c: *Link) bool { + if (c.r.diverged) |reason| { + c.notify("joined again: this client diverged from the session ({s})", .{reason}); + return true; + } + if (c.refused) |words| { + c.notify("joined again: {s}", .{words}); + std.heap.c_allocator.free(words); + c.refused = null; + return true; + } + return false; + } + + /// The session again, after a divergence or an upgrade: an upgrade's + /// takes the time its exec and load take, and its daemon's, so it is + /// dialled every 100 ms for 20 s. 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 { + var tries: u32 = 0; + while (true) : (tries += 1) { + why.* = @splat(0); + if (dial(gpa, path, session, why, have)) |j| { + if (j.semantics == wire.semantics) return j; + bye(j.fd); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (tries > 200) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + } +}; + +fn appendTo(path: [:0]const u8, line: []const u8) void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, line.ptr, line.len); + _ = libc.close(fd); +} + pub fn digestOf(a: *Authority) u64 { const was = Stepping.begin(&a.t, null); defer was.end(); diff --git a/src/tape.zig b/src/tape.zig index f0766bd1..70a22ab0 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -61,6 +61,15 @@ pub const Input = union(enum) { open_view, close_view: u8, to_view: u8, + /// What a window says of its font and its opacity (gui.zig). + take_font_request, + observe_font: Font, + acknowledge_font: Font, + reject_font, + disable_scene_effects, + window_opacity_done: struct { applied: u8, failure: ?[]const u8 }, + + pub const Font = struct { name: []const u8, size: u16, unit: pardes.config.Runtime.FontSizeUnit }; }; /// The core's own syscalls (each `turn.yield()`s around itself), numbered @@ -544,6 +553,12 @@ pub const Tape = struct { .open_view => _ = p.openView(), .close_view => |k| p.closeView(k), .to_view => |k| p.toView(k), + .take_font_request => _ = p.takeFontRequest(), + .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), + .acknowledge_font => |f| _ = p.acknowledgeFont(f.name, f.size, f.unit), + .reject_font => p.rejectFont(), + .disable_scene_effects => p.disableSceneEffects(), + .window_opacity_done => |w| p.windowOpacityDone(w.applied, w.failure), } t.finishInput(); } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 2007c6f7..a1a28181 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -156,14 +156,8 @@ pub const Command = struct { 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, - /// ...a terminal's delta or rows, before the units after it. - screen: []u8, - /// ...why it let this client go (an Upgrade's exec), the stream's end. - refused: []u8, - /// The session's digest, asked by PARDES_REPLICA_CHECK. - checked: u64, + /// A replica client's: what the session streamed (replica.Link). + said: replica.Said, link_gone, } = .nop; }; @@ -1274,191 +1268,42 @@ 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`). +/// A replica client's link to its session (`remote`), drawn here. 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, - /// What the session said as it let this client go (owned, the C heap's). - refused: ?[]u8 = null, - /// A line over its frame, until its next key (`notify`): why it joined - /// again. The client's own, never the session's core. - notice_buf: [240]u8 = undefined, - notice_len: usize = 0, - /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core - /// is put off the session's (a digested counter moved), the next unit's - /// digest finds it, and it joins again (test/daemon.py). - test_diverge: u32 = 0, - /// Put off so in this join: its checks are not written (they differ). - put_off: bool = false, - /// How its last join went (replica.Joined's counts), and how long its - /// dial and load took (PARDES_REPLICA_STATS). - join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, - join_ms: u64 = 0, - - /// The replica the dial's dump makes, checked against the session as - /// it was dumped; null, and why in `why`. The dump is let go, a far - /// join's to the thread that keeps the blobs it was sent (`Have.store`). - fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8, have: ?*replica.Have) ?*replica.Replica { - defer { - if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); - joined.dump = &.{}; - } - const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { - _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; - return null; - }; - // PARDES_NO_ECHO: no predicted echo (the gates' baseline). - if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; - if (!r.loaded(joined.digest)) { - r.deinit(); - _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; - return null; - } - return r; - } - - /// A join's link, from its start. - fn reset(c: *Remote, r: *replica.Replica, fd: posix.fd_t) void { - c.r = r; - c.fd = fd; - c.quit = false; - c.gone = false; - c.dirty = true; - c.owed = false; - c.changed = false; - c.asked = false; - c.mispredicted = 0; - c.put_off = false; - } - - fn notify(c: *Remote, comptime fmt: []const u8, args: anytype) void { - const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; - for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { - b.* = '?'; - }; - c.notice_len = text.len; - } - - /// 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} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {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, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, - c.join.sent_bytes >> 10, - }) 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() or c.put_off) 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); - } + link: replica.Link, /// 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 r = c.link.r; + const core = 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 }); + 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); + const drew = core.frames.bytes > 0; + c.link.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, c.link.owed); core.present_skipped = skipped; core.needs_frame = needs; - c.dirty = false; - // The history rows it shows and does not hold (a scroll back), - // asked for: blank until they come. - while (c.r.screenRequest()) |req| if (!replica.sendScreen(c.fd, req)) { - c.quit = true; - break; - }; + c.link.drawn(drew); } }; /// 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 {}; - }, - .screen => { - const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; - l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); - }, - .refused => { - const words = (wire.decodeServer(tag, payload) catch return false).refused; - const bytes = std.heap.c_allocator.dupe(u8, words) catch return false; - l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes); - return false; - }, - .pong, .welcome, .dump, .blobs, .zstd => {}, - } + const Post = struct { + fn post(l: *Loop, said: replica.Said) bool { + l.postEvent(.{ .said = said }) catch return false; return true; } }; - replica.readStream(gpa, fd, rest, loop, Take.take); + replica.readLink(gpa, fd, rest, loop, Post.post); loop.postEvent(.link_gone) catch {}; } @@ -1489,7 +1334,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se const have: ?*replica.Have = if (far) &have_store else null; var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; var joined = replica.dial(gpa, path, session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); @@ -1504,7 +1349,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var o = opts; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - var r = Remote.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); // Its join's time, the dial and the load (PARDES_REPLICA_STATS). var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); @@ -1528,10 +1373,10 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se 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); - if (init.environ_map.get("PARDES_TEST_DIVERGE")) |n| link.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; + var remote_link: Remote = .{ .link = .{ .r = r, .fd = joined.fd } }; + const link = &remote_link.link; + try link.fromEnv(gpa, init.environ_map); + defer link.deinit(gpa); var sh: Shell = .{ .io = io, .gpa = gpa, @@ -1543,7 +1388,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se .paste_buf = &paste_buf, .inotify_fd = -1, .from = joined.view, - .remote = &link, + .remote = &remote_link, }; try startInput(&loop, &screen); @@ -1561,18 +1406,18 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se sh.terms[joined.view] = &screen; screen.feed.view = joined.view; link.reset(r, joined.fd); - link.join = .{ .blobs = joined.blobs, .from_memory = joined.from_memory, .from_disk = joined.from_disk, .sent_bytes = joined.sent_bytes }; - link.join_ms = dial_ms; + link.joinedAs(&joined, dial_ms); var reader_done: std.atomic.Value(bool) = .init(false); const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); - // Its size is its view's first input. + // What it is, then its size, are its view's first inputs. + link.input(.{ .frontend = .{ .windowed = false } }); screen.size = null; sh.winched(); screen.vx.queueRefresh(); while (!link.quit) { // Its predicted echo judged and caught up, once a frame. if (r.echoFrame()) link.dirty = true; - if (link.dirty) link.draw(&sh); + if (link.dirty) remote_link.draw(&sh); const first = (loop.tryEvent() catch null) orelse idle: { // Idle, its frame drawn: the next key needs no copy taken. r.ready(); @@ -1596,14 +1441,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se if (link.owed or r.echo.changed) link.dirty = true; // Predictions out: judged a frame at a time while they are. if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) requestWake(io, @intCast(pardes.animation.frame_ns / std.time.ns_per_ms)); - 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 (link.quietCheck()) |ms| requestWake(io, ms); if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); @@ -1616,7 +1454,10 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se while (true) { const ended = reader_done.load(.acquire); while (loop.tryEvent() catch null) |ev| switch (ev) { - .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .said => |said| switch (said) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }, .paste => |b| gpa.free(@constCast(b)), else => {}, }; @@ -1627,13 +1468,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se _ = libc.close(joined.fd); joined.rest.deinit(gpa); // Why it joins again, in a line over its frame until a key. - if (r.diverged) |reason| { - link.notify("joined again: this client diverged from the session ({s})", .{reason}); - } else if (link.refused) |words| { - link.notify("joined again: {s}", .{words}); - std.heap.c_allocator.free(words); - link.refused = null; - } else { + if (!link.rejoins()) { // What it made since it joined, kept for its next start. if (have) |h| h.keep(r.core()); r.deinit(); @@ -1644,38 +1479,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se return; } // The session again, the core it leaves held meanwhile: its blobs - // are not sent again. An upgrade's takes the time its exec and load - // take, and its daemon's. + // are not sent again. if (have) |h| h.leaving(r.core()); - var tries: u32 = 0; dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = while (true) : (tries += 1) { - why = @splat(0); - if (replica.dial(gpa, path, session, &why, have)) |j| { - if (j.semantics == wire.semantics) break j; - replica.bye(j.fd); - _ = libc.close(j.fd); - var jj = j; - jj.rest.deinit(gpa); - gpa.free(j.dump); - screen.leave(); - Said.no(io, "{s}", .{upgrade_words}); - } - const words = std.mem.sliceTo(&why, 0); - if (std.mem.indexOf(u8, words, "runs another build") != null) { - screen.leave(); - Said.no(io, "{s}", .{upgrade_words}); - } - if (tries > 200) { - screen.leave(); - Said.no(io, "{s}", .{words}); - } - const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); + joined = replica.Link.redial(gpa, path, session, &why, have) orelse { + screen.leave(); + Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; if (have) |h| h.left(); r.deinit(); - r = Remote.load(gpa, &joined, o, &why, have) orelse { + r = replica.Link.load(gpa, &joined, o, &why, have) orelse { screen.leave(); Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; @@ -1683,7 +1496,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se } } -const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; /// 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, @@ -2191,11 +2003,7 @@ const Shell = struct { /// (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); - // Its notice read: a key of its own puts it away. - if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; - const msg = c.r.input(ev) catch return; - if (!replica.sendInput(c.fd, msg)) c.quit = true; - c.dirty = true; + c.link.input(ev); } /// Whether a terminal of this process's shows the session, or @@ -2800,7 +2608,7 @@ const Shell = struct { }, .quit => { if (s.remote) |c| { - c.quit = true; + c.link.quit = true; return true; } // One of several: its view goes with it. @@ -2827,39 +2635,16 @@ const Shell = struct { s.input_seen = true; }, .replica_mail => s.input_seen = true, - .unit => |bytes| { - if (s.remote) |c| { - c.r.confirm(bytes); - if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { - c.test_diverge = 0; - c.put_off = true; - c.r.core().next_serial +%= 1000; - } - if (c.r.moved) c.dirty = true; - c.r.moved = false; - c.changed = true; - } - std.heap.c_allocator.free(bytes); - }, - .screen => |bytes| { - if (s.remote) |c| { - if (c.r.screen(bytes)) |reply| if (!replica.sendScreen(c.fd, reply)) { - c.quit = true; - }; - c.dirty = true; - } - std.heap.c_allocator.free(bytes); + .said => |said| if (s.remote) |c| { + c.link.take(said); + if (said == .refused) return true; + } else switch (said) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, }, - .checked => |d| if (s.remote) |c| c.checked(d), - .refused => |words| if (s.remote) |c| { - if (c.refused) |old| std.heap.c_allocator.free(old); - c.refused = words; - c.quit = true; - return true; - } else std.heap.c_allocator.free(words), .link_gone => if (s.remote) |c| { - c.quit = true; - c.gone = true; + c.link.quit = true; + c.link.gone = true; return true; }, .focus_in => s.feed(.{ .focus = true }), @@ -3055,7 +2840,7 @@ const Shell = struct { const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; paintCells(win, trailed.cells, trailed.cols, trailed.rows); - if (s.remote) |c| if (c.notice_len > 0) noticeLine(win, c.notice_buf[0..c.notice_len], trailed.cols, trailed.rows); + if (s.remote) |c| if (c.link.notice_len > 0) noticeLine(win, c.link.notice(), trailed.cols, trailed.rows); tz_cells.end(); for (surface.images[0..surface.nimages]) |maybe| { const place = maybe orelse continue; diff --git a/src/views.zig b/src/views.zig index 361d14a3..b9731089 100644 --- a/src/views.zig +++ b/src/views.zig @@ -523,6 +523,19 @@ pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *cons /// 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 { + try unparked(p, k, present, drawLive); + copy(&drawn_fields, &p.parked[k].?.view, p); +} + +fn drawLive(present: Present, p: *Pardes) anyerror!void { + _ = 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); +} + +/// `f(ctx, p)` with parked view `k` unparked, its slot's selections its own +/// again after (`drawParked`). +fn unparked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !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 }; @@ -532,10 +545,7 @@ fn drawParked(p: *Pardes, k: u8, present: Present) !void { 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); + try f(ctx, p); } /// View `k` drawn and presented outside any step, by a replica client @@ -543,12 +553,32 @@ fn drawParked(p: *Pardes, k: u8, present: Present) !void { /// 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; - } + if (k == p.view or !isOpen(p, k)) return drawLive(present, p); + const Draw = struct { + fn draw(pr: Present, q: *Pardes, kk: u8) anyerror!void { + try drawParked(q, kk, pr); + } + }; + try withParked(p, k, present, Draw.draw); +} + +/// `f(ctx, p)` with view `k` live, outside any step, and the live view put +/// back after exactly as it was, as `drawOne` draws one: what a replica +/// client's frontend reads of its own view (its pointer's cells, its size) +/// while another client's input was the session's last. `f` reads only. +pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { + if (k == p.view or !isOpen(p, k)) return f(ctx, p); + const Read = struct { + fn read(x: @TypeOf(ctx), q: *Pardes, kk: u8) anyerror!void { + try unparked(q, kk, x, f); + } + }; + try withParked(p, k, ctx, Read.read); +} + +/// The live view parked, `f(ctx, p, k)`, and the live view put back after +/// exactly as it was. +fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes, u8) anyerror!void) !void { const live = p.view; park(p); defer { @@ -562,10 +592,10 @@ pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { pane.cols = pv.cols; pane.rows = pv.rows; } - if (pane.file) |*f| f.scroll = pv.scroll; + if (pane.file) |*file| file.scroll = pv.scroll; }; } - try drawParked(p, k, present); + try f(ctx, p, k); } // ---- tests ---- diff --git a/test/daemon.py b/test/daemon.py index d9ca7153..ebee286f 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -25,6 +25,9 @@ private HOME and runtime directory: the replica clients alone python3 -B test/daemon.py --far the far clients and their blob cache alone + python3 -B test/daemon.py --window-clients + window replica clients (hidden, offscreen) of a terminal's session, + at 0/30/80/207 ms, and a window's session serving replicas """ import fcntl import os @@ -528,6 +531,189 @@ def manage(binary): shutil.rmtree(root, ignore_errors=True) +class Window: + """A pardes-gui in test mode, its window offscreen and hidden, its keys + written to its stdin (the test feed: Escape as `CSI 27 u`, which a + pipe's lone ESC would hold for the next key).""" + + def __init__(self, root, name, argv, env): + (root / name).mkdir() + self.err = root / f'{name}.err' + self.p = subprocess.Popen(argv, env=dict(env, PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(root / name), SDL_VIDEO_DRIVER='offscreen'), + cwd=root, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=open(self.err, 'w'), start_new_session=True) + self.pid = self.p.pid + + def keys(self, text): + self.p.stdin.write(text.replace(b'\x1b', b'\x1b[27u')) + self.p.stdin.flush() + + def said(self): + return self.err.read_text(errors='replace') if self.err.exists() else '' + + def close(self): + if self.p.poll() is None: + self.p.stdin.close() + self.p.wait(timeout=15) + return self.p.returncode + + +def window_clients(binary, gui_binary): + """Window replica clients (`pardes-gui --attach=:`): of a + terminal's detached session, directly and through the delay relay at + 30, 80 and 207 ms, beside a terminal's client, one put off the session's + state that joins again, one that detaches; and a window's session served + to a terminal's client and a window's. Each one's keys in the session's + text, every client's digest the session's once quiet; nothing left.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='wc-', dir=os.environ.get('TMPDIR') or 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 = [] + windows = [] + 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') + body = lambda: nine.read(f'/pane/{pane}/body') + alone = fds(sess.pid) + # A terminal's client beside the windows, typing as they do. + ta = Term(30, 100) + terms.append(ta) + a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check'))) + started.append(a.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in ta.text(), timeout=30) + agreements = [] + for i, rtt in enumerate((0, 30, 80, 207)): + to = sock + relay = None + if rtt: + relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'), + str(root / f'relay{rtt}.sock'), str(sock), '--delay', str(rtt / 2)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + started.append(relay.pid) + wait_for('the relay', (root / f'relay{rtt}.sock').exists) + to = root / f'relay{rtt}.sock' + check = root / f'w{rtt}.check' + w = Window(root, f'w{rtt}', [gui_binary, f'--attach={to}:files'], dict(env, PARDES_REPLICA_CHECK=str(check))) + windows.append(w) + started.append(w.pid) + seen = len(checks(root / 'a.check')) + w.keys(b'jjjjjjj' * (i + 1) + f'Ofrom-window-{rtt}\x1b'.encode()) + os.write(ta.master, b'jjjjOfrom-terminal-%d\x1b' % rtt) + wait_for(f'the window\'s and the terminal\'s keys ({rtt} ms)', lambda: f'from-window-{rtt}'.encode() in body() + and b'from-terminal-%d' % rtt in body(), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(ta), timeout=20) + wait_for(f'the window client agrees ({rtt} ms)', lambda: agreed(check, 0), timeout=30) + wait_for('the terminal client agrees', lambda: agreed(root / 'a.check', seen), timeout=30) + # Moved and scrolled, its own view only: agreed again. + 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) + agreements.append(len(checks(check))) + assert w.close() == 0, w.said()[-800:] + if relay: + relay.kill() + relay.wait() + # One put off the session's state (PARDES_TEST_DIVERGE): the next + # unit it replays finds it, it joins again, and its keys after go in. + dcheck = root / 'wd.check' + wd = Window(root, 'wd', [gui_binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(dcheck), PARDES_TEST_DIVERGE='1')) + windows.append(wd) + started.append(wd.pid) + time.sleep(2) + wd.keys(b'jj') + time.sleep(.5) + os.write(ta.master, b'jj') + wait_for('the window client diverged and joined again', lambda: 'joined again' in wd.said(), timeout=30) + assert wd.p.poll() is None + seen = len(checks(dcheck)) + wd.keys(b'Oafter-rejoin-W\x1b') + wait_for('the window\'s keys after its rejoin', lambda: b'after-rejoin-W' in body(), timeout=30) + wait_for('the window client agrees after its rejoin', lambda: agreed(dcheck, seen), timeout=30) + # Detach typed in it: it alone goes, and says why. + wd.keys(b' sD') + wait_for('the window client let go', lambda: wd.p.poll() is not None, timeout=15) + assert 'let this client go' in wd.said(), wd.said()[-500:] + os.write(ta.master, b' sD') + wait_for('the terminal client let go', lambda: a.poll() is not None, timeout=10) + wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) + print(f'daemon: window clients of a terminal\'s session at 0/30/80/207 ms beside a terminal\'s, digests agreed ' + f'{agreements} and {len(checks(root / "a.check"))} times; one put off the session\'s state joined again and ' + f'agreed {len(checks(dcheck))} times; one detached') + 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() + # A window's session (its daemon on, as a desktop's is), served to a + # terminal's client and a window's, the window typing as they do. + win = Window(root, 'win', [gui_binary, '--9p=win', str(notes)], dict(env, PARDES_DAEMON='on')) + windows.append(win) + started.append(win.pid) + wait_for('the window session\'s 9P socket', (root / 'pardes-9p-win.sock').exists, timeout=30) + wait_for('the replica socket', sock.exists) + nine = Client(root / 'pardes-9p-win.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + tb = Term(30, 100) + terms.append(tb) + b = tb.spawn([binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check'))) + started.append(b.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in tb.text(), timeout=30) + wg = Window(root, 'wg', [gui_binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'g.check'))) + windows.append(wg) + started.append(wg.pid) + time.sleep(2) + os.write(tb.master, b'jjjOfrom-T\x1b') + wg.keys(b'jjjjjjOfrom-G\x1b') + win.keys(b'jjjjjjjjjOfrom-W\x1b') + wait_for('everyone\'s keys in the window\'s session', lambda: all(x in body() for x in (b'from-T', b'from-G', b'from-W')), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(tb), timeout=20) + for k in range(3): + nb, ng = len(checks(root / 'b.check')), len(checks(root / 'g.check')) + os.write(tb.master, b'jk') + wg.keys(b'kj') + win.keys(b'jj') + wait_for('the terminal client agrees with the window', lambda: agreed(root / 'b.check', nb), timeout=30) + wait_for('the window client agrees with the window', lambda: agreed(root / 'g.check', ng), timeout=30) + assert wg.close() == 0, wg.said()[-800:] + os.write(tb.master, b' sD') + wait_for('the terminal client let go', lambda: b.poll() is not None, timeout=10) + assert win.p.poll() is None + print(f'daemon: a window\'s session served a terminal\'s client and a window\'s, digests agreed ' + f'{len(checks(root / "b.check"))} and {len(checks(root / "g.check"))} times') + win.close() + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=15) + print('daemon: window clients\' sessions ended, nothing left behind') + finally: + for t in terms: + t.close() + for w in windows: + if w.p.poll() is None: + w.p.kill() + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + def joins(path): """A replica client's joins (PARDES_REPLICA_STATS, a line a link): how many blobs each listed, how many it held already from the core it left @@ -1341,6 +1527,8 @@ if __name__ == '__main__': manage(sys.argv[2]) elif sys.argv[1:2] == ['--far']: far(sys.argv[2]) + elif sys.argv[1:2] == ['--window-clients']: + window_clients(sys.argv[2], sys.argv[3]) 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/output.zig b/test/output.zig index 2d872bd1..2b8bf1f5 100644 --- a/test/output.zig +++ b/test/output.zig @@ -684,7 +684,8 @@ test "every enabled setting builtin mutates the State Config reports" { var chosen_font: ?[]const u8 = null; var buf: [512]u8 = undefined; for (config.Runtime.settings) |setting| { - if (!setting.enabled(builtins.capabilities)) continue; + // The view's own (a terminal's takes no window's words). + if (!setting.enabled(builtins.viewCapabilities(p))) continue; const command: []const u8 = switch (setting.action) { .theme => try std.fmt.bufPrint(&buf, "{s} acme", .{setting.word}), .shell => try std.fmt.bufPrint(&buf, "{s} fish", .{setting.word}), @@ -716,7 +717,7 @@ test "every enabled setting builtin mutates the State Config reports" { "Theme acme\n", "Shell fish\n", }) |line| try std.testing.expect(std.mem.indexOf(u8, report, line) != null); - const transition = if (!builtins.capabilities.panel_transitions) + const transition = if (!builtins.viewCapabilities(p).panel_transitions) "PanelSlide unsupported\n" else if (p.settings.panel_transition == .off) "PanelSlide off\n" @@ -724,12 +725,12 @@ test "every enabled setting builtin mutates the State Config reports" { try std.fmt.bufPrint(&buf, "{s} on\n", .{config.Runtime.findAction(.{ .transition = p.settings.panel_transition }).?.word}); try std.testing.expect(std.mem.indexOf(u8, report, transition) != null); // A bare word turns a bundled pass on at its default level. - const scene_status = if (builtins.capabilities.scene_shaders) "2" else "unsupported"; + const scene_status = if (builtins.viewCapabilities(p).scene_shaders) "2" else "unsupported"; for ([_][]const u8{"Crt"}) |name| { const line = try std.fmt.bufPrint(&buf, "{s} {s}\n", .{ name, scene_status }); try std.testing.expect(std.mem.indexOf(u8, report, line) != null); } - const tagline = if (!builtins.capabilities.tagline_font_size) + const tagline = if (!builtins.viewCapabilities(p).tagline_font_size) "TaglineSize unsupported\n" else try std.fmt.bufPrint(&buf, "TaglineSize {d}\n", .{p.settings.font.tagline_percent}); -- cgit v1.3 From 7753126756dd6a8dc371fa200e6ba84b3c0eb6f2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 10:21:51 -0300 Subject: remote client: a write to the session that fails is its hangup (a client cut off as it lags said nothing and exited 0 when its own write found the link shut before its reader did); a window client's frame is marked rendered as it presents, so the live harness reads its render as its core's time; test/daemon.py's window clients: one far (PARDES_REPLICA_FAR) put off the session's state rejoins from the core it left, and again across an Upgrade (its `refused`); a client's echo stats in PARDES_REPLICA_STATS; --ps beside them lists the session alone, a window's as (gui, attached), its clients none Co-Authored-By: Claude Opus 5.5 --- src/gui/gui.zig | 5 ++++- src/replica.zig | 16 +++++++++++----- test/daemon.py | 43 +++++++++++++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 12 deletions(-) (limited to 'src/replica.zig') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index f62871c4..ff1c7793 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2669,8 +2669,9 @@ const Client = struct { const needs = core.needs_frame; core.frames.mark_start = pardes.FrameStats.nowNs(); core.frames.mark_update = core.frames.mark_start; - r.draw(.{ .ctx = s, .f = present }); + // Its render's end is marked as it presents (`present`). core.frames.mark_render = core.frames.mark_update; + r.draw(.{ .ctx = s, .f = present }); cl.link.owed = core.present_skipped; core.frames.record(pardes.FrameStats.nowNs(), false, cl.link.owed); core.present_skipped = skipped; @@ -5194,6 +5195,8 @@ test "background opacity acknowledges success and rolls back unsupported surface fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const s = shellOf(ctx); const core = s.core; + // A replica client draws outside the pump, which marks it there. + if (s.remote != null) core.frames.mark_render = pardes.FrameStats.nowNs(); s.follow(); const g = s.gui orelse return; frame_started_ns = monoNs(); diff --git a/src/replica.zig b/src/replica.zig index 32fd4fbe..02141f44 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -2646,7 +2646,7 @@ pub const Link = struct { if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; c.input_ns = nowNs(); const msg = c.r.input(ev) catch return; - if (!sendInput(c.fd, msg)) c.quit = true; + if (!sendInput(c.fd, msg)) c.cut(); c.dirty = true; } @@ -2667,9 +2667,7 @@ pub const Link = struct { }, .screen => |bytes| { defer std.heap.c_allocator.free(bytes); - if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) { - c.quit = true; - }; + if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) c.cut(); c.dirty = true; }, .checked => |d| c.checked(d), @@ -2693,11 +2691,18 @@ pub const Link = struct { } c.dirty = false; while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) { - c.quit = true; + c.cut(); break; }; } + /// A write to the session failed: it hung up (it cut this client off, + /// lagging), as its reader will say. + fn cut(c: *Link) void { + c.quit = true; + c.gone = true; + } + /// Each pass of the frontend's loop: quiet (nothing of its own /// pending) 300 ms after its last input and its last check, and /// changed since, the session's digest is asked once, answered in @@ -2711,6 +2716,7 @@ pub const Link = struct { if (since < quiet_ns) return @intCast((quiet_ns - since) / std.time.ns_per_ms + 1); c.changed = false; c.asked = askCheck(c.fd); + if (!c.asked) c.cut(); c.asked_ns = nowNs(); return null; } diff --git a/test/daemon.py b/test/daemon.py index ebee286f..1086dac3 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -557,6 +557,14 @@ class Window: return self.p.returncode +def sessions_listed(binary, env, root): + """`pardes --ps`'s session lines, each `[name, pid, (kind, state)]`.""" + out = subprocess.run([binary, '--ps'], env=env, cwd=root, capture_output=True, timeout=30) + assert out.returncode == 0, out + rows = [line.split(maxsplit=3)[1:] for line in out.stdout.decode().splitlines() if line.lstrip().startswith('session ')] + return [[name, pid, rest] for name, pid, rest in rows] + + def window_clients(binary, gui_binary): """Window replica clients (`pardes-gui --attach=:`): of a terminal's detached session, directly and through the delay relay at @@ -587,7 +595,6 @@ def window_clients(binary, gui_binary): 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') body = lambda: nine.read(f'/pane/{pane}/body') - alone = fds(sess.pid) # A terminal's client beside the windows, typing as they do. ta = Term(30, 100) terms.append(ta) @@ -617,6 +624,10 @@ def window_clients(binary, gui_binary): wait_for('the terminal client normal again', lambda: normal_again(ta), timeout=20) wait_for(f'the window client agrees ({rtt} ms)', lambda: agreed(check, 0), timeout=30) wait_for('the terminal client agrees', lambda: agreed(root / 'a.check', seen), timeout=30) + if not rtt: + # Its clients are no sessions of the daemon's. + listed = sessions_listed(binary, env, root) + assert [(n, k) for n, _, k in listed] == [('files', '(tty, detached)')], listed # Moved and scrolled, its own view only: agreed again. n = len(checks(check)) w.keys(b'jjjjjkkk\x06\x06\x02') @@ -628,8 +639,12 @@ def window_clients(binary, gui_binary): relay.wait() # One put off the session's state (PARDES_TEST_DIVERGE): the next # unit it replays finds it, it joins again, and its keys after go in. + # Far (PARDES_REPLICA_FAR, as through ssh): its first join sent its + # blobs by hash, in zstd; its rejoin finds them in the core it left. dcheck = root / 'wd.check' - wd = Window(root, 'wd', [gui_binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(dcheck), PARDES_TEST_DIVERGE='1')) + wd = Window(root, 'wd', [gui_binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(dcheck), PARDES_TEST_DIVERGE='1', + PARDES_REPLICA_FAR='1', PARDES_REPLICA_STATS=str(root / 'wd.stats'), + XDG_CACHE_HOME=str(root / 'wd-cache'))) windows.append(wd) started.append(wd.pid) time.sleep(2) @@ -642,16 +657,30 @@ def window_clients(binary, gui_binary): wd.keys(b'Oafter-rejoin-W\x1b') wait_for('the window\'s keys after its rejoin', lambda: b'after-rejoin-W' in body(), timeout=30) wait_for('the window client agrees after its rejoin', lambda: agreed(dcheck, seen), timeout=30) + # An Upgrade (to the same build): the session lets it go in words + # (`refused`), it dials again, joins, and its keys after go in. + nine.write('/ctl', b'Upgrade\n') + wait_for('the window client joined again after the upgrade', lambda: 'joined again: the session is upgrading' in wd.said(), timeout=60) + nine.socket.close() + nine = reconnect(root / 'pardes-9p-files.sock') + seen = len(checks(dcheck)) + wd.keys(b'jjjOafter-upgrade-W\x1b') + wait_for('the window\'s keys after the upgrade', lambda: b'after-upgrade-W' in body(), timeout=30) + wait_for('the window client agrees after the upgrade', lambda: agreed(dcheck, seen), timeout=30) + assert wd.p.poll() is None # Detach typed in it: it alone goes, and says why. wd.keys(b' sD') wait_for('the window client let go', lambda: wd.p.poll() is not None, timeout=15) assert 'let this client go' in wd.said(), wd.said()[-500:] + # A link a line: the first join sent every blob, its rejoins held them. + links = joins(root / 'wd.stats') + assert len(links) == 3 and links[0]['blobs'] > 0 and links[0]['from_memory'] + links[0]['from_disk'] == 0, links + assert links[1]['from_memory'] == links[1]['blobs'] and links[1]['sent_kb'] == 0, links os.write(ta.master, b' sD') wait_for('the terminal client let go', lambda: a.poll() is not None, timeout=10) - wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) print(f'daemon: window clients of a terminal\'s session at 0/30/80/207 ms beside a terminal\'s, digests agreed ' - f'{agreements} and {len(checks(root / "a.check"))} times; one put off the session\'s state joined again and ' - f'agreed {len(checks(dcheck))} times; one detached') + f'{agreements} and {len(checks(root / "a.check"))} times; one put off the session\'s state joined again, ' + f'joined again across an Upgrade, agreed {len(checks(dcheck))} times and detached (far: its joins {joins(root / "wd.stats")})') try: nine.write(f'/pane/{pane}/exec', b'Exit\n') except OSError as e: @@ -690,12 +719,14 @@ def window_clients(binary, gui_binary): win.keys(b'jj') wait_for('the terminal client agrees with the window', lambda: agreed(root / 'b.check', nb), timeout=30) wait_for('the window client agrees with the window', lambda: agreed(root / 'g.check', ng), timeout=30) + listed = sessions_listed(binary, env, root) + assert [(n, k) for n, _, k in listed] == [('win', '(gui, attached)')], listed assert wg.close() == 0, wg.said()[-800:] os.write(tb.master, b' sD') wait_for('the terminal client let go', lambda: b.poll() is not None, timeout=10) assert win.p.poll() is None print(f'daemon: a window\'s session served a terminal\'s client and a window\'s, digests agreed ' - f'{len(checks(root / "b.check"))} and {len(checks(root / "g.check"))} times') + f'{len(checks(root / "b.check"))} and {len(checks(root / "g.check"))} times; --ps listed it alone, (gui, attached)') win.close() nine.socket.close() wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=15) -- cgit v1.3 From 1b5e4b3eaa5a25d98e41b20c13af15565b03c93f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 12:24:33 -0300 Subject: replica: the two tests that count a replica's fast confirmations start early in a second: an edit's file time is the clock's second, which the replica guesses, so a second's turn between a key and the authority's take of it was a misprediction (another client's input first: expected 4 fast, found 3, in 11 runs of 40 of a loaded Debug window build) Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index 02141f44..d6a2e0ef 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -2818,6 +2818,19 @@ const TestHost = struct { } }; +/// An edit's file time is the clock's second, which a replica guesses: a +/// second's turn between a key and the authority's take of it is a fair +/// misprediction (11 runs in 40 of a loaded Debug window build's test +/// below). The tests that count fast confirmations start early in a second. +fn freshSecond() void { + while (true) { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(.REALTIME, &ts) != 0 or ts.nsec < 50 * std.time.ns_per_ms) return; + const rest: std.c.timespec = .{ .sec = 0, .nsec = std.time.ns_per_s - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } +} + fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { const text = [_]u8{@intCast(cp & 0x7f)}; const msg = try r.input(.{ .key = .{ .cp = cp, .text = if (cp < 0x80 and cp >= 0x20) &text else "" } }); @@ -2836,6 +2849,7 @@ test "a replica's own keys apply at once, confirm on the fast path, and end at t for (sent.items) |m| gpa.free(m); sent.deinit(gpa); } + freshSecond(); for ("oabc") |k| try key(r, k, &sent); try key(r, pardes.Key.escape, &sent); try std.testing.expectEqual(@as(usize, 5), r.applied); @@ -2868,6 +2882,7 @@ test "another client's input first: a replica rolls back, replays it and applies s1.deinit(gpa); s2.deinit(gpa); } + freshSecond(); for ("oone") |k| try key(r1, k, &s1); for ("Otwo") |k| try key(r2, k, &s2); // The authority hears r2 first, then r1; both hear the units in that -- 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/replica.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 020a7d24103a7c6f788db1091fd921d41d0df9e8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:38:35 -0300 Subject: views: a view opened or closed settles in its own input: a unit taped outside a step (a leave's close_view, a join's open_view) ended with effects queued, which a replica's copy asserted on and a joiner's dump dropped A client's leave is the session's closeView, whose sync sizes again a terminal the closed view sized (`sizes`) and queues its pty's resize; the session did it at its next pump, another unit. A replica that replayed the close was left with the effect queued between steps, and its next idle copy (Replica.ready, rollback.Copy.take's assert) aborted it: a client killed when another left (H10). A client joining at that boundary was dumped with the effect still queued, which a core loaded from a dump has none of (Replica.initDump), and diverged at the session's next pump (H6: "the core called poll_frame where the tape has a host (12)"). closeView and openView settle now, inside their own taped input, so every side does the same there; openView's covers what else was queued since the last pump (a 9P request's step). Test (failing before at each of the two asserts): a client leaves, and one joins: what the leave queued is done in its unit, on every side, before the join's dump. Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 10 ++++++++++ src/replica.zig | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) (limited to 'src/replica.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 36ce6bdb..5b37342e 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -5669,6 +5669,11 @@ pub const Pardes = struct { pub fn openView(p: *Pardes) ?u8 { const taped = tape.enter(p, .open_view); defer tape.leave(p, taped); + // What was queued since the last pump (a leaving client's view + // closed, a 9P request's step) done first, as every client of the + // tape does it here: a join dumps the session next, and a core + // loaded from a dump has no queue (Replica.initDump). + p.settle(); p.needs_frame = true; return views.open(p); } @@ -5680,6 +5685,11 @@ pub const Pardes = struct { defer tape.leave(p, taped); views.close(p, k); p.sync(); + // What its sync queued (a terminal it sized, sized again) done here, + // in its own unit on every side: the session's next pump is another + // unit, and a replica takes a copy only of a core with nothing + // queued (rollback.Copy.take). + p.settle(); } /// Into view `k`, before the input its client sent. diff --git a/src/replica.zig b/src/replica.zig index 8b1d84f4..22de375f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3439,3 +3439,58 @@ test "a replica's Del of a file its session's Recent knows asks the host what th try agrees(a, r); } +/// A host with ptys whose pump polls for a frame, as a client's does. +const PtyHost = struct { + fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {} + fn poll(_: ?*anyopaque) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 { + const n = (try wire.encodeClient(buf[head_len..], .{ .event = ev })).len; + header(buf, client, seq); + return buf[0 .. head_len + n]; +} + +test "a client leaves, and one joins: what the leave queued (a terminal's resize) is done in its unit, on every side, before the join's dump" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, true); + defer old.deinit(); + // A second client, bigger, types into the terminal: it sizes it. + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 120, .rows = 40 } }))); + old.confirm(try a.take(try inputOf(&buf, 2, 2, .{ .key = .{ .cp = 'x', .text = "x" } }))); + // It leaves: the terminal is the first view's size again, its pty's + // resize queued for the next pump; and a client joins at that boundary. + var was = Stepping.begin(&a.t, null); + a.core.closeView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + // The client left idle takes its copy ahead (Replica.ready): of a core + // with nothing queued. + old.ready(); + was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse return error.NoView; + was.end(); + old.confirm(try a.unit(0, 101)); + const joiner = try joinedNow(a, 3, k); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + // The next pump's unit, every client's. + const u = try gpa.dupe(u8, try a.local(.tick)); + defer gpa.free(u); + old.confirm(u); + joiner.confirm(u); + try agrees(a, old); + try agrees(a, joiner); +} -- 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/replica.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 From 5925ddbe1aa39a64b484b68a8ddb3649b3d180e4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 16:52:24 -0300 Subject: Adopt and a terminal reader's failure are inputs (Pardes.adoptShell, readerFailed; tape.Input adopt_shell, reader_failed): tty.zig's and gui.zig's adoptShell ran the Tty word and cleared the pane's greeting straight on the core, and the window's reader failure set its pane's mode, between steps and untaped, so every client of a session that took in a dead session's shells joined again (the hunt's H15: `the unit of 0/48 diverged: the core called poll_frame where the tape has a host (10)`, gone after); replica.zig's test, which the old writes fail Co-Authored-By: Claude Opus 5.5 --- src/gui/gui.zig | 18 ++++-------------- src/pardes.zig | 30 ++++++++++++++++++++++++++++++ src/replica.zig | 27 +++++++++++++++++++++++++++ src/tape.zig | 6 ++++++ src/tty/tty.zig | 13 +++---------- 5 files changed, 70 insertions(+), 24 deletions(-) (limited to 'src/replica.zig') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index ead89a8a..be287647 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -4563,21 +4563,14 @@ const Shell = struct { var buf: [96]u8 = undefined; return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); } - var newest: u32 = 0; - for (core.panes) |slot| if (slot) |pn| { - newest = @max(newest, pn.serial); - }; if (s.adoptions_len < s.adoptions.len) { s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; s.adoptions_len += 1; - _ = core.executeBuiltinLine(core.active, "Tty"); - for (core.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { - // Its shell is up already: nothing to greet with `ls`. - pn.greet = false; - s.adoptions[s.adoptions_len - 1].pane = @intCast(id); + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; s.adopted += 1; return; - }; + } s.adoptions_len -= 1; } _ = libc.close(fd); @@ -4662,10 +4655,7 @@ const Shell = struct { if (s.gens[e.pane] == e.gen) eof: { if (e.failure) |err| { s.closePty(e.pane); - if (s.core.panes[e.pane]) |pane| { - pane.body.mode = .normal; - s.core.reportError(e.pane, "terminal reader", err); - } + s.core.readerFailed(e.pane, err); break :eof; } const pt = if (s.ptys[e.pane]) |*pt| pt else { diff --git a/src/pardes.zig b/src/pardes.zig index a4fcb2d3..81ea0cdd 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -5557,6 +5557,36 @@ pub const Pardes = struct { return if (selected.len == 0) config.defaultShell() else selected; } + /// A shell a dead session left, taken in (Adopt): a terminal pane made + /// for it as the Tty word makes one, its greeting off (the shell is up + /// already), whose spawn the host answers with that shell's pty. Its id, + /// or null with no room. An input, as every host write into the core + /// is: a replica of the session makes the same pane. + pub fn adoptShell(p: *Pardes) ?u8 { + const taped = tape.enter(p, .adopt_shell); + defer tape.leave(p, taped); + var newest: u32 = 0; + for (p.panes) |slot| if (slot) |pn| { + newest = @max(newest, pn.serial); + }; + _ = p.executeBuiltinLine(p.active, "Tty"); + for (p.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { + pn.greet = false; + return @intCast(id); + }; + return null; + } + + /// A terminal's reader failed (no end of file: the host closed its pty): + /// its pane takes keys again, and why is said. + pub fn readerFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .reader_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); + const pane = p.panes[id] orelse return; + pane.body.mode = .normal; + p.reportError(id, "terminal reader", err); + } + pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); defer tape.leave(p, taped); diff --git a/src/replica.zig b/src/replica.zig index 2af32cdb..6a01ec26 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3205,6 +3205,33 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expectEqual(digestOf(a), r.digest()); } +test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { + const gpa = std.testing.allocator; + // Its terminals as screen state, as a session's for its clients are. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), true); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + // What tty.zig's and gui.zig's adoptShell and a failed reader do between + // steps: they wrote the core straight, and every client joined again. + const host = Stepping.begin(&a.t, null); + const id = a.core.adoptShell().?; + try std.testing.expect(a.core.panes[id].?.isTerminal() and !a.core.panes[id].?.greet); + a.core.readerFailed(id, error.InputOutput); + a.core.pump(a.host) catch {}; + host.end(); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + test "a join's dump, its blobs put back where they go, is the session's dump" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); diff --git a/src/tape.zig b/src/tape.zig index 12e3bdec..e446c06a 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -46,6 +46,10 @@ pub const Input = union(enum) { take_restore, set_cwd: struct { id: usize, cwd: []const u8 }, acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool }, + /// A dead session's shell taken in (Adopt), and a terminal's reader + /// that failed: what a host writes into the core, made inputs. + adopt_shell, + reader_failed: struct { id: u8, err: anyerror }, shell_failed: struct { id: u8, err: anyerror }, restart_failed: struct { id: u8, err: anyerror }, save_failed: struct { id: u8, path: []const u8, err: anyerror }, @@ -590,6 +594,8 @@ pub const Tape = struct { .take_restore => _ = p.takeRestore(), .set_cwd => |a| p.setCwd(a.id, a.cwd), .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks), + .adopt_shell => _ = p.adoptShell(), + .reader_failed => |a| p.readerFailed(a.id, a.err), .shell_failed => |a| p.shellFailed(a.id, a.err), .restart_failed => |a| p.restartFailed(a.id, a.err), .save_failed => |a| p.saveFailed(a.id, a.path, a.err), diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 03b7a7e2..430c020d 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -2397,23 +2397,16 @@ const Shell = struct { var buf: [96]u8 = undefined; return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); } - var newest: u32 = 0; - for (core.panes) |slot| if (slot) |pn| { - newest = @max(newest, pn.serial); - }; if (s.adoptions_len < s.adoptions.len) { // A terminal pane, as the Tty word makes one; its spawn takes // this pty instead of forking a shell (`spawn`). s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; s.adoptions_len += 1; - _ = core.executeBuiltinLine(core.active, "Tty"); - for (core.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { - // Its shell is up already: nothing to greet with `ls`. - pn.greet = false; - s.adoptions[s.adoptions_len - 1].pane = @intCast(id); + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; s.adopted += 1; return; - }; + } s.adoptions_len -= 1; } // No room for it: its master goes, and the shell with this session. -- cgit v1.3 From edbdfdd988870caaa11f4676d48c6cecc16e45be Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 18:15:20 -0300 Subject: terminal: a step that reads a terminal's rows as the core keeps them marks it read too: `fill` sent a client that came since (its history all holes) none of the rows the step read shellRows set `read` only when it built the rows, not when it handed out the ones kept since the last write, so after a client joined (or was started afresh) a step reading through the cache left the client's holes unsent, and it replayed the step on blanks. Found on the way to H18, whose fix is fixer 1's (wwkwwlzt). Test (failing before): a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/terminal.zig | 7 +++++- 2 files changed, 75 insertions(+), 1 deletion(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index 6a01ec26..c44eebd7 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3635,3 +3635,72 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize try agrees(a, old); try agrees(a, joiner); } + +/// An input of a replica's in the session's order, as `Peers.input` takes +/// it: the terminals flushed to every peer, the step, the rows it read that +/// each peer holds as holes (`fill`), then its unit to every replica. +fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void { + const gpa = std.testing.allocator; + for (rs, ps) |r, peer| try screensTo(a, screens, peer, r); + const u = try gpa.dupe(u8, try a.take(msg)); + defer gpa.free(u); + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (rs, ps) |r, peer| { + rows.clearRetainingCapacity(); + try screens.fill(a.core, peer, &rows); + for (rows.items) |m| { + _ = r.screen(m); + gpa.free(m); + } + } + screens.filled(a.core); + for (rs) |r| { + r.confirm(u); + r.settle(); + } +} + +fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void { + const msg = try std.testing.allocator.dupe(u8, try r.input(ev)); + defer std.testing.allocator.free(msg); + try takeAll(a, screens, rs, ps, msg); +} + +test "a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + var line: [64]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); + const out = try gpa.dupe(u8, (try a.pending()).?); + defer gpa.free(out); + for ([_]*Replica{ r1, r2 }) |r| r.confirm(out); + // The first client alone has the terminal; it reads all of its rows + // (its text, the cursor on the first): the core keeps them. + const p1 = [_]u16{try screens.addPeer()}; + for ([_]pardes.Event{ .{ .key = .{ .cp = 'b', .ctrl = true } }, .{ .key = .{ .cp = 'g', .text = "g" } }, .{ .key = .{ .cp = 'g', .text = "g" } } }) |ev| { + const msg = try gpa.dupe(u8, try r1.input(ev)); + defer gpa.free(msg); + try takeAll(a, &screens, &.{r1}, &p1, msg); + r2.confirm(a.out.items); + } + // A second comes: its history all holes. The first reads the rows the + // core kept, nothing written since. + const ps = [_]u16{ p1[0], try screens.addPeer() }; + try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } }); + for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r); +} diff --git a/src/terminal.zig b/src/terminal.zig index 01a9777e..ade2311d 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1800,7 +1800,12 @@ pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { if (comptime !enabled) return &empty_grid; if (pane.terminal == null) return &empty_grid; const c = &p.shell_rows; - if (!c.stale and c.pane == pane) return c.rows; + if (!c.stale and c.pane == pane) { + // A step's read whether the rows are kept or made: a client that + // came since holds them as holes, and is sent them (Screens.fill). + if (!drawing) pane.terminal.?.read = true; + return c.rows; + } // Another pane's rows, or a dropped pane's still waiting for the step's // sweep (dropPane keeps them, as they may still be borrowed): this // step's rows are scratch, and nothing in the slot is written over. -- cgit v1.3 From ef0fb2f9614bd6ad35991022d7be1cb40ca156ae Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 18:11:57 -0300 Subject: terminal: an authority with screen-state replicas sizes a terminal after the step's unit, not inside it (resize_to, made by Peers.unit, null from `create` on): a client typing at another size than the last typist read history rows its replay held as holes, and the rows the session sent for them came in the new grid's numbering, which the replica cannot take, so its replay read blank text (the soak's local80 cell: jumps, njumps, jcur, pane 1.body at a b; fixer 2's H18, pane N.ovl); test/replicas.zig sizes them after its fill too; an in-process test reads the history in the step that resizes, with the session's order of flush, fill and unit, and another makes a terminal from 0xAA memory and finds no size waiting Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tape.zig | 5 +++ src/terminal.zig | 59 +++++++++++++++++++++++++++++++++ test/replicas.zig | 2 ++ 4 files changed, 165 insertions(+) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index c44eebd7..1cf68db2 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1655,6 +1655,8 @@ pub const Peers = struct { 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); + // The sizes its steps gave terminals, now: the next flush says so. + if (ps.t.core) |core| terminal.resizeDeferred(core); } /// After each of the session's pumps: its unit sent, the clients that @@ -1989,6 +1991,7 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; + terminal.resizeDeferred(core); ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; @@ -3121,6 +3124,102 @@ test "a view that sized a terminal closing leaves nothing queued: the others' re try std.testing.expectEqual(digestOf(a), r1.digest()); } +/// What the session sends a peer around a step's unit (Peers.input): the +/// rows the step read that the peer lacks, then the sizes the step gave +/// terminals (`terminal.resizeDeferred`), then the unit. +fn fillTo(a: *Authority, screens: *Screens, peer: u16, r: *Replica) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try screens.fill(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; + screens.filled(a.core); + terminal.resizeDeferred(a.core); +} + +test "a terminal sized again by a client's key: the step reads the grid it had, on both sides" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 50, .rows = 20, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + // History the replica holds as holes, of lines a narrow grid wraps in + // two, which a wider one unwraps. + var line: [96]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + // Out of terminal mode in the session's view, which sizes it; then a + // client's own view (a copy of it), wider: its first key sizes the + // terminal again, in that key's step (the soak's tty and window at two + // sizes, 2026-10-30: the client's replay read holes the session's step + // had read as text, and the fill came in the new grid's numbers). + r.confirm(try a.local(.{ .key = .{ .cp = 'b', .ctrl = true } })); + for ([_]u21{ 'g', 'g' }) |cp| r.confirm(try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + const opened = try a.openView(1); + r.view = opened.view; + r.confirm(opened.unit); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + const Step = struct { + fn take(au: *Authority, sc: *Screens, pe: u16, rr: *Replica, msg: []const u8) !void { + try screensTo(au, sc, pe, rr); + const u = try std.testing.allocator.dupe(u8, try au.take(msg)); + defer std.testing.allocator.free(u); + try fillTo(au, sc, pe, rr); + rr.confirm(u); + rr.settle(); + } + }; + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .resize = .{ .cols = 120, .rows = 30 } }))); + try key(r, 'g', &sent); + try key(r, 'g', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + // The session's view types into it, which sizes it back (the client's + // copy of its history all holes again, a new numbering); then the + // client's key that reads all of it, and sizes it again. + try screensTo(a, &screens, peer, r); + { + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + a.core.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + a.core.step(); + was.end(); + } + a.seq += 1; + const back = try gpa.dupe(u8, try a.unit(0, a.seq)); + defer gpa.free(back); + try fillTo(a, &screens, peer, r); + r.confirm(back); + r.settle(); + try screensTo(a, &screens, peer, r); + try key(r, 'w', &sent); + try key(r, 'w', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + try screensTo(a, &screens, peer, r); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + /// 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/tape.zig b/src/tape.zig index 9152bcb0..1ca064bc 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -990,6 +990,11 @@ pub fn screenReplica() bool { return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; } +/// An authority recording for screen-state replicas. +pub fn screenAuthority() bool { + return if (on) |t| t.screen_state and !t.replay and !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 { diff --git a/src/terminal.zig b/src/terminal.zig index ade2311d..e9d628aa 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -172,6 +172,11 @@ pub const State = struct { /// 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 size a step gave it, on an authority with screen-state replicas: + /// made after the step's unit (`resizeDeferred`), so that the rest of + /// the step reads the grid it had, as its replicas' replays do, and the + /// rows sent them for it (Screens.fill) are of that grid's numbering. + resize_to: ?struct { cols: u16, rows: u16 } = null, /// 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 @@ -646,11 +651,30 @@ 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; + // ...and the authority's once the step's unit is out (a client typing + // at another size than the last typist's read history rows its replay + // held as holes, and the rows came renumbered, 2026-10-30). + if (pardes.tape.screenAuthority()) { + state.resize_to = .{ .cols = cols, .rows = rows }; + return; + } state.changes +%= 1; _ = gpa; state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; } +/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an +/// authority with screen-state replicas (replica.Peers.unit). +pub fn resizeDeferred(p: *Pardes) void { + if (comptime !enabled) return; + for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| { + state.resize_to = null; + state.changes +%= 1; + p.shell_rows.markStale(pane); + state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; + }; +} + /// DECSET 2004: the program wants its pastes bracketed. pub fn bracketedPaste(pane: *const Pane) bool { if (comptime !enabled) return false; @@ -888,6 +912,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { state.reply_len = 0; state.changes = 0; state.read = false; + state.resize_to = null; // 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; @@ -960,6 +985,40 @@ test "replay ownership is terminal-only and construction rolls back on allocatio try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{true}); } +test "a terminal made fresh has no size waiting: one never resized keeps its grid when a unit is cut" { + if (comptime !enabled) return error.SkipZigTest; + // Fresh memory as a Debug build's allocator hands it out: 0xAA. A field + // `create` left unset read a size of 43690x43690 (fixer 2's, 2026-10-30). + const Filling = struct { + child: std.mem.Allocator, + fn alloc(ctx: *anyopaque, len: usize, a: std.mem.Alignment, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + const ptr = self.child.rawAlloc(len, a, ra) orelse return null; + @memset(ptr[0..len], 0xAA); + return ptr; + } + fn resize(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawResize(buf, a, n, ra); + } + fn remap(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawRemap(buf, a, n, ra); + } + fn free(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, ra: usize) void { + const self: *@This() = @ptrCast(@alignCast(ctx)); + self.child.rawFree(buf, a, ra); + } + const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free }; + }; + var filling: Filling = .{ .child = std.testing.allocator }; + const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; + const pane = try create(gpa, 40, 12); + defer gpa.destroy(pane); + defer deinitEmulator(pane, gpa); + try std.testing.expect(pane.terminal.?.resize_to == null); +} + test "document construction allocates only the pane and has inert terminal defaults" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1 }); const allocator = failing.allocator(); diff --git a/test/replicas.zig b/test/replicas.zig index b1ceffab..18060763 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -502,6 +502,8 @@ fn sendUnit(q: *std.PriorityQueue(Msg, void, Msg.order), n: *u64, unit: []const } } screens.filled(core); + // The sizes its steps gave terminals, after their rows (Peers.unit). + pardes.panes.terminal.resizeDeferred(core); totals.unit_bytes += unit.len * down.len; if (flooding) totals.flood_unit_bytes += unit.len * down.len; for (down, 1..) |*l, who| { -- cgit v1.3 From 6cb07327b717603be983fdd6df0e6d9009aae39a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 16:00:52 -0300 Subject: replica: what stopped each input's speculation is counted by name (PARDES_REPLICA_STATS's `stops`: a host function's, a site's, or a poison's reason, which tape.poison now takes; PARDES_REPLICA_TRACE says it with the input); a window client's PARDES_TEST_LATENCY says each input (`I seq ns at-once`) and, after each unit, the first of its own not confirmed (`U ns seq`), so a harness finds the frame that shows an input that waited Co-Authored-By: Claude Opus 5.5 --- src/gui/gui.zig | 14 +++++++++++++- src/replica.zig | 40 +++++++++++++++++++++++++++++++++++----- src/tape.zig | 15 +++++++++++---- src/terminal.zig | 16 ++++++++-------- 4 files changed, 67 insertions(+), 18 deletions(-) (limited to 'src/replica.zig') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index be287647..dd21680f 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2627,7 +2627,14 @@ const Client = struct { /// What the window's input made, applied at once and sent. fn feed(cl: *Client) void { - for (cl.pending.items) |ev| cl.link.input(ev); + for (cl.pending.items) |ev| { + const r = cl.link.r; + const predicted = r.echo.stats.predicted; + cl.link.input(ev); + // PARDES_TEST_LATENCY: the input, and whether it shows at once + // (applied, or its echo predicted), for the frame that shows it. + latencyNote("I {d} {d} {d}\n", .{ r.next_seq - 1, monoNs(), @intFromBool(r.applied == r.pending.items.len or r.echo.stats.predicted > predicted) }); + } cl.pending.clearRetainingCapacity(); _ = cl.texts.reset(.retain_capacity); } @@ -2651,6 +2658,11 @@ const Client = struct { // PARDES_TEST_LATENCY: when a terminal's delta came (an echo's). if (msg == .screen) latencyNote("S {d}\n", .{monoNs()}); cl.link.take(msg); + // And after a unit, the first of its own inputs not confirmed. + if (msg == .unit) { + const r = cl.link.r; + latencyNote("U {d} {d}\n", .{ monoNs(), if (r.pending.items.len > 0) r.pending.items[0].seq else r.next_seq }); + } } cl.taken.clearRetainingCapacity(); if (ended) { diff --git a/src/replica.zig b/src/replica.zig index 1cf68db2..a88873ac 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -480,6 +480,30 @@ pub const Stats = struct { copies: u32 = 0, /// Nanoseconds: a terminal's delta applied. apply_ns: std.ArrayListUnmanaged(u64) = .empty, + /// What stopped them (`Tape.poisoned_by`), a count a name. + stops: [16]Stop = @splat(.{}), + + const Stop = struct { name: []const u8 = "", n: u32 = 0 }; + + fn stop(s: *Stats, name: []const u8) void { + for (&s.stops) |*st| if (st.n == 0 or std.mem.eql(u8, st.name, name)) { + st.name = name; + st.n += 1; + return; + }; + } + + /// `name:n,...` (spaces as dashes), or `-`. + fn writeStops(s: *const Stats, w: *std.Io.Writer) !void { + var any = false; + for (s.stops) |st| if (st.n > 0) { + if (any) try w.writeByte(','); + any = true; + for (st.name) |ch| try w.writeByte(if (ch == ' ') '-' else ch); + try w.print(":{d}", .{st.n}); + }; + if (!any) try w.writeByte('-'); + } pub fn deinit(s: *Stats, gpa: Allocator) void { s.spec_ns.deinit(gpa); @@ -854,7 +878,7 @@ pub const Replica = struct { // emulator (rows, cursor, modes), which here is the last delta's and // there what the pty has said since. It waits; phase 6 predicts the // echo. - if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison(); + if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison("a terminal has the keyboard"); c.update(in.event); c.step(); const clean = tape.endSpeculation(&r.t); @@ -865,7 +889,8 @@ 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.stats.stop(r.t.poisoned_by); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call ({s}): it waits\n", .{ r.id, p.seq, in.event, r.t.poisoned_by }); r.blocked = p.seq; const upto = r.applied; r.toConfirmed(); @@ -2796,17 +2821,22 @@ pub const Link = struct { /// went, a line at its end (test/daemon.py, the gates). pub fn writeStats(c: *Link, path: []const u8) void { const st = &c.r.stats; - var buf: [600]u8 = undefined; + var buf: [1024]u8 = undefined; const e = &c.r.echo.stats; - 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} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}\n", .{ + var w: std.Io.Writer = .fixed(&buf); + w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}", .{ st.inputs, st.speculated, st.stopped, st.units, st.fast, c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, }) catch return; + // What stopped its inputs, last: a word a pair before it. + w.writeAll(" stops ") catch return; + st.writeStops(&w) catch return; + w.writeByte('\n') catch return; var z: [4096]u8 = undefined; - appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, line); + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, w.buffered()); } /// The link ended: whether the client joins again, and why in its diff --git a/src/tape.zig b/src/tape.zig index 1ca064bc..7b7c02f2 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -216,6 +216,9 @@ pub const Tape = struct { /// checks. speculating: bool = false, poisoned: bool = false, + /// What stopped it: a host function's or a site's name, or a poison's + /// reason (PARDES_REPLICA_STATS counts them). + poisoned_by: []const u8 = "", /// The allocator of the core a replay makes (a replica's rollback epoch, /// src/rollback.zig), when it is not the tape's own. core_gpa: ?std.mem.Allocator = null, @@ -801,6 +804,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype return r; } t.poisoned = true; + t.poisoned_by = name; return poisonOf(R); } const real = @field(t.real.vtable.*, name).?; @@ -839,7 +843,7 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty /// 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(); + poison("the authority's own state"); return value(.authority, f, args); } @@ -872,6 +876,7 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt // the authority's answer. if (yields and t.speculating) { t.poisoned = true; + t.poisoned_by = @tagName(s); return poisonOf(R); } if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| { @@ -995,11 +1000,12 @@ pub fn screenAuthority() bool { return if (on) |t| t.screen_state and !t.replay and !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 { +/// What a speculation reached is the authority's (`why`): the input is +/// undone and waits for the authority's order. +pub fn poison(why: []const u8) void { if (on) |t| if (t.speculating) { t.poisoned = true; + t.poisoned_by = why; }; } @@ -1098,6 +1104,7 @@ pub fn beginSpeculation(t: *Tape) void { t.replay = false; t.speculating = true; t.poisoned = false; + t.poisoned_by = ""; t.no_digest = true; t.buf.clearRetainingCapacity(); t.seeFields(); diff --git a/src/terminal.zig b/src/terminal.zig index e9d628aa..be886044 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -607,16 +607,16 @@ 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 { +fn authorityOnly(comptime why: []const u8) bool { if (!pardes.tape.speculating()) return false; - pardes.tape.poison(); + pardes.tape.poison(why); return true; } /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). pub fn scrollGrid(pane: *Pane, delta: i32) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("scroll a terminal")) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); state.changes +%= 1; @@ -632,7 +632,7 @@ pub fn atBottom(pane: *const Pane) bool { /// Snap the viewport back onto live output. pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("follow a terminal")) return; const state = pane.terminal orelse return; const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); @@ -643,7 +643,7 @@ pub fn followOutput(pane: *Pane) void { /// a scrollback; the next resize retries with the same numbers. pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("resize a terminal")) return; const state = pane.terminal orelse return; if (cols != pane.cols) for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); @@ -868,7 +868,7 @@ pub fn screenTextLen(pane: *Pane) usize { /// Release the emulator's heap. The Pane allocation itself is the core's. pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("close a terminal")) return; const state = pane.terminal orelse return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); @@ -898,7 +898,7 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { /// Allocate the live emulator half of a terminal pane. Slot ownership, serial /// assignment, and spawn effects remain core lifecycle invariants. pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (authorityOnly()) return error.OutOfMemory; + if (authorityOnly("open a terminal")) return error.OutOfMemory; const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); if (comptime !enabled) return pane; @@ -1163,7 +1163,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. if (comptime !enabled) return; if (pane.terminal == null) return; - if (authorityOnly()) return; + if (authorityOnly("a terminal's output")) 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; -- cgit v1.3 From 05826d5148b8d75c32397380b9f64e0b3477e5bb Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 16:16:25 -0300 Subject: replica: pardes's own steps over a terminal speculated, not every input while one has the keyboard (H14): what reaches its program is a pty write and stops as before; its view (normal-mode keys, the wheel, Ctrl-u) moves here at once, the rollback copy keeping each terminal's view (it holds no emulator) and a delta setting it, put back between the units it replays in the order the deltas came among them; what was applied over a terminal applied again over each delta or rows that come while it waits, and its unit taken at once rather than kept to replay over later deltas; closing a terminal stops (its pty's end is the authority's) Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 3 + src/replica.zig | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/tape.zig | 11 +++ src/terminal.zig | 43 +++++++++-- 4 files changed, 272 insertions(+), 16 deletions(-) (limited to 'src/replica.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 81ea0cdd..22f446a5 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -5226,6 +5226,9 @@ pub const Pardes = struct { for (p.panes, 0..) |slot, id| if (slot == pane) { if (watched) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = @intCast(id) } }); + // Its pty's end and its emulator are the authority's: a replica's + // speculation that closes a terminal waits for it. + if (pane.terminal != null) tape.poison("close a terminal"); ctlfs.events.noteRetire(p, pane); // ...and so do bytes still queued for the pty it no longer has. p.dropPendingWrite(id); diff --git a/src/replica.zig b/src/replica.zig index a88873ac..b4c3de8c 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -513,7 +513,15 @@ pub const Stats = struct { } }; -const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0 }; +/// `terminal`: its speculation read a terminal or moved one's view (or a +/// terminal had the keyboard), so what it did depends on the emulator, and +/// `digest` is the state it ended on: the same records are not enough. +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, digest: u64 = 0 }; + +/// A terminal's view as the authority's was once `after` of the units +/// since the copy were taken (`Replica.since`): a delta's comes after the +/// units it shows, so those before it are not replayed over it. +const View = struct { at: ?usize = null, after: usize = 0 }; pub const Replica = struct { gpa: Allocator, @@ -562,6 +570,10 @@ pub const Replica = struct { /// Its terminals' screen state, as the authority's deltas made it. screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), + /// Each terminal's view (`terminal.viewOf`) as the copy was taken, or + /// as the last delta since set it: the copy holds no emulator, and a + /// speculation may move a view, so a rollback puts these back. + views: [pardes.MAX_PANES]View = @splat(.{}), 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`. @@ -714,9 +726,24 @@ pub const Replica = struct { const was = Stepping.begin(&r.t, &r.epoch); defer was.end(); if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and - tape.sameRecords(r.pending.items[0].spec.?, u.tape)) + tape.sameRecords(r.pending.items[0].spec.?, u.tape) and endsOn(r.pending.items[0], u.tape)) { r.stats.fast += 1; + if (r.pending.items[0].terminal and r.applied > 1) { + // Right, but what it did read a terminal: replayed later (a + // rollback's `since`) it would read the deltas that came + // after it. So it is taken now, over the emulator its step + // read, and the ones on top applied again over it. + r.toConfirmed(); + if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why); + if (lastDigest(u.tape)) |d| { + r.last_digest.clearRetainingCapacity(); + r.last_digest.appendSlice(r.gpa, d) catch {}; + } + r.freePending(r.pending.orderedRemove(0)); + r.dirty = true; + return; + } r.freePending(r.pending.orderedRemove(0)); r.applied -= 1; if (r.applied == 0) { @@ -816,7 +843,8 @@ pub const Replica = struct { /// Back to the confirmed position: the copy put back, the units fast- /// pathed since replayed. fn toConfirmed(r: *Replica) void { - if (r.copy.held) { + const held = r.copy.held; + if (held) { r.copy.restore(); rollback.Epoch.select(null); // The rows a terminal was read as are no copy's: what the @@ -826,7 +854,13 @@ pub const Replica = struct { r.core().shell_rows = .{}; } r.applied = 0; - for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); + // Its views as the copy was taken, or as a delta set them between + // the units since (which may move them again). + for (r.since.items, 0..) |u, i| { + if (held) r.putViews(i); + if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); + } + if (held) r.putViews(r.since.items.len); r.clearSince(); for (r.pending.items) |*p| if (p.spec) |s| { r.gpa.free(s); @@ -860,6 +894,7 @@ pub const Replica = struct { // A grid's worth of text the copy would save, and put back stale. c.shell_rows.reset(c.gpa); r.copy.take(&r.epoch, c) catch return false; + r.keepViews(); rollback.Epoch.select(&r.epoch); r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; r.stats.copy_bytes += r.copy.savedBytes(); @@ -874,13 +909,23 @@ pub const Replica = struct { 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 - // there what the pty has said since. It waits; phase 6 predicts the - // echo. - if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison("a terminal has the keyboard"); + // Input into a terminal: what reaches its program (a key in tty + // mode, a paste, a mouse report) is a pty write, the authority's, + // and stops here (phase 6 predicts its echo). The rest (pardes's + // own keys over it, the wheel) reads its emulator, here the last + // delta's and there what the pty has said since: it is applied, + // and applied again over each delta while it waits (`screen`), so + // what the authority's unit confirms read the emulator its step + // read (the deltas come before it). + const keyboard = if (c.panes[c.active]) |pane| pane.terminal != null else false; c.update(in.event); c.step(); + p.terminal = r.t.screen_state and (keyboard or r.t.touched); + // A delta the authority's step read and this did not (one held + // back at the bound in flight) would leave the same records and + // another state: its digest says (no emulator's in it, only what + // pardes made of one). + if (p.terminal) p.digest = tape.digestOf(c); const clean = tape.endSpeculation(&r.t); const dt = nowNs() - t0; // Where a blob was written, should one be: the input that did. @@ -904,7 +949,10 @@ pub const Replica = struct { if (checked) { var after: [pardes.MAX_PANES]u64 = undefined; terminalDigests(c, &after); - if (!std.mem.eql(u64, &terminals, &after)) @panic("replica: a speculation wrote a terminal's emulator"); + if (!std.mem.eql(u64, &terminals, &after)) { + for (terminals, after, 0..) |x, y, id| if (x != y) std.debug.print("replica {d}: its input {d} ({any}) wrote terminal {d}\n", .{ r.id, p.seq, in.event, id }); + @panic("replica: a speculation wrote a terminal's emulator"); + } } r.stats.speculated += 1; r.stats.spec_ns.append(r.gpa, dt) catch {}; @@ -941,11 +989,22 @@ pub const Replica = struct { rollback.Epoch.select(&r.epoch); defer rollback.Epoch.select(null); r.copy.take(&r.epoch, c) catch return; + r.keepViews(); r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; r.stats.copy_bytes += r.copy.savedBytes(); r.stats.copies += 1; } + fn keepViews(r: *Replica) void { + if (!r.t.screen_state) return; + for (r.core().panes, &r.views) |slot, *v| v.* = .{ .at = if (slot) |pane| terminal.viewOf(pane) else null }; + } + + fn putViews(r: *Replica, after: usize) void { + if (!r.t.screen_state) return; + for (r.core().panes, r.views) |slot, v| if (v.after == after) if (slot) |pane| if (pane.terminal != null) terminal.viewTo(pane, v.at); + } + /// Whether its core loaded from the dump is the session as it was /// dumped, by the welcome's `digest`. pub fn loaded(r: *Replica, want: u64) bool { @@ -1006,6 +1065,10 @@ pub const Replica = struct { 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); + // Its view is the authority's now: a rollback puts it back + // here, and what was applied over a terminal goes on top. + if (r.copy.held) r.views[m.pane] = .{ .at = terminal.viewOf(pane), .after = r.since.items.len }; + r.overTerminals(); var seq: [4]u8 = undefined; std.mem.writeInt(u32, &seq, applied.seq, .little); return r.screenMessage(m.pane, m.serial, kind_ack, &seq); @@ -1014,12 +1077,28 @@ pub const Replica = struct { sc.asked = null; sc.rep.applyRows(state.gpa, &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); c.shell_rows.markStale(pane); + r.overTerminals(); return null; }, else => return null, } } + /// A terminal was written (a delta, rows): what is applied over the + /// emulator (`Pending.terminal`) read the one before it, and the + /// authority's step reads this one (it comes before that step's unit). + /// Back to the confirmed position, to be applied again over it. + fn overTerminals(r: *Replica) void { + const any = for (r.pending.items[0..r.applied]) |p| { + if (p.terminal) break true; + } else false; + if (!any) return; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + r.toConfirmed(); + r.dirty = true; + } + /// A request for the rows a terminal shows here and does not hold (its /// holes in view, after a scroll back), one a terminal at a time; null /// when none is owed. Until the answer, a hole shows blank. @@ -1061,6 +1140,25 @@ pub const Replica = struct { } }; +/// Whether a unit ends where speculation `p` did: always, but for one over +/// a terminal, whose digest must be the unit's last (a word, the stream's; +/// a unit of a traced session, a field a word, is taken on its records). +fn endsOn(p: Pending, unit: []const u8) bool { + if (!p.terminal) return true; + var r: walk.Reader = .{ .bytes = unit }; + var last: ?[]const u8 = null; + while (true) { + const head = r.take(5) catch break; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch break; + if (head[0] == tape.digest_kind) last = payload; + } + var in: walk.Reader = .{ .bytes = last orelse return true }; + var room: [64]u8 = undefined; + var fixed: std.heap.FixedBufferAllocator = .init(&room); + const words = walk.decode([]const u64, &in, fixed.allocator()) catch return true; + return words.len != 1 or words[0] == p.digest; +} + /// 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). @@ -1081,7 +1179,8 @@ 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.digestScreen(&h); + // Its view aside: a speculation may move it (`views`). + state.digestScreen(&h, false); d.* = h.final(); } } @@ -3250,6 +3349,116 @@ test "a terminal sized again by a client's key: the step reads the grid it had, try std.testing.expectEqual(digestOf(a), r.digest()); } +fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + switch (what) { + .flush => try screens.flush(a.core, peer, &msgs), + .fill => { + try screens.fill(a.core, peer, &msgs); + screens.filled(a.core); + }, + } + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +test "pardes's own keys over a terminal's history apply at once: its view moves here, a delta while they wait puts them on top again, the authority's ends the same" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + r.confirm(try a.local(.{ .focus = true })); + var line: [64]u8 = undefined; + for (0..200) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "history line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Normal mode in the shell, up a few rows, then half a screen and more: + // the cursor leaves the view, and the view follows it back. + const keys = [_]pardes.Key{ + .{ .cp = a.core.opts.tty_toggle, .ctrl = true }, .{ .cp = 'k', .text = "k" }, .{ .cp = 'k', .text = "k" }, + .{ .cp = 'u', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, .{ .cp = 'k', .text = "k" }, + }; + for (keys) |k| try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = k }))); + try std.testing.expectEqual(@as(u32, 0), r.stats.stopped); + try std.testing.expectEqual(sent.items.len, r.applied); + const here = r.core().panes[shell].?; + try std.testing.expect(terminal.viewOf(here) != null); + const scrolled = terminal.viewOf(here); + // The shell prints while they wait: its unit, then its delta, which + // sets the view the authority's (at the bottom still): what was applied + // goes on top of it again. + for (0..5) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "late line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + try std.testing.expectEqual(sent.items.len, r.applied); + try std.testing.expect(terminal.viewOf(r.core().panes[shell].?) != null); + // The authority takes them in order, the terminal sent before each and + // the rows its step read after, as the session's loop does. + for (sent.items) |m| { + try deliver(a, &screens, peer, r, .flush); + r.settle(); + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expect(terminal.viewOf(a.core.panes[shell].?) != null); + try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); + _ = scrolled; + // A rollback with nothing confirmed on top puts the view back: one more + // key up, and another's unit first. + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'u', .ctrl = true } }))); + const before = terminal.viewOf(a.core.panes[shell].?); + try std.testing.expect(terminal.viewOf(r.core().panes[shell].?).? < before.?); + r.confirm(try a.local(.{ .focus = false })); + r.settle(); + const last = try gpa.dupe(u8, try a.take(sent.items[sent.items.len - 1])); + defer gpa.free(last); + r.toConfirmed(); + try std.testing.expectEqual(before, terminal.viewOf(r.core().panes[shell].?)); + r.dirty = true; + r.settle(); + try deliver(a, &screens, peer, r, .fill); + r.confirm(last); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); +} + /// 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/tape.zig b/src/tape.zig index 7b7c02f2..3975681e 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -219,6 +219,9 @@ pub const Tape = struct { /// What stopped it: a host function's or a site's name, or a poison's /// reason (PARDES_REPLICA_STATS counts them). poisoned_by: []const u8 = "", + /// It read a terminal (its rows) or moved one's view: what it did + /// depends on the emulator, which deltas write (`touchTerminal`). + touched: bool = false, /// The allocator of the core a replay makes (a replica's rollback epoch, /// src/rollback.zig), when it is not the tape's own. core_gpa: ?std.mem.Allocator = null, @@ -1009,6 +1012,13 @@ pub fn poison(why: []const u8) void { }; } +/// A speculation read a terminal's rows or moved its view (`Tape.touched`). +pub fn touchTerminal() void { + if (on) |t| if (t.speculating) { + t.touched = true; + }; +} + /// A harmless stand-in for what a host call or a syscall would have /// returned to a speculation that is thrown away once it returns: an /// error where one can be said, else zeroes. @@ -1105,6 +1115,7 @@ pub fn beginSpeculation(t: *Tape) void { t.speculating = true; t.poisoned = false; t.poisoned_by = ""; + t.touched = false; t.no_digest = true; t.buf.clearRetainingCapacity(); t.seeFields(); diff --git a/src/terminal.zig b/src/terminal.zig index be886044..20a9a015 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -192,15 +192,17 @@ pub const State = struct { // 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); + s.digestScreen(h, true); } - pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher) void { + /// `view`: where its view is too (a replica's speculation may move it). + pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher, view: bool) void { if (comptime !enabled) return; const screen = s.vt.screens.active; const sb = screen.pages.scrollbar(); const cur = screen.cursor; - const facts = .{ sb.total, sb.offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; + const offset = if (view) sb.offset else 0; + const facts = .{ sb.total, offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); h.update(s.reply[0..s.reply_len]); var buf: [4096]u8 = undefined; @@ -613,10 +615,40 @@ fn authorityOnly(comptime why: []const u8) bool { return true; } +/// A speculation that moves a terminal's view: a screen-state replica's +/// may (its rollback puts the view back, replica.zig `views`, and a delta +/// sets it), any other stops (`authorityOnly`). +fn viewOnly(comptime why: []const u8) bool { + if (!pardes.tape.speculating()) return false; + if (pardes.tape.screenState()) { + pardes.tape.touchTerminal(); + return false; + } + pardes.tape.poison(why); + return true; +} + +/// Where a terminal's view is: its top row counted from the top of its +/// history, or null following its output (a replica's rollback copy, +/// which holds no emulator, keeps it so). +pub fn viewOf(pane: *const Pane) ?usize { + if (comptime !enabled) return null; + const state = pane.terminal orelse return null; + const pages = &state.vt.screens.active.pages; + if (pages.viewport == .active) return null; + return pages.scrollbar().offset; +} + +pub fn viewTo(pane: *Pane, at: ?usize) void { + if (comptime !enabled) return; + const state = pane.terminal orelse return; + state.vt.screens.active.scroll(if (at) |row| .{ .row = row } else .active); +} + /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). pub fn scrollGrid(pane: *Pane, delta: i32) void { if (comptime !enabled) return; - if (authorityOnly("scroll a terminal")) return; + if (viewOnly("scroll a terminal")) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); state.changes +%= 1; @@ -632,7 +664,7 @@ pub fn atBottom(pane: *const Pane) bool { /// Snap the viewport back onto live output. pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; - if (authorityOnly("follow a terminal")) return; + if (viewOnly("follow a terminal")) return; const state = pane.terminal orelse return; const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); @@ -1858,6 +1890,7 @@ fn dumpedBytes(pin: ghostty_vt.Pin, cell: *const ghostty_vt.Cell) usize { pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { if (comptime !enabled) return &empty_grid; if (pane.terminal == null) return &empty_grid; + pardes.tape.touchTerminal(); const c = &p.shell_rows; if (!c.stale and c.pane == pane) { // A step's read whether the rows are kept or made: a client that -- cgit v1.3 From 494aeadc46212766b3d7869a2ed8b811ac490ffa Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 17:54:43 -0300 Subject: replica: an input's time is its client's: it is sent stamped with the client's guess of the session's clocks (wire 18's `stamp`), the guess its speculation records, and the authority's step reads that, not its own clocks (within a second of them, else its own), so a step's clock read never mispredicts: an edit's second across a turn, a message's millisecond (Ctrl-W's, a jump's) were each a rollback (fixer 1: 4 seen in 2 minutes), and every reapply of one now reads the same time Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 15 ++++++- src/replica.zig | 116 ++++++++++++++++++++++++++++++++++++++++++-------- src/tape.zig | 7 ++- test/daemon.py | 2 +- 4 files changed, 119 insertions(+), 21 deletions(-) (limited to 'src/replica.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index a809bebd..6090dedb 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"); +/// 18: a replica's input is stamped with its guess of the session's +/// clocks, the time its step reads (phase 6's instant interaction notes); /// 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 @@ -20,7 +22,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 = 17; +pub const version: u16 = 18; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -85,6 +87,10 @@ pub const ClientTag = enum(u8) { far = 0x06, /// The join's blobs a far client lacks, a bit each in `blobs` order. want = 0x07, + /// The session's clocks (realtime and monotonic, ns) as the client + /// guessed them for the input that follows: its time, which the step + /// reads (src/replica.zig). + stamp = 0x08, key = 0x10, mouse = 0x11, @@ -269,6 +275,7 @@ pub const ClientMsg = union(enum) { check, far: bool, want: []const u8, + stamp: [2]i64, event: pardes.Event, }; @@ -497,6 +504,7 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { .screen_ack, .want => |bytes| try w.writeAll(bytes), .check => {}, .far => |far| try w.writeByte(@intFromBool(far)), + .stamp => |s| for (s) |v| try w.writeInt(i64, v, .little), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -572,6 +580,7 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { .check => .check, .far => .{ .far = try getBool(&r) }, .want => return .{ .want = payload }, + .stamp => .{ .stamp = .{ try r.takeInt(i64, .little), try r.takeInt(i64, .little) } }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -633,7 +642,7 @@ const msg_slack = header_len + 64; pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { .hello => |h| h.session.len, - .bye, .ping, .check, .far => 0, + .bye, .ping, .check, .far, .stamp => 0, .screen_ack, .want => |bytes| bytes.len, .event => |ev| switch (ev) { // Its hits' places (14 bytes each) past the slack. @@ -775,6 +784,8 @@ test "detached wire: a session is never told to do a frontend's remembering" { "hello", "bye", "ping", "screen_ack", "check", // ...and how far it is, and which of the join's blobs it lacks. "far", "want", + // ...and when its next input was, by the session's clocks. + "stamp", // ...and everything a person can do to a window. "key", "mouse", "resize", "paste", "command", "pdf_scroll", "pinch", "touch_scroll", diff --git a/src/replica.zig b/src/replica.zig index b4c3de8c..e483693a 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -36,6 +36,8 @@ const Pardes = pardes.Pardes; const checked = builtin.mode == .debug or builtin.mode == .safe; const head_len = 6; // origin or client: u16le, seq: u32le +/// A `stamp` frame (wire.zig): its header and the two clocks. +const stamp_len = wire.header_len + 16; /// Confirmed units fast-pathed under one copy before it is taken afresh: /// a rollback replays them all. const rebase_after = 32; @@ -75,17 +77,36 @@ fn unitHeader(buf: []u8, origin: u16, seq: u32) void { std.mem.writeInt(i64, buf[14..22], clockNs(.MONOTONIC), .little); } -pub const Input = struct { client: u16, seq: u32, event: pardes.Event }; +pub const Input = struct { client: u16, seq: u32, event: pardes.Event, stamp: ?[2]i64 = null }; -/// A client's input message; its event borrows `msg`. +/// A client's input message: its head, a `stamp` frame (when its client +/// guessed the session's clocks), and its event's frame, which the event +/// borrows. pub fn parseInput(msg: []const u8) ?Input { if (msg.len < head_len) return null; - const f = (wire.framed(msg[head_len..]) catch return null) orelse return null; + var rest = msg[head_len..]; + var f = (wire.framed(rest) catch return null) orelse return null; + var stamp: ?[2]i64 = null; + if (f.tag == @backingInt(wire.ClientTag.stamp)) { + stamp = (wire.decodeClient(f.tag, f.payload) catch return null).stamp; + rest = rest[f.total..]; + f = (wire.framed(rest) catch return null) orelse return null; + } const ev = switch (wire.decodeClient(f.tag, f.payload) catch return null) { .event => |ev| ev, else => return null, }; - return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev }; + return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev, .stamp = stamp }; +} + +/// An input's stamp as the authority takes it: the client's guess of its +/// clocks, so the guess its speculation recorded is the step's own; none +/// from a client a second or more off (a stall, a bad round trip), whose +/// step reads the clocks here. +fn stampOf(in: Input) ?tape.Guess { + const s = in.stamp orelse return null; + if (@abs(s[0] - clockNs(.REALTIME)) >= std.time.ns_per_s or @abs(s[1] - clockNs(.MONOTONIC)) >= std.time.ns_per_s) return null; + return .{ .real_ns = s[0], .mono_ns = s[1], .gmtoff = gmtoff() }; } /// The core whose tape is `t`, stepped: tape.on is process-wide, and so @@ -252,8 +273,10 @@ pub const Authority = struct { const in = parseInput(msg) orelse return error.BadInput; const was = Stepping.begin(&a.t, null); a.core.toView(a.view_of[in.client]); + a.t.stamp = stampOf(in); a.core.update(in.event); a.core.step(); + a.t.stamp = null; was.end(); return a.unit(in.client, in.seq); } @@ -516,7 +539,7 @@ pub const Stats = struct { /// `terminal`: its speculation read a terminal or moved one's view (or a /// terminal had the keyboard), so what it did depends on the emulator, and /// `digest` is the state it ended on: the same records are not enough. -const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, digest: u64 = 0 }; +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, digest: u64 = 0, guess: ?tape.Guess = null }; /// A terminal's view as the authority's was once `after` of the units /// since the copy were taken (`Replica.since`): a delta's comes after the @@ -688,12 +711,20 @@ pub const Replica = struct { r.stats.inputs += 1; const seq = r.next_seq; r.next_seq += 1; - const room = try r.gpa.alloc(u8, head_len + wire.clientBound(.{ .event = ev })); + // Its time, as the session's clocks will read when it steps it (a + // round trip on from the last unit's, the clocks here in process): + // the step's clock reads, here and there (`stampOf`). + const g: tape.Guess = if (r.guess) guess: { + const since: i64 = @intCast(nowNs() -| r.stamp.at_ns + r.rtt_ns); + break :guess .{ .real_ns = r.stamp.real_ns + since, .mono_ns = r.stamp.mono_ns + since, .gmtoff = r.gmtoff }; + } else .{ .real_ns = clockNs(.REALTIME), .mono_ns = clockNs(.MONOTONIC), .gmtoff = gmtoff() }; + const room = try r.gpa.alloc(u8, head_len + stamp_len + wire.clientBound(.{ .event = ev })); defer r.gpa.free(room); - const n = (try wire.encodeClient(room[head_len..], .{ .event = ev })).len; + _ = try wire.encodeClient(room[head_len..][0..stamp_len], .{ .stamp = .{ g.real_ns, g.mono_ns } }); + const n = (try wire.encodeClient(room[head_len + stamp_len ..], .{ .event = ev })).len; header(room, r.id, seq); - const msg = try r.gpa.dupe(u8, room[0 .. head_len + n]); - try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs() }); + const msg = try r.gpa.dupe(u8, room[0 .. head_len + stamp_len + n]); + try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs(), .guess = g }); r.dirty = true; r.settle(); if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, seq, r.applied == r.pending.items.len); @@ -904,10 +935,8 @@ 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; + // The time it was sent with, every time it is applied. + r.t.guess = p.guess; c.toView(r.view); // Input into a terminal: what reaches its program (a key in tty // mode, a paste, a mouse report) is a pty write, the authority's, @@ -1411,6 +1440,9 @@ pub const Peer = struct { sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). seq: u32 = 0, + /// The `stamp` frame its next input came with (its reader's). + stamp: [stamp_len]u8 = undefined, + stamped: bool = false, reader: ?std.Thread = null, writer: ?std.Thread = null, lock: std.Io.Mutex = .init, @@ -1652,14 +1684,25 @@ pub const Peer = struct { peer.cond.signal(io); return true; }, + // Its next input's time, kept for it (this thread's alone). + .stamp => { + const s = (wire.decodeClient(tag, payload) catch return false).stamp; + _ = wire.encodeClient(&peer.stamp, .{ .stamp = s }) catch return false; + peer.stamped = true; + return true; + }, else => {}, } peer.seq += 1; - const msg = peer.peers.gpa.alloc(u8, head_len + wire.header_len + payload.len) catch return false; + const stamp: []const u8 = if (peer.stamped) &peer.stamp else &.{}; + peer.stamped = false; + const msg = peer.peers.gpa.alloc(u8, head_len + stamp.len + wire.header_len + payload.len) catch return false; header(msg, peer.id, peer.seq); - 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); + @memcpy(msg[head_len..][0..stamp.len], stamp); + const at = head_len + stamp.len; + msg[at] = tag; + std.mem.writeInt(u32, msg[at + 1 ..][0..4], @intCast(payload.len), .little); + @memcpy(msg[at + wire.header_len ..], payload); if (parseInput(msg) == null) { peer.peers.gpa.free(msg); return false; @@ -1843,8 +1886,10 @@ pub const Peers = struct { // it read, its unit (part B terminal notes' order). ps.screensOut(core, .flush); core.toView(peer.view); + ps.t.stamp = stampOf(in); core.update(in.event); core.step(); + ps.t.stamp = null; ps.screensOut(core, .fill); ps.unit(peer.id, in.seq); } @@ -3063,6 +3108,43 @@ test "a replica's own keys apply at once, confirm on the fast path, and end at t try std.testing.expectEqual(digestOf(a), r.digest()); } +test "an input's time is its client's: an edit's second confirms on the fast path though the authority takes it in the next" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'o', &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + // Typed 30 ms before a second's turn, taken 30 ms after it: the edit's + // file time is the clock's second, read in the step. + while (true) { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.REALTIME, &ts); + if (ts.nsec >= 970 * std.time.ns_per_ms) break; + const rest: std.c.timespec = .{ .sec = 0, .nsec = 970 * std.time.ns_per_ms - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } + try key(r, 'x', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + const late: std.c.timespec = .{ .sec = 0, .nsec = 60 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&late, null); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 2), r.stats.fast); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + test "another client's input first: a replica rolls back, replays it and applies its own again" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); diff --git a/src/tape.zig b/src/tape.zig index 12f7fe3d..4759d598 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -257,6 +257,11 @@ pub const Tape = struct { /// (src/replica.zig): its clock reads (a message's stamp, a file's /// mtime) answer this instead of the clocks here. guess: ?Guess = null, + /// The same guess, as the client sent it with its input, while the + /// authority steps that input: the step's clock reads are the input's + /// time, so the guess a speculation recorded is what the authority + /// records (a message's millisecond, an edit's second never miss). + stamp: ?Guess = null, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -914,7 +919,7 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt t.poisoned_by = @tagName(s); return poisonOf(R); } - if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| { + if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |g| { const r = guessed(R, g); t.record(.host, u16, &id); t.record(.ret, R, &r); diff --git a/test/daemon.py b/test/daemon.py index b55c9b24..8633238f 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -208,7 +208,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 = 17 + WIRE = 18 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) -- cgit v1.3 From 2fb0d38b1e233d8ccba0f185bc7c8acec94234e8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 19:31:57 -0300 Subject: replica: no digest is taken in a speculation: one over a terminal kept its digest to check its unit's against, and a blob born under the copy and digested there keeps its word past the epoch's free of it (src/walk.zig), so a later blob at that address read as it (the replicas harness, Debug, seed 11 at 207 ms: "not what its kept word was"; in a release build a divergence); a speculation over a terminal is confirmed by its records, as any other, its unit taken at once when more is applied on top Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index e483693a..439f71a2 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -537,9 +537,8 @@ pub const Stats = struct { }; /// `terminal`: its speculation read a terminal or moved one's view (or a -/// terminal had the keyboard), so what it did depends on the emulator, and -/// `digest` is the state it ended on: the same records are not enough. -const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, digest: u64 = 0, guess: ?tape.Guess = null }; +/// terminal had the keyboard), so what it did depends on the emulator. +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, guess: ?tape.Guess = null }; /// A terminal's view as the authority's was once `after` of the units /// since the copy were taken (`Replica.since`): a delta's comes after the @@ -757,7 +756,7 @@ pub const Replica = struct { const was = Stepping.begin(&r.t, &r.epoch); defer was.end(); if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and - tape.sameRecords(r.pending.items[0].spec.?, u.tape) and endsOn(r.pending.items[0], u.tape)) + tape.sameRecords(r.pending.items[0].spec.?, u.tape)) { r.stats.fast += 1; if (r.pending.items[0].terminal and r.applied > 1) { @@ -950,11 +949,9 @@ pub const Replica = struct { c.update(in.event); c.step(); p.terminal = r.t.screen_state and (keyboard or r.t.touched); - // A delta the authority's step read and this did not (one held - // back at the bound in flight) would leave the same records and - // another state: its digest says (no emulator's in it, only what - // pardes made of one). - if (p.terminal) p.digest = tape.digestOf(c); + // No digest is taken here: a blob born under the copy and digested + // keeps its word past the epoch's free of it (src/walk.zig), and a + // later blob at that address read as it. const clean = tape.endSpeculation(&r.t); const dt = nowNs() - t0; // Where a blob was written, should one be: the input that did. @@ -1169,25 +1166,6 @@ pub const Replica = struct { } }; -/// Whether a unit ends where speculation `p` did: always, but for one over -/// a terminal, whose digest must be the unit's last (a word, the stream's; -/// a unit of a traced session, a field a word, is taken on its records). -fn endsOn(p: Pending, unit: []const u8) bool { - if (!p.terminal) return true; - var r: walk.Reader = .{ .bytes = unit }; - var last: ?[]const u8 = null; - while (true) { - const head = r.take(5) catch break; - const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch break; - if (head[0] == tape.digest_kind) last = payload; - } - var in: walk.Reader = .{ .bytes = last orelse return true }; - var room: [64]u8 = undefined; - var fixed: std.heap.FixedBufferAllocator = .init(&room); - const words = walk.decode([]const u64, &in, fixed.allocator()) catch return true; - return words.len != 1 or words[0] == p.digest; -} - /// 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). -- cgit v1.3 From e6a114eed043dd1ca50be1cab912662fa9150fda Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:04:02 -0300 Subject: dump: a closed terminal brought back on a screen-state replica gets its emulator on the C heap, as terminal.create makes one (terminal.emulatorGpa): loadPane named no `.terminal` allocator, so its emulator was the rollback epoch's, and its screen replica's blobs, made with State.gpa, were freed with the C heap's behind the epoch at a client's end and again by it (H21: `double free or corruption` in Epoch.deinit at a hangup, `free(): invalid pointer` at a rejoin), and a rollback freed what a delta under a held copy made of them Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 21 +++++++++++++++++++++ src/dump.zig | 2 +- src/replica.zig | 25 +++++++++++++++++++++++++ src/terminal.zig | 10 ++++++++-- 4 files changed, 55 insertions(+), 3 deletions(-) (limited to 'src/replica.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index b1ee51d5..6dfa799c 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -4354,3 +4354,24 @@ Not done, and why: - The window's present is its frame (a software present in the test, up to a frame): a prediction is drawn in the same pass as its key, so it cannot come sooner than the window's own frame. + +After the line, the hunt's H21 (a client's normal end aborted): +- **A closed terminal brought back put its emulator on the rollback + epoch.** Both clients still alive at a box soak's end died at their + hangup with `double free or corruption (!prev)` in `Epoch.deinit`; a + local soak at 207 ms did it in 2.5 min, `free(): invalid pointer` in a + terminal's screen replica's teardown at a rejoin. A terminal closed and + brought back (Panes, `Look @p`) is loaded by `dump.loadPane`, which + named no `.terminal` allocator, so on a screen-state replica its + emulator was made with the core's, the epoch's, against `State.gpa`'s + rule. Its screen replica's blobs (`apply`, with `State.gpa`) were the + epoch's then, freed with the C heap's at the end behind its back, and + freed again by the epoch; a delta under a held copy made them born in + it, and a rollback freed them, and the emulator's pages, from under it. + - Fix: `terminal.emulatorGpa`, `create`'s rule, is `loadPane`'s + `.terminal` too. + - Test: replica.zig's closed terminal brought back on a screen-state + replica, its State not the epoch's (failed before). The local soak at + 207 ms, ReleaseSafe (before, on lppumkvm, both its clients aborted): + 2.5 min, both end 0; 4 min at seed 32, four clients, none aborts (its + one breach, a window's panes gone from its view, is not this one). diff --git a/src/dump.zig b/src/dump.zig index c64f83c4..5f5f5870 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -539,7 +539,7 @@ pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![ pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane { var l = try walk.Load(&.{pardes.panes.Pane}).init(p.gpa, bytes); defer l.deinit(); - l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa }; + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = pardes.panes.terminal.emulatorGpa(p.gpa) }; const pane = try p.gpa.create(pardes.panes.Pane); errdefer { l.rollback(); diff --git a/src/replica.zig b/src/replica.zig index 439f71a2..da0c3e64 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -3266,6 +3266,31 @@ 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 closed terminal brought back on a screen-state replica: its emulator on the C heap, never the rollback epoch's" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + r.confirm(try a.local(.{ .command = "Del" })); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + var line: [32]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) })); + try std.testing.expect(r.diverged == null); + const back = for (r.core().panes) |slot| { + if (slot) |pane| if (pane.terminal) |state| break state; + } else return error.NotBack; + // Its deltas write it while a copy is held, and its screen replica's + // blobs are freed with the C heap's (`deinit`): the epoch's, a rollback + // freed what they held and the exit freed them behind its back (H21). + try std.testing.expect(r.epoch.owns(@intFromPtr(back)) == null); +} + test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" { if (comptime !terminal.enabled) return error.SkipZigTest; const gpa = std.testing.allocator; diff --git a/src/terminal.zig b/src/terminal.zig index d4238c83..86eec32d 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -607,6 +607,13 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { /// A screen-state replica's emulators' allocator (`State.gpa`). pub const emulator_gpa = std.heap.c_allocator; +/// What a terminal's emulator is made with, by `create` or a pane's load +/// (dump.loadPane, a closed terminal brought back): `gpa`, but on a screen- +/// state replica `emulator_gpa`. A hosted build's: the web has no C heap. +pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator { + return if (comptime pardes.hosted) (if (pardes.tape.screenReplica()) emulator_gpa else gpa) else gpa; +} + /// 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(comptime why: []const u8) bool { @@ -936,8 +943,7 @@ 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; - // A screen-state replica is a hosted build's: the web has no C heap. - const own = if (comptime pardes.hosted) (if (pardes.tape.screenReplica()) emulator_gpa else gpa) else gpa; + const own = emulatorGpa(gpa); const state = try own.create(State); errdefer own.destroy(state); state.gpa = own; -- cgit v1.3 From d00c40694dc239854263616f710b4a197d25d145 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:00:30 -0300 Subject: views: a client that joins again has its own view back, layout, focus and the panes opened in it: an Upgrade's clients and one whose bye says it joins again (a divergence's, wire 19's `bye.again`) leave their view kept for them (`views_away`, digested and dumped, so an Upgrade's new build has it; taped `awayView`), the client names it in its next hello (`Hello.view`, handed on by the daemon with the connection) and its join takes it back (taped `rejoinView`) rather than a copy of the session's; the live view is never a kept one, `views.count` counts the others, and a join with every view taken lets one go (every Upgrade made each client's own panes leave its view: the hunt's 80 ms cell lost `+Search` and `+New`) Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 48 ++++++++++++++++++ src/daemon.zig | 14 +++-- src/detached/wire.zig | 29 ++++++++--- src/gui/gui.zig | 13 ++--- src/pardes.zig | 31 ++++++++++++ src/replica.zig | 138 ++++++++++++++++++++++++++++++++++++-------------- src/tape.zig | 4 ++ src/tty/tty.zig | 25 +++++---- src/views.zig | 83 +++++++++++++++++++++++++++--- test/daemon.py | 90 +++++++++++++++++++++++--------- 10 files changed, 378 insertions(+), 97 deletions(-) (limited to 'src/replica.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 6dfa799c..c7be4b5a 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -4375,3 +4375,51 @@ After the line, the hunt's H21 (a client's normal end aborted): 207 ms, ReleaseSafe (before, on lppumkvm, both its clients aborted): 2.5 min, both end 0; 4 min at seed 32, four clients, none aborts (its one breach, a window's panes gone from its view, is not this one). + +**A rejoin's view is its own again** (2026-10-30; `src/views.zig`'s +`away`, `Pardes.awayView`/`rejoinView`, wire 19). Every rejoin gave the +client a new view, a copy of the session's: a divergence's, and above all +an Upgrade's, which every client makes after each install (the hunt's +80 ms cell: the terminal client lost `+Search` and `+New` from its view; +likely the user's "some panes just close randomly right after I open +them"). `leaveView` covered only a session's last client, while no one +else watched it. +- **A view kept for its client.** An Upgrade's clients + (`Peers.refuseAll`) and one whose bye says it joins again (a + divergence's: `bye.again`) leave their view open as it is, kept for them + (`views_away`, a session field: digested, and dumped, so the new build + has it). The client names the view it had in its next hello + (`Hello.view`; the daemon hands it on with the connection, + `replica `), and its join takes that view back (`rejoinView`, settled + as `openView` is) rather than a new one. Both are taped inputs. Any other + end closes the view as before. +- **The live view is never one.** One kept going live sends the session + into another, or into a copy of its own. `views.count` counts the views + someone looks at, so the last of those stays, a tty's Detach is "one of + several" only beside another, and `closeKeeping` never writes into a kept + one. Drawing skips them, and an Upgrade's new build opens no window for + one and closes none for want of a terminal (`resumeTerminals`, + `resumeWindows`). +- **Bounded by room.** A kept view goes when a join finds every view taken + (`openView`, the lowest first): a client that never comes back (one of + the old build, told to upgrade too) leaves one. ponytail: no timer; until + then it costs a parked slot, and a terminal it sized last keeps that size + until another view types into it. ponytail: a divergence's redial that + reached the session before its old link's end takes a new view. +- **Tests.** views.zig's: two clients' views through an Upgrade's dump, + each back in its own, and a join with no room lets one go. replica.zig's: + a divergence's rejoin, its pane in its view again, and the other client + agrees. `test/daemon.py --upgrade`: two replica clients each open a pane + in their own view, and after the Upgrade's rejoin each one's `/layout` + holds its own pane and not the other's; and the plain run's client D, + put off now at its 100th unit, opens a pane in its own view first, and + after its divergence's rejoin its view holds it and its keys go into it. + main's binaries fail both (`('its own view again', 4, 5, ...)`, `('client + D in its own view again', 2, ...)`). +- **Not this one:** the H21 soak's "panes [...] left its view while still + open, with no rejoin since the last quiet point" (local 207 ms, seed 32, + a window client with one link all through) is no rejoin's. +- **Gates** (on zponxort): Debug `core-test` tty, `replicas`, `replay`, + `test/daemon.py` 20 cycles, `--upgrade` (against a `-Dstamp-commit` + build), `--window-clients` and `--gui`; ReleaseSafe `--upgrade` (from + the Debug build) and `--window-clients`: all pass. diff --git a/src/daemon.zig b/src/daemon.zig index 467af8b8..b93f5d43 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -379,10 +379,13 @@ 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, + /// A replica's connection, its hello taken (src/replica.zig `Peers`), + /// and the view it joins again for, if it does. + replica: Joining, }; +pub const Joining = struct { fd: c_int, view: ?u8 = null }; + /// Hands every note the daemon sends on to `deliver`, on a thread of its /// own, until the link ends (`quit`). Called each loop: started once. pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { @@ -409,7 +412,8 @@ 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) deliver(ctx, .{ .replica = got.fds[0] }); + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); 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 } }; @@ -951,7 +955,9 @@ const Daemon = struct { } else return refuse(r, std.fmt.bufPrint(&why, "no session called '{s}'", .{h.session}) catch "no such session"); const t = &d.conns[target]; if (t.semantics != h.semantics) return refuse(r, std.fmt.bufPrint(&why, "session '{s}' runs another build of pardes (semantics {x:0>16}, this client's {x:0>16}): a replica needs the same build at both ends, as `pardes --version` shows", .{ h.session, t.semantics, h.semantics }) catch "another build"); - if (!send(t.fd, "replica", &.{r.fd})) { + var words: [16]u8 = undefined; + const note = if (h.view) |k| std.fmt.bufPrint(&words, "replica {d}", .{k}) catch unreachable else "replica"; + if (!send(t.fd, note, &.{r.fd})) { d.hangUp(@intCast(target)); return refuse(r, "that session does not answer"); } diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 6090dedb..337211b7 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"); +/// 19: a client joining again names the view it had in its hello, and a +/// bye says whether it joins again for it (views.away); /// 18: a replica's input is stamped with its guess of the session's /// clocks, the time its step reads (phase 6's instant interaction notes); /// 17: a pointer's hit carries the place it was made on (Layer.Place) and @@ -22,7 +24,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 = 18; +pub const version: u16 = 19; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -134,12 +136,15 @@ pub const Hello = struct { semantics: u64 = semantics, /// Which session (its 9P name, as `--attach=` names it). session: []const u8, + /// The view it had, joining again for it (a divergence's rejoin, an + /// Upgrade's): the daemon hands it on with the connection. 0xff: none. + view: ?u8 = null, }; /// A session's name on the wire, as the daemon keeps it. pub const max_name = 128; /// The longest hello there is, header and all. -pub const hello_max = header_len + 2 + 8 + 2 + max_name; +pub const hello_max = header_len + 2 + 8 + 2 + max_name + 1; /// Daemon or session -> client. pub const ServerTag = enum(u8) { @@ -268,7 +273,8 @@ pub fn helloVersion(payload: []const u8) Error!u16 { /// one call and no longer. pub const ClientMsg = union(enum) { hello: Hello, - bye, + /// The client's end: `again`, it joins again for its view at once. + bye: struct { again: bool = false }, /// A round trip's probe, answered `pong` with the same value. ping: u64, screen_ack: []const u8, @@ -498,8 +504,9 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { try w.writeInt(u64, h.semantics, .little); if (h.session.len > max_name) return error.Overlong; try putSlice16(&w, h.session); + try w.writeByte(h.view orelse 0xff); }, - .bye => {}, + .bye => |b| try w.writeByte(@intFromBool(b.again)), .ping => |v| try w.writeInt(u64, v, .little), .screen_ack, .want => |bytes| try w.writeAll(bytes), .check => {}, @@ -573,8 +580,12 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { if (n > max_name) return error.BadValue; break :blk try r.take(n); }, + .view = switch (try r.takeByte()) { + 0xff => null, + else => |k| k, + }, } }, - .bye => .bye, + .bye => .{ .bye = .{ .again = try getBool(&r) } }, .ping => .{ .ping = try r.takeInt(u64, .little) }, .screen_ack => return .{ .screen_ack = payload }, .check => .check, @@ -693,7 +704,8 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqual(semantics, got.hello.semantics); try testing.expectEqualStrings("work", got.hello.session); } - try testing.expectEqual(ClientMsg.bye, try roundClient(&buf, .bye)); + try testing.expect((try roundClient(&buf, .{ .bye = .{ .again = true } })).bye.again); + try testing.expectEqual(@as(?u8, 2), (try roundClient(&buf, .{ .hello = .{ .session = "work", .view = 2 } })).hello.view); try testing.expectEqual(@as(u64, 0xfeed_beef_0000_0001), (try roundClient(&buf, .{ .ping = 0xfeed_beef_0000_0001 })).ping); { // The other way: framed the same, its tag the ServerTag's number. @@ -920,8 +932,9 @@ test "detached wire: values a field cannot mean are refused" { std.mem.writeInt(u16, hello[10..12], max_name + 1, .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.hello), &hello)); std.mem.writeInt(u16, hello[10..12], max_name, .little); - try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), hello[0 .. hello.len - 1])).hello.session.len); - try testing.expect(header_len + hello.len - 1 == hello_max); + hello[hello.len - 1] = 0xff; + try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), &hello)).hello.session.len); + try testing.expect(header_len + hello.len == hello_max); } // A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps. { diff --git a/src/gui/gui.zig b/src/gui/gui.zig index dd21680f..d3acf454 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2752,7 +2752,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ defer have_store.deinit(); const have: ?*replica.Have = if (far) &have_store else null; var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - var joined = replica.dial(gpa, to.path, to.session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var joined = replica.dial(gpa, to.path, to.session, &why, have, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); const allocs = pardes.memory.init(gpa); @@ -2878,7 +2878,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(joined.fd); + if (!link.gone) replica.bye(joined.fd, r.diverged != null); // The reader ends with the link: what it took meanwhile is let go. _ = libc.shutdown(joined.fd, 2); reader.join(); @@ -2901,7 +2901,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, r.diverged == null) orelse Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); + joined = replica.Link.redial(gpa, to.path, to.session, &why, have, r.diverged == null, joined.view) 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)}); @@ -4553,13 +4553,14 @@ const Shell = struct { } /// An upgraded session's views, a window each again: the Gui's is the - /// live view's, the others open as `--attach` opens one. + /// live view's, the others open as `--attach` opens one; a replica + /// client's, kept for its rejoin (views.away), none. fn resumeWindows(s: *Shell) void { const live = s.core.view; s.gview = live; for (0..pardes.views.max) |i| { const k: u8 = @intCast(i); - if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + if (k != live and pardes.views.isOpen(s.core, k) and !pardes.views.isAway(s.core, k)) s.openAnotherWindow(k); s.windowed |= @as(u8, 1) << @intCast(live); } s.core.toView(live); @@ -5362,7 +5363,7 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); }, // Joined at the next step boundary (Peers, any thread). - .replica => |fd| s.peers.handed(fd), + .replica => |j| s.peers.handed(j.fd, j.view), } } diff --git a/src/pardes.zig b/src/pardes.zig index 22f446a5..edab4a58 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4732,6 +4732,8 @@ pub const Pardes = struct { /// there are, one a window or terminal; each not live is in `parked`. view: u8 = 0, views_open: u8 = 1, + /// The open views kept for their clients' rejoin (views.away). + views_away: u8 = 0, 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. @@ -5706,6 +5708,14 @@ pub const Pardes = struct { pub fn openView(p: *Pardes) ?u8 { const taped = tape.enter(p, .open_view); defer tape.leave(p, taped); + // Every view taken: one kept for a client that has not come back + // for it (views.away) goes first. ponytail: no timer lets one go; + // until its place is wanted it costs a parked slot, and a terminal + // it sized last keeps that size until another view types into it. + if (@popCount(p.views_open) == views.max and p.views_away != 0) { + views.close(p, @ctz(p.views_away)); + p.sync(); + } // What was queued since the last pump (a leaving client's view // closed, a 9P request's step) done first, as every client of the // tape does it here: a join dumps the session next, and a core @@ -5740,6 +5750,27 @@ pub const Pardes = struct { p.settle(); } + /// The client of view `k` gone, to join again for it (an Upgrade's + /// rejoin, a divergence's): its view kept as it is (views.away). + pub fn awayView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .away_view = k }); + defer tape.leave(p, taped); + views.away(p, k); + } + + /// A client back for view `k`, kept for it (`awayView`): its own again, + /// as it left it; false when it was let go meanwhile. Settled as + /// `openView` is, for the join's dump next. + pub fn rejoinView(p: *Pardes, k: u8) bool { + const taped = tape.enter(p, .{ .rejoin_view = k }); + defer tape.leave(p, taped); + if (!views.isAway(p, k)) return false; + p.views_away &= ~(@as(u8, 1) << @intCast(k)); + p.settle(); + p.needs_frame = true; + return true; + } + /// Into view `k`, before the input its client sent. pub fn toView(p: *Pardes, k: u8) void { if (k == p.view) return; diff --git a/src/replica.zig b/src/replica.zig index da0c3e64..4f84712f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1414,6 +1414,9 @@ pub const Peer = struct { fd: c_int, id: u16, view: u8, + /// Its bye said it joins again for its view (a divergence's rejoin): + /// kept for it (views.away), not closed. Its reader's, read after its end. + again: bool = false, /// Its number in the session's `Screens`. sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). @@ -1626,7 +1629,11 @@ pub const Peer = struct { 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, + .hello => return false, + .bye => { + peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + return false; + }, .ping => { var out: [16]u8 = undefined; const v = (wire.decodeClient(tag, payload) catch return false).ping; @@ -1747,12 +1754,14 @@ pub const Peers = struct { msgs: std.ArrayListUnmanaged([]u8) = .empty, /// Clients waiting to join while the session's texts are hashed /// (`joins`), the texts and the thread hashing them. - held: std.ArrayListUnmanaged(c_int) = .empty, + held: std.ArrayListUnmanaged(Joining) = .empty, texts: std.ArrayListUnmanaged([]const u8) = .empty, hasher: ?std.Thread = null, rounds: u8 = 0, - pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; + pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; + /// A connection to join, and the view it joins again for, if it does. + pub const Joining = struct { fd: c_int, view: ?u8 = null }; pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { @@ -1766,7 +1775,7 @@ pub const Peers = struct { ps.inbox.append(ps.gpa, note) catch { ps.lock.unlock(ps.io); switch (note) { - .join => |fd| _ = libc.close(fd), + .join => |j| _ = libc.close(j.fd), .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check, .joined, .hashed => {}, } @@ -1778,9 +1787,10 @@ pub const Peers = struct { } /// 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 }); + /// there), and the view its hello says it had: joined at the next step + /// boundary. Any thread. + pub fn handed(ps: *Peers, fd: c_int, view: ?u8) void { + ps.post(.{ .join = .{ .fd = fd, .view = view } }); } /// What was recorded since the last unit, to every client as one, its @@ -1824,9 +1834,9 @@ pub const Peers = struct { ps.mail.store(false, .release); ps.lock.unlock(ps.io); for (ps.taken.items) |note| switch (note) { - .join => |fd| { - ps.held.append(ps.gpa, fd) catch { - refuse(fd, "out of memory"); + .join => |j| { + ps.held.append(ps.gpa, j) catch { + refuse(j.fd, "out of memory"); continue; }; ps.joins(core); @@ -1922,7 +1932,7 @@ pub const Peers = struct { } } ps.rounds = 0; - for (ps.held.items) |fd| ps.join(core, fd); + for (ps.held.items) |j| ps.join(core, j); ps.held.clearRetainingCapacity(); } @@ -1939,7 +1949,8 @@ pub const Peers = struct { ps.texts.clearRetainingCapacity(); } - fn join(ps: *Peers, core: *Pardes, fd: c_int) void { + fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + const fd = joining.fd; const t0 = nowNs(); defer { ps.join_ns = @max(ps.join_ns, nowNs() - t0); @@ -1964,7 +1975,13 @@ pub const Peers = struct { } // Someone looks at it now: its messages go by keys, not the clock. core.unattended = false; - const k = core.openView() orelse { + // Its own view again, kept for it since it left (views.away), or one + // new. ponytail: a divergence's redial that came before its old + // link's end (posted by that link's reader, microseconds after the + // bye; the redial is a dial and the daemon's hand-over later) finds + // its view not kept yet, and takes a new one. + const back = if (joining.view) |v| core.rejoinView(v) else false; + const k = (if (back) joining.view else core.openView()) orelse { core.unattended = ps.count() == 0 and ps.alone_unattended; if (ps.count() == 0) ps.stop(core); return refuse(fd, "that session has as many views as it takes"); @@ -2035,7 +2052,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)); - ps.closeViewOf(core, peer); + ps.closeViewOf(core, peer, peer.again); if (ps.count() == 0) core.unattended = ps.alone_unattended; ps.seq += 1; ps.unit(0, ps.seq); @@ -2043,11 +2060,12 @@ 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); + /// A gone client's view closed (its slot let go already), or kept for + /// it (`again`: it joins again for it, views.away): the last one of a + /// session no one else watches leaves the session its layout, for a + /// client that comes after to find. + fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer, again: bool) void { + if (again) core.awayView(peer.view) else if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view); } /// Its threads ended and joined, its link closed. @@ -2114,7 +2132,8 @@ pub const Peers = struct { const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } - ps.dropAll(core); + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); } /// A join's dump and pins let go: written, or its client gone. @@ -2149,21 +2168,26 @@ pub const Peers = struct { /// Every client let go (the session's end, a Restore): each sees its /// link end. pub fn dropAll(ps: *Peers, core: *Pardes) void { + ps.drop(core, false); + } + + /// ...each view kept for its client if `again` (an Upgrade's). + fn drop(ps: *Peers, core: *Pardes, again: bool) void { ps.unhashedDone(); - for (ps.held.items) |fd| _ = libc.close(fd); + for (ps.held.items) |j| _ = libc.close(j.fd); ps.held.clearRetainingCapacity(); ps.rounds = 0; for (&ps.list) |*slot| if (slot.*) |peer| { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - ps.closeViewOf(core, peer); + ps.closeViewOf(core, peer, again); ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; ps.stop(core); ps.lock.lockUncancelable(ps.io); for (ps.inbox.items) |note| switch (note) { - .join => |fd| _ = libc.close(fd), + .join => |j| _ = libc.close(j.fd), .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check, .joined, .hashed => {}, }; @@ -2238,8 +2262,9 @@ pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; /// Connected to the replica socket at `path`, its hello said for /// `session`, welcomed, and the dump read whole: what a client joins with. /// A far client (`have`) is sent its dump's blobs by hash first, and asks -/// for those it holds nowhere. Errors are said in `why`. -pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have) ?Joined { +/// for those it holds nowhere. Errors are said in `why`. `view`: the one +/// it had, joining again for it. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); if (fd < 0) return said(why, "no socket", .{}); var addr: libc.sockaddr.un = .{ .path = @splat(0) }; @@ -2252,14 +2277,14 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha _ = libc.close(fd); return said(why, "nothing answers at {s}", .{path}); } - return joinOn(gpa, fd, session, why, have); + return joinOn(gpa, fd, session, why, have, view); } /// `dial`'s, on a connection made: the hello, then the join. The /// connection is closed if the join fails. -fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have) ?Joined { +fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { var out: [wire.hello_max + 16]u8 = undefined; - const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch { + const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); }; @@ -2690,10 +2715,10 @@ pub fn askCheck(fd: c_int) bool { return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); } -/// The client's last word. -pub fn bye(fd: c_int) void { +/// The client's last word: `again`, it joins again for its view at once. +pub fn bye(fd: c_int, again: bool) void { var out: [16]u8 = undefined; - if (wire.encodeClient(&out, .bye)) |b| _ = writeAll(fd, b) else |_| {} + if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} } /// What follows the dump on the client's connection, each message handed @@ -2985,13 +3010,14 @@ pub const Link = struct { /// `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 { + /// `view`: the one it had, which the session kept for it (views.away). + pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined { var tries: u32 = 0; while (true) : (tries += 1) { why.* = @splat(0); - if (dial(gpa, path, session, why, have)) |j| { + if (dial(gpa, path, session, why, have, view)) |j| { if (j.semantics == wire.semantics) return j; - bye(j.fd); + bye(j.fd, false); _ = libc.close(j.fd); var jj = j; jj.rest.deinit(gpa); @@ -3013,7 +3039,7 @@ pub const Link = struct { 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(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); } @@ -3777,7 +3803,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; const Serve = struct { fn run(p: *Peer) void { - var hello: [wire.header_len + 13]u8 = undefined; + var hello: [wire.header_len + 14]u8 = undefined; var got: usize = 0; while (got < hello.len) { const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); @@ -3791,7 +3817,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); defer ps.letGo(peer); var why: [300]u8 = @splat(0); - return joinOn(std.testing.allocator, fds[1], "t", &why, have) orelse { + return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse { std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); return error.JoinFailed; }; @@ -4059,6 +4085,44 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize try agrees(a, joiner); } +test "a client that diverged joins again for its view: kept for it meanwhile, its pane in it again; the other client agrees" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, false); + defer old.deinit(); + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .command = "New" }))); + const made = a.core.active; + // Its bye says it joins again: its view is kept, as every replica keeps it. + var was = Stepping.begin(&a.t, null); + a.core.awayView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + try agrees(a, old); + // Back for it: its own, its pane in it, in the dump it loads. + was = Stepping.begin(&a.t, null); + try std.testing.expect(a.core.rejoinView(opened.view)); + was.end(); + old.confirm(try a.unit(0, 101)); + const back = try joinedNow(a, 3, opened.view); + defer back.deinit(); + try std.testing.expectEqual(digestOf(a), back.digest()); + a.view_of[3] = opened.view; + const u = try gpa.dupe(u8, try a.take(try back.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + defer gpa.free(u); + old.confirm(u); + back.confirm(u); + try std.testing.expectEqual(made, a.core.active); + try std.testing.expect(pardes.layout.findPane(a.core, made) != null); + try agrees(a, old); + try agrees(a, back); +} + /// An input of a replica's in the session's order, as `Peers.input` takes /// it: the terminals flushed to every peer, the step, the rows it read that /// each peer holds as holes (`fill`), then its unit to every replica. diff --git a/src/tape.zig b/src/tape.zig index 4759d598..74576eed 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -67,6 +67,8 @@ pub const Input = union(enum) { open_view, close_view: u8, leave_view: u8, + away_view: u8, + rejoin_view: u8, to_view: u8, /// What a window says of its font and its opacity (gui.zig). take_font_request, @@ -630,6 +632,8 @@ pub const Tape = struct { .open_view => _ = p.openView(), .close_view => |k| p.closeView(k), .leave_view => |k| p.leaveView(k), + .away_view => |k| p.awayView(k), + .rejoin_view => |k| _ = p.rejoinView(k), .to_view => |k| p.toView(k), .take_font_request => _ = p.takeFontRequest(), .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 430c020d..4c054021 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -153,7 +153,7 @@ pub const Command = struct { files_changed, fs_ready, /// A replica's connection, handed over by the daemon (Peers). - replica: posix.fd_t, + replica: daemon.Joining, /// A replica's reader posted: the step boundary takes it in. replica_mail, /// A replica client's: what the session streamed (replica.Link). @@ -1333,7 +1333,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se defer have_store.deinit(); const have: ?*replica.Have = if (far) &have_store else null; var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - var joined = replica.dial(gpa, path, session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var joined = replica.dial(gpa, path, session, &why, have, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); const allocs = pardes.memory.init(gpa); @@ -1445,7 +1445,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(joined.fd); + if (!link.gone) replica.bye(joined.fd, r.diverged != null); // The link's reader ends with the link, posting as it goes: what it // and the terminal posted is let go until it has. Its end is read // before the drain, not after: its last post (`link_gone`) comes @@ -1482,7 +1482,7 @@ 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, r.diverged == null) orelse { + joined = replica.Link.redial(gpa, path, session, &why, have, r.diverged == null, joined.view) orelse { screen.leave(); Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); }; @@ -1916,8 +1916,8 @@ 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); + .replica => |j| loop.postEvent(.{ .replica = j }) catch { + _ = libc.close(j.fd); }, } } @@ -2338,7 +2338,7 @@ const Shell = struct { /// An upgraded session's terminals, each its view's again (`primary`, /// run's, entered already); in a fresh session each but the primary a /// view of its own, as `--attach` makes one. A view left with none - /// closes. + /// closes, but a replica client's, kept for its rejoin (views.away). fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { if (comptime builtin.is_test) unreachable; const core = s.core; @@ -2348,8 +2348,11 @@ const Shell = struct { } s.terms = @splat(null); if (rec.nttys == 0) { - pardes.views.only(core); + // The live view alone (never one kept for a rejoin) is the + // session's own. + for (0..pardes.views.max) |k| if (k != core.view and pardes.views.isOpen(core, @intCast(k)) and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); s.terms[core.view] = primary; + primary.feed.view = core.view; return; } for (rec.ttys[0..rec.nttys]) |t| { @@ -2386,7 +2389,7 @@ const Shell = struct { sc.feed.view = t.view; s.terms[t.view] = sc; } - for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null) core.closeView(@intCast(k)); + for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); if (s.terms[core.view] == null) s.terms[core.view] = primary; } @@ -2623,8 +2626,8 @@ 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); + .replica => |j| { + s.peers.handed(j.fd, j.view); s.input_seen = true; }, .replica_mail => s.input_seen = true, diff --git a/src/views.zig b/src/views.zig index ac274621..58d7a1f7 100644 --- a/src/views.zig +++ b/src/views.zig @@ -173,6 +173,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { }; // Nothing fails from here. p.views_open &= take | bit(p.view); + p.views_away &= p.views_open; // The live view's slot owning nothing, as `installed` makes it. for (p.panes) |s| if (s) |pane| { if (take != 0) pane.pv[p.view] = fresh(pane); @@ -214,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// The live view alone, as view 0: every other closed. A session loaded /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { + p.views_away = 0; for (0..max) |k| if (k != p.view) close(p, @intCast(k)); emptied(p, p.view); const k = p.view; @@ -255,8 +257,20 @@ pub fn isOpen(p: *const Pardes, k: u8) bool { return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; } +/// Kept for its client's rejoin (`away`). +pub fn isAway(p: *const Pardes, k: u8) bool { + return k < max and p.views_away & (@as(u8, 1) << @intCast(k)) != 0; +} + +/// The views someone looks at: open, none kept for a rejoin. pub fn count(p: *const Pardes) usize { - return @popCount(p.views_open); + return @popCount(p.views_open & ~p.views_away); +} + +/// An open view but `k` someone looks at (`count`), if there is one. +fn looked(p: *const Pardes, k: u8) ?u8 { + for (0..max) |i| if (i != k and isOpen(p, @intCast(i)) and !isAway(p, @intCast(i))) return @intCast(i); + return null; } /// More than one view: the one test the single client's path pays. @@ -448,9 +462,8 @@ const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; 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; + // Never into one kept for a rejoin (`away`): that one is its client's. + const other = looked(p, k).?; 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. @@ -469,11 +482,12 @@ pub fn closeKeeping(p: *Pardes, k: u8) void { } /// 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. +/// The last view someone looks at stays: a session without a client keeps +/// its own. One kept for a rejoin (`away`) goes whenever it is closed. pub fn close(p: *Pardes, k: u8) void { - if (!isOpen(p, k) or count(p) == 1) return; + if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; if (k == p.view) { - const other: u8 = for (0..max) |i| { + const other: u8 = looked(p, k) orelse for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; park(p); @@ -493,6 +507,19 @@ pub fn close(p: *Pardes, k: u8) void { if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); }; p.views_open &= ~(@as(u8, 1) << @intCast(k)); + p.views_away &= ~(@as(u8, 1) << @intCast(k)); +} + +/// View `k`'s client gone a moment, to join again for it (an Upgrade's +/// rejoin, a divergence's): the view kept as it is, layout, focus and the +/// panes opened in it, until its client is back (Pardes.rejoinView) or a +/// join wants its place (Pardes.openView). Dumped with the rest, so an +/// Upgrade's new build has it too. The live view is never one: the session +/// goes into another someone looks at, or into a copy of this one of its own. +pub fn away(p: *Pardes, k: u8) void { + if (!isOpen(p, k)) return; + if (k == p.view) if (looked(p, k) orelse open(p)) |other| to(p, other); + p.views_away |= bit(k); } /// The session's end: what each view not live holds. @@ -541,7 +568,7 @@ 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) or p.undrawn & (@as(u8, 1) << @intCast(k)) != 0) continue; + if (k == live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(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. @@ -836,6 +863,46 @@ test "the last client's view going leaves the session its layout, which a rejoin try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } +test "a client's view kept for its rejoin: through an Upgrade's dump, each client back in its own; a join with no room lets one go" { + const gpa = testing.allocator; + const opts: pardes.Options = .{ .tty_only = true, .cols = 100, .rows = 30 }; + const p = try Pardes.init(gpa, opts); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + // Two clients, each a pane of its own in its own view. + const ks: [2]u8 = .{ p.openView().?, p.openView().? }; + var made: [2]usize = undefined; + for (ks, &made) |k, *m| { + p.toView(k); + p.update(.{ .command = "New" }); + m.* = p.active; + } + // Both let go to upgrade, the live view one of theirs: theirs are kept, + // and the session's own is live. + for (ks) |k| p.awayView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expect(!isAway(p, p.view)); + // The new build loads the dump; each client is back in its own view. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const q = try pardes.dump.load(gpa, opts, bytes, null); + defer q.deinit(); + for (ks, made, 0..) |k, m, i| { + try testing.expect(isAway(q, k)); + try testing.expect(q.rejoinView(k)); + q.toView(k); + try testing.expectEqual(m, q.active); + try testing.expect(layout.findPane(q, m) != null); + try testing.expect(layout.findPane(q, made[1 - i]) == null); + } + try testing.expect(!q.rejoinView(ks[0])); + // A client that never comes back: its view goes when a join has no room. + p.toView(0); + try testing.expectEqual(@as(?u8, 3), p.openView()); + try testing.expectEqual(ks[0], p.openView()); + try testing.expect(!isAway(p, ks[0]) and isAway(p, ks[1])); +} + 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 8633238f..bf575fd7 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -208,7 +208,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 = 18 + WIRE = 19 def __init__(self, path, timeout=10): self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -235,7 +235,7 @@ class Replica: """('welcome', pid, name) or ('refused', why); a welcome's dump is read, whole.""" name = session.encode() - self.send(0x01, struct.pack(' after +def made_live(term, check): + """A key in a replica client's terminal (`gg`), stepped by its session + (a check after it agreed): its view is the live one, the one /layout + reads and /exec runs in, until the next input. A client asks one check + at a time, and checks come on (each answer wakes its session): the + first after the key may have been asked before it, the second was not.""" + seen = len(checks(check)) + os.write(term.master, b'gg') + wait_for('the client\'s key stepped', lambda: agreed(check, seen + 1), timeout=20) + + +def view_panes(client): + """The panes the live view lays out (/layout's pane serials).""" + return {int(x) for line in client.read('/layout').decode().splitlines() + if line.split()[:1] != ['active'] for x in line.split()[6:]} + + 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 @@ -388,23 +405,31 @@ def clients(binary): wait_for('the attached terminal\'s frame', lambda: b'line 3 of the notes' in tt.text(), timeout=20) stub.send_signal(signal.SIGHUP) stub.wait(timeout=10) - # One put off the session's state (PARDES_TEST_DIVERGE): the next - # unit's digest finds it, it says so in a line and joins again, and - # its keys after go in and it agrees. + # One put off the session's state (PARDES_TEST_DIVERGE, at its + # 100th unit): the next unit's digest finds it, it says so in a line + # and joins again for its view, a pane of its own in it, its keys + # after go into that pane and it agrees. td = Term(24, 90) terms.append(td) - d = td.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'd.check'), PARDES_TEST_DIVERGE='1')) + d = td.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'd.check'), PARDES_TEST_DIVERGE='100')) started.append(d.pid) wait_for('client D\'s first frame', lambda: b'line 3 of the notes' in td.text(), timeout=20) - os.write(td.master, b'jj') - time.sleep(.5) - # Another's unit, replayed on D's core: its digest finds it. - os.write(ta.master, b'jj') - wait_for('client D diverged and joined again', lambda: b'joined again' in td.text(), timeout=20) + made_live(td, root / 'd.check') + shown = view_panes(nine) + nine.write('/exec', b'New\n') + mine = wait_for('a pane in client D\'s view', lambda: view_panes(nine) - shown).pop() + # Others' units, replayed on D's core: one after its 100th finds it. + deadline = time.monotonic() + 60 + while b'joined again' not in td.text(): + assert time.monotonic() < deadline, 'client D diverged and joined again' + os.write(ta.master, b'jk') + time.sleep(.2) assert d.poll() is None + made_live(td, root / 'd.check') + assert mine in view_panes(nine), ('client D in its own view again', mine, nine.read('/layout')) seen = len(checks(root / 'd.check')) os.write(td.master, b'Oafter-rejoin-D\x1b') - wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{pane}/body')) + wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{mine}/body')) wait_for('client D out of insert mode', lambda: normal_again(td)) wait_for('client D agrees after its rejoin', lambda: agreed(root / 'd.check', seen)) os.write(td.master, b' sD') @@ -446,7 +471,7 @@ def clients(binary): 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 put off the session\'s state joined again and agreed {len(checks(root / "d.check"))} times; ' + f'one put off the session\'s state joined again in its own view and agreed {len(checks(root / "d.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') exit_session(nine, pane) @@ -980,7 +1005,7 @@ def far_joins(sock, session, binary, n): def join(): r = Replica(sock, timeout=30) name = session.encode() - r.send(0x01, struct.pack(' Date: Fri, 30 Oct 2026 23:48:44 -0300 Subject: panes: a pane's view (Pane.view, and a parked view's share of it) is digested and dumped, no longer derived: a key's move reads it before its step syncs (halfPageMove, ensureCursorVisible, through File.geometry), and a core loaded from a dump had the default, Wrap on and no row heights, so a window that joined after `SPC t w` scrolled further at the terminal view's next Ctrl-D (the box soak's `pane 23.file.scroll`, seed 7: win207-3 at its join); replica.zig's test, main's fails at `pane 0.file.scroll` Co-Authored-By: Claude Opus 5.5 --- src/panes.zig | 5 ++++- src/replica.zig | 29 +++++++++++++++++++++++++++++ src/views.zig | 5 +++-- 3 files changed, 36 insertions(+), 3 deletions(-) (limited to 'src/replica.zig') diff --git a/src/panes.zig b/src/panes.zig index 20523de6..095e215f 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -29,7 +29,7 @@ pub const Pane = struct { /// the pane's opens and readers; the other views' share of it (views.zig). pub const replica = .{ .local = .{ "pv", "pv_known" }, - .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices", "pan" }, + .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "notices", "pan" }, .authority = .{"fs"}, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's and a PDF's memory is their own allocators' (a dump @@ -268,6 +268,9 @@ pub const Pane = struct { geometry_key: u64 = 0, geometry_len: usize = 0, /// The session's part of the geometry, as of the last step (`sync`). + /// Digested and dumped, not made again: a key's move reads it before its + /// step's `sync` (Text.halfPageMove's scroll), and a core loaded from a + /// dump has had none (Replica.initDump). view: SessionView = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), diff --git a/src/replica.zig b/src/replica.zig index 4f84712f..ca8612a9 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -4123,6 +4123,35 @@ test "a client that diverged joins again for its view: kept for it meanwhile, it try agrees(a, back); } +test "a client joined from a dump moves through another view's pane as the session does, its lines not wrapped" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The first client's Wrap off (SPC t w), and its cursor down into the + // file's long lines, near the bottom of its view. + var buf: [256]u8 = undefined; + var seq: u32 = 1; + const before = [_]pardes.Event{.{ .command = "Wrap" }} ++ + @as([10]pardes.Event, @splat(.{ .key = .{ .cp = 'd', .ctrl = true } })) ++ + @as([15]pardes.Event, @splat(.{ .key = .{ .cp = 'j', .text = "j" } })); + for (before) |ev| { + _ = try a.take(try inputOf(&buf, 1, seq, ev)); + seq += 1; + } + // Another client joins, its view not live; the first's keys go on. + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + for (0..3) |_| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, seq, .{ .key = .{ .cp = 'd', .ctrl = true } }))); + defer gpa.free(u); + seq += 1; + joiner.confirm(u); + try agrees(a, joiner); + } +} + /// An input of a replica's in the session's order, as `Peers.input` takes /// it: the terminals flushed to every peer, the step, the rows it read that /// each peer holds as holes (`fill`), then its unit to every replica. diff --git a/src/views.zig b/src/views.zig index 58d7a1f7..b1786f83 100644 --- a/src/views.zig +++ b/src/views.zig @@ -80,8 +80,9 @@ pub const PaneView = struct { cols: u16 = 1, rows: u16 = 1, - /// In a dump (`Dumped`): what a pane's step works out again. - pub const replica = .{ .derived = .{ "view", "notices", "pan" } }; + /// In a dump (`Dumped`): what a pane's step works out again. Its `view` + /// is dumped: the step after `unpark` reads it before it syncs. + pub const replica = .{ .derived = .{ "notices", "pan" } }; }; /// A view not live: its share of the session, and the serial of each pane -- cgit v1.3 From 94b282971453849eb567124c12bd801a82908392 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 31 Oct 2026 00:46:29 -0300 Subject: locations: a row's colours are in no digest (a `digest` of Row's own; a dump keeps them): they are painted from what the process has painted, the owner's highlights a frame made and the locations cache, both derived, so a window joined from a dump made a `/` query's rows plain where its session had coloured them (the box soak's `pane 21.file.location_rows` at win207-4's `/r`, unit 4/447); replica.zig's test, main fails it at `pane 1.file.location_rows` Co-Authored-By: Claude Opus 5.5 --- src/locations.zig | 13 +++++++++++++ src/replica.zig | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'src/replica.zig') diff --git a/src/locations.zig b/src/locations.zig index d0904607..b14b3a2c 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -24,6 +24,19 @@ pub const Row = struct { location_end: usize, code_start: usize, colors: []u8 = &.{}, + + /// The replay digest's (src/walk.zig): all of it but its colours. They + /// are painted from what this process has painted (the owner's + /// highlights a frame made, the locations cache: both derived), so a + /// client joined from a dump, or one that drew another view, colours a + /// row its session left plain. They are how a row looks, not where it + /// points; a dump keeps them. + pub fn digest(r: *const Row, h: *pardes.walk.Hasher) void { + inline for (@typeInfo(Row).@"struct".field_names) |name| { + if (comptime std.mem.eql(u8, name, "colors")) continue; + pardes.walk.hash(@FieldType(Row, name), &@field(r, name), h); + } + } }; /// Raw (no safe build's overwrite): a replica's rollback copy pins a row diff --git a/src/replica.zig b/src/replica.zig index ca8612a9..8cacb075 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -4152,6 +4152,32 @@ test "a client joined from a dump moves through another view's pane as the sessi } } +test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The session draws: its owner's colours painted for the view (File.refreshHighlights). + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try a.core.render(arena.allocator()); + } + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + var buf: [256]u8 = undefined; + for ("/co", 1..) |c, seq| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, @intCast(seq), .{ .key = .{ .cp = c, .text = &.{c} } }))); + defer gpa.free(u); + joiner.confirm(u); + try agrees(a, joiner); + } +} + /// An input of a replica's in the session's order, as `Peers.input` takes /// it: the terminals flushed to every peer, the step, the rows it read that /// each peer holds as holes (`fill`), then its unit to every replica. -- cgit v1.3