From 5c23bd8b1bbdbbacd99b11e75d38f7ce1801cd8a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 10:48:56 -0300 Subject: the frame wire goes: detached/server.zig, client.zig and the cell frames of wire.zig Nothing draws a session's screen from frames any more: a session is always an in-process authority with its terminal or window, and remote clients come back as replicas (docs/next-daemon.md, phase 5). server.zig, the third host and its frontend socket, the smallest-common-grid rule and its 32 frontends; client.zig, the frontend half; wire.zig's frames, cell and layer codecs, server messages and the post chain on the wire. wire.zig keeps the framing, the hello and its version (now 13) and the input events, with their tests, for phase 5. Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 1296 +------------------------------------------------ 1 file changed, 17 insertions(+), 1279 deletions(-) (limited to 'src/detached/wire.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 0e141686..20d75274 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -1,32 +1,13 @@ -//! Detached input and frames use fixed-width little-endian fields and explicit tags. -//! Native struct layout never reaches the wire. +//! The framed stream a remote client will speak (docs/next-daemon.md, +//! phase 5): fixed-width little-endian fields and explicit tags, the hello +//! and its version, and the input Events. Native struct layout never reaches +//! the wire. The cell frames that went the other way, and the frontends that +//! drew them, went with phase 3. const std = @import("std"); const pardes = @import("../pardes.zig"); -const limits = @import("../memory.zig").limits; -const ShaderBuild = @import("../ShaderBuild.zig"); -const Scene = pardes.config.Runtime.Scene; -const ShaderAnimation = pardes.config.Runtime.ShaderAnimation; -/// 7: tag layers gained the `.notice` kind, so a frame can carry a tag value -/// an older frontend would reject mid-session. The greeting is where that has -/// to be refused, which is what this number is for. -/// 8: one layer type: a tag band carries all its rows (not a layer per -/// line), and a frame carries the core's placed regions after its layers. -pub const version: u16 = 12; - -/// The Chrome palette is carried field by field as its declaration lists -/// them, so any change to it changes the wire: bump `version`, then this. -const chrome_layout: u64 = 0xbe608bed776ff502; -comptime { - @setEvalBranchQuota(10_000); - var hash = std.hash.Fnv1a_64.init(); - const info = @typeInfo(pardes.Chrome).@"struct"; - for (info.field_names, info.field_types) |name, T| { - hash.update(name); - hash.update(@typeName(T)); - } - if (hash.final() != chrome_layout) @compileError(std.fmt.comptimePrint("pardes.Chrome changed (layout 0x{x}): bump wire.version, then chrome_layout", .{hash.final()})); -} +/// 13: the frames, and every message a session sent, are gone (phase 3). +pub const version: u16 = 13; /// EndOfStream: the message ends early; WriteFailed: no room for it. /// ReadFailed is in only because std.Io.Reader says so: a fixed one never @@ -38,16 +19,6 @@ pub const max_rows: u16 = 128; pub const max_payload: u32 = 16 << 20; pub const header_len = 5; // tag:u8, payload length:u32le -const cell_max = 1 + 1 + 7 + 4 + 4 + 1 + 1 + 1; -const run_header = 4 + 2; // start:u32, count:u16 -const run_max = std.math.maxInt(u16); -const frame_head = 1 + 2 + 2 + 6 + 4 + 4; - -// Worst case: every cell changed, each in its own run. -pub fn frameBound(cols: u16, rows: u16) usize { - return header_len + frame_head + @as(usize, cols) * @as(usize, rows) * (run_header + cell_max); -} - // Stable wire numbers; gaps are retired tags. Unknown tags are rejected by the decoder. pub const ClientTag = enum(u8) { hello = 0x01, @@ -65,43 +36,6 @@ pub const ClientTag = enum(u8) { focus = 0x1e, }; -// Session control precedes display-local effects. -pub const ServerTag = enum(u8) { - welcome = 0x01, - refuse = 0x02, - frame = 0x03, - quit = 0x04, - /// One frontend is done, and the session is NOT over. The `Detach` word, - /// routed back to the frontend whose keystroke ran it (server.zig ORIGIN, - /// ELSE PRIMARY, the same rule `read_clipboard` takes and for the same - /// reason). Every other frontend, the core and the pane shells are - /// untouched, so leaving is success rather than a failure to report. - detach = 0x05, - - set_clipboard = 0x10, - read_clipboard = 0x11, - open_link = 0x12, - /// The post chain this frontend draws its frames through: sent on - /// attach and whenever it or a file's SPIR-V changes. - post = 0x13, -}; - -/// Why the server hung up on a connect. Sent as a `refuse` and followed by a -/// close, so a frontend can say something specific instead of "connection -/// closed". -pub const Refusal = enum(u8) { - /// `Hello.version` is not `version`. The frontend and the core are two - /// builds of pardes. - version = 0x01, - /// Every client slot is taken (see server.zig `max_clients`). - full = 0x02, - /// The core has already quit; this session is ending. - quitting = 0x03, -}; - -const ColorTag = enum(u8) { default = 0x00, index = 0x01, rgb = 0x02 }; -const UlTag = enum(u8) { off = 0x00, single = 0x01, double = 0x02, curly = 0x03, dotted = 0x04, dashed = 0x05 }; -const FontTag = enum(u8) { body = 0x00, tagline = 0x01 }; const ButtonTag = enum(u8) { left = 0x00, middle = 0x01, @@ -116,24 +50,6 @@ const ButtonTag = enum(u8) { }; const KindTag = enum(u8) { press = 0x00, release = 0x01, motion = 0x02, drag = 0x03 }; -/// A full frame resets the receiver's grid to unpainted cells and then applies -/// its runs; a diff applies its runs on top of what is already there. One bit -/// of semantics and one code path, and it is what makes a full frame of a -/// mostly-empty grid cheap. -pub const FrameKind = enum(u8) { full = 0x01, diff = 0x02 }; -const FrameHeader = enum(u8) { full = 0x01, diff = 0x02, full_link = 0x03, diff_link = 0x04 }; - -/// Bit per `CellStyle` bool, packed into one byte. Bit 7 is unassigned and a -/// set bit 7 is a decode error: it is a byte this protocol cannot mean. -const attr_bold: u8 = 1 << 0; -const attr_dim: u8 = 1 << 1; -const attr_italic: u8 = 1 << 2; -const attr_blink: u8 = 1 << 3; -const attr_reverse: u8 = 1 << 4; -const attr_invisible: u8 = 1 << 5; -const attr_strikethrough: u8 = 1 << 6; -const attr_reserved: u8 = 1 << 7; - // --------------------------------------------------------------------------- // messages // --------------------------------------------------------------------------- @@ -143,8 +59,8 @@ const attr_reserved: u8 = 1 << 7; /// when the rest of the layout is the part that changed. pub const Hello = struct { version: u16 = version, - /// This frontend's grid. Never zero — see `Cursor` for why zero is refused - /// rather than clamped. + /// This client's grid. Never zero: a client not sized yet must not + /// collapse a shared session (getCols). cols: u16, rows: u16, }; @@ -166,182 +82,6 @@ pub fn helloVersion(payload: []const u8) Error!u16 { return r.takeInt(u16, .little); } -pub const Welcome = struct { - version: u16 = version, - /// Which client slot this connection got. Carried because it is what the - /// server's own diagnostics name, so both sides say the same number. - slot: u8, - /// The session's grid as of this attach — the smallest common one, which - /// may be smaller than the `Hello` asked for. See server.zig `geometry`. - cols: u16, - rows: u16, -}; - -pub const Cursor = struct { x: u16, y: u16, bar: bool }; - -/// One frame, head decoded and runs left encoded. The runs are NOT expanded -/// into a slice of cells here: a frame of the largest grid is 1.6 MiB, this -/// union is passed by value, and the receiver already owns the grid the runs -/// belong in. `apply` is the bounds-checked walk. -pub const Frame = struct { - layers: []const u8 = &.{}, - pointer_shape: pardes.Surface.PointerShape = .arrow, - kind: FrameKind, - cols: u16, - rows: u16, - cursor: ?Cursor, - nruns: u32, - runs: []const u8, - - pub fn applyLayers(f: Frame, gpa: std.mem.Allocator, layers: *[pardes.MAX_PANES]pardes.Layer, tags: *[pardes.MAX_TAG_LAYERS]pardes.Layer, regions: *std.ArrayListUnmanaged(pardes.Region), chrome: *pardes.Chrome) !void { - regions.clearRetainingCapacity(); - chrome.* = .{}; - for (layers) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - for (tags) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - errdefer { - for (layers) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - for (tags) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - regions.clearRetainingCapacity(); - chrome.* = .{}; - } - if (f.layers.len == 0) return; - var r: std.Io.Reader = .fixed(f.layers); - const count = try r.takeInt(u16, .little); - if (count > layers.len) return error.Overlong; - for (layers[0..count]) |*layer| { - layer.kind = .body; - layer.id = try r.takeInt(u16, .little); - layer.serial = try r.takeInt(u32, .little); - layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - layer.cols = try r.takeInt(u16, .little); - layer.rows = try r.takeInt(u16, .little); - layer.context_rows = try r.takeInt(u16, .little); - if (layer.id >= pardes.MAX_PANES or layer.cols == 0 or layer.cols > f.cols or - layer.rows == 0 or layer.rows > 2 * max_rows or layer.context_rows > layer.rows or layer.context_rows > limits.wrap_rows or - layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or - layer.viewport.y > f.rows or layer.viewport.h > f.rows - layer.viewport.y) return error.BadValue; - for (0..layer.context_rows) |row| { - if (try getBool(&r)) layer.context_separators.set(row); - } - const cursor = try getCursor(&r, layer.cols, layer.rows); - layer.cursor = if (cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null; - layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); - for (layer.cells) |*cell| cell.* = try decodeCell(&r); - } - const tag_count = try r.takeInt(u16, .little); - if (tag_count > tags.len) return error.Overlong; - for (tags[0..tag_count]) |*layer| { - layer.kind = try getTag(&r, pardes.TagKind); - layer.id = try r.takeInt(u16, .little); - layer.serial = try r.takeInt(u32, .little); - layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - layer.cols = try r.takeInt(u16, .little); - layer.rows = try r.takeInt(u16, .little); - for (&layer.bg) |*channel| channel.* = try r.takeByte(); - if (layer.cols == 0 or layer.viewport.w == 0 or layer.rows == 0 or layer.rows > pardes.MAX_TAG_ROWS or - layer.viewport.h != layer.rows or - layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or - layer.viewport.y >= f.rows or layer.viewport.h > f.rows - layer.viewport.y or (switch (layer.kind) { - .workspace => layer.id != 0, - .column => layer.id >= pardes.MAX_COLS, - .pane, .notice => layer.id >= pardes.MAX_PANES, - .body => true, - })) return error.BadValue; - const cursor = try getCursor(&r, layer.cols, layer.rows); - layer.cursor = if (cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null; - layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); - for (layer.cells) |*cell| cell.* = try decodeCell(&r); - } - const region_count = try r.takeInt(u16, .little); - if (region_count > pardes.MAX_REGIONS) return error.Overlong; - try regions.ensureTotalCapacity(gpa, region_count); - for (0..region_count) |_| { - var region: pardes.Region = .{ .kind = try getTag(&r, pardes.Region.Kind), .rect = undefined }; - region.owner = try r.takeByte(); - region.serial = try r.takeInt(u32, .little); - region.rect = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - const flags = try r.takeByte(); - if (flags > 3) return error.BadValue; - region.active = flags & 1 != 0; - region.dirty = flags & 2 != 0; - region.thumb_y = try r.takeInt(u16, .little); - region.thumb_h = try r.takeInt(u16, .little); - region.notice = try r.takeByte(); - region.clip = try r.takeInt(u16, .little); - region.lift = @bitCast(try r.takeInt(u32, .little)); - region.line = @bitCast(try r.takeInt(u32, .little)); - // A springy flavour overshoots, and a wind-up dips below. - if (!(region.lift >= -1 and region.lift <= 2)) return error.BadValue; - if (region.rect.x > f.cols or region.rect.w > f.cols - region.rect.x or - region.rect.y > f.rows or region.rect.h > f.rows - region.rect.y or - region.clip > region.rect.w or region.thumb_y > region.rect.h or region.thumb_h > region.rect.h - region.thumb_y) return error.BadValue; - regions.appendAssumeCapacity(region); - } - const info = @typeInfo(pardes.Chrome).@"struct"; - inline for (info.field_names, info.field_types) |name, T| { - const rgb = &@field(chrome, name); - if (T == ?[3]u8) { - if (try getBool(&r)) { - rgb.* = .{ 0, 0, 0 }; - for (&rgb.*.?) |*channel| channel.* = try r.takeByte(); - } - } else if (T == u8) { - rgb.* = try r.takeByte(); - } else if (T == u32) { - rgb.* = try r.takeInt(u32, .little); - } else if (T == f32) { - rgb.* = @bitCast(try r.takeInt(u32, .little)); - if (!std.math.isFinite(rgb.*)) return error.BadValue; - } else if (T == [8]f32) { - for (rgb) |*value| { - value.* = @bitCast(try r.takeInt(u32, .little)); - if (!std.math.isFinite(value.*)) return error.BadValue; - } - } else for (rgb) |*channel| channel.* = try r.takeByte(); - } - try end(&r); - } - - /// Paint this frame into `grid`, which must be exactly `cols * rows` cells - /// — the receiver resizes on a geometry change before applying, and a grid - /// of the wrong size is a receiver bug, not a wire condition. - /// - /// Every run is range-checked against the grid before a single cell is - /// written, so a run claiming to start past the end writes nothing. - pub fn apply(f: Frame, grid: []pardes.Cell) Error!void { - if (grid.len != @as(usize, f.cols) * @as(usize, f.rows)) return error.BadValue; - if (f.kind == .full) @memset(grid, .{}); - var r: std.Io.Reader = .fixed(f.runs); - var i: u32 = 0; - while (i < f.nruns) : (i += 1) { - const start = try r.takeInt(u32, .little); - const count = try r.takeInt(u16, .little); - // A zero-length run is not something `encodeFrameLayers` emits, and - // accepting one would let a peer spend the run budget saying - // nothing. - if (count == 0) return error.BadValue; - // THE bounds check. Written as `count > len - start` rather than - // `start + count > len` because the sum of two attacker-chosen - // 32-bit numbers is the classic way this check is bypassed. - if (start > grid.len or count > grid.len - start) return error.Overlong; - for (grid[start..][0..count]) |*c| c.* = try decodeCell(&r); - } - try end(&r); - } -}; - /// Frontend -> core, decoded. The `Event`'s slices BORROW the payload buffer, /// exactly like the pty chunks the tty host hands to `update`: valid for that /// one call and no longer. @@ -351,38 +91,6 @@ pub const ClientMsg = union(enum) { event: pardes.Event, }; -/// Core -> frontend, decoded. Slices borrow the payload buffer the same way. -pub const ServerMsg = union(enum) { - welcome: Welcome, - refuse: Refusal, - frame: Frame, - /// The session is over. Sent before the listener closes so a frontend can - /// exit rather than report a broken pipe. - quit, - /// One frontend is done, and the session is NOT over — see `ServerTag`. - detach, - - set_clipboard: []const u8, - read_clipboard, - open_link: []const u8, - post: Post, -}; - -/// The session's post chain, as a GUI attached to it draws it: its passes in -/// order, each a bundled scene at its level or a Shadertoy file's SPIR-V -/// (compiled by the session, ShaderBuild.zig: a frontend reads no disk and -/// runs no program), and when it animates on its own. Slices borrow the -/// payload. -pub const Post = struct { - animation: ShaderAnimation = .on, - passes: [ShaderBuild.max]ShaderBuild.Pass = @splat(.{}), - len: u8 = 0, - - pub fn list(p: *const Post) []const ShaderBuild.Pass { - return p.passes[0..p.len]; - } -}; - // --------------------------------------------------------------------------- // primitives // --------------------------------------------------------------------------- @@ -490,368 +198,13 @@ pub fn framed(buf: []const u8) Error!?Framed { return .{ .tag = buf[0], .payload = buf[header_len..][0..len], .total = header_len + len }; } -// --------------------------------------------------------------------------- -// cells -// --------------------------------------------------------------------------- - -fn putColor(w: *std.Io.Writer, c: pardes.Color) Error!void { - switch (c) { - .default => try w.writeByte(@backingInt(ColorTag.default)), - .index => |i| { - try w.writeByte(@backingInt(ColorTag.index)); - try w.writeByte(i); - }, - .rgb => |v| { - try w.writeByte(@backingInt(ColorTag.rgb)); - try w.writeAll(&v); - }, - } -} - -fn getColor(r: *std.Io.Reader) Error!pardes.Color { - return switch (try getTag(r, ColorTag)) { - .default => .default, - .index => .{ .index = try r.takeByte() }, - .rgb => .{ .rgb = (try r.take(3))[0..3].* }, - }; -} - -const Ul = @FieldType(pardes.CellStyle, "ul"); - -/// The four enum mappings — underline, font role, mouse button, mouse kind — -/// and `clientTag`/`serverTag` further down all have one shape: the WIRE -/// member of the same NAME. So the byte on the socket is still `@intFromEnum` -/// of an explicitly numbered enum in THIS file and never of a core type — -/// reordering `CellStyle.ul` changes nothing — and adding a variant to the -/// core without adding one here does not compile: -/// -/// error: enum 'wire.UlTag' has no member named 'wavy' -/// -/// which is the whole property the six hand-written switches this replaced -/// existed for, in a form that cannot fall out of step. A variant that has to -/// travel under a DIFFERENT name than the core's gets an arm of its own before -/// the `inline else`, exactly as `clientTag` keeps the machine-local reports. -fn putUl(w: *std.Io.Writer, u: Ul) Error!void { - try w.writeByte(switch (u) { - inline else => |t| @backingInt(@field(UlTag, @tagName(t))), - }); -} - -fn getUl(r: *std.Io.Reader) Error!Ul { - return switch (try getTag(r, UlTag)) { - inline else => |t| @field(Ul, @tagName(t)), - }; -} - -fn putFont(w: *std.Io.Writer, f: pardes.FontRole) Error!void { - try w.writeByte(switch (f) { - inline else => |t| @backingInt(@field(FontTag, @tagName(t))), - }); -} - -fn getFont(r: *std.Io.Reader) Error!pardes.FontRole { - return switch (try getTag(r, FontTag)) { - inline else => |t| @field(pardes.FontRole, @tagName(t)), - }; -} - -/// How many bytes `putCell` will write. Exact, because `encodeFrameLayers` weighs it -/// against `run_header` to decide whether to coalesce a run across it. -fn cellSize(c: *const pardes.Cell) usize { - if (c.default) return 1; - return 1 + 1 + c.len + colorSize(c.style.fg) + colorSize(c.style.bg) + 1 + 1 + 1; -} - -fn colorSize(c: pardes.Color) usize { - return switch (c) { - .default => 1, - .index => 2, - .rgb => 4, - }; -} - -/// An unpainted cell is ONE byte: `default` means "the shell renders the -/// terminal's default cell" (surface.zig `Cell`), so its text and style are not -/// merely equal to the defaults, they are not part of the frame at all. -fn putCell(w: *std.Io.Writer, c: *const pardes.Cell) Error!void { - try w.writeByte(@intFromBool(c.default)); - if (c.default) return; - try w.writeByte(c.len); - try w.writeAll(c.grapheme()); - const s = c.style; - try putColor(w, s.fg); - try putColor(w, s.bg); - var attrs: u8 = 0; - if (s.bold) attrs |= attr_bold; - if (s.dim) attrs |= attr_dim; - if (s.italic) attrs |= attr_italic; - if (s.blink) attrs |= attr_blink; - if (s.reverse) attrs |= attr_reverse; - if (s.invisible) attrs |= attr_invisible; - if (s.strikethrough) attrs |= attr_strikethrough; - try w.writeByte(attrs); - try putUl(w, s.ul); - try putFont(w, s.font_role); -} - -fn decodeCell(r: *std.Io.Reader) Error!pardes.Cell { - if (try getBool(r)) return .{}; - var c: pardes.Cell = .{ .default = false }; - const len = try r.takeByte(); - // `Cell.text` is 7 bytes and `grapheme()` slices to `len`. A zero-length - // grapheme is a cell with nothing to draw and no way to advance a column. - if (len == 0 or len > c.text.len) return error.BadValue; - c.len = len; - @memcpy(c.text[0..len], try r.take(len)); - c.style.fg = try getColor(r); - c.style.bg = try getColor(r); - const attrs = try r.takeByte(); - if (attrs & attr_reserved != 0) return error.BadValue; - c.style.bold = attrs & attr_bold != 0; - c.style.dim = attrs & attr_dim != 0; - c.style.italic = attrs & attr_italic != 0; - c.style.blink = attrs & attr_blink != 0; - c.style.reverse = attrs & attr_reverse != 0; - c.style.invisible = attrs & attr_invisible != 0; - c.style.strikethrough = attrs & attr_strikethrough != 0; - c.style.ul = try getUl(r); - c.style.font_role = try getFont(r); - return c; -} - -// --------------------------------------------------------------------------- -// frames -// --------------------------------------------------------------------------- - -fn putCursor(w: *std.Io.Writer, cursor: ?Cursor, cols: u16, rows: u16) Error!void { - // Fixed six bytes whether or not there is a cursor. An optional field - // would save five bytes on a message that is already hundreds, and cost a - // branch on both sides of the wire. - try w.writeByte(@intFromBool(cursor != null)); - const c = cursor orelse Cursor{ .x = 0, .y = 0, .bar = false }; - // Refused here as well as on decode, so this side cannot build a frame its - // own decoder rejects: a dropped frame (sendFrame logs it) leaves a stale - // screen, and a refused one takes the frontend's connection with it. - if (c.x >= cols or c.y >= rows) return error.BadValue; - try w.writeInt(u16, c.x, .little); - try w.writeInt(u16, c.y, .little); - try w.writeByte(@intFromBool(c.bar)); -} - -fn getCursor(r: *std.Io.Reader, cols: u16, rows: u16) Error!?Cursor { - const present = try getBool(r); - const c: Cursor = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .bar = try getBool(r) }; - // THE cursor bounds check. Every other field of a frame is checked against - // the grid and these two were not, and they are the two a frontend indexes - // with directly — `Surface.at(cursor.x, cursor.y)` on a grid this frame - // says is 4x2 is an out-of-bounds write in EVERY frontend, not a wrong - // glyph. Checked whether or not the cursor is present, because the absent - // case is six bytes of padding this encoder writes as zero and a peer that - // fills them with anything else is not speaking this protocol. - if (c.x >= cols or c.y >= rows) return error.BadValue; - return if (present) c else null; -} - -pub fn layersBound(layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region) usize { - var n: usize = 6; - for (layers) |layer| if (layer.rows != 0) { - n += 26 + layer.context_rows + layer.cells.len * cell_max; - }; - for (tags) |layer| if (layer.rows != 0) { - n += 28 + layer.cells.len * cell_max; - }; - return n + regions.len * region_len + chrome_len; -} - -const region_len = 1 + 1 + 4 + 8 + 1 + 2 + 2 + 1 + 2 + 4 + 4; -/// The palette: each colour three bytes, a theme's optional ones a flag -/// before them. -const chrome_len = @typeInfo(pardes.Chrome).@"struct".field_names.len * 4 + 8 * 4; - -/// Encode one frame of `cells`, as a DIFF against `prev` when `prev` is the -/// same grid this receiver last acknowledged, and as a FULL frame otherwise. -/// -/// A late joiner and a resize are the same case and are handled by the same -/// test: nothing on the far side is comparable to this grid, so `prev` is -/// empty or a different length and the frame becomes full. -/// -/// Why a diff at all. Measured against a real `pardes --detach` at 80x24 with -/// the default theme, which paints every cell and gives most of them an rgb -/// pair (14 bytes a cell rather than the 8 an uncoloured one costs): -/// * a full frame is 21965-26699 bytes, depending on what is on screen, -/// * a frame that changes nothing is 20 — the head, and no runs at all, -/// * a one-row change (`Msg hi`) is 54 bytes in one run, and a wordier one -/// 166, -/// * and an IDLE session sends nothing whatever, because the core is asleep -/// in `poll(2)` and produces no frame until something happens. -/// So the diff is worth roughly 500x on the traffic a session actually -/// generates. The byte-count test below asserts the arithmetic on the board's -/// own 56x14 grid with uncoloured cells, where it is exact: 6298 against 56. -/// This transport exists for a frontend on the far end of a slow link, and -/// shipping the Surface every frame would be 3 MiB/s at the animation tick. -pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, pointer_shape: pardes.Surface.PointerShape, cells: []const pardes.Cell, prev: []const pardes.Cell, layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region, chrome: pardes.Chrome) Error![]const u8 { - // The protocol's ceiling, enforced by the ENCODER too, and BEFORE the - // assert below so a caller can be told rather than tripped. `max_payload` - // is derived from these two, so a larger grid is a frame this decoder - // refuses as Overlong — and sendFrame's log line already claims that this - // is the error it is catching, which was true of nothing until here. - if (cols == 0 or cols > max_cols or rows == 0 or rows > max_rows) return error.Overlong; - std.debug.assert(cells.len == @as(usize, cols) * @as(usize, rows)); - const full = prev.len != cells.len; - var w: std.Io.Writer = .fixed(out); - const at = try beginMessage(&w, @backingInt(ServerTag.frame)); - const header: FrameHeader = if (pointer_shape == .link) - (if (full) .full_link else .diff_link) - else - (if (full) .full else .diff); - try w.writeByte(@backingInt(header)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, cursor, cols, rows); - const layers_len_at = w.end; - try w.writeInt(u32, 0, .little); - const layers_start = w.end; - if (layers.len != 0 or tags.len != 0 or regions.len != 0) { - var count: u16 = 0; - for (layers) |layer| if (layer.rows != 0) { - count += 1; - }; - try w.writeInt(u16, count, .little); - for (layers) |layer| { - if (layer.rows == 0) continue; - if (layer.context_rows > limits.wrap_rows or layer.context_rows > layer.rows) return error.BadValue; - try w.writeInt(u16, layer.id, .little); - try w.writeInt(u32, layer.serial, .little); - try w.writeInt(u16, layer.viewport.x, .little); - try w.writeInt(u16, layer.viewport.y, .little); - try w.writeInt(u16, layer.viewport.w, .little); - try w.writeInt(u16, layer.viewport.h, .little); - try w.writeInt(u16, layer.cols, .little); - try w.writeInt(u16, layer.rows, .little); - try w.writeInt(u16, layer.context_rows, .little); - for (0..layer.context_rows) |row| try w.writeByte(@intFromBool(layer.context_separators.isSet(row))); - try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); - for (layer.cells) |*cell| try putCell(&w, cell); - } - var tag_count: u16 = 0; - for (tags) |layer| if (layer.rows != 0) { - tag_count += 1; - }; - try w.writeInt(u16, tag_count, .little); - for (tags) |layer| { - if (layer.rows == 0) continue; - if (layer.cells.len != @as(usize, layer.cols) * layer.rows) return error.BadValue; - try w.writeByte(@backingInt(layer.kind)); - try w.writeInt(u16, layer.id, .little); - try w.writeInt(u32, layer.serial, .little); - try w.writeInt(u16, layer.viewport.x, .little); - try w.writeInt(u16, layer.viewport.y, .little); - try w.writeInt(u16, layer.viewport.w, .little); - try w.writeInt(u16, layer.viewport.h, .little); - try w.writeInt(u16, layer.cols, .little); - try w.writeInt(u16, layer.rows, .little); - for (layer.bg) |channel| try w.writeByte(channel); - try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); - for (layer.cells) |*cell| try putCell(&w, cell); - } - try w.writeInt(u16, @intCast(regions.len), .little); - for (regions) |region| { - try w.writeByte(@backingInt(region.kind)); - try w.writeByte(region.owner); - try w.writeInt(u32, region.serial, .little); - try w.writeInt(u16, region.rect.x, .little); - try w.writeInt(u16, region.rect.y, .little); - try w.writeInt(u16, region.rect.w, .little); - try w.writeInt(u16, region.rect.h, .little); - try w.writeByte(@as(u8, @intFromBool(region.active)) | @as(u8, @intFromBool(region.dirty)) << 1); - try w.writeInt(u16, region.thumb_y, .little); - try w.writeInt(u16, region.thumb_h, .little); - try w.writeByte(region.notice); - try w.writeInt(u16, region.clip, .little); - try w.writeInt(u32, @bitCast(region.lift), .little); - try w.writeInt(u32, @bitCast(region.line), .little); - } - const info = @typeInfo(pardes.Chrome).@"struct"; - inline for (info.field_names, info.field_types) |name, T| { - const rgb = @field(chrome, name); - if (T == ?[3]u8) { - try w.writeByte(@intFromBool(rgb != null)); - if (rgb) |value| for (value) |channel| try w.writeByte(channel); - } else if (T == u8) { - try w.writeByte(rgb); - } else if (T == u32) { - try w.writeInt(u32, rgb, .little); - } else if (T == f32) { - try w.writeInt(u32, @bitCast(rgb), .little); - } else if (T == [8]f32) { - for (rgb) |value| try w.writeInt(u32, @bitCast(value), .little); - } else for (rgb) |channel| try w.writeByte(channel); - } - } - std.mem.writeInt(u32, w.buffer[layers_len_at..][0..4], @intCast(w.end - layers_start), .little); - const nruns_at = w.end; - try w.writeInt(u32, 0, .little); - - var nruns: u32 = 0; - var i: usize = 0; - while (i < cells.len) { - if (!sendCell(cells, prev, full, i)) { - i += 1; - continue; - } - const start = i; - var run_end = i + 1; - i += 1; - // `i - start` and not `run_end - start`, because the gap lookahead - // below moves `i` ahead of `run_end` by up to `run_header - 1` before - // the next iteration adopts it: bounding the cursor bounds the run, - // and bounding the run afterwards would not. - while (i < cells.len and i - start < run_max) { - if (sendCell(cells, prev, full, i)) { - run_end = i + 1; - i += 1; - continue; - } - // A gap. Re-sending cells the far side already has is cheaper than - // a second run header whenever their encoded size adds up to less - // than one — true for short stretches of unpainted cells at a byte - // each, and false as soon as one painted cell (eight bytes at its - // smallest) is in the way. So the lookahead can never need to pass - // `run_header - 1` cells. - var gap: usize = 0; - var j = i; - while (j < cells.len and gap < run_header) : (j += 1) { - if (sendCell(cells, prev, full, j)) break; - gap += cellSize(&cells[j]); - } - if (j >= cells.len or gap >= run_header) break; - i = j; - } - try w.writeInt(u32, @intCast(start), .little); - try w.writeInt(u16, @intCast(run_end - start), .little); - for (cells[start..run_end]) |*c| try putCell(&w, c); - nruns += 1; - i = run_end; - } - std.mem.writeInt(u32, w.buffer[nruns_at..][0..4], nruns, .little); - try finishMessage(&w, at); - return w.buffered(); -} - -fn sendCell(cells: []const pardes.Cell, prev: []const pardes.Cell, full: bool, i: usize) bool { - // Full: the receiver reset the grid, so unpainted cells are already right. - // Diff: anything the receiver cannot already be showing. - if (full) return !cells[i].default; - return !cells[i].visuallyEqual(&prev[i]); -} - // --------------------------------------------------------------------------- // frontend -> core // --------------------------------------------------------------------------- /// Which tag a message travels under: the `ClientTag` of the same NAME, so a -/// new `Event` variant does not compile until it has a number here. See -/// `putUl` for the error it produces and why this is not a written-out table. +/// new `Event` variant does not compile until it has a number here, rather +/// than a written-out table that can drift. fn clientTag(msg: ClientMsg) ClientTag { return switch (msg) { .event => |ev| switch (ev) { @@ -1054,81 +407,12 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { return msg; } -// --------------------------------------------------------------------------- -// core -> frontend -// --------------------------------------------------------------------------- - -/// ...and the same rule in the same shape: the `ServerTag` of the same name. -fn serverTag(msg: ServerMsg) ServerTag { - return switch (msg) { - inline else => |_, t| @field(ServerTag, @tagName(t)), - }; -} - -/// Every server message EXCEPT a frame, which has its own encoder because its -/// payload is a walk of two grids rather than a value (see `encodeFrameLayers`). -pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { - var w: std.Io.Writer = .fixed(out); - const at = try beginMessage(&w, @backingInt(serverTag(msg))); - switch (msg) { - .welcome => |v| { - try w.writeInt(u16, v.version, .little); - try w.writeByte(v.slot); - try w.writeInt(u16, v.cols, .little); - try w.writeInt(u16, v.rows, .little); - }, - .refuse => |why| try w.writeByte(@backingInt(why)), - // A frame's runs are not a value this union can hold; the server calls - // encodeFrameLayers directly and this arm exists so the switch stays - // exhaustive over ServerMsg. - .frame => return error.BadValue, - .quit, .detach => {}, - .set_clipboard => |t| try putSlice32(&w, t), - .read_clipboard => {}, - .open_link => |u| try putSlice16(&w, u), - .post => |p| { - try w.writeByte(@backingInt(p.animation)); - try w.writeByte(p.len); - for (p.list()) |pass| { - // 0 a file, else a bundled scene and one. - try w.writeByte(if (pass.scene) |scene| @as(u8, @backingInt(scene)) + 1 else 0); - try w.writeByte(pass.level); - try w.writeByte(@intFromBool(pass.animated)); - try w.writeInt(u32, pass.revision, .little); - try putSlice16(&w, pass.path); - try putSlice32(&w, pass.spirv); - } - }, - } - try finishMessage(&w, at); - return w.buffered(); -} - /// Slack over a message's variable payload, covering every fixed field any -/// message here has plus its own header. One loose constant rather than a -/// field-by-field count: a bound that is 32 bytes generous costs one `memcpy` -/// worth of nothing, and a bound that is one byte tight is a bug that only -/// shows up on the message nobody tested. +/// message here has plus its own header. const msg_slack = header_len + 64; -/// An upper bound on `encodeServer`'s output, so a caller sizes its buffer +/// An upper bound on `encodeClient`'s output, so a caller sizes its buffer /// once instead of guessing and retrying. -pub fn serverBound(msg: ServerMsg) usize { - return msg_slack + switch (msg) { - .welcome, .refuse, .quit, .detach, .read_clipboard => 0, - // A frame is bounded by its grid, not by this: see `frameBound`. - .frame => |f| frameBound(f.cols, f.rows), - .set_clipboard => |t| t.len, - .open_link => |u| u.len, - .post => |p| blk: { - var n: usize = 0; - for (p.list()) |pass| n += 13 + pass.path.len + pass.spirv.len; - break :blk n; - }, - }; -} - -/// ...and the same for the frontend's side of the wire. pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { .hello, .bye => 0, @@ -1143,76 +427,6 @@ pub fn clientBound(msg: ClientMsg) usize { }; } -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 = .{ - .version = try r.takeInt(u16, .little), - .slot = try r.takeByte(), - .cols = try getCols(&r), - .rows = try getRows(&r), - } }, - .refuse => .{ .refuse = try getTag(&r, Refusal) }, - .frame => blk: { - const header = try getTag(&r, FrameHeader); - const kind: FrameKind = switch (header) { - .full, .full_link => .full, - .diff, .diff_link => .diff, - }; - const cols = try getCols(&r); - const rows = try getRows(&r); - const cursor = try getCursor(&r, cols, rows); - const layers = try getSlice32(&r); - const nruns = try r.takeInt(u32, .little); - // One run per cell is the worst an encoder emits, so a bigger - // count cannot be describing this grid. Checked HERE rather than - // in `apply`'s loop so the number is refused before it is used to - // bound anything. - if (nruns > @as(u32, cols) * @as(u32, rows)) return error.Overlong; - const runs = r.buffered(); - r.tossBuffered(); - break :blk .{ .frame = .{ - .layers = layers, - .kind = kind, - .pointer_shape = if (header == .full_link or header == .diff_link) .link else .arrow, - .cols = cols, - .rows = rows, - .cursor = cursor, - .nruns = nruns, - .runs = runs, - } }; - }, - .quit => .quit, - .detach => .detach, - .set_clipboard => .{ .set_clipboard = try getSlice32(&r) }, - .read_clipboard => .read_clipboard, - .open_link => .{ .open_link = try r.take(try r.takeInt(u16, .little)) }, - .post => blk: { - var p: Post = .{ .animation = try getTag(&r, ShaderAnimation) }; - p.len = try r.takeByte(); - if (p.len > ShaderBuild.max) return error.Overlong; - for (p.passes[0..p.len]) |*pass| { - const kind = try r.takeByte(); - pass.* = .{ - .scene = if (kind == 0) null else std.enums.fromInt(Scene, kind - 1) orelse return error.BadValue, - .level = try r.takeByte(), - .animated = switch (try r.takeByte()) { - 0 => false, - 1 => true, - else => return error.BadValue, - }, - .revision = try r.takeInt(u32, .little), - .path = try r.take(try r.takeInt(u16, .little)), - .spirv = try getSlice32(&r), - }; - } - break :blk .{ .post = p }; - }, - }; - try end(&r); - return msg; -} - // --------------------------------------------------------------------------- // tests // --------------------------------------------------------------------------- @@ -1233,13 +447,6 @@ fn roundClient(buf: []u8, msg: ClientMsg) !ClientMsg { return decodeClient(f.tag, f.payload); } -fn roundServer(buf: []u8, msg: ServerMsg) !ServerMsg { - const bytes = try encodeServer(buf, msg); - const f = (try framed(bytes)).?; - try testing.expectEqual(bytes.len, f.total); - return decodeServer(f.tag, f.payload); -} - test "detached wire: every Event variant round-trips" { var buf: [4096]u8 = undefined; @@ -1311,50 +518,6 @@ test "detached wire: every Event variant round-trips" { ); } -test "detached wire: every server message round-trips" { - var buf: [4096]u8 = undefined; - - { - const got = (try roundServer(&buf, .{ .welcome = .{ .slot = 2, .cols = 56, .rows = 14 } })).welcome; - try testing.expectEqual(version, got.version); - try testing.expectEqual(@as(u8, 2), got.slot); - try testing.expectEqual(@as(u16, 56), got.cols); - try testing.expectEqual(@as(u16, 14), got.rows); - } - for (std.enums.values(Refusal)) |why| - try testing.expectEqual(why, (try roundServer(&buf, .{ .refuse = why })).refuse); - try testing.expectEqual(ServerMsg.quit, try roundServer(&buf, .quit)); - try testing.expectEqual(ServerMsg.detach, try roundServer(&buf, .detach)); - try testing.expectEqualStrings("yank", (try roundServer(&buf, .{ .set_clipboard = "yank" })).set_clipboard); - try testing.expectEqual(ServerMsg.read_clipboard, try roundServer(&buf, .read_clipboard)); - try testing.expectEqualStrings("https://x", (try roundServer(&buf, .{ .open_link = "https://x" })).open_link); - var chain: Post = .{ .animation = .always, .len = 2 }; - chain.passes[0] = .{ .scene = .bloom, .level = 3 }; - chain.passes[1] = .{ .path = "~/wave.glsl", .spirv = "\x03\x02\x23\x07spirv", .revision = 7, .animated = true }; - const back = (try roundServer(&buf, .{ .post = chain })).post; - try testing.expectEqual(ShaderAnimation.always, back.animation); - try testing.expectEqual(@as(u8, 2), back.len); - try testing.expectEqual(Scene.bloom, back.passes[0].scene.?); - try testing.expectEqual(@as(u8, 3), back.passes[0].level); - try testing.expect(back.passes[1].scene == null); - try testing.expectEqualStrings("~/wave.glsl", back.passes[1].path); - try testing.expectEqualStrings("\x03\x02\x23\x07spirv", back.passes[1].spirv); - try testing.expectEqual(@as(u32, 7), back.passes[1].revision); - try testing.expect(back.passes[1].animated and !back.passes[0].animated); -} - -test "detached wire: only the display's own effects are on the wire" { - // Two guards, because the mistake has two directions. The exhaustive - // switch fails to COMPILE if a machine-local effect is added back, which - // is the direction that matters: it would hand a frontend work that must - // outlive it. The count fails if one of the three is dropped, which would - // silently leave a clipboard or a link unanswered on every frontend. - try testing.expectEqual(@as(usize, 9), std.enums.values(ServerTag).len); - for (std.enums.values(ServerTag)) |t| switch (t) { - .welcome, .refuse, .frame, .quit, .detach, .set_clipboard, .read_clipboard, .open_link, .post => {}, - }; -} - test "detached wire: a session is never told to do a frontend's remembering" { // The mirror of the test above, and of client.zig's "a frontend is never // asked to fork, write, or watch". That one pins what may reach a FRONTEND; @@ -1422,223 +585,7 @@ test "detached wire: a session is never told to do a frontend's remembering" { try testing.expectEqual(allowed.len, accepted); } -/// A grid with something in every corner: a default cell, a plain ASCII cell, -/// an indexed pair, an rgb pair with every attribute on, and a multi-byte -/// grapheme — the five shapes `putCell` branches on. -fn sampleGrid(cells: []pardes.Cell) void { - @memset(cells, .{}); - cells[0] = .{ .text = "x".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[1] = .{ - .text = "y".* ++ @as([6]u8, @splat(0)), - .len = 1, - .default = false, - .style = .{ .fg = .{ .index = 3 }, .bg = .{ .index = 250 }, .bold = true, .ul = .curly }, - }; - cells[2] = .{ - .text = "→".* ++ @as([4]u8, @splat(0)), - .len = 3, - .default = false, - .style = .{ - .fg = .{ .rgb = .{ 1, 2, 3 } }, - .bg = .{ .rgb = .{ 250, 251, 252 } }, - .bold = true, - .dim = true, - .italic = true, - .blink = true, - .reverse = true, - .invisible = true, - .strikethrough = true, - .ul = .dashed, - .font_role = .tagline, - }, - }; - cells[cells.len - 1] = .{ .text = "z".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; -} - -fn expectGridEqual(want: []const pardes.Cell, have: []const pardes.Cell) !void { - try testing.expectEqual(want.len, have.len); - // `visuallyEqual` and not `std.meta.eql`: `Cell.text` past `len` is - // scratch left by an earlier grapheme, the decoder does not invent it, and - // pardes.zig says in as many words that it must never manufacture a diff. - for (want, have, 0..) |*a, *b, i| if (!a.visuallyEqual(b)) { - std.debug.print("cell {d} differs: {any} vs {any}\n", .{ i, a.*, b.* }); - return error.CellMismatch; - }; -} - -test "detached wire: a full frame carries the grid, a diff carries the change" { - const cols: u16 = 56; - const rows: u16 = 14; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const mirror = try gpa.alloc(pardes.Cell, n); - defer gpa.free(mirror); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - sampleGrid(cells); - @memset(mirror, .{}); - - // FULL: nothing on the far side is comparable, which is the late joiner - // and the resize both. - const full_bytes = try encodeFrameLayers(buf, cols, rows, .{ .x = 3, .y = 4, .bar = true }, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); - { - const f = (try framed(full_bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.full, msg.kind); - try testing.expectEqual(cols, msg.cols); - try testing.expectEqual(rows, msg.rows); - try testing.expectEqual(@as(u16, 3), msg.cursor.?.x); - try testing.expectEqual(@as(u16, 4), msg.cursor.?.y); - try testing.expect(msg.cursor.?.bar); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } - - // DIFF: two cells move. The mirror is already in sync, so this is what a - // steady-state frame looks like. - const before = try gpa.dupe(pardes.Cell, cells); - defer gpa.free(before); - cells[100] = .{ .text = "q".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[0] = .{}; // ...and one goes back to unpainted, which a diff must say - const diff_bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{}); - { - const f = (try framed(diff_bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.diff, msg.kind); - try testing.expectEqual(@as(?Cursor, null), msg.cursor); - try testing.expectEqual(@as(u32, 2), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } - - // An unchanged frame is the head and nothing else: no runs, and the - // receiver keeps what it has. This is what makes an idle session silent. - { - const idle = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, cells, &.{}, &.{}, &.{}, .{}); - try testing.expectEqual(@as(usize, header_len + frame_head), idle.len); - const f = (try framed(idle)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(@as(u32, 0), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } -} - -test "detached wire: the largest grid is one cell past a run, and survives it" { - // `max_cols * max_rows` is 65536 and a run's `count` is a u16, so THE ONE - // grid this protocol calls legal at both bounds is the one grid whose full - // frame cannot be described by a single run. It reached nobody while a - // frontend sent its window raw — a display that big had its hello refused - // before any frame was composed — and it is the ordinary case now that a - // frontend clamps to exactly this. In a safe build the `@intCast` panicked - // and took the session down; in a fast one it wrote a zero-length run the - // frontend answered with `BadValue`. - const cols = max_cols; - const rows = max_rows; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const mirror = try gpa.alloc(pardes.Cell, n); - defer gpa.free(mirror); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - // Every cell painted, which is what `render` produces and therefore what a - // full frame always is: one run, if a run could be that long. - for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - @memset(mirror, .{}); - - const bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); - const f = (try framed(bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.full, msg.kind); - // Split, and split as late as it can be: 65535 cells then 1. - try testing.expectEqual(@as(u32, 2), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - // ...at exactly the two headers the split costs and not one byte more. - // `bytes.len <= frameBound(...)` would prove nothing here — the buffer IS - // `frameBound` bytes, so an over-run is `WriteFailed` above, not a false - // assertion here — and what wants pinning is that the second run is the - // whole of the cost. - try testing.expectEqual( - @as(usize, header_len + frame_head + 2 * run_header + n * 8), - bytes.len, - ); -} - -test "detached wire: the diff is worth having, in bytes, on the board's grid" { - // The numbers quoted in `encodeFrameLayers`'s comment, asserted so the claim - // cannot rot. 56x14 is the ESP32-P4 board's default grid (esp32p4.zig). - const cols: u16 = 56; - const rows: u16 = 14; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - // Every cell painted with a plain ASCII glyph and default colors, which is - // what a pardes frame overwhelmingly is: eight bytes a cell. - for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const full = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{})).len; - try testing.expectEqual(@as(usize, header_len + frame_head + run_header + n * 8), full); - try testing.expectEqual(@as(usize, 6302), full); - - const before = try gpa.dupe(pardes.Cell, cells); - defer gpa.free(before); - // A keystroke: one glyph replaced, and the cursor's old and new cells - // repainted. Three cells, adjacent enough to coalesce into two runs. - cells[300] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[301] = .{ .text = "c".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[500] = .{ .text = "d".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const diff = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{})).len; - try testing.expectEqual(@as(usize, header_len + frame_head + 2 * run_header + 3 * 8), diff); - try testing.expectEqual(@as(usize, 60), diff); - // The whole reason this codec exists rather than shipping the Surface. - try testing.expect(full / diff > 100); -} - -test "detached wire: a run is coalesced across a gap only when that is cheaper" { - const cols: u16 = 8; - const rows: u16 = 1; - var cells: [8]pardes.Cell = @splat(.{}); - var prev: [8]pardes.Cell = @splat(.{}); - var buf: [512]u8 = undefined; - - // Two changes with three UNPAINTED cells between them. Re-sending those is - // three bytes; a second run header is six. One run. - cells[0] = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[4] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - { - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try testing.expectEqual(@as(u32, 1), (try decodeServer(f.tag, f.payload)).frame.nruns); - } - // Now put a PAINTED cell in the gap that both sides already agree about. - // Eight bytes to re-send against six for a header: two runs. - const painted: pardes.Cell = .{ .text = "-".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[2] = painted; - prev[2] = painted; - { - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try testing.expectEqual(@as(u32, 2), (try decodeServer(f.tag, f.payload)).frame.nruns); - } - // Whichever it chose, the receiver ends up with the same grid. - var mirror: [8]pardes.Cell = prev; - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try (try decodeServer(f.tag, f.payload)).frame.apply(&mirror); - try expectGridEqual(&cells, &mirror); -} - -test "detached wire: a truncated frame is refused at every length" { +test "detached wire: a truncated message is refused at every length" { var buf: [4096]u8 = undefined; // Every prefix of a real message. The framing must say "not yet" for the @@ -1659,7 +606,6 @@ test "detached wire: a truncated frame is refused at every length" { try testing.expectEqual(@as(?Framed, null), try framed(copy[0..cut])); // A payload with bytes LEFT OVER is refused too: it is not this message. try testing.expectError(error.Trailing, decodeClient(@backingInt(ClientTag.pointer_leave), "x")); - try testing.expectError(error.Trailing, decodeServer(@backingInt(ServerTag.quit), "x")); } test "detached wire: an unknown tag is refused, never guessed" { @@ -1667,11 +613,8 @@ test "detached wire: an unknown tag is refused, never guessed" { // the session tags and the input tags. All three are the same answer. for ([_]u8{ 0x00, 0x0f, 0x1f, 0xff }) |tag| { try testing.expectError(error.BadTag, decodeClient(tag, "")); - try testing.expectError(error.BadTag, decodeServer(tag, "")); } - // A tag NESTED in a payload gets the same treatment: a color, an - // underline style, a mouse button, a refusal reason. - try testing.expectError(error.BadTag, decodeServer(@backingInt(ServerTag.refuse), &.{0x7f})); + // A tag NESTED in a payload gets the same treatment: a mouse button. try testing.expectError(error.BadTag, decodeClient( @backingInt(ClientTag.mouse), &.{ 0xff, 0x00, 0, 0, 0, 0, 0 }, @@ -1701,140 +644,6 @@ test "detached wire: an over-long length prefix is refused before it is believed try testing.expectError(error.Overlong, decodeClient(@backingInt(ClientTag.paste), &paste)); } -test "detached wire: a frame that lies about its runs cannot walk out of the grid" { - const cols: u16 = 4; - const rows: u16 = 2; - var grid: [8]pardes.Cell = @splat(.{}); - - // start = 6, count = 4 on an 8-cell grid: the check that matters, and the - // one an `start + count > len` spelling would miss on overflow. - var runs: [10]u8 = undefined; - std.mem.writeInt(u32, runs[0..4], 6, .little); - std.mem.writeInt(u16, runs[4..6], 4, .little); - runs[6] = 1; - const f: Frame = .{ .kind = .diff, .cols = cols, .rows = rows, .cursor = null, .nruns = 1, .runs = runs[0..7] }; - try testing.expectError(error.Overlong, f.apply(&grid)); - - // A start past the end entirely, and the 32-bit wrap. - std.mem.writeInt(u32, runs[0..4], std.math.maxInt(u32) - 1, .little); - std.mem.writeInt(u16, runs[4..6], 4, .little); - try testing.expectError(error.Overlong, (Frame{ - .kind = .diff, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 1, - .runs = runs[0..7], - }).apply(&grid)); - - // A zero-length run says nothing and is not something the encoder emits. - std.mem.writeInt(u32, runs[0..4], 0, .little); - std.mem.writeInt(u16, runs[4..6], 0, .little); - try testing.expectError(error.BadValue, (Frame{ - .kind = .diff, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 1, - .runs = runs[0..6], - }).apply(&grid)); - - // A run count larger than the grid has cells is refused at DECODE, before - // it is used to bound the walk. - var head: [frame_head]u8 = undefined; - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, null, cols, rows); - try w.writeInt(u32, 0, .little); // no body layers - try w.writeInt(u32, 9, .little); - try testing.expectError(error.Overlong, decodeServer(@backingInt(ServerTag.frame), w.buffered())); - - // ...and a grid of the wrong size is the receiver's own bug, not a frame - // it should paint half of. - var small: [4]pardes.Cell = @splat(.{}); - try testing.expectError(error.BadValue, (Frame{ - .kind = .full, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 0, - .runs = "", - }).apply(&small)); -} - -test "detached wire: link pointer survives full and unchanged grid frames" { - const cells = [_]pardes.Cell{.{}}; - var buffer: [256]u8 = undefined; - for ([_]pardes.Surface.PointerShape{ .link, .arrow, .link }) |shape| { - for ([_]bool{ true, false }) |full| { - const bytes = try encodeFrameLayers(&buffer, 1, 1, null, shape, &cells, if (full) &.{} else &cells, &.{}, &.{}, &.{}, .{}); - const packet = (try framed(bytes)).?; - const frame = (try decodeServer(packet.tag, packet.payload)).frame; - try std.testing.expectEqual(shape, frame.pointer_shape); - try std.testing.expectEqual(if (full) FrameKind.full else FrameKind.diff, frame.kind); - if (!full) try std.testing.expectEqual(@as(u32, 0), frame.nruns); - } - } -} - -test "detached wire: a cursor outside the grid is refused, not painted" { - // The one field of a frame a frontend indexes with rather than copies: - // tty.zig moves the terminal's own cursor to `cursor.x`/`cursor.y`, and - // the ESP32-P4 panel writes the cell there. A frame that says 4x2 and puts - // the cursor at (9,0) is an out-of-bounds write in every frontend, so it - // has to be a decode error and not a clamp — a clamped cursor is a wrong - // screen that nobody reports. - const cols: u16 = 4; - const rows: u16 = 2; - var head: [frame_head]u8 = undefined; - for ([_][2]u16{ .{ cols, 0 }, .{ 0, rows }, .{ 0xffff, 0xffff } }) |xy| { - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - // Written by hand: `putCursor` now refuses this too, which is the - // other half of the same fix and is asserted below. - try w.writeByte(@intFromBool(true)); - try w.writeInt(u16, xy[0], .little); - try w.writeInt(u16, xy[1], .little); - try w.writeByte(@intFromBool(false)); - try w.writeInt(u32, 0, .little); - try testing.expectError( - error.BadValue, - decodeServer(@backingInt(ServerTag.frame), w.buffered()), - ); - } - // The last cell IS in the grid, and a frame carrying it decodes. - { - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, .{ .x = cols - 1, .y = rows - 1, .bar = true }, cols, rows); - try w.writeInt(u32, 0, .little); // no body layers - try w.writeInt(u32, 0, .little); - const got = (try decodeServer(@backingInt(ServerTag.frame), w.buffered())).frame; - try testing.expectEqual(@as(u16, cols - 1), got.cursor.?.x); - try testing.expectEqual(@as(u16, rows - 1), got.cursor.?.y); - } - // ...and the ENCODER refuses to build one, so a core bug is a dropped - // frame with a log line rather than a frame every frontend hangs up over. - var cells: [8]pardes.Cell = @splat(.{}); - var buf: [512]u8 = undefined; - try testing.expectError( - error.BadValue, - encodeFrameLayers(&buf, cols, rows, .{ .x = cols, .y = 0, .bar = false }, .arrow, &cells, &.{}, &.{}, &.{}, &.{}, .{}), - ); - // A grid past the protocol's own ceiling is refused by the encoder for the - // same reason: `max_payload` is derived from it, so the decoder would. - try testing.expectError( - error.Overlong, - encodeFrameLayers(&buf, max_cols + 1, 1, null, .arrow, cells[0..0], &.{}, &.{}, &.{}, &.{}, .{}), - ); -} - test "detached wire: values a field cannot mean are refused" { // A bool is 0 or 1. `2` used to be "true" in every hand-written codec that // ever silently accepted a corrupt stream. @@ -1875,28 +684,10 @@ test "detached wire: values a field cannot mean are refused" { std.mem.writeInt(u32, &pinch, @bitCast(std.math.inf(f32)), .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.touch_scroll), &pinch)); } - // A cell with the reserved attribute bit set, and one with an impossible - // grapheme length. Both are bytes this protocol has no meaning for. - { - var grid: [1]pardes.Cell = @splat(.{}); - // default=0, len=1, 'x', fg default, bg default, attrs=0x80, ul, font - var runs = [_]u8{ 0, 0, 0, 0, 1, 0, 0, 1, 'x', 0, 0, 0x80, 0, 0 }; - const f: Frame = .{ .kind = .diff, .cols = 1, .rows = 1, .cursor = null, .nruns = 1, .runs = &runs }; - try testing.expectError(error.BadValue, f.apply(&grid)); - runs[11] = 0; - runs[7] = 8; // len past Cell.text - try testing.expectError(error.BadValue, f.apply(&grid)); - runs[7] = 0; // ...and a grapheme of no bytes at all - try testing.expectError(error.BadValue, f.apply(&grid)); - } } test "detached wire: max_payload bounds every message this protocol can build" { - // The derivation in `max_payload`'s comment, asserted: the worst frame - // this protocol admits fits, so a receiver sized for max_payload can - // always hold one. - try testing.expect(frameBound(max_cols, max_rows) <= max_payload); - // ...and a 4 MiB paste, which is the tty frontend's own cap. + // A 4 MiB paste, which is the tty's own cap, fits. try testing.expect((4 << 20) + header_len + 4 <= max_payload); // A slice past the width of its own length prefix is refused by the // ENCODER, rather than written and rejected at the far end. A command line @@ -1914,47 +705,12 @@ test "detached wire: max_payload bounds every message this protocol can build" { try testing.expectError(error.WriteFailed, encodeClient(&small, .{ .event = .{ .paste = "0123456789" } })); } -test "detached wire carries compact body rows and exact pointer metrics" { - const gpa = std.testing.allocator; - var cells: [8]pardes.Cell = @splat(.{}); +test "detached wire carries exact pointer metrics" { var logical: [12]pardes.Cell = @splat(.{}); - logical[10] = .{ .text = "z".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; var layer: pardes.Layer = .{ .id = 0, .serial = 42, .viewport = .{ .x = 0, .y = 0, .w = 2, .h = 4 }, .cols = 2, .rows = 6, .context_rows = 2, .cells = &logical, .cursor = .{ .x = 0, .y = 5, .bar = true } }; layer.context_separators.set(0); var tag_cells: [3]pardes.Cell = @splat(.{}); - tag_cells[2] = logical[10]; const tag: pardes.Layer = .{ .kind = .column, .id = 1, .viewport = .{ .x = 0, .y = 0, .w = 2, .h = 1 }, .cols = 3, .rows = 1, .bg = .{ 11, 22, 33 }, .cells = &tag_cells, .cursor = .{ .x = 2, .y = 0, .bar = true } }; - const placed = [_]pardes.Region{.{ .kind = .rail, .owner = 3, .serial = 42, .rect = .{ .x = 0, .y = 1, .w = 2, .h = 3 }, .active = true, .dirty = true, .thumb_y = 1, .thumb_h = 2, .clip = 1, .lift = 0.5 }}; - const out = try gpa.alloc(u8, frameBound(2, 4) + layersBound(&.{layer}, &.{tag}, &placed)); - defer gpa.free(out); - const palette: pardes.Chrome = .{ .page = .{ 1, 2, 3 }, .border = .{ 4, 5, 6 }, .tag_focus_bg = .{ 7, 8, 9 }, .scroll_thumb = .{ 10, 11, 12 }, .rule_px = 3, .tag_rule = .{ 13, 14, 15 }, .rail_px = 12, .grip_border = .{ 16, 17, 18 }, .grip_focus_ring = .{ 19, 20, 21 }, .grip_dirty = .{ 22, 23, 24 }, .lift_style = 2, .cursor_glide = 3, .cursor_alpha = 0.5, .cursor_idle_ms = 1234, .cursor_quad = .{ 1, 2, 3.5, 2, 3.5, 3, 1, 3 } }; - const encoded = try encodeFrameLayers(out, 2, 4, null, .link, &cells, &cells, &.{layer}, &.{tag}, &placed, palette); - const packet = (try framed(encoded)).?; - const frame = (try decodeServer(packet.tag, packet.payload)).frame; - var decoded: [pardes.MAX_PANES]pardes.Layer = @splat(.{}); - var tags: [pardes.MAX_TAG_LAYERS]pardes.Layer = @splat(.{}); - defer for (&tags) |*item| gpa.free(item.cells); - defer for (&decoded) |*item| gpa.free(item.cells); - try frame.apply(&cells); - var regions: std.ArrayListUnmanaged(pardes.Region) = .empty; - defer regions.deinit(gpa); - var chrome: pardes.Chrome = .{}; - try frame.applyLayers(gpa, &decoded, &tags, ®ions, &chrome); - try std.testing.expectEqualDeep(placed[0..], regions.items); - try std.testing.expectEqualDeep(palette, chrome); - try std.testing.expectEqual(@as(u16, 6), decoded[0].rows); - try std.testing.expect(decoded[0].context_separators.isSet(0)); - try std.testing.expect(!decoded[0].context_separators.isSet(1)); - try std.testing.expect(decoded[0].hasContextBorderAfter(0)); - try std.testing.expect(decoded[0].hasContextBorderAfter(1)); - try std.testing.expectEqualStrings("z", decoded[0].cells[10].grapheme()); - try std.testing.expectEqual(@as(u16, 5), decoded[0].cursor.?.y); - try std.testing.expectEqual(tag.kind, tags[0].kind); - try std.testing.expectEqual(tag.id, tags[0].id); - try std.testing.expectEqual(tag.bg, tags[0].bg); - try std.testing.expectEqual(tag.cols, tags[0].cols); - try std.testing.expectEqual(@as(u16, 2), tags[0].cursor.?.x); - try std.testing.expectEqualStrings("z", tags[0].cells[2].grapheme()); const pointer = layer.bodyHit(4, 27, 8, 16, 6, 8).?; const tag_pointer = tag.tagHit(11, 8, 8, 16, 4, 8).?; const message: ClientMsg = .{ .event = .{ .mouse = .{ .col = 0, .row = 1, .button = .left, .kind = .press, .body_hit = pointer, .tag_hit = tag_pointer } } }; @@ -1965,22 +721,4 @@ test "detached wire carries compact body rows and exact pointer metrics" { try std.testing.expectEqualDeep(pointer, mouse.body_hit.?); try std.testing.expectEqualDeep(tag_pointer, mouse.tag_hit.?); try std.testing.expect(mouse_encoded.len <= clientBound(message)); - var truncated = frame; - truncated.layers = frame.layers[0 .. frame.layers.len - 1]; - try std.testing.expectError(error.EndOfStream, truncated.applyLayers(gpa, &decoded, &tags, ®ions, &chrome)); - for (decoded) |item| { - try std.testing.expectEqual(@as(u16, 0), item.rows); - try std.testing.expectEqual(@as(usize, 0), item.cells.len); - try std.testing.expectEqual(@as(usize, 0), item.context_separators.count()); - } - for (tags) |item| { - try std.testing.expectEqual(@as(u16, 0), item.rows); - try std.testing.expectEqual(@as(usize, 0), item.cells.len); - } - try std.testing.expectEqual(@as(usize, 0), regions.items.len); - const no_layers = try encodeFrameLayers(out, 2, 4, null, .arrow, &cells, &cells, &.{}, &.{}, &.{}, .{}); - const cleared_packet = (try framed(no_layers)).?; - const cleared = (try decodeServer(cleared_packet.tag, cleared_packet.payload)).frame; - try cleared.applyLayers(gpa, &decoded, &tags, ®ions, &chrome); - try std.testing.expectEqual(@as(u16, 0), decoded[0].rows); } -- cgit v1.3 From 6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 16:55:47 -0300 Subject: the replica socket: the daemon listens on pardes-replica.sock, a stream `ssh -L` can forward, refuses another build's hello in words and hands the connection to its session, which welcomes it and answers pings The daemon binds `pardes-replica.sock` beside its own (SOCK_STREAM, so ssh can forward it; the SEQPACKET socket that carries fds stays local) once it holds its lock. A replica's first message is a hello (detached/wire.zig, now version 14): the protocol version at offset 0, the client's semantics hash and the session's name. The daemon reads exactly that much and no further, so whatever the client sends next is the session's, keeps up to 16 hellos still on their way (ponytail: one that never comes holds its slot until it hangs up), and refuses in words another protocol, a session nobody has, or another build: every session now says its own hash in its registration (daemon protocol 2), so a mismatch never reaches it. An accepted connection goes to the session over its link (SCM_RIGHTS) and the daemon keeps no copy; the session welcomes it with its pid and name on a thread of its own and answers each ping with a pong, until bye or a hangup. ponytail: that stub is all a session does with a replica until phase 5 proper; it is what times a round trip through the handoff and `ssh -L`. The semantics hash (wire.semantics) is phase 1's undone item: the protocol, the release and commit, the platform, the build options, every dependency the manifest pins by its content hash (grammars, ghostty, the Unicode tables: build.zig's new `deps` option) and the walker codec's assumptions (pointer width, byte order). Not the OS: a Linux client of a FreeBSD session is the main case. ponytail: an unstamped build has no commit, so two dev trees of one release with the same options are not told apart. `pardes --version` prints it on a second line. The hello lost its grid (a replica's view takes a resize like any other input). test/daemon.py: a hello of this build welcomed by the session itself (its pid, one fd more in the session and none in the daemon, and back once it hangs up), 50 pings, another build's hello and an unknown session's refused, and `--ping [n]`, which times a hello and n pings on any replica socket (a forwarded one, say). Co-Authored-By: Claude Opus 5.5 --- build.zig | 13 ++++ docs/typ/building.typ | 17 ++++-- src/CHANGELOG.md | 6 ++ src/daemon.zig | 166 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/detached/wire.zig | 157 ++++++++++++++++++++++++++++++++++++----------- src/main.zig | 6 +- test/daemon.py | 110 ++++++++++++++++++++++++++++++++- 7 files changed, 427 insertions(+), 48 deletions(-) (limited to 'src/detached/wire.zig') diff --git a/build.zig b/build.zig index 5e337dfc..24476753 100644 --- a/build.zig +++ b/build.zig @@ -13,6 +13,18 @@ const display_version = if (std.mem.endsWith(u8, zon.version, ".0") and std.mem. else zon.version; +/// Every dependency the manifest pins, by its content hash: a grammar, +/// ghostty or the Unicode tables steps a core differently, so they are part +/// of a replica's semantics hash (src/detached/wire.zig `semantics`). +const deps_text = blk: { + var text: []const u8 = ""; + for (@typeInfo(@TypeOf(zon.dependencies)).@"struct".field_names) |name| { + const dep = @field(zon.dependencies, name); + text = text ++ name ++ " " ++ (if (@hasField(@TypeOf(dep), "hash")) dep.hash else "path") ++ "\n"; + } + break :blk text; +}; + pub const Platform = enum { tty, gui, web, macos, esp32p4 }; const PerfBuild = struct { @@ -2120,6 +2132,7 @@ fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build. // ...and the commit it was built from, when there is one and the build // is stamped at all (see `stamp_commit`). Read at configure time. o.addOption(?[]const u8, "commit", cfg.commit); + o.addOption([]const u8, "deps", deps_text); return o; } diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 152be838..00088d7b 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -31,7 +31,8 @@ leaves that copy out. Zig 0.17 keeps the prefix from the build script, so so always pass `-Dplatform` while developing. Test, benchmark and run steps build what they need without installing. `pardes --version` prints `pardes ` (from `build.zig.zon`), plus the commit for release builds and the `~/.local` -install; `pardes --help` lists every flag. +install, and on a second line the build's semantics hash, which a replica +must share with its session; `pardes --help` lists every flag. The ESP32-P4 firmware is linked in the sibling `../05-zig-p4` toolchain, which needs ESP-IDF register headers: after building the editor object @@ -223,9 +224,17 @@ process; `pardes-gui --attach` opens one again. `PARDES_DAEMON=off` (the snapshot harness) runs a session as before the daemon: in the terminal's own process, ended by its hangup. -`src/detached/wire.zig` is the input half of the protocol a remote client -will speak (version 13): fixed-width little-endian fields, a 5-byte header -(tag, then a u32 length), the hello and its version, the input events. +A replica (a client running its own copy of the core, phase 5 of +`docs/next-daemon.md`) connects to `pardes-replica.sock`, a stream socket +beside it that `ssh -L` can forward. `src/detached/wire.zig` is its +protocol (version 14): fixed-width little-endian fields, a 5-byte header +(tag, then a u32 length), the hello, a ping, the input events. The hello +names a session and carries the client's semantics hash (the protocol, +release, commit, platform, build options and every dependency's hash); the +daemon refuses another build's, or a session nobody has, in words, and +hands an accepted connection to its session (`SCM_RIGHTS`), which welcomes +it and from then on is the one it talks to. Today the session answers +pings and nothing more. = 9P diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 10cc451d..27337781 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +- The daemon also listens on `pardes-replica.sock`, a stream socket `ssh + -L` can forward, for the replicated clients to come (docs/next-daemon.md, + phase 5): a hello names a session and the client's build, another + build is refused in words, and an accepted connection is handed to its + session, which so far only welcomes it and answers pings. + `pardes --version` prints that build's semantics hash on a second line. - One `pardes --daemon` per user keeps sessions, started by the first session and gone once it keeps nothing. `pardes` in a terminal forks its session off and stays as its stub: `Detach` (`SPC s D`), or a diff --git a/src/daemon.zig b/src/daemon.zig index ac9e6eb4..702d11b1 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -16,14 +16,22 @@ //! //! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it //! carries (SCM_RIGHTS). +//! +//! A replica (docs/next-daemon.md, phase 5) comes in on a stream socket of +//! its own, `pardes-replica.sock`, which `ssh -L` can forward: its hello +//! (detached/wire.zig) names a session, and once its build is the session's +//! the connection is handed to that session, which it talks to from then on. const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; const posix = std.posix; const ninep_io = @import("9p_io.zig"); +const wire = @import("detached/wire.zig"); +const log = std.log.scoped(.replica); pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; -const version = 1; +/// 2: a session's hello carries its semantics hash (wire.semantics). +const version = 2; pub const max_fds = 64; const msg_max = 4096; /// Idle this long, it exits: long enough for the session that started it to @@ -31,6 +39,9 @@ const msg_max = 4096; const linger_ms = 1000; const max_conns = 64; const max_held = 256; +/// Replicas whose hello is still on its way. ponytail: one that never sends +/// it holds its slot until it hangs up; past 16 a new one is closed at once. +const max_replicas = 16; const sun_path_len = ninep_io.sun_path_len; const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; @@ -258,7 +269,7 @@ fn spawn(listener: c_int, lock: c_int) bool { /// (`watch`) takes the `ok`, and only then is the session kept (`kept`). fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) bool { var buf: [256]u8 = undefined; - const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {s}", .{ version, k, pid, if (detached) "detached" else "attached", named }) catch return false; + const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {x} {s}", .{ version, k, pid, if (detached) "detached" else "attached", wire.semantics, named }) catch return false; return send(fd, words, &.{}); } @@ -376,6 +387,11 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no .stub = if (got.nfds >= 2) got.fds[1] else -1, } }); 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]); + } + 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 } }; const a = ¬e.adopted; @@ -395,6 +411,49 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no } } +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; +} + +/// 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); + 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 session ended by a signal (TERM, HUP, INT): its shells go with it, as /// at a quit. Async-signal-safe: one send. One killed outright (KILL) or /// crashed says nothing, and its shells live on for an Adopt. @@ -610,6 +669,8 @@ const Conn = struct { kind: ?Kind = null, pid: libc.pid_t = 0, detached: bool = false, + /// Its build's wire.semantics: a replica's must be the same. + semantics: u64 = 0, name: [128]u8 = undefined, name_len: usize = 0, @@ -625,9 +686,18 @@ const Held = struct { owner: ?u8 = null, }; +/// A replica's connection until its hello is whole: read no further than +/// the hello, so what follows it is the session's to read. +const Replica = struct { + fd: c_int = -1, + buf: [wire.hello_max]u8 = undefined, + len: usize = 0, +}; + const Daemon = struct { conns: [max_conns]Conn = @splat(.{}), held: [max_held]Held = @splat(.{}), + replicas: [max_replicas]Replica = @splat(.{}), fn idle(d: *const Daemon) bool { for (d.conns) |c| if (c.fd >= 0) return false; @@ -668,6 +738,7 @@ const Daemon = struct { const k = std.meta.stringToEnum(Kind, words.next() orelse ""); const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; const state = words.next() orelse ""; + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; const rest = std.mem.trim(u8, words.rest(), " "); if (v != version or k == null or pid <= 0) { // Another build's: it runs without a daemon. @@ -677,6 +748,7 @@ const Daemon = struct { c.kind = k; c.pid = pid; c.detached = std.mem.eql(u8, state, "detached"); + c.semantics = semantics; c.name_len = @min(rest.len, c.name.len); @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); return d.reply(i, "ok", &.{}); @@ -796,6 +868,58 @@ const Daemon = struct { return error.Said; } + /// More of a replica's hello; once it is whole, the connection refused + /// in words or handed to its session. + fn hear(d: *Daemon, r: *Replica) void { + const want = if (r.len < wire.header_len) wire.header_len else wire.header_len + @as(usize, std.mem.readInt(u32, r.buf[1..5], .little)); + if (want > r.buf.len) return refuse(r, "the first message is not a hello"); + const n = libc.read(r.fd, r.buf[r.len..want].ptr, want - r.len); + if (n < 0 and libc.errno(n) == .INTR) return; + if (n <= 0) { + _ = libc.close(r.fd); + r.* = .{}; + return; + } + r.len += @intCast(n); + if (r.len < wire.header_len or r.len < wire.header_len + std.mem.readInt(u32, r.buf[1..5], .little)) return; + const f = (wire.framed(r.buf[0..r.len]) catch null) orelse return refuse(r, "the first message is not a hello"); + if (f.tag != @backingInt(wire.ClientTag.hello)) return refuse(r, "the first message is not a hello"); + var why: [512]u8 = undefined; + const v = wire.helloVersion(f.payload) catch 0; + if (v != wire.version) return refuse(r, std.fmt.bufPrint(&why, "this client speaks replica protocol {d}, and this daemon {d}: run the same pardes at both ends", .{ v, wire.version }) catch "another protocol"); + const h = (wire.decodeClient(f.tag, f.payload) catch return refuse(r, "a malformed hello")).hello; + const target = for (&d.conns, 0..) |*c, i| { + if (c.fd >= 0 and c.kind != null and std.mem.eql(u8, c.named(), h.session)) break i; + } 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})) { + d.hangUp(@intCast(target)); + return refuse(r, "that session does not answer"); + } + log.info("a replica handed to '{s}'", .{h.session}); + _ = libc.close(r.fd); + r.* = .{}; + } + + fn refuse(r: *Replica, why: []const u8) void { + var out: [600]u8 = undefined; + // A fresh connection's buffer is empty: this never blocks. + if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {} + _ = libc.close(r.fd); + r.* = .{}; + } + + fn acceptReplica(d: *Daemon, listener: c_int) void { + const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); + if (fd < 0) return; + for (&d.replicas) |*r| if (r.fd < 0) { + r.* = .{ .fd = fd }; + return; + }; + _ = libc.close(fd); + } + fn accept(d: *Daemon, listener: c_int) void { const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); if (fd < 0) return; @@ -807,6 +931,21 @@ const Daemon = struct { } }; +/// The replicas' stream socket, bound and listening; -1 when it cannot be +/// (a runtime directory too long for a socket's path: 104 bytes on FreeBSD). +/// Ours to replace: the daemon's lock is held. +fn listenReplicas() c_int { + var sock_buf: [sun_path_len:0]u8 = undefined; + const sock = place(&sock_buf, "pardes-replica.sock") orelse return -1; + _ = libc.unlink(sock); + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (fd < 0) return -1; + const addr = address(sock); + if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) == 0 and libc.chmod(sock, 0o600) == 0 and libc.listen(fd, max_replicas) == 0) return fd; + _ = libc.close(fd); + return -1; +} + /// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the /// lock (fd 4). Returns only to exit, idle. pub fn serve() noreturn { @@ -815,22 +954,33 @@ pub fn serve() noreturn { ninep_io.setCloexec(4); const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null); + const replicas = listenReplicas(); var d: Daemon = .{}; - const slots = 1 + max_conns + max_held; + const slots = 2 + max_conns + max_replicas + max_held; var fds: [slots]libc.pollfd = undefined; var conn_of: [slots]u8 = undefined; var held_of: [slots]u16 = undefined; + var replica_of: [slots]u8 = undefined; var buf: [msg_max]u8 = undefined; while (true) { var n: usize = 0; fds[n] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 }; n += 1; + // -1 when there is none: poll skips a negative fd. + fds[n] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 }; + n += 1; const conns_at = n; for (&d.conns, 0..) |*c, i| if (c.fd >= 0) { fds[n] = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 }; conn_of[n] = @intCast(i); n += 1; }; + const replicas_at = n; + for (&d.replicas, 0..) |*r, i| if (r.fd >= 0) { + fds[n] = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 }; + replica_of[n] = @intCast(i); + n += 1; + }; const held_at = n; // No events asked of a master: its hangup (every slave closed, the // shell and all it ran gone) is reported anyway, and its output is @@ -858,7 +1008,12 @@ pub fn serve() noreturn { break; } if (fds[0].revents & libc.POLL.IN != 0) d.accept(listener); - for (fds[conns_at..held_at], conn_of[conns_at..held_at]) |pfd, i| { + if (fds[1].revents & libc.POLL.IN != 0) d.acceptReplica(replicas); + for (fds[replicas_at..held_at], replica_of[replicas_at..held_at]) |pfd, i| { + if (pfd.revents == 0 or d.replicas[i].fd != pfd.fd) continue; + d.hear(&d.replicas[i]); + } + for (fds[conns_at..replicas_at], conn_of[conns_at..replicas_at]) |pfd, i| { if (pfd.revents == 0 or d.conns[i].fd != pfd.fd) continue; const got = recv(pfd.fd, &buf) orelse { d.hangUp(i); @@ -874,6 +1029,9 @@ pub fn serve() noreturn { var sock_buf: [sun_path_len:0]u8 = undefined; _ = libc.close(listener); if (place(&sock_buf, "pardes-daemon.sock")) |sock| _ = libc.unlink(sock); + if (replicas >= 0) { + if (place(&sock_buf, "pardes-replica.sock")) |sock| _ = libc.unlink(sock); + } libc._exit(0); } diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 20d75274..097cbee7 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -1,13 +1,41 @@ -//! The framed stream a remote client will speak (docs/next-daemon.md, -//! phase 5): fixed-width little-endian fields and explicit tags, the hello -//! and its version, and the input Events. Native struct layout never reaches -//! the wire. The cell frames that went the other way, and the frontends that -//! drew them, went with phase 3. +//! The framed stream a replica speaks (docs/next-daemon.md, phase 5), on +//! the daemon's `pardes-replica.sock` and then its session's: fixed-width +//! little-endian fields and explicit tags, the hello and its version, a +//! ping, and the input Events. Native struct layout never reaches the wire. +//! The cell frames that went the other way, and the frontends that drew +//! them, went with phase 3. const std = @import("std"); +const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); - -/// 13: the frames, and every message a session sent, are gone (phase 3). -pub const version: u16 = 13; +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; + +/// 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. +/// 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}", + .{ + version, config.version, config.commit orelse "unstamped", + config.platform, 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), + builtin.cpu.arch.endian(), config.deps, + }, + )); +}; /// EndOfStream: the message ends early; WriteFailed: no room for it. /// ReadFailed is in only because std.Io.Reader says so: a fixed one never @@ -23,6 +51,7 @@ pub const header_len = 5; // tag:u8, payload length:u32le pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, + ping = 0x03, key = 0x10, mouse = 0x11, @@ -59,12 +88,50 @@ const KindTag = enum(u8) { press = 0x00, release = 0x01, motion = 0x02, drag = 0 /// when the rest of the layout is the part that changed. pub const Hello = struct { version: u16 = version, - /// This client's grid. Never zero: a client not sized yet must not - /// collapse a shared session (getCols). - cols: u16, - rows: u16, + /// The client's `semantics`: the daemon refuses it unless the session's + /// is the same. + semantics: u64 = semantics, + /// Which session (its 9P name, as `--attach=` names it). + session: []const u8, +}; + +/// 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; + +/// Daemon or session -> client. +pub const ServerTag = enum(u8) { + /// The hello is accepted and the connection is the session's now: its pid + /// and name. + welcome = 0x01, + /// Refused, said in words; the connection ends. + refused = 0x02, + /// A ping's value back. + pong = 0x03, }; +pub const ServerMsg = union(ServerTag) { + welcome: struct { pid: u32, session: []const u8 }, + refused: []const u8, + pong: u64, +}; + +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))); + switch (msg) { + .welcome => |h| { + try w.writeInt(u32, h.pid, .little); + try putSlice16(&w, h.session); + }, + .refused => |words| try putSlice16(&w, words), + .pong => |v| try w.writeInt(u64, v, .little), + } + try finishMessage(&w, at); + return w.buffered(); +} + /// `Hello.version` alone, read out of the still-undecoded payload. /// /// Separate from `decodeClient` because of the guarantee the fixed offset above @@ -88,6 +155,8 @@ pub fn helloVersion(payload: []const u8) Error!u16 { pub const ClientMsg = union(enum) { hello: Hello, bye, + /// A round trip's probe, answered `pong` with the same value. + ping: u64, event: pardes.Event, }; @@ -291,10 +360,12 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { switch (msg) { .hello => |h| { try w.writeInt(u16, h.version, .little); - try w.writeInt(u16, h.cols, .little); - try w.writeInt(u16, h.rows, .little); + try w.writeInt(u64, h.semantics, .little); + if (h.session.len > max_name) return error.Overlong; + try putSlice16(&w, h.session); }, .bye => {}, + .ping => |v| try w.writeInt(u64, v, .little), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -352,10 +423,15 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { const msg: ClientMsg = switch (std.enums.fromInt(ClientTag, tag) orelse return error.BadTag) { .hello => .{ .hello = .{ .version = try r.takeInt(u16, .little), - .cols = try getCols(&r), - .rows = try getRows(&r), + .semantics = try r.takeInt(u64, .little), + .session = blk: { + const n = try r.takeInt(u16, .little); + if (n > max_name) return error.BadValue; + break :blk try r.take(n); + }, } }, .bye => .bye, + .ping => .{ .ping = try r.takeInt(u64, .little) }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -415,7 +491,8 @@ const msg_slack = header_len + 64; /// once instead of guessing and retrying. pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { - .hello, .bye => 0, + .hello => |h| h.session.len, + .bye, .ping => 0, .event => |ev| switch (ev) { .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, .key => |k| k.text.len, @@ -458,12 +535,21 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqual(@as(u8, 0x1d), @backingInt(ClientTag.pointer_leave)); { - const got = try roundClient(&buf, .{ .hello = .{ .cols = 80, .rows = 24 } }); + const got = try roundClient(&buf, .{ .hello = .{ .session = "work" } }); try testing.expectEqual(version, got.hello.version); - try testing.expectEqual(@as(u16, 80), got.hello.cols); - try testing.expectEqual(@as(u16, 24), got.hello.rows); + 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.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. + 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.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); + } { const key: pardes.Key = .{ .cp = pardes.Key.page_down, .text = "ü", .ctrl = true, .alt = false, .shift = true }; @@ -530,20 +616,19 @@ test "detached wire: a session is never told to do a frontend's remembering" { // Adding a name here is meant to be an ARGUMENT, not a formality, and the // bar is one sentence: A HUMAN DID IT. A keystroke, a click, a pinch, a // window resized, a paste, a command line executed, a pointer leaving the - // window, the window's focus — plus the two session words that say who is speaking. A pty's + // window, the window's focus — plus the session words that say who is speaking, time the link and end it. A pty's // output, a worker's answer, a watched file's new bytes and an animation // tick all fail that bar the same way: nobody did them, a machine reported // them, and the machine that reports them is the one already holding the // core. const allowed = [_][]const u8{ - // Who this connection is, and that it is finished. - "hello", "bye", + // Who this connection is, its round trip, and that it is finished. + "hello", "bye", "ping", // ...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 @@ -663,18 +748,16 @@ test "detached wire: values a field cannot mean are refused" { @backingInt(ClientTag.pdf_scroll), &.{pardes.MAX_PANES}, )); - // A zero-column grid would collapse a shared session; an over-wide one is - // past what this protocol carries. - try testing.expectError(error.BadValue, decodeClient( - @backingInt(ClientTag.hello), - &.{ 1, 0, 0, 0, 24, 0 }, - )); + // A session name longer than the daemon keeps one: refused, never cut + // to another session's name. { - var hello: [6]u8 = undefined; + var hello: [12 + max_name + 1]u8 = @splat('x'); std.mem.writeInt(u16, hello[0..2], version, .little); - std.mem.writeInt(u16, hello[2..4], max_cols + 1, .little); - std.mem.writeInt(u16, hello[4..6], 24, .little); + 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); } // A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps. { diff --git a/src/main.zig b/src/main.zig index 378dbea8..f310bf3c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -288,10 +288,12 @@ fn badArgs(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn { std.process.exit(1); } -const version_text = if (pardes.commit) |c| +/// The second line is what a replica's build must share with its session's +/// (detached/wire.zig `semantics`). +const version_text = (if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")\n" else - "pardes " ++ pardes.version ++ "\n"; + "pardes " ++ pardes.version ++ "\n") ++ std.fmt.comptimePrint("semantics {x:0>16}\n", .{@import("detached/wire.zig").semantics}); const nested_text = \\pardes: this shell is already inside pardes, and a pardes inside a pardes diff --git a/test/daemon.py b/test/daemon.py index 41cfbaf7..2be3f355 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -6,16 +6,24 @@ private HOME and runtime directory: from another terminal, N times; - 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; - nothing left behind: no session, shell, daemon or socket. python3 -B test/daemon.py [cycles] python3 -B test/daemon.py --gui [cycles] (a hidden window; needs a Wayland display, and a pardes beside it for the refusal) + 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 """ import fcntl import os import shutil import signal +import socket +import statistics import struct import subprocess import sys @@ -153,6 +161,103 @@ def fds(pid): return -1 +def semantics(binary): + """What a replica's build must share with its session's (--version).""" + out = subprocess.run([binary, '--version'], capture_output=True, check=True).stdout.decode() + return int(next(line.split()[1] for line in out.splitlines() if line.startswith('semantics ')), 16) + + +class Replica: + """A replica's connection (src/detached/wire.zig): length-prefixed + messages, a tag byte and a little-endian u32 length.""" + WIRE = 14 + + def __init__(self, path, timeout=10): + self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.s.settimeout(timeout) + self.s.connect(os.fspath(path)) + + def send(self, tag, payload): + self.s.sendall(struct.pack(' 5 else 50) + elif '--gui' in sys.argv: args = [a for a in sys.argv[1:] if a != '--gui'] gui(args[0], int(args[1]) if len(args) > 1 else 50) else: -- 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/detached/wire.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 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/detached/wire.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 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/detached/wire.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/detached/wire.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 f7a0b6d902f001cff0343e11263868c5356f33c6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 15:57:57 -0300 Subject: tape: an error crosses by its name, a stat as fs.OsStat, and the tape's shape is in wire.semantics: a replica of another build read its session's units as its own build's, so a window client of a terminal's session, or of the box's, read every error by a number its build gives another (451 of 505 names numbered otherwise in the Linux terminal's and window's builds, 244 between Linux and FreeBSD: pardes-gui replaying a terminal's tape panicked at `unexpected error code, found error.NonLocalPath`, and so did main's window client of a session on the box at its Look of a fifo) and a FreeBSD stat out of step (nlink 8 bytes there, 4 here); a name this build's set lacks is a divergence (error.NoSuchError), never another error; an anyerror (a host's report) reads back as one of tape.Failure's, a Debug build panicking at one outside it as it is taped; tape.shape (each taped type's fields, tags, enum names and numbers) in semantics refuses, in words, builds whose tapes differ (the stat taped as before: the Linux and FreeBSD builds hash to two), and a site whose result tape.results does not list is a compile error; walk's test of an error by its name Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 14 ++++-- src/fs.zig | 16 ++++-- src/ninep/pty.zig | 2 +- src/tape.zig | 52 ++++++++++++++++++++ src/terminal.zig | 2 +- src/walk.zig | 132 ++++++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 199 insertions(+), 19 deletions(-) (limited to 'src/detached/wire.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 119601fc..1c9e554e 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -29,9 +29,13 @@ const family = switch (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. +/// hash (grammars, ghostty, Unicode), the walker codec's assumptions +/// (pointer width, byte order) and the tape's shape (tape.shape: what each +/// input and each answer of the host's is laid out as). Not the OS: a Linux +/// client of a FreeBSD session is the main case, and what differs there is +/// the host's, taped, in a shape every OS shares (an error by its name, a +/// stat as fs.OsStat). Nor the build mode: a Debug build steps as a +/// release one does (regexp.zig's one budget). /// 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. @@ -41,14 +45,14 @@ const family = switch (config.platform) { 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{s} 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}\n{s}", .{ version, config.version, config.commit orelse "unstamped", 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), - builtin.cpu.arch.endian(), config.deps, + builtin.cpu.arch.endian(), config.deps, pardes.tape.shape, }, )); }; diff --git a/src/fs.zig b/src/fs.zig index 4bbf09bb..733418da 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -171,8 +171,8 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .release) return .{ .tag = req.tag }; const io = std.Io.Threaded.global_single_threaded.io(); - const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT); - const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; + const stat = pardes.tape.site(.os_stat, null, osStat, .{ io, path }) catch return Reply.fail(req.tag, E.NOENT); + const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = stat.mtime }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, .open => { @@ -223,6 +223,16 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { } } +/// What /os keeps of a stat, as the tape carries it to a replica: the same +/// bytes on every OS, which std.Io.File.Stat is not (a FreeBSD one's +/// `nlink` and `permissions` are another width than a Linux one's). +pub const OsStat = struct { kind: std.Io.File.Kind, size: u64, mtime: u32 }; + +fn osStat(io: std.Io, path: []const u8) !OsStat { + const stat = try statPath(io, path, .{}); + return .{ .kind = stat.kind, .size = stat.size, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; +} + /// A directory's listing under /os, the entries from `off` until `size` /// bytes of them are staged, into `listing`: the syscalls of a readdir. fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 { @@ -249,7 +259,7 @@ fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * return listed.items; } -const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; +pub const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; /// The syscalls of an /os read, write or truncate: the reply, or the bytes /// read into `buf`. diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig index e68ad84f..53fc5c68 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -411,7 +411,7 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void { answer(p, slot, "exit {s}", .{status}); } -const Printed = struct { text: []const u8, whole: bool }; +pub const Printed = struct { text: []const u8, whole: bool }; /// What pane `pane`'s command printed (terminal.commandOutput), in `arena`. fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed { diff --git a/src/tape.zig b/src/tape.zig index 9f1cde42..3c8dd765 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -20,6 +20,8 @@ const std = @import("std"); const pardes = @import("pardes.zig"); const walk = @import("walk.zig"); +const host_io = @import("host_io.zig"); +const terminal = @import("terminal.zig"); const Pardes = pardes.Pardes; const VTable = pardes.Host.VTable; const libc = std.c; @@ -107,6 +109,50 @@ pub const Site = enum(u16) { prompt, }; +/// What the sites return. wire.semantics hashes their shape with the +/// rest of the tape's (`shape`), so two builds whose tapes differ in it +/// refuse each other's replicas in words, and a site returning a type not +/// listed is a compile error (`call`). An error union is listed as +/// `anyerror!` its payload: an error goes by its name (walk.encode). +pub const results = .{ + void, bool, + u32, i64, + [8]u8, ?u64, + []const u8, ?[]const u8, + ?std.Io.File.Kind, ?pardes.Presentation.CellPosition, + ?pardes.filesystem.Resolved, pardes.filesystem.OsReadWrite, + anyerror!pardes.filesystem.OsStat, anyerror![]const u8, + anyerror![]u8, anyerror!void, + terminal.Told, ?pardes.ctlfs.pty.Printed, + @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, +}; + +/// What a host reports to the core as failed (`shell_failed`, `save_failed`, +/// `dump_failed`, `report_error`), as a replica reads it back: an error is +/// taped by its name, and `anyerror` keeps no list of names to look one up +/// in. ponytail: kept by hand, the sets of what a host reports from; one +/// outside them is a Debug build's panic as it is taped (walk.encode) and a +/// release build's divergence (the replica joins again). No host, none. +pub const Failure = if (!pardes.hosted) error{} else ErrorOf(host_io.forkShell) || ErrorOf(pardes.filesystem.write) || + ErrorOf(pardes.filesystem.readRestore) || ErrorOf(pardes.dump.restore) || + @typeInfo(@TypeOf(pardes.config.User.dumpThemes(undefined, undefined, undefined, pardes.themes))).error_union.error_set || + ErrorOf(host_io.Lsp.snapshot) || + ErrorOf(host_io.Inbox.startReader) || std.Io.File.ReadStreamingError || + error{ NoDumpDirectory, ListenFailed, WorkersUnavailable, WorkersBusy }; + +fn ErrorOf(comptime f: anytype) type { + return @typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?).error_union.error_set; +} + +/// Every type the tape carries, as walk.shape says it: wire.semantics's. +pub const shape = blk: { + var types: []const type = &.{ Input, []const u64, struct { u8, u64 } }; + for (results) |R| types = types ++ .{R}; + for (host_fns) |f| types = types ++ .{@typeInfo(@typeInfo(@typeInfo(@FieldType(VTable, f)).optional.child).pointer.child).@"fn".return_type.?}; + for (watched) |name| types = types ++ .{@FieldType(Pardes, name)}; + break :blk walk.shape(types); +}; + /// Fields a host writes straight into the core between its calls. const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; @@ -769,6 +815,12 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty 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 { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } // No tape where there is no host to record or replay one. if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); diff --git a/src/terminal.zig b/src/terminal.zig index acff68f4..63ce4f36 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -1188,7 +1188,7 @@ fn pointerRowCount(pane: *const Pane) usize { /// 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 }; +pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; fn told(t: Told) Told { return t; diff --git a/src/walk.zig b/src/walk.zig index 1c5bbbfc..88f62736 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -388,9 +388,14 @@ fn hashMap(comptime T: type, v: *const T, h: *Hasher) void { h.update(std.mem.asBytes(&sum)); } -/// The value as bytes, the same walk: what the tape carries (src/tape.zig). -/// Only for the process that wrote it, or one of the same build: native -/// byte order, and an error as its number in this build. +/// The value as bytes, the same walk: what the tape carries (src/tape.zig), +/// to a replica of another build too (a window of a terminal's session, a +/// Linux client of a FreeBSD one). Native byte order, which every machine +/// pardes runs on shares, and an error by its name: its number is its +/// build's own (a Linux terminal's FileNotFound is a window's +/// NoDefaultValue). What a field is laid out as (a std.Io.File.Stat's +/// `nlink` is another width on FreeBSD) is the tape's shape, which +/// wire.semantics hashes (`shape`). pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Error!void { switch (@typeInfo(T)) { .void, .null, .undefined => {}, @@ -424,18 +429,91 @@ pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Er inline else => |*payload| try encode(@TypeOf(payload.*), payload, w), } }, - .error_set => try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(v.*)))), + .error_set => try encodeError(T, v.*, w), .error_union => |e| if (v.*) |*payload| { try w.writeByte(1); try encode(e.payload, payload, w); } else |err| { try w.writeByte(0); - try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(err)))); + try encodeError(e.error_set, err, w); }, else => @compileError("walk: cannot encode " ++ @typeName(T)), } } +/// What `encode` lays each of `types` out as, in words: each field, element +/// and tag where it goes, an enum's names and numbers, an error as an +/// error (it goes by its name). wire.semantics hashes the tape's: builds +/// whose tapes differ in shape, as a FreeBSD std.Io.File.Stat differs from +/// a Linux one, refuse each other's replicas in words, not read each other +/// out of step. +pub fn shape(comptime types: []const type) []const u8 { + comptime { + @setEvalBranchQuota(10_000_000); + var out: []const u8 = ""; + for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n"; + return out; + } +} + +fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { + comptime { + if (depth > 32) @compileError("walk: the tape's shape nests past 32 deep at " ++ @typeName(T) ++ ": a type that holds itself?"); + return switch (@typeInfo(T)) { + .void, .null, .undefined => "v", + .bool => "b", + .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }), + .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), + .@"enum" => |e| blk: { + var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; + for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v }); + break :blk out ++ "}"; + }, + .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), + .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1), + .pointer => |p| switch (p.size) { + .one => shapeOf(p.child, depth + 1), + .slice => "[]" ++ shapeOf(p.child, depth + 1), + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| blk: { + if (machinery(T)) break :blk ""; + var out: []const u8 = if (s.layout == .@"packed") "p{" else "{"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or excluded(T, name)) continue; + out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + } + break :blk out ++ "}"; + }, + .@"union" => |u| blk: { + var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "("; + for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + break :blk out ++ ")"; + }, + .error_set => "error", + .error_union => |e| "!" ++ shapeOf(e.payload, depth + 1), + else => @compileError("walk: cannot encode " ++ @typeName(T)), + }; + } +} + +/// An error by its name. An `anyerror` must be one `decode` can read back +/// (`errorNamed`): a Debug build stops at one that is not, as it is taped. +fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void { + if (comptime E == anyerror and builtin.mode == .debug) if (errorNamed(E, @errorName(err)) == null) + std.debug.panic("walk: error.{s} is taped as an anyerror, which a replica reads back only as one of tape.Failure's: add the set it comes from", .{@errorName(err)}); + try writeName(w, @errorName(err)); +} + +/// The error of `E` called `name`. `anyerror` keeps no list of its names +/// to look one up in: one is read back as one of tape.Failure's, what a +/// host reports as failed (the tape's only anyerrors). +fn errorNamed(comptime E: type, name: []const u8) ?E { + const Named = if (E == anyerror) @import("tape.zig").Failure else E; + inline for (@typeInfo(Named).error_set.error_names.?) |n| if (std.mem.eql(u8, name, n)) return @field(Named, n); + return null; +} + pub const Reader = struct { bytes: []const u8, at: usize = 0, @@ -451,9 +529,13 @@ pub const Reader = struct { } }; +/// What an error read back is when this build has none of that name (an +/// OS's own, a newer build's): the tape is another build's, and diverges. +pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError }; + /// The inverse of `encode`; slices (and what a pointer points at) are made /// in `a`. A field `encode` left out is its default. -pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTape, OutOfMemory }!T { +pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T { switch (@typeInfo(T)) { .void, .null, .undefined => return {}, .bool => return (try r.take(1))[0] != 0, @@ -500,7 +582,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa @field(out, name) = @as(*const F, @ptrCast(@alignCast(default))).*; } else if (comptime @typeInfo(F) == .error_set) { // No `!anyerror` to return it through: read in place. - @field(out, name) = @errorCast(@errorFromInt(try r.scalar(u16))); + try decodeError(F, r, &@field(out, name)); } else @field(out, name) = try decode(F, r, a); } return out; @@ -512,17 +594,24 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa }; unreachable; }, - .error_set => return @errorCast(@errorFromInt(try r.scalar(u16))), .error_union => |e| { if ((try r.take(1))[0] != 0) return try decode(e.payload, r, a); // The value's error, not decode's own. - const err: e.error_set = @errorCast(@errorFromInt(try r.scalar(u16))); + var err: e.error_set = undefined; + try decodeError(e.error_set, r, &err); return @as(T, err); }, else => @compileError("walk: cannot decode " ++ @typeName(T)), } } +/// An error by its name, as `encode` wrote it, into `out`: no error union +/// holds an error set. +fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void { + const len = try r.scalar(u16); + out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError; +} + test "a value comes back from its bytes" { const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 }; var buf: [256]u8 = undefined; @@ -536,6 +625,31 @@ test "a value comes back from its bytes" { try std.testing.expectEqual(r.bytes.len, r.at); } +test "an error is taped by its name, which another build reads back as its own" { + // The build's number for an error is its own: 451 of 505 names were + // numbered otherwise in a Linux window's build than a terminal's, so a + // window replica read a terminal session's FileNotFound as another. + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir; + try encode(@TypeOf(failed), &failed, &w); + try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered()); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator())); + // A name this build's set lacks (an OS's own): the tape is another + // build's, and the replay diverges rather than read some other error. + r = .{ .bytes = "\x00\x0c\x00FileTooLarge" }; + try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator())); + // An anyerror (a host's report) is read back as one of tape.Failure's. + const Report = struct { err: anyerror }; + w = .fixed(&buf); + try encode(Report, &.{ .err = error.FileNotFound }, &w); + r = .{ .bytes = w.buffered() }; + try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); +} + test "a digest is of values: the same text at two addresses is one digest" { const S = struct { name: []const u8, n: u32, next: ?*const u32 }; const a = "same text"; -- 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/detached/wire.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 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/detached/wire.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 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/detached/wire.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('