From 4ffe5c93b3087f245f5b57a822ab0ecf41cc085c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 10:48:56 -0300 Subject: sessions register with the daemon: a terminal's session forked off its stub, Detach and --attach in place for terminals and windows, pty masters kept, Adopt pardes in a terminal reaches (or starts) the daemon, opens /dev/tty and forks: the child is the session, in a session of its own (setsid), drawing on that terminal (tty.Term.fd, a raw vaxis Tty made from the fd); the parent stays as its stub, the job the shell waits on. Not the daemon spawning it from a passed fd, env and argv: the user's terminals each run in a 9ns user and mount namespace, which a session spawned by a per-user daemon would not share, and a fork costs one small process's page tables where the spawn cost an IPC and an exec. GUI windows and --detach sessions just register. tty: Detach (and a terminal's end: EOF, EIO, a hangup the stub passes on, taken on the signal thread) puts the terminal back, drops it and the stub's stdio, tells the stub (which ends, exit 0), and leaves the session running with nothing drawn (Host.watched), its messages on the clock and `size` its own. pardes --attach[=name] hands its terminal through the daemon; the session enters it as the first one, sized, queried and drawn whole, and that process is its stub until it leaves. A hangup never ends a kept session; TERM and INT do, telling the daemon so the shells go with it (one async-signal-safe send). gui: Detach closes the window (its swapchain or renderer) and keeps the device, atlas and pipelines; the loop sleeps on SDL's queue; pardes-gui --attach[=name] opens a window again. Closing a window still quits. --detach is a session started detached: the tty host with no terminal yet, the GUI with a hidden window closed at once. forkShell sends the daemon a dup of every master and the hosts tell it what they close, so a session killed outright leaves its shells running; the new Adopt word takes them in as terminal panes (the Tty word's pane, its spawn taking the pty, a 1x1 size then the pane's to make it redraw). The Attach word, which switched a session's own terminal to a detached session through the frames, goes, with its effect and request in the core. PARDES_DAEMON=off (the e2e harness's) runs a session as before. test/daemon.py: 50 detach/attach cycles at a level fd count with no stale socket, kill -9 and Adopt, and nothing left; --gui the same for a window. test/fs.py: a forked session's socket is found by its own pid, and a hangup leaves a kept session running. Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 72 ++-------------------------------------------------------- 1 file changed, 2 insertions(+), 70 deletions(-) (limited to 'src/pardes.zig') diff --git a/src/pardes.zig b/src/pardes.zig index c7ae6704..2bee1f98 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -538,53 +538,8 @@ fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 { return if (len) |n| buf[0..n] else null; } -fn drainForAttach(p: *Pardes) ?AttachRequest { - while (p.nextEffect()) |effect| p.perform(effect); - return p.takeAttach(); -} - -test "Attach asks for a session and tears nothing down" { - if (comptime !can_attach) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - - // Bare means whichever session is there, and that is an EMPTY name rather - // than an absent request: the frontend still has to go and look. - try std.testing.expect(p.executeBuiltinLine(0, "Attach")); - const bare = drainForAttach(p) orelse return error.NoAttachAsked; - try std.testing.expectEqualStrings("", bare.name); - try std.testing.expectEqual(@as(u8, 0), bare.pane); - - // ...and the named form carries its tail, the way `Theme ` does. - try std.testing.expect(p.executeBuiltinLine(0, "Attach work")); - const named = drainForAttach(p) orelse return error.NoAttachAsked; - try std.testing.expectEqualStrings("work", named.name); - try std.testing.expect(p.takeAttach() == null); // taken once, then gone - - // The word only ASKS. A core that tore itself down here could not be handed - // back intact when the connect fails, and that is the entire guarantee. - try std.testing.expect(!p.quit); - try std.testing.expect(p.panes[0] != null); -} - -test "Attach in a detached session: bare is wrong #args, a name no session has says so, and nothing is asked" { - if (comptime !can_attach) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .detached_session = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = p.panes[0].?; - try std.testing.expect(p.executeBuiltinLine(0, "Attach")); - try std.testing.expect(drainForAttach(p) == null); - try std.testing.expectStringStartsWith(pane.msg[0..pane.msg_len], "wrong #args"); - try std.testing.expect(p.executeBuiltinLine(0, "Attach zz-no-such-session-qq")); - try std.testing.expect(drainForAttach(p) == null); - try std.testing.expectEqualStrings("Attach: zz-no-such-session-qq: no such session", pane.msg[0..pane.msg_len]); -} - -test "Detach asks the frontend to leave, and says so when there is nothing to leave" { - if (comptime !can_attach) return; // compiled out where nothing polls `takeAttach` — see `can_attach` +test "Detach asks the host to let its terminal go, and says so when there is nothing to leave" { + if (comptime !can_attach) return; // the word is compiled out elsewhere: see `can_attach` const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); @@ -3753,10 +3708,6 @@ pub const Event = union(enum) { tick, }; -pub const attach_name_max = 256; - -pub const AttachRequest = struct { pane: u8, name: []const u8 }; - pub const PendingWrite = struct { bytes: []u8, off: usize = 0 }; /// `command` is Kill's: SIGTERM to the terminal's foreground job, and never @@ -3807,7 +3758,6 @@ pub const Effect = union(enum) { /// Write the build-time theme ring below the per-user config directory. /// The pane receives the completion/error message from the native host. dump_themes: struct { pane: u8 }, - attach: struct { pane: u8, name: Buf(attach_name_max) }, detach: struct { pane: u8 }, quit, @@ -4387,9 +4337,6 @@ pub const Options = struct { /// window opens one as big as that, Handover.zig). geometry: ?[]const u8 = null, nested: bool = false, - /// The core of a `--detach` session (detached/server.zig): it has no - /// terminal of its own, so Attach there is refused with why. - detached_session: bool = false, startup_config: ?[]const u8 = null, ninep_name: []const u8 = "", ninep_tcp: ?[]const u8 = null, @@ -4738,8 +4685,6 @@ pub const Pardes = struct { /// shell consumes it via takeRestore each frame (restore contents stay host-fed) restore_req: ?[]const u8 = null, restore_buf: [1024]u8 = undefined, - attach_req: ?AttachRequest = null, - attach_buf: [attach_name_max]u8 = undefined, surface: Surface = .{}, /// per-update scratch (paneCursorLines, selection text); reset each update @@ -5088,14 +5033,6 @@ pub const Pardes = struct { return r; } - /// the shell polls this each frame beside takeRestore: a pending Attach's - /// session and reporting pane, or null - pub fn takeAttach(p: *Pardes) ?AttachRequest { - const a = p.attach_req; - p.attach_req = null; - return a; - } - /// The workspace tag's height: one row while the setting keeps it, as /// acme's row tag is, zero once a native shell takes its commands over. /// Layout and hit testing read this rather than the constant, the way @@ -5981,11 +5918,6 @@ pub const Pardes = struct { }, .theme_file => |t| if (v.watch_theme) |f| f(p.host.ctx, t.generation, t.on), .dump_themes => |d| if (v.dump_themes) |f| f(p.host.ctx, d.pane), - .attach => |a| { - const name = a.name.slice(); - @memcpy(p.attach_buf[0..name.len], name); - p.attach_req = .{ .pane = a.pane, .name = p.attach_buf[0..name.len] }; - }, .detach => |d| if (v.detach) |f| f(p.host.ctx) else -- cgit v1.3 From 044da0f75ab5fb5be0213e512741860d95dad8e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 11:14:38 -0300 Subject: tape: every input the core consumes recorded in turn order, replayed into replicas that must reach the same digests; the core's leaks of render, clock and host closed The recorder (src/tape.zig) is null unless PARDES_RECORD= names a tape (.tape); the local path pays a null check a call. It records a tree at the host boundary, both ways: each call the host makes into the core (`Input`: update, serveFs, pump, perform, the shell's acknowledgements and failures, a Restore, ...) with its arguments, and each call the core makes out -- a Host function (the vtable wrapped generically while recording) or a syscall of its own (`tape.site` for every old `turn.yield()`, `tape.value` for a clock or environment read) -- with its result; `done`/`ret` close each, a 9P request served while a step is out lands inside that step's syscall, and fields a host writes between calls (native_images, present_skipped, ...) are found by a diff at every crossing. After each call the host made at the top, the session's digest (src/walk.zig), one per field of the core, its stream and each pane, file and places. `PARDES_REPLAY= pardes` (main.zig) replays it with the same build into three replicas: two at different addresses (the first's memory held while the second runs) and one that never draws. A host call answers what it answered, a site returns what it returned, nothing is performed and no syscall is made; `turn.yield` panics in a replica, so a syscall no site covers is a stack trace; a call the recording did not make, or a digest that differs, is a divergence that names the fields. The headless replica steps over the presents it does not make, applying what the host told the core inside them. What the replicas found, fixed: - Render wrote core state. The view's geometry (wrap map, context rows, body layer rows) is File.geometry, a lazy cache keyed by a hash of what it is made of (revision, scroll, size, tags, tables read, context declarations, the session's view: wrap, layered context, row metrics, rect); the render makes it afresh, a step reading it makes it on demand (wrapAt, wrapRow, ensureCursorVisible). Context declarations, notice bands and the header's reveal are the step's (sync), the tables' reading the tick's (a first look of 4096 lines, a count, wherever it is asked; the rest a frame's clock budget a tick), and a view's paint-ahead is asked of the host as the frame is drawn rather than queued as an effect. - The clock: advance's wakes are the recording session's (a picture decoding or a fade adds no tick to a replica), as are the budgets of Places, Names and Tables (`tape.over`), a message's time of day (the stamp; msg_said_ms and the unattended expiry use the session clock), File.mtime, and the pump reads its clock whether or not it waits. - The host: the session directory (getcwd, asked once at init), $SHELL and its existence, $HOME for `~`, `Shell` naming one that is not there, a missing interpreter, a directory refused above, a deleted file, a pointer seen through a sliding pane (Pardes.pointerCell). - Worker handles (futures, the Io), the 9P server's state, the store file every window shares (a replica keeps its own in memory) and Recent read from it are authority-only; frames, animations and the render's caches local. Query rows' paints count apart from a view's. Terminals replay from their taped pty bytes and are digested as their active screen, cursor and scroll; kitty images are left out until phase 5's screen-state terminals. tagnav.golden: the header's caret reveal now runs every step, as a pane's does, not once per frame drawn, so after `g l B B` typed in one burst the bar stays scrolled as a human typing it sees it. Co-Authored-By: Claude Opus 5.5 --- src/Dir.zig | 8 +- src/File.zig | 106 ++++++- src/Messages.zig | 69 ++-- src/Names.zig | 6 +- src/Output.zig | 3 +- src/Places.zig | 8 +- src/Query.zig | 6 +- src/Stream.zig | 82 ++++- src/Tables.zig | 41 ++- src/Text.zig | 4 + src/body_layer.zig | 25 +- src/builtins.zig | 12 +- src/config.zig | 5 + src/draw.zig | 21 +- src/dump.zig | 13 +- src/exec.zig | 13 +- src/fs.zig | 143 +++++---- src/image.zig | 4 + src/look.zig | 2 +- src/main.zig | 2 + src/mouse.zig | 5 +- src/ninep/pane.zig | 3 +- src/ninep/tree.zig | 2 + src/normal.zig | 1 + src/panes.zig | 29 +- src/pardes.zig | 159 +++++++++- src/pdf.zig | 3 + src/pdf_view.zig | 4 + src/selection_pipe.zig | 1 + src/tape.zig | 741 +++++++++++++++++++++++++++++++++++++++++++ src/terminal.zig | 24 ++ test/panes.zig | 11 +- test/snapshots/tagnav.golden | 4 +- 33 files changed, 1341 insertions(+), 219 deletions(-) create mode 100644 src/tape.zig (limited to 'src/pardes.zig') diff --git a/src/Dir.zig b/src/Dir.zig index b994bc06..ebe6b69c 100644 --- a/src/Dir.zig +++ b/src/Dir.zig @@ -53,6 +53,10 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 { var own: [4096]u8 = undefined; if (local.len > own.len) return error.NameTooLong; @memcpy(own[0..local.len], local); + return pardes.tape.site(.dir_listing, gpa, readHost, .{ gpa, own[0..local.len] }); +} + +fn readHost(gpa: std.mem.Allocator, local: []const u8) ![]u8 { var entries: std.ArrayList([]u8) = .empty; defer { for (entries.items) |entry| gpa.free(entry); @@ -60,10 +64,8 @@ pub fn read(gpa: std.mem.Allocator, dir: []const u8) ![]u8 { } var total: usize = 0; { - pardes.turn.yield(); - defer pardes.turn.back(); const io = std.Io.Threaded.global_single_threaded.io(); - var d = try std.Io.Dir.cwd().openDir(io, own[0..local.len], .{ .iterate = true }); + var d = try std.Io.Dir.cwd().openDir(io, local, .{ .iterate = true }); defer d.close(io); var it = d.iterate(); while (try it.next(io)) |entry| { diff --git a/src/File.zig b/src/File.zig index e4bca000..d795eaa1 100644 --- a/src/File.zig +++ b/src/File.zig @@ -104,6 +104,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void { } pub const History = struct { + pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } }; undo: [limits.undo_max]Snapshot = undefined, undo_len: usize = 0, redo: [limits.undo_max]Snapshot = undefined, @@ -137,6 +138,10 @@ pub const History = struct { /// A file pane's backing: owned content, its derived caches, and undo history. pub const State = struct { + /// What the replay digest leaves out (src/walk.zig): what is made again + /// from the text whenever it is wanted. + pub const replica = .{ .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } }; + path: []u8, content: []u8, revision: u32 = 0, @@ -1252,7 +1257,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) { f.saved_revision = f.revision; }; - f.mtime = pardes.ctlfs.events.now(); + // The clock the session that recorded read (src/tape.zig). + f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{}); if (f.line_starts.len > 0) p.gpa.free(f.line_starts); f.line_starts = starts; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); @@ -1647,6 +1653,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void { /// ` deleted on disk (Save writes it again)`, logged `changed /// deleted`. pub fn deleted(p: *Pardes, id: u8) void { + const taped = pardes.tape.enter(p, .{ .file_deleted = id }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; const f = if (pane.file) |*file| file else return; if (f.disk_gone) return; @@ -1703,6 +1711,17 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { /// re-highlight the visible window of any file whose syntax went stale /// (edit, scroll, load) — visible-range-first so big files stay snappy +/// The declarations a TreeContext pane's context rows are drawn from, for +/// its text as it is now: found in the step (Pardes.sync), never only by a +/// render, since the view's geometry is made of them (`geometry`). +pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { + if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return; + const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return; + if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); + f.context_declarations = declarations; + f.context_revision = f.revision; +} + pub fn refreshHighlights(p: *Pardes) void { const tz = tracy.zone(@src(), "refreshHighlights"); defer tz.end(); @@ -1710,12 +1729,6 @@ pub fn refreshHighlights(p: *Pardes) void { const pane = slot orelse continue; if (pane.file == null) continue; const f = &pane.file.?; - if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) { - const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue; - if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); - f.context_declarations = declarations; - f.context_revision = f.revision; - } if (f.mini != null) { f.syntax_dirty = false; continue; @@ -1907,18 +1920,79 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow { } } -pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 { - const width = wrapWidth(pane, wrap); +/// The body's text as it is shown: its geometry's rows, each a line's +/// number and its stretch of the line. +pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 { + geometry(pane); + const out = try arena.alloc(u8, pane.geometry_len); + const filled = fillBody(out, pane, f, wrapWidth(pane, pane.view.wrap), false); + std.debug.assert(filled == out.len); + return out; +} + +/// The pane's view geometry for its state as it is now: the wrap map (the +/// line, column and pad each body row starts at), the context rows, and how +/// many rows the body layer has. Made again when anything it is made of +/// changes (`geometryKey`), by whoever asks first, the render or a step that +/// reads it: so a core that never draws has the geometry of one that does +/// (docs/next-daemon.md). What it reads of the session is the pane's `view`, +/// which each step sets. +pub fn geometry(pane: *Pane) void { + const f = if (pane.file) |*file| file else return; + const key = geometryKey(pane, f); + if (pane.geometry_key == key) return; + pane.geometry_key = key; + pane.body_rows = 0; + pane.body_visible_rows = 0; + pane.context_row_limit = null; + pane.geometry_len = mapRows(pane, f); + // A body with context rows is a layer taller than the grid's rows, its + // context rows at the tagline's height (body_layer.joinBody). + const v = pane.view; + if (!v.layered or pane.collapsed or v.rect_h <= pane.tag_rows) return; + const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); + if (pane.context_rows == 0 or logical_rows <= pane.context_rows) return; + pane.body_rows = @intCast(logical_rows); + const bh: usize = @max(1, v.body_h); + const th: usize = std.math.clamp(v.tagline_h, 1, bh); + const body_pixels = @as(usize, v.rect_h - pane.tag_rows) * bh; + const limit: u16 = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); + pane.context_row_limit = limit; + pane.body_visible_rows = @intCast(@min(logical_rows, limit + @max(1, (body_pixels -| @as(usize, limit) * th) / bh))); + pane.geometry_len = mapRows(pane, f); +} + +/// One walk of the rows the body shows, recording the wrap map: the bytes +/// its text takes. +fn mapRows(pane: *Pane, f: *State) usize { + const width = wrapWidth(pane, pane.view.wrap); pane.body_wrapped = width > 0; f.tables.ensure(pane.gpa, f); prepareContextRows(pane, f); - pane.wrap_n = 0; - - const len = fillBody(null, pane, f, width, false); - const out = try arena.alloc(u8, len); - const filled = fillBody(out, pane, f, width, true); - std.debug.assert(filled == out.len); - return out; + return fillBody(null, pane, f, width, true); +} + +/// Everything the geometry is made of, hashed: the text (its revision and +/// length), where it is scrolled, the pane's size, its tables as far as +/// they have been read, its context declarations, the session's view. +fn geometryKey(pane: *const Pane, f: *const State) u64 { + const t = &f.tables; + const facts = .{ + f.revision, f.content.len, f.scroll, pane.hscroll, + pane.rows, pane.cols, pane.tag_rows, pane.collapsed, + pane.query != null, f.pager, f.mini != null, f.output != null, + if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0, + f.context_declarations.len, + f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize), + pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, + pane.view.rect_h, + }; + // One hash of them laid out as words: every reader of the map asks for + // this key, a hundred times a frame. + var words: [facts.len]u64 = undefined; + inline for (facts, &words) |fact, *w| w.* = if (@TypeOf(fact) == bool) @intFromBool(fact) else if (@typeInfo(@TypeOf(fact)).int.signedness == .signed) @bitCast(@as(i64, fact)) else fact; + // Never the "made for nothing" key a reset leaves (`geometry_key = 0`). + return std.hash.Wyhash.hash(0x9e0, std.mem.sliceAsBytes(&words)) | 1; } /// Run the file-body row walk. With no destination it is the exact sizing diff --git a/src/Messages.zig b/src/Messages.zig index cb52907a..4cf81ac5 100644 --- a/src/Messages.zig +++ b/src/Messages.zig @@ -31,6 +31,7 @@ len: usize = 0, pub const LoggedMessage = struct { pub const cap = 256; + pub const replica = .{ .len = .{.{ "text", "len" }} }; text: [cap]u8 = undefined, len: u16 = 0, /// The serial of the pane that said it, or 0 for none in particular. A @@ -117,6 +118,24 @@ pub const Message = struct { }; extern "c" fn localtime_r(timep: *const libc.time_t, result: *Tm) ?*Tm; + fn timeOfDay() [8]u8 { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.REALTIME, &ts); + const secs: libc.time_t = ts.sec; + var tm: Tm = undefined; + if (localtime_r(&secs, &tm) != null) { + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ + @as(u32, @intCast(tm.hour)), + @as(u32, @intCast(tm.min)), + @as(u32, @intCast(tm.sec)), + }) catch {}; + } + return clock; + } + pub fn body(text: []const u8) []const u8 { if (text.len < 10) return text; if (text[2] != ':' or text[5] != ':' or text[8] != ' ' or text[9] != ' ') return text; @@ -127,21 +146,9 @@ pub const Message = struct { } pub fn stamp(buf: []u8, verb: []const u8, subject: []const u8) []const u8 { - var clock: [8]u8 = "--:--:--".*; - const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; - if (!notime) { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.REALTIME, &ts); - const secs: libc.time_t = ts.sec; - var tm: Tm = undefined; - if (localtime_r(&secs, &tm) != null) { - _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ - @as(u32, @intCast(tm.hour)), - @as(u32, @intCast(tm.min)), - @as(u32, @intCast(tm.sec)), - }) catch {}; - } - } + // The time of day as the session that said it read it, in its + // zone: a replica's clock and TZ are its own (src/tape.zig). + const clock = pardes.tape.value(.clock, timeOfDay, .{}); const head = std.fmt.bufPrint(buf, "{s} {s} ", .{ &clock, verb }) catch return buf[0..0]; const room = buf.len - head.len; const tail = if (subject.len <= room) subject else subject[subject.len - room ..]; @@ -153,10 +160,14 @@ pub const Message = struct { /// A status updates the message line in place (a progress count does not /// pile up); a message is an event, and stacks. pub fn setStatus(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_status = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); showMessage(p, id, text, false); } pub fn setMessage(p: *Pardes, id: usize, text: []const u8) void { + const taped = pardes.tape.enter(p, .{ .set_message = .{ .id = id, .text = text } }); + defer pardes.tape.leave(p, taped); logMessage(p, id, text); showMessage(p, id, text, true); } @@ -186,7 +197,7 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { const arrives = pane.msg_len == 0 or pane.msg_life.phase == .leaving; for (text[0..n], pane.msg[0..n]) |c, *cell| cell.* = if (c < 0x20 or c == 0x7f) ' ' else c; pane.msg_len = @intCast(n); - pane.msg_said_ms = nowMs(); + pane.msg_said_ms = @intCast(p.now_ns / std.time.ns_per_ms); p.needs_frame = true; if (arrives) { pane.msg_life = .{ .phase = if (p.settings.message_animation and n > 0) .entering else .shown }; @@ -200,13 +211,6 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { } } -fn nowMs() i64 { - if (comptime !pardes.hosted) return 0; - var ts: std.c.timespec = undefined; - if (std.c.clock_gettime(.MONOTONIC, &ts) != 0) return 0; - return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000); -} - /// With no frontend attached no input dismisses a message, and a detached /// session's /screen and messages kept what was said an hour ago: each /// pane's lines go once its newest has been up `message_linger_ms`, on the @@ -217,7 +221,9 @@ fn nowMs() i64 { /// clock; the render pipeline's clock will give every line its own. pub fn expireUnattended(p: *Pardes) void { if (!p.unattended) return; - const now = nowMs(); + // The session's clock (`advance`), as every tick's: no replica reads + // its own. + const now: i64 = @intCast(p.now_ns / std.time.ns_per_ms); for (p.panes) |slot| { const pane = slot orelse continue; if (pane.msg_len == 0 and pane.msg_older_len == 0) continue; @@ -453,6 +459,8 @@ pub fn dialReason(err: anyerror) ?[]const u8 { } pub fn reportError(p: *Pardes, id: usize, operation: []const u8, err: anyerror) void { + const taped = pardes.tape.enter(p, .{ .report_error = .{ .id = id, .operation = operation, .err = err } }); + defer pardes.tape.leave(p, taped); // Room for an operation naming the longest path and the reason after // it: a shorter one cut the reason off (`Save /long…`), and the reason // is what a failure is for. Where it must be shorter, the path gives. @@ -575,6 +583,19 @@ test "an error reads as words and a failure is cut between them" { /// so it does not paint under them, and the paint pass so it can stack /// them instead of letting the last one win the row. pub fn collectNotices(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { + collectBands(p, pane, r, id); + // Each band's chip at the right edge, as wide as its own text: a click + // finds it whether or not a frame has drawn it. + const tx = r.x + config.GUTTER; + const tw = r.w -| config.GUTTER; + var leader_buf: [16]u8 = undefined; + for (0..pane.notices.len) |i| { + const chip = noticeCols(p, noticeText(p, pane, i, &leader_buf), tw); + pane.notices.left[i] = if (chip == 0) std.math.maxInt(u16) else tx + tw - chip; + } +} + +fn collectBands(p: *Pardes, pane: *Pane, r: Rect, id: usize) void { pane.notices.len = 0; if (r.w <= config.GUTTER or r.h <= pane.tag_rows) return; // Oldest message line on top, so lines already up do not move when a diff --git a/src/Names.zig b/src/Names.zig index 0c8ba075..66f71bb9 100644 --- a/src/Names.zig +++ b/src/Names.zig @@ -146,8 +146,7 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void { pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const q = n.queryText(); const empty = std.mem.trim(u8, q, " ").len == 0; - const io = std.Io.Threaded.global_single_threaded.io(); - const started = std.Io.Clock.awake.now(io); + const started = pardes.FrameStats.nowNs(); const had = n.hits.items.len; const was_scored = n.scored; var count: usize = 0; @@ -164,8 +163,7 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const s = if (empty) 0 else match(q, key, n.lower.items[c.start + c.key ..][0..c.key_len], c.base, null, null, null); if (s) |score| n.hits.append(gpa, .{ .cand = i, .score = score }) catch break; // The clock every 256 candidates: reading it is not free either. - if (!empty and count % 256 == 255 and - started.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds() > score_budget_ns) break; + if (!empty and count % 256 == 255 and pardes.tape.over(started, score_budget_ns)) break; } // An empty query keeps the source's order (the walk's, recency): no // row moves as the walk goes on, it only grows at the foot. diff --git a/src/Output.zig b/src/Output.zig index a17b0459..83914d52 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -43,6 +43,7 @@ pub const Origin = union(enum) { pub const max_arg = dump.max_origin_arg; pub const State = struct { + pub const replica = .{ .len = .{.{ "arg_buf", "arg_len" }} }; from: Origin, arg_buf: [max_arg]u8 = undefined, arg_len: u16 = 0, @@ -1115,7 +1116,7 @@ pub fn resetBody(p: *Pardes, pane: *Pane) void { pane.body.cur_col = 0; pane.body.cur_pinned = true; pane.hscroll = 0; - pane.wrap_n = 0; + pane.geometry_key = 0; pane.body.msel = .{}; pane.body.vsel = .{}; pane.body.nsel = 0; diff --git a/src/Places.zig b/src/Places.zig index cff7c22e..3dca3adf 100644 --- a/src/Places.zig +++ b/src/Places.zig @@ -67,6 +67,10 @@ pub fn nowNs() u64 { return @as(u64, @intCast(ts.sec)) *| std.time.ns_per_s +| @as(u64, @intCast(ts.nsec)); } +/// What the replay digest leaves out (src/walk.zig): the rail's ticks, +/// made again from the places when they are drawn. +pub const replica = .{ .derived = .{ "ticks", "ticks_gen", "re" } }; + started: bool = false, provenance: Provenance = .search, /// The query pane that fed them, by serial; 0 for `*` and the register. @@ -477,7 +481,9 @@ pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) voi if (short) break; // A frame's time spent (a whole search's for n and N, whose steps // are mvzr's whole budget). - if (started_ns != 0 and bytes != std.math.maxInt(usize) and nowNs() - started_ns > frame_ns) break; + // The clock's verdict is the session's (src/tape.zig): a replica + // finds as many places a frame as this did. + if (started_ns != 0 and bytes != std.math.maxInt(usize) and pardes.tape.over(started_ns, frame_ns)) break; } if (set.hits.items.len != found_before) { set.gen +%= 1; diff --git a/src/Query.zig b/src/Query.zig index 09b46f3b..7440dd0e 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -1743,11 +1743,7 @@ fn displayPreview(p: *Pardes, qid: usize, path: []const u8, at: look.Spot) void const f = &pane.file.?; if (!std.mem.eql(u8, f.path, path)) { if (exec.kindOf(path)) |kind| if (kind != .file) return; - const bytes = blk: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :blk filesystem.read(p, path) catch return; - }; + const bytes = pardes.tape.site(.preview, p.gpa, filesystem.read, .{ p, path }) catch return; if (std.mem.indexOfScalar(u8, bytes[0..@min(bytes.len, 8000)], 0) != null) { p.gpa.free(bytes); return; diff --git a/src/Stream.zig b/src/Stream.zig index da7d12eb..8d9663dd 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -104,7 +104,13 @@ pub const ParseJob = struct { } }; -const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const ParseTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *ParseJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// The colours of the text a scroll is heading into, worked out on a /// worker before it gets there, as a game streams in the level ahead of @@ -134,12 +140,21 @@ pub const PaintJob = struct { } }; -const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const PaintTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *PaintJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// A paint's generation has the top bit, so it can never be a search's or /// a parse's (those count from 0 in `gen`, and it would take two billion /// searches to reach it). const paint_bit: u32 = 1 << 31; +/// A query rows' paint's has the next bit too: counted apart from the +/// paints a view's frames ask for (`rows_count`), which are its own. +const rows_bit: u32 = 1 << 30; const max_paint_bytes = 512 << 10; /// Where a worker hands a batch: false when it was turned away (the inbox @@ -172,6 +187,19 @@ pub const Job = struct { const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io }; +/// What the replay digest leaves out (src/walk.zig): the workers' timings, +/// and the colours painted ahead of a view for its frames, which is the +/// render's own prefetch (a replica that draws nothing paints none). +pub const replica = .{ + // The worker a search runs on, which the host starts. + .authority = .{"task"}, + .local = .{ + "started_ns", "first_ns", "finished_ns", "worker_ns", "parse_ns", + "paint", "paint_retired", "paint_count", "ahead", "ahead_for", + "painted_ahead", "painted_on_loop", "ahead_seen", "ahead_ask", + }, +}; + gen: u32 = 0, state: State = .idle, kind: Kind = .grep, @@ -210,6 +238,10 @@ paint: ?PaintTask = null, /// loop for half a second). No new paint starts while it is out. paint_retired: ?PaintTask = null, paint_count: u32 = 0, +rows_count: u32 = 0, +/// The paint a frame asked for, its worker started once the frame is +/// presented (`askAhead`): starting one is a thread, not a frame's to wait. +ahead_ask: ?u32 = null, ahead: []u8 = &.{}, ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, /// Stretches a view took painted ahead, and painted on the loop itself @@ -321,7 +353,17 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont const gen = paint_bit | (s.paint_count & ~paint_bit); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; s.paint = .{ .job = job }; - p.emit(.{ .search = .{ .gen = gen } }); + // Asked of the host once the frame is presented, not queued as an + // effect of the session's: a replica that draws nothing asks for none. + s.ahead_ask = gen; +} + +/// The worker for the paint the last frame asked for: the host starts it +/// after the present (Pardes.pump), off the keystroke's way to the screen. +pub fn askAhead(p: *Pardes) void { + const gen = p.stream.ahead_ask orelse return; + p.stream.ahead_ask = null; + if (p.host.vtable.search) |f| f(p.host.ctx, gen); } /// A file's colours for the query rows that show its lines, the whole @@ -363,8 +405,8 @@ pub fn paintRows(p: *Pardes, path: []const u8) void { }; s.rows_asked[s.rows_asked_at] = key; s.rows_asked_at +%= 1; - s.paint_count +%= 1; - const gen = paint_bit | (s.paint_count & ~paint_bit); + s.rows_count +%= 1; + const gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit)); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source }; s.rows_paint = .{ .job = job }; p.emit(.{ .search = .{ .gen = gen } }); @@ -1145,22 +1187,30 @@ const testing = std.testing; /// worker, hand what it posts to the core. const TestShell = struct { q: host_io.Inbox, + p: ?*Pardes = null, fn init() TestShell { return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; } + /// A shell's search: a worker started for the generation, as the tty's. const vtable: pardes.Host.VTable = .{ .search = struct { - fn f(_: ?*anyopaque, _: u32) void {} + fn f(ctx: ?*anyopaque, gen: u32) void { + const t: *TestShell = @ptrCast(@alignCast(ctx.?)); + start(testing.io, testing.allocator, t.p.?, gen, &t.q, host_io.Inbox.pushSearch); + } }.f }; - fn attach(p: *Pardes) void { - p.host = .{ .vtable = &vtable }; + fn attach(t: *TestShell, p: *Pardes) void { + t.p = p; + p.host = .{ .ctx = t, .vtable = &vtable }; } fn pump(t: *TestShell, p: *Pardes) void { + // What the last frame asked for, as Pardes.pump asks after a present. + askAhead(p); while (p.nextEffect()) |e| switch (e) { - .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch), + .search => p.perform(e), else => {}, }; var batch: host_io.Inbox.Batch = .{}; @@ -1226,7 +1276,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1328,7 +1378,7 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1367,7 +1417,7 @@ test "Stream: a scroll's colours are painted ahead on a worker, so PageDown afte pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1410,7 +1460,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=)" { pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1464,7 +1514,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1508,7 +1558,7 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1604,7 +1654,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them, p.gpa.free(pane.file.?.path); pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelPaint(); diff --git a/src/Tables.zig b/src/Tables.zig index 2e65a716..44918559 100644 --- a/src/Tables.zig +++ b/src/Tables.zig @@ -95,8 +95,13 @@ pending: ?Resume = null, /// and whether the line before ended a block. const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; -/// What one frame spends reading a big text from the top. +/// What one frame spends reading a big text from the top: on the session's +/// tick, by the clock (src/tape.zig keeps a replica's reading in step). pub const frame_ns: u64 = 4 * std.time.ns_per_ms; +/// What a new text's first look reads at once, wherever it is asked for +/// (the render, or a step reading the view's geometry): a count, never the +/// clock, so whoever asks first finds the same tables. +pub const first_lines: u32 = 4096; pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { t.list.deinit(gpa); @@ -130,10 +135,7 @@ pub fn find(t: *const Tables, line: usize) ?*const Table { /// touch shift and only the block around it is read again (`reflow`): a /// keystroke costs its table, not the file. pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { - if (t.current(f)) { - if (t.pending != null) t.go(gpa, f.content, frame_ns); - return; - } + if (t.current(f)) return; defer { t.revision = f.revision; t.content = @intFromPtr(f.content.ptr); @@ -157,13 +159,20 @@ pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { // read: it goes on. if (lineHolding(starts, f.splice.head) >= r.n) { t.lines = starts.len; - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); return; } } else if (t.reflow(gpa, f)) |_| return else |_| {} } t.begin(starts.len); - t.go(gpa, f.content, frame_ns); + t.go(gpa, f.content, 0, first_lines); +} + +/// A frame's more of a reading under way: on the session's tick, so a core +/// that draws reads exactly as far as one that does not. +pub fn step(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { + t.ensure(gpa, f); + if (t.pending != null) t.go(gpa, f.content, frame_ns, std.math.maxInt(u32)); } /// Whether a reading of the text is still under way (Pardes.nextWake). @@ -183,11 +192,11 @@ fn begin(t: *Tables, lines: usize) void { t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }; } -/// Read on for about `budget_ns` (0: to the end). On no memory there are -/// no tables (the text is shown as it is). -fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void { +/// Read on for about `budget_ns` (0: no clock) or `max_lines`, whichever +/// ends first. On no memory there are no tables (the text is shown as it is). +fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64, max_lines: u32) void { var at = t.pending orelse return; - const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch { + const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns, max_lines) catch { t.list.clearRetainingCapacity(); t.pending = null; return; @@ -290,7 +299,7 @@ fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void { const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#"); const head = lineOf(content, starts, r0); var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; - _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0); + _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0, std.math.maxInt(u32)); const fresh = try gpa.dupe(Table, t.list.items[before..]); defer gpa.free(fresh); t.list.shrinkRetainingCapacity(before); @@ -500,7 +509,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { t.aligns.clearRetainingCapacity(); t.begin(std.mem.count(u8, content, "\n") + 1); var at = t.pending.?; - _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0); + _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0, std.math.maxInt(u32)); t.pending = null; } @@ -508,7 +517,7 @@ pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { /// appending them; with `fences`, keep the fences. With a budget, stop /// when it is spent, `at` where to go on from. Whether it reached the end /// (or `stop`). -fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool { +fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64, max_lines: u32) !bool { var fence = at.fence; var prev_blank = at.prev_blank; var line = lineAt(content, at.at, at.n); @@ -516,7 +525,7 @@ fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume var lines: usize = 0; while (line) |l| : (lines += 1) { if (l.n > stop) break; - if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| t0 > budget_ns) { + if (lines % 256 == 255 and (lines >= max_lines or (budget_ns > 0 and pardes.tape.over(t0, budget_ns)))) { at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank }; return false; } @@ -1049,6 +1058,7 @@ test "tables: a big text's tables are read a frame's budget at a time from the t File.setContent(p, f, new); edits += 1; } + p.update(.tick); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(frames < 10_000); @@ -1077,6 +1087,7 @@ test "tables: a table wider than a wrapping pane is shown as its source, laid ou // Source: a pipe is a pipe, at its source column. try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7)); try testing.expect(p.executeBuiltinLine(0, "Wrap off")); + p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); try testing.expect(!pane.body_wrapped); diff --git a/src/Text.zig b/src/Text.zig index b57c953a..3f107542 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -49,6 +49,10 @@ pub const SelRange = struct { sticky: i32 = -1, }; +/// The replay digest's walk (src/walk.zig) of the histories: as far as +/// they go. +pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" } } }; + what: What = .body, /// The characters of a text that holds its own (gpa-owned): everything but /// a body, which reads its pane's. A tag nobody has changed has none yet and diff --git a/src/body_layer.zig b/src/body_layer.zig index bfca268a..5bffb691 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -169,22 +169,17 @@ pub fn buttonInks(th: *const colors.Theme) [3][3]u8 { /// rows the grid cannot show, and is painted straight onto the grid. The /// rail is the grid's alone. pub fn joinBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rail: pardes.Region) !void { - const file = if (pane.file) |*f| f else return renderBody(p, s, arena, pane, r, id, active, rail, null); - if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows) + if (pane.file == null) return renderBody(p, s, arena, pane, r, id, active, rail, null); + // Made afresh for every frame from the pane as it is, as a step reading + // it would make it (File.geometry): never only by drawing. + pane.geometry_key = 0; + panes.File.geometry(pane); + // Which rows are context rows is decided by the text the body shows: a + // layer taller than the grid's rows when there are any. + if (!p.settings.tree_context_tag_style or pane.collapsed or r.h <= pane.tag_rows or pane.body_rows == 0) return renderBody(p, s, arena, pane, r, id, active, rail, null); - // Which rows are context rows is decided by the text the body shows. - const text = try panes.File.bodyText(arena, pane, file, p.settings.wrap); const viewport_rows = r.h - pane.tag_rows; - const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); - if (pane.context_rows == 0 or logical_rows <= pane.context_rows) - return renderBody(p, s, arena, pane, r, id, active, rail, text); - pane.body_rows = @intCast(logical_rows); - const bh: usize = @max(1, p.row_metrics.body_h); - const th: usize = std.math.clamp(p.row_metrics.tagline_h, 1, bh); - const body_pixels = @as(usize, viewport_rows) * bh; - pane.context_row_limit = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); - const context_pixels = @as(usize, pane.context_row_limit.?) * th; - pane.body_visible_rows = @intCast(@min(logical_rows, pane.context_row_limit.? + @max(1, (body_pixels -| context_pixels) / bh))); + const logical_rows: usize = pane.body_rows; const width = r.w - config.GUTTER; const layer = &s.body_layers[id]; const count = logical_rows * width; @@ -512,6 +507,6 @@ pub fn bodyText(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) ![]const u8 { ); unreachable; } - if (pane.file) |*f| return panes.File.bodyText(arena, pane, f, p.settings.wrap); + if (pane.file) |*f| return panes.File.bodyText(arena, pane, f); return panes.terminal.bodyText(arena, pane); } diff --git a/src/builtins.zig b/src/builtins.zig index 3a66a316..a89b1180 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -793,11 +793,7 @@ pub const Mount = struct { }; // A mount that cannot be reached is said now, and taken back, not // found out by the first look through it. - const reached = reached: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :reached @import("9p_io.zig").Client.probe(c.p.gpa, dial); - }; + const reached = pardes.tape.site(.probe, null, @import("9p_io.zig").Client.probe, .{ c.p.gpa, dial }); reached catch |err| { if (c.p.fs.mounts.pop()) |gone| { c.p.gpa.free(gone.name); @@ -927,11 +923,7 @@ pub const TreeContext = struct { }; file.tree_context = on; file.syntax_dirty = true; - c.pane.wrap_n = 0; - c.pane.context_rows = 0; - c.pane.body_rows = 0; - c.pane.body_visible_rows = 0; - c.pane.context_row_limit = null; + c.pane.geometry_key = 0; c.p.surface.body_layers[c.id].rows = 0; } }; diff --git a/src/config.zig b/src/config.zig index f0023193..13404461 100644 --- a/src/config.zig +++ b/src/config.zig @@ -455,6 +455,11 @@ test "wheel drift guard" { /// The shell when no `Shell` is set: the user's login shell ($SHELL), as a /// terminal would start, else /bin/sh. An explicit `Shell` overrides it. pub fn defaultShell() []const u8 { + // As the session that recorded found it (src/tape.zig). + return pardes.tape.value(.env, defaultShellHost, .{}); +} + +fn defaultShellHost() []const u8 { if (comptime pardes.hosted) if (std.c.getenv("SHELL")) |s| { const shell = std.mem.span(s); // One that is not there, or not executable, is no shell: /bin/sh. diff --git a/src/draw.zig b/src/draw.zig index e141ce31..4f9c6c87 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -96,12 +96,10 @@ pub fn place(p: *Pardes, s: *Surface) void { if (r.h <= pane.tag_rows) continue; const tx = r.x + config.GUTTER; const tw = r.w - config.GUTTER; - var leader_buf: [16]u8 = undefined; for (0..pane.notices.len) |i| { - const text = Messages.noticeText(p, pane, i, &leader_buf); - const chip = Messages.noticeCols(p, text, tw); - if (chip == 0) continue; - pane.notices.left[i] = tx + tw - chip; + const left = pane.notices.left[i]; + if (left == std.math.maxInt(u16)) continue; + const chip = tx + tw - left; s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } }); } } @@ -521,6 +519,8 @@ fn resolved(color: Color, default: ?[3]u8, palette: ?[16][3]u8) ?[3]u8 { } pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { + const taped = pardes.tape.enter(p, .render); + defer pardes.tape.leave(p, taped); panes.File.refreshHighlights(p); const s = &p.surface; const ncells = @as(usize, p.screen_w) * p.screen_h; @@ -606,9 +606,6 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { for (&p.panes, 0..) |*slot, id| { const pane = slot.* orelse continue; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; s.body_layers[id].rows = 0; try renderPane(p, s, arena, pane, p.rects[id], id, id == p.active); } @@ -976,7 +973,6 @@ pub fn paintHeaders(p: *Pardes, s: *Surface, arena: std.mem.Allocator) !void { const chrome = p.chromeTheme(); // global tagbar: full width, top rows — unless a native shell has // taken its commands over, in which case the rows are not ours to draw - tagline.revealHeader(p); if (s.region(.workspace_tag, Region.none)) |region| { const top = if (p.header_focus and p.header_column == null) p.header_top else 0; const layer = &s.tag_layers[MAX_PANES + MAX_COLS]; @@ -2286,12 +2282,9 @@ test "a body with context rows is painted once, and the grid's rows are what a p const cells = try arena.allocator().alloc(Cell, s.cells.len); @memset(cells, .{}); var plain: Surface = .{ .cols = s.cols, .rows = s.rows, .cells = cells }; - const saved = .{ pane.body_rows, pane.body_visible_rows, pane.context_row_limit }; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; + pane.view.layered = false; try body_layer.renderBody(p, &plain, arena.allocator(), pane, p.rects[0], 0, true, null, null); - pane.body_rows, pane.body_visible_rows, pane.context_row_limit = saved; + pane.view.layered = true; for (0..body.rect.h) |row| { const y = body.rect.y + row; for (0..body.rect.w) |col| { diff --git a/src/dump.zig b/src/dump.zig index 0da2513f..f9f01950 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -880,12 +880,16 @@ fn applySettings(p: *Pardes, lines: []const []const u8) void { /// The replacement core for a Restore of the dump at `from`, whose log says /// so after its panes' `new`s, for a client that reconnects to read. pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes { + const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = zon_bytes, .from = from } }); + defer pardes.tape.leave(p, taped); var opts = p.opts; opts.cols = p.screen_w; opts.rows = p.screen_h; var old: OldSerials = .{}; const replacement = try initDump(p.gpa, opts, zon_bytes, p, &old); replacement.pointRestoreAt(from); + // The tape follows the replacement from here (it is the session). + pardes.tape.adopt(replacement); pardes.ctlfs.events.notePath(replacement, "restore", from); // Which serial each pane and column had, for a script that held them. for (replacement.panes, old.panes) |slot, was| if (slot) |pane| if (was != 0) { @@ -900,7 +904,14 @@ pub fn restore(p: *Pardes, zon_bytes: []const u8, from: []const u8) !*Pardes { } pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8) !*Pardes { - return initDump(gpa, opts, zon_bytes, null, null); + pardes.tape.startFromEnv(); + const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = zon_bytes } }); + const p = initDump(gpa, opts, zon_bytes, null, null) catch |err| { + pardes.tape.leave(null, taped); + return err; + }; + pardes.tape.leave(p, taped); + return p; } /// The serials a dump's panes and columns had, read as it is restored. diff --git a/src/exec.zig b/src/exec.zig index c14a648c..8f84d2c6 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -485,8 +485,10 @@ pub fn isFile(path: []const u8) bool { pub fn kindOf(path: []const u8) ?std.Io.File.Kind { var buf: [4096]u8 = undefined; const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null; - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.kind, null, kindHost, .{z}); +} + +fn kindHost(z: [:0]const u8) ?std.Io.File.Kind { const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), z, .{}) catch return null; return stat.kind; } @@ -1116,7 +1118,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] } if (comptime pardes.hosted) { var why: [320]u8 = undefined; - if (@import("host_io.zig").Shell.refusal(want[0..@min(want.len, 200)], &why)) |refused| { + if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ want[0..@min(want.len, 200)], &why })) |refused| { var text: [340]u8 = undefined; return p.reportFailure(p.active, std.fmt.bufPrint(&text, "Shell: {s}", .{refused}) catch "Shell: no such shell"); } @@ -1161,10 +1163,7 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] for (p.panes, 0..) |slot, id| { p.surface.body_layers[id].rows = 0; const pane = slot orelse continue; - pane.body_rows = 0; - pane.body_visible_rows = 0; - pane.context_row_limit = null; - pane.wrap_n = 0; + pane.geometry_key = 0; } p.abandonPanelAnimations(); } diff --git a/src/fs.zig b/src/fs.zig index 9104c93f..e4373bf0 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -171,11 +171,7 @@ 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 = stat: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT); - }; + 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 }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, @@ -199,32 +195,10 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { // Listed into a buffer of this request's own first; the shared // reply buffer is another request's to use while the turn is out. var listing: [16 * 1024]u8 = undefined; - var fixed: std.heap.FixedBufferAllocator = .init(&listing); - var listed: std.ArrayList(u8) = .empty; - { - pardes.turn.yield(); - defer pardes.turn.back(); - var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return Reply.fail(req.tag, E.NOTDIR); - defer dir.close(io); - var it = dir.iterate(); - var skip = req.off; - while (it.next(io) catch return Reply.fail(req.tag, E.IO)) |entry| { - var buf: [4096]u8 = undefined; - const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; - var normalized_buf: [4096]u8 = undefined; - const normalized = resolveOs(joined, &normalized_buf) orelse continue; - const child = dir.statFile(io, entry.name, .{}) catch continue; - if (skip > 0) { - skip -= 1; - continue; - } - const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); - tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); - if (listed.items.len >= @max(req.size, 512)) break; - } - } + const listed = pardes.tape.site(.os_listing, null, osListing, .{ io, path, req.off, req.size, &listing }) catch |err| + return Reply.fail(req.tag, if (err == error.NotDir) E.NOTDIR else E.IO); const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, listed.items) catch return Reply.fail(req.tag, E.NOMEM); + out.appendSlice(p.gpa, listed) catch return Reply.fail(req.tag, E.NOMEM); return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } }; }, .read, .write, .setattr => { @@ -237,37 +211,70 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return Reply.fail(req.tag, E.NOENT); var buf: [ninep_io.msize]u8 = undefined; // a read never asks for more than a frame - const got = io: { - pardes.turn.yield(); - defer pardes.turn.back(); - const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); - if (fd < 0) return Reply.fail(req.tag, E.PERM); - defer _ = libc.close(fd); - if (req.op == .setattr) { - if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero"); - var truncated = attr; - truncated.size = 0; - return .{ .tag = req.tag, .attr = truncated }; - } - if (req.off > std.math.maxInt(i64)) return tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold"); - if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return Reply.fail(req.tag, E.IO); - if (req.op == .write) { - const written = libc.write(fd, req.data.ptr, req.data.len); - if (written < 0) return Reply.fail(req.tag, E.IO); - return .{ .tag = req.tag, .written = @intCast(written) }; - } - const got = libc.read(fd, &buf, @min(req.size, buf.len)); - if (got < 0) return Reply.fail(req.tag, E.IO); - break :io @as(usize, @intCast(got)); + const got = switch (pardes.tape.site(.os_rw, null, osReadWrite, .{ path_z, req, attr, &buf })) { + .reply => |reply| return reply, + .read => |bytes| bytes, }; const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, buf[0..got]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got) } }; + out.appendSlice(p.gpa, got) catch return Reply.fail(req.tag, E.NOMEM); + return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got.len) } }; }, else => return Reply.fail(req.tag, E.PERM), } } +/// 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 { + var fixed: std.heap.FixedBufferAllocator = .init(listing); + var listed: std.ArrayList(u8) = .empty; + var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return error.NotDir; + defer dir.close(io); + var it = dir.iterate(); + var skip = off; + while (it.next(io) catch return error.Io) |entry| { + var buf: [4096]u8 = undefined; + const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; + var normalized_buf: [4096]u8 = undefined; + const normalized = resolveOs(joined, &normalized_buf) orelse continue; + const child = dir.statFile(io, entry.name, .{}) catch continue; + if (skip > 0) { + skip -= 1; + continue; + } + const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); + tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); + if (listed.items.len >= @max(size, 512)) break; + } + return listed.items; +} + +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`. +fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsReadWrite { + const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); + if (fd < 0) return .{ .reply = Reply.fail(req.tag, E.PERM) }; + defer _ = libc.close(fd); + if (req.op == .setattr) { + if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero") }; + var truncated = attr; + truncated.size = 0; + return .{ .reply = .{ .tag = req.tag, .attr = truncated } }; + } + if (req.off > std.math.maxInt(i64)) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold") }; + if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + if (req.op == .write) { + const written = libc.write(fd, req.data.ptr, req.data.len); + if (written < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .reply = .{ .tag = req.tag, .written = @intCast(written) } }; + } + const got = libc.read(fd, buf.ptr, @min(req.size, buf.len)); + if (got < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .read = buf[0..@intCast(got)] }; +} + pub fn validMountName(name: []const u8) bool { if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false; if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false; @@ -687,8 +694,10 @@ test "a path with a part over 255 bytes is NameTooLong, never a panic" { pub fn makeDirs(dir: []const u8) void { if (comptime !platform_has_fs) return; if (dir.len == 0) return; - pardes.turn.yield(); - defer pardes.turn.back(); + pardes.tape.site(.make_dirs, null, makeDirsHost, .{dir}); +} + +fn makeDirsHost(dir: []const u8) void { _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {}; } @@ -798,6 +807,12 @@ pub fn dotRelative(word: []const u8) bool { /// `~` to expand, or no home for it. pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 { if (comptime !pardes.hosted) return path; + if (path.len == 0 or path[0] != '~') return path; + // Home as the session that recorded found it (src/tape.zig). + return pardes.tape.value(.env, expandHomeHost, .{ path, out }); +} + +fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 { return expandHomeFrom(path, out, std.c.getenv("HOME")); } @@ -846,8 +861,10 @@ pub fn resolveOs(path: []const u8, out: *[4096]u8) ?Resolved { if (comptime !platform_has_fs) return null; var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return null; - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.resolve, null, resolveHost, .{ path_z, out }); +} + +fn resolveHost(path_z: [:0]const u8, out: *[4096]u8) ?Resolved { const resolved = realpath(path_z, out) orelse return null; return .{ .path = std.mem.span(resolved), .dir = isDir(resolved) }; } @@ -904,9 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 { if (!std.mem.eql(u8, name, mount.name)) continue; // A peer answers on its own time, and the peer may be this very // editor: the turn goes out with the request. - pardes.turn.yield(); - defer pardes.turn.back(); - return ninep_io.Client.readLimit(p.gpa, mount.dial, remote_path, path, limit); + return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit }); } return error.FileNotFound; } @@ -1233,8 +1248,10 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 { const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{native}, 0) catch return error.PathTooLong; // The path may be a mount this editor serves: the turn goes out with the // syscalls, and the bytes come back into memory of the caller's own. - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.read_file, gpa, readHost, .{ gpa, path_z, limit }); +} + +fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u8 { const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .NONBLOCK = true }); if (fd < 0) return switch (libc.errno(fd)) { .ACCES, .PERM => error.PermissionDenied, diff --git a/src/image.zig b/src/image.zig index e334965a..20d6d6f0 100644 --- a/src/image.zig +++ b/src/image.zig @@ -931,6 +931,10 @@ const GridKey = struct { }; pub const State = struct { + /// What the replay digest leaves out (src/walk.zig): the picture as + /// decoded from its bytes, off the loop as the render first wants it, + /// and the glyph art drawn from that. + pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "grid", "grid_key", "decoder", "decoded", "answer" } }; path: []u8, glyph_art: bool = false, pmode: PaletteMode = .commodore, diff --git a/src/look.zig b/src/look.zig index 1a574212..226c96e3 100644 --- a/src/look.zig +++ b/src/look.zig @@ -954,7 +954,7 @@ fn noteLookHover(p: *Pardes, col: u16, row: u16, body_hit: ?Mouse.BodyHit, tag_h pub fn refreshLookHoverFromRaw(p: *Pardes) void { if (!p.pointer_inside or !p.raw_hover_intent) return; - const mapped = p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row) orelse + const mapped = p.pointerCell(p.pointer_raw_col, p.pointer_raw_row) orelse return cancelLookHover(p); p.hover_col = mapped.col; p.hover_row = mapped.row; diff --git a/src/main.zig b/src/main.zig index 3d26d1fd..378dbea8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -333,6 +333,8 @@ fn webMain() !void { fn nativeMain(std_init: std.process.Init) !void { var init = std_init; if (builtin.mode != .debug) init.gpa = pardes.memory.host(std.heap.c_allocator); + // A recorded session's tape replayed, no session of its own (src/tape.zig). + if (init.environ_map.get("PARDES_REPLAY")) |tape| std.process.exit(pardes.tape.replayMain(init.io, tape)); var stderr_probe: [64]u8 = undefined; _ = std.debug.lockStderr(&stderr_probe); std.debug.unlockStderr(); diff --git a/src/mouse.zig b/src/mouse.zig index 2ff4fef3..67ebf7cf 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -380,7 +380,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool { else => return false, }; const bit: u3 = if (button) |b| @as(u3, 1) << @intCast(@backingInt(b)) else 0; - const at = p.presentation.pointer(p.screen_w, p.screen_h, @min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1)); + const at = p.pointerCell(@min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1)); var mods: panes.terminal.MouseMods = .{ .shift = m.shift, .alt = m.alt, .ctrl = m.ctrl }; var buf: [64]u8 = undefined; if (p.mouse_grab) |*grab| { @@ -535,7 +535,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { return; } p.raw_hover_intent = false; - const mapped = p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) orelse { + const mapped = p.pointerCell(raw_col, raw_row) orelse { look.cancelLookHover(p); if (m.kind == .release) { if (p.header_sweep) |sweep| if (sweep.button == m.button) { @@ -931,6 +931,7 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo const r = p.rects[s.id]; const b = @backingInt(s.button); 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| { const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return; pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER; diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index f9112ae2..33f816fe 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -1002,8 +1002,7 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) ! f.highlight_start = 0; f.syntax_dirty = true; f.context_revision = null; - pane.context_rows = 0; - pane.wrap_n = 0; + pane.geometry_key = 0; look.invalidateLookHover(p, id); p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); events.noteLog(p, .rename, pane); diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 7b107ea7..bda2c1d1 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -1192,6 +1192,8 @@ fn linesOpen(target: Target, req: Req) bool { /// The editor's step: the last lines of closed opens run, each as a write /// of its own; a refusal is the log's err, as no open is left to answer. pub fn runClosedLines(p: *Pardes) void { + const taped = pardes.tape.enter(p, .run_closed_lines); + defer pardes.tape.leave(p, taped); for (p.fs.closed_lines.items) |*line| { defer line.bytes.deinit(p.gpa); const t = Node.target(line.node) orelse continue; diff --git a/src/normal.zig b/src/normal.zig index 9cac18b0..f214121a 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -713,6 +713,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void .middle => { // helix align_view_middle: the cursor's column to the middle // of an unwrapped view + panes.File.geometry(pane); if (pane.body_wrapped) return; const line = text[modal.lineStartOffset(text, modal.lineAtOffset(text, cur))..modal.lineEndOffset(text, modal.lineAtOffset(text, cur))]; const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane)))); diff --git a/src/panes.zig b/src/panes.zig index 51710d08..a782e3e0 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -24,6 +24,14 @@ const Key = pardes.Key; pub const EditText = struct { text: []u8, row0: i32 }; pub const Pane = struct { + /// What the replay digest leaves out (src/walk.zig): the view's geometry, + /// made again from the rest (File.geometry); the 9P server's record of + /// the pane's opens and readers. + pub const replica = .{ + .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices" }, + .authority = .{"fs"}, + .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, + }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; @@ -128,7 +136,12 @@ pub const Pane = struct { /// The last command line pardes typed into this terminal (an exec, a /// middle click, a pty/run): its first word, which Kill matches, and /// the shell's command count its start (C mark) reaches. - sent_command: ?struct { word: [64]u8 = undefined, len: u8 = 0, seq: u32 = 0 } = null, + sent_command: ?struct { + pub const replica = .{ .len = .{.{ "word", "len" }} }; + word: [64]u8 = undefined, + len: u8 = 0, + seq: u32 = 0, + } = null, /// A command pane (exec.zig runCommand): the line its child runs as /// `$SHELL -c`, owned, where an interactive terminal has null. command: ?[]u8 = null, @@ -245,6 +258,12 @@ pub const Pane = struct { body_rows: u16 = 0, body_visible_rows: u16 = 0, context_row_limit: ?u16 = null, + /// What the geometry above was made for (File.geometry), and the bytes + /// its text takes; 0 has it made again by whoever reads it next. + geometry_key: u64 = 0, + geometry_len: usize = 0, + /// The session's part of the geometry, as of the last step (`sync`). + view: struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 } = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -292,6 +311,7 @@ pub const Pane = struct { /// Older message lines kept on screen under a new one. pub const message_stack = 3; pub const OlderMessage = struct { + pub const replica = .{ .len = .{.{ "text", "len" }} }; text: [256]u8 = undefined, len: u16 = 0, life: MessageLife = .{}, @@ -515,6 +535,7 @@ pub const Pane = struct { } pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } { + File.geometry(pane); if (pane.wrap_n > 0 and vr >= 0 and vr < @as(i32, pane.wrap_n)) return .{ .line = pane.wrap_line[@intCast(vr)], .at = pane.wrap_col[@intCast(vr)], .pad = pane.wrap_pad[@intCast(vr)] }; // unwrapped: rows ARE lines, and the byte column a row starts at is the @@ -523,6 +544,7 @@ pub const Pane = struct { } pub fn wrapRow(pane: *Pane, line: i32, col: i32) struct { row: i32, at: i32, pad: i32 = 0 } { + File.geometry(pane); if (pane.wrap_n == 0) return .{ .row = line - pane.scroll(), .at = pane.hscroll }; var i: u16 = 0; while (i < pane.wrap_n) : (i += 1) { @@ -572,10 +594,11 @@ pub const Pane = struct { } pub fn ensureCursorVisible(pane: *Pane) void { + File.geometry(pane); const off = pane.scroll(); var in_context = false; - // Maps survive until the next render. A jump may have changed scroll - // already, so only treat an ancestor as pinned in the matching view. + // The map is the view's as it is now (File.geometry): an ancestor is + // pinned only in the view it was found for. if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == off) { for (pane.wrap_line[0..pane.context_rows]) |row| { if (row == pane.body.cur_row) { diff --git a/src/pardes.zig b/src/pardes.zig index 2bee1f98..b5e729bb 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -61,6 +61,8 @@ pub const config = @import("config.zig"); pub const pdf_enabled = panes.Pdf.enabled; pub const pdf = panes.Pdf.pdf; pub const memory = @import("memory.zig"); +pub const tape = @import("tape.zig"); +pub const walk = @import("walk.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -165,6 +167,8 @@ 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"); const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; @@ -504,6 +508,7 @@ test { _ = @import("Finder.zig"); _ = @import("Tables.zig"); _ = @import("Handover.zig"); + _ = @import("walk.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -759,6 +764,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest pane.file.?.tree_context = true; pane.file.?.scroll = 8; pane.msg_len = 0; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expect(p.settings.tree_context_tag_style); const full_height = p.surface.body_layers[0].viewport.h; @@ -766,6 +772,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqual(FontRole.tagline, p.surface.body_layers[0].cells[0].style.font_role); pane.msg[0] = '!'; pane.msg_len = 1; + p.sync(); _ = try p.render(p.scratch.allocator()); // An overlay takes nothing: the message is a chip OVER the body's first // row, so the viewport keeps every row it had. @@ -783,10 +790,12 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqualStrings("!", band.cells[p.tagCapacity(band.viewport.w) - 2].grapheme()); pane.msg_len = 0; p.leader_on = true; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(full_height, p.surface.body_layers[0].viewport.h); p.leader_on = false; try std.testing.expect(p.executeBuiltinLine(0, "TreeContextTagStyle")); + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(@as(u16, 0), p.surface.body_layers[0].rows); try std.testing.expectEqual(@as(u16, 0), pane.body_rows); @@ -3763,6 +3772,7 @@ pub const Effect = union(enum) { fn Buf(comptime n: usize) type { return struct { + pub const replica = .{ .len = .{.{ "data", "len" }} }; data: [n]u8 = undefined, len: u16 = 0, pub const cap = n; @@ -4701,6 +4711,26 @@ pub const Pardes = struct { /// The message row's entry points, called from everywhere as /// `p.setMessage(...)`; they live with the rest of it in Messages.zig. + /// What the replay digest leaves out (src/walk.zig): what drawing + /// keeps, what is recomputed, and what only the session that performs + /// effects has. + pub const replica = .{ + .local = .{ + // A frame, and the animations and timings that make it. + "surface", "presentation", "frames", "chrome_animation", "lifts", "lift_serials", + "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "frame_arena", "scratch", + "needs_frame", "present_skipped", "shell_continuous", "native_image_patches", + // Drained by every pump before its digest; a replica's queues + // are its own. + "effects", "in_q", + }, + .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, + // The host's handles, the 9P server's fids and log, the store file + // every window of the user shares (and Recent read from it). + .authority = .{ "host", "fs", "store", "recent" }, + .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, + }; + pub const setStatus = Messages.setStatus; pub const setMessage = Messages.setMessage; pub const reportError = Messages.reportError; @@ -4739,12 +4769,26 @@ pub const Pardes = struct { return p; } + /// A new session, its tape written from here when PARDES_RECORD asks. pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes { + tape.startFromEnv(); + const taped = tape.enter(null, .{ .init = opts }); + const p = initSession(gpa, opts) catch |err| { + tape.leave(null, taped); + return err; + }; + tape.leave(p, taped); + return p; + } + + fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { // A new session draws a tab at the default until `Tab` says otherwise. config.tab_now = config.tab_width; const p = try create(gpa, opts); errdefer p.deinit(); p.boot = Boot.of(opts); + // Asked once, at the start, whoever asks first after. + _ = p.sessionDir(); switch (p.boot) { .document => { const path = opts.file.?; @@ -4877,7 +4921,9 @@ pub const Pardes = struct { /// The store read once at start: a new session's, Recent from it. pub fn loadRecent(p: *Pardes) void { - p.store = Store.init(p.opts.store_path); + // A replica's store is its own, in memory: the recording session's + // file is every window's of its user (src/tape.zig). + p.store = Store.init(if (tape.replaying()) null else p.opts.store_path); p.store.load(p); } @@ -4941,6 +4987,9 @@ pub const Pardes = struct { } pub fn deinit(p: *Pardes) void { + const taped = tape.enter(p, .{ .deinit = tape.current(p) }); + tape.forget(p); + defer tape.leave(null, taped); if (comptime hosted) if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); @@ -5001,6 +5050,8 @@ pub const Pardes = struct { /// shell feedback after write_dump: remember where the dump landed (the /// topbar grows a `Restore ` word pointing at it) pub fn setLastDump(p: *Pardes, path: []const u8) void { + const taped = tape.enter(p, .{ .set_last_dump = path }); + defer tape.leave(p, taped); p.pointRestoreAt(path); ctlfs.events.notePath(p, "dump", path); } @@ -5028,6 +5079,8 @@ pub const Pardes = struct { /// the shell polls this each frame: a pending Restore's dump path, or null pub fn takeRestore(p: *Pardes) ?[]const u8 { + const taped = tape.enter(p, .take_restore); + defer tape.leave(p, taped); const r = p.restore_req; p.restore_req = null; return r; @@ -5386,6 +5439,8 @@ pub const Pardes = struct { /// The shell reports the spawned pane's working directory (and later cwd /// changes it observes, e.g. via /proc//cwd before each frame). pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void { + const taped = tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; // A command pane stays the directory's it was run for (newCommand). if (pane.command != null) return; @@ -5415,6 +5470,8 @@ pub const Pardes = struct { } pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { + const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); + defer tape.leave(p, taped); if (id < MAX_PANES) if (p.panes[id]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; @@ -5511,10 +5568,14 @@ pub const Pardes = struct { /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. pub fn invalidate(p: *Pardes) void { + const taped = tape.enter(p, .invalidate); + defer tape.leave(p, taped); p.needs_frame = true; } pub fn reloadWatchedFile(p: *Pardes, id: u8, bytes: []const u8) bool { + const taped = tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } }); + defer tape.leave(p, taped); p.invalidate(); look.invalidateLookHover(p, id); const applied = p.applyWatchedFileChanged(id, bytes); @@ -5531,6 +5592,8 @@ pub const Pardes = struct { } pub fn nextEffect(p: *Pardes) ?Effect { + const taped = tape.enter(p, .next_effect); + defer tape.leave(p, taped); p.refillPendingWrites(); if (p.effects_len == 0) { p.effects_head = 0; @@ -5667,6 +5730,8 @@ pub const Pardes = struct { /// on it, or written to it from now on, hears `error shell gone` /// rather than wait for a prompt that never comes. pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; pane.shell_failed = true; ctlfs.pty.shellGone(p, pane, false); @@ -5688,6 +5753,8 @@ pub const Pardes = struct { /// A shell started again in a terminal whose shell runs (pty/ctl's /// exec) did not start: the host kept the running one, so only said. pub fn restartFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id] == null) return; p.sayShellFailure(id, err); } @@ -5700,9 +5767,7 @@ pub const Pardes = struct { // The script is there; the program its `#!` names is not. var said: [320]u8 = undefined; var interp: [256]u8 = undefined; - var buf: [std.fs.max_path_bytes]u8 = undefined; - const Shell = if (comptime hosted) @import("host_io.zig").Shell else void; - const name: ?[]const u8 = if (comptime hosted) (if (Shell.find(pane.shell orelse p.shellBin(), &buf)) |f| Shell.missingInterpreter(f, &interp) else null) else null; + const name: ?[]const u8 = if (comptime hosted) tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }) else null; p.reportFailure(id, if (name) |n| std.fmt.bufPrint(&said, "shell: interpreter {s} not found", .{n}) catch "shell: interpreter not found" else "shell: interpreter not found"); } else { p.reportError(id, "shell", err); @@ -5712,6 +5777,13 @@ pub const Pardes = struct { p.noteLateFailure(id); } + /// The program the shell's `#!` names that is not there, in `out`. + fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 { + const Shell = @import("host_io.zig").Shell; + var buf: [std.fs.max_path_bytes]u8 = undefined; + return Shell.missingInterpreter(Shell.find(bin, &buf) orelse return null, out); + } + /// A Save makes the directories its file goes in, and any above them, /// first: one rule, for the pane's own name and `Save path` alike. A /// directory it cannot make leaves the write to fail and say why. @@ -5729,11 +5801,13 @@ pub const Pardes = struct { const local = if (comptime hosted) filesystem.localPath(dir) else null; const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory; const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; - const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false); + const denied = missing and (if (comptime hosted) (if (local) |l| tape.value(.denied, filesystem.deniedAbove, .{l}) else false) else false); return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied }; } pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id]) |pane| if (pane.file) |*f| { // The `-%` spelling fs.zig already uses for "make this dirty". f.saved_revision = f.revision -% 1; @@ -5755,6 +5829,8 @@ pub const Pardes = struct { /// log, and the 9P write that asked for it fails with it, as a failed /// Save's does. pub fn dumpFailed(p: *Pardes, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } }); + defer tape.leave(p, taped); var what: [limits.host_path_cap + 8]u8 = undefined; const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump"; // Into a DumpDir that is not there: said so, ENOENT, as a Save is; @@ -5797,6 +5873,8 @@ pub const Pardes = struct { } pub fn perform(p: *Pardes, e: Effect) void { + const taped = tape.enter(p, .{ .perform = e }); + defer tape.leave(p, taped); // A Save performed on one twin is every twin's (File.syncTwins). defer panes.File.syncTwins(p); const v = p.host.vtable; @@ -5927,16 +6005,23 @@ pub const Pardes = struct { } } - pub fn pump(p: *Pardes, h: Host) !void { - p.host = h; + pub fn pump(p: *Pardes, host: Host) !void { + p.host = host; + const taped = tape.enter(p, .pump); + defer tape.leave(p, taped); + // The host as the tape sees it, while it records. + const h = p.host; const v = h.vtable; if (v.wait_input) |f| { // Asleep until input or until core animation next needs a // frame; 0 is no timeout at all. // A render-time animation draws every loop: the wait only takes // what input is there. + // The clock read whether or not it is wanted: what a replica + // waits on is its own (a picture decoding wakes only a frame + // that draws it), and the tape has the same calls either way. + const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const timeout: u32 = if (p.continuous()) 1 else if (p.nextWake()) |due| wake: { - const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms; break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32))); } else 0; @@ -5972,6 +6057,7 @@ pub const Pardes = struct { const surface = try p.render(p.frame_arena.allocator()); p.frames.mark_render = FrameStats.nowNs(); if (v.present) |f| f(h.ctx, surface); + Stream.askAhead(p); // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. const skipped = p.present_skipped; @@ -5990,6 +6076,8 @@ pub const Pardes = struct { /// that changes a pane costs a frame, which is why a round trip on a /// local socket costs microseconds and not a vsync. pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { + const taped = tape.enter(p, .{ .serve_fs = req }); + defer tape.leave(p, taped); // The held write of an Edit whose commands run, asked again by a // retry of its park: still waiting, or its answer, never run twice. if (p.fs.edit_hold) |*h| if (h.names(req)) { @@ -6028,6 +6116,8 @@ pub const Pardes = struct { } pub fn update(p: *Pardes, ev: Event) void { + const taped = tape.enter(p, .{ .update = ev }); + defer tape.leave(p, taped); defer panes.File.syncTwins(p); defer p.reportFocus(); // A tick with nothing animating is the one event that cannot change @@ -6074,6 +6164,7 @@ pub const Pardes = struct { .tick => { Stream.step(p); Messages.advanceMessages(p); + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.tables.step(pane.gpa, f); for (0..p.panes.len) |id| exec.flushReplEnter(p, id, false); }, else => {}, @@ -6661,10 +6752,16 @@ pub const Pardes = struct { pub fn sessionDir(p: *Pardes) []const u8 { if (p.session_dir_len) |n| return p.session_dir[0..n]; if (comptime !hosted) return "/"; - if (std.c.getcwd(&p.session_dir, p.session_dir.len) == null) return "/"; - const n: u16 = @intCast(std.mem.indexOfScalar(u8, &p.session_dir, 0) orelse return "/"); - p.session_dir_len = n; - return p.session_dir[0..n]; + // The directory the session that recorded was started in. + const dir = tape.value(.cwd, processDir, .{&p.session_dir}) orelse return "/"; + std.mem.copyForwards(u8, &p.session_dir, dir); + p.session_dir_len = @intCast(dir.len); + return p.session_dir[0..dir.len]; + } + + fn processDir(buf: *[limits.host_path_cap]u8) ?[]const u8 { + if (std.c.getcwd(buf, buf.len) == null) return null; + return buf[0 .. std.mem.indexOfScalar(u8, buf, 0) orelse return null]; } /// The directory a word run from pane `id`'s text or tag runs in, or, @@ -7007,6 +7104,8 @@ pub const Pardes = struct { /// What was said of a failure just now on pane `id` is also the waiting /// 9P write's (9p_io, `fs.late_failure`). pub fn noteLateFailure(p: *Pardes, id: usize) void { + const taped = tape.enter(p, .{ .note_late_failure = id }); + defer tape.leave(p, taped); // Said by reportFailure just now: its words fitted whole, the // reason kept; else what the message row has. const said = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0) @@ -7182,9 +7281,11 @@ pub const Pardes = struct { // ---- dumb undo/redo: whole-state snapshots, one per edit op ---- pub fn acknowledgePanelPresentation(p: *Pardes, tracks: []const animation.Track) void { + const taped = tape.enter(p, .{ .acknowledge_panel_presentation = tracks }); + defer tape.leave(p, taped); p.presentation.acknowledge(p, tracks); if (p.pointer_inside and p.drag != .none) { - if (p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row)) |mapped| { + if (p.pointerCell(p.pointer_raw_col, p.pointer_raw_row)) |mapped| { p.hover_col = mapped.col; p.hover_row = mapped.row; mouse.dragUpdate(p, mapped.col, mapped.row, p.pointer_body_hit); @@ -7193,6 +7294,14 @@ pub const Pardes = struct { look.refreshLookHoverFromRaw(p); } + /// The cell of the session's grid a pointer at this cell of the frame + /// is over, seen through the frame (a pane sliding in): the recording + /// session's answer, whose frames these were (src/tape.zig). Null over + /// a pane on its way out. + pub fn pointerCell(p: *const Pardes, col: u16, row: u16) ?Presentation.CellPosition { + return tape.value(.pointer, Presentation.pointer, .{ &p.presentation, p.screen_w, p.screen_h, col, row }); + } + pub fn abandonPanelAnimations(p: *Pardes) void { p.presentation.cancel(); look.cancelLookHover(p); @@ -7588,6 +7697,10 @@ pub const Pardes = struct { state.reply_len = 0; }; const r = p.rects[id]; + // The session's part of the pane's view geometry (File.geometry), + // and its context declarations. + if (pane.file) |*f| panes.File.refreshContext(p, pane, f); + pane.view = .{ .wrap = p.settings.wrap, .layered = p.settings.tree_context_tag_style, .body_h = p.row_metrics.body_h, .tagline_h = p.row_metrics.tagline_h, .rect_h = r.h }; // The tag takes a row per line, leaving the body one row if it // had any; a folded pane shows its first line only. // Measured on the stack: this runs on every event, and not @@ -7661,6 +7774,9 @@ pub const Pardes = struct { // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols); panes.terminal.releasePendingCommandIfReady(p, id, pane); + // Which bands a pane shows is the step's, as its geometry is: + // what is drawn under them, and where a click lands, read it. + Messages.collectNotices(p, pane, r, id); // The greeting `ls` of a new terminal in a directory is typed at // its shell's FIRST prompt, or never: typed later it would land // in whatever runs then, or in the middle of a line someone @@ -7675,6 +7791,9 @@ pub const Pardes = struct { }, }; } + // A header's caret kept in its view, as a pane's is: the step's, not + // the frame's (paintHeaders draws what this leaves). + tagline.revealHeader(p); } pub fn theme(p: *const Pardes) *const Theme { @@ -7780,6 +7899,8 @@ pub const Pardes = struct { /// the look-hover delay) is jumped to its end in one step and drawn /// once. Idle, time is not banked: the next animation starts from now. pub fn advance(p: *Pardes, now_ns: u64) void { + const taped = tape.enter(p, .{ .advance = now_ns }); + defer tape.leave(p, taped); // The first time a shell's clock is seen, core time starts there. const started = p.clock_started; p.clock_started = true; @@ -7787,7 +7908,10 @@ pub const Pardes = struct { if (!started) p.stepped_ns = now_ns; const frame = animation.frame_ns; var moving: u32 = 0; - while (p.nextWake()) |due| { + // The session's ticks are the recording session's: a picture still + // decoding or a fade still moving asks for frames the way a message + // waiting to go does, and a replica that draws nothing has none. + while (tape.value(.wake, nextWake, .{p})) |due| { if (due > now_ns) break; // Only waiting until `due`: its counters move there at once. const held = (due - p.stepped_ns) / frame; @@ -8136,8 +8260,12 @@ test "a prompt chip is a band with its caret, still on the grid, and only the ch // Inside it is chrome: mapped as body it would land on Sel row 0, which is // the TAG row, so a sweep there would highlight the tagline instead. + // The sweep closed the prompt, and its chip with it: open it again. pane.clearPointerSelection(0); pane.sel[0] = .{}; + p.startPrompt(pane, &pane.body, .{ .search = "/" }); + p.sync(); + try std.testing.expectEqual(left, pane.notices.left[0]); p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + r.w - 3, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = r.x + r.w - 2, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + r.w - 2, .row = row } }); @@ -8523,6 +8651,9 @@ test "Esc back into a file leaves its view where it was" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "src/memory.zig" }); defer p.deinit(); + // Rows are lines: the centring below counts lines, and a wrapped line + // above the cursor would push it out of the view it centres. + p.settings.wrap = false; p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc const shell = p.active; diff --git a/src/pdf.zig b/src/pdf.zig index 18037bb7..d3796d70 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -703,6 +703,9 @@ pub const Document = struct { handle: *c.pardes_pdf_document, pages: usize, + /// The open document is MuPDF's, made from the file's bytes (src/walk.zig). + pub const replica = .{ .derived = .{"handle"} }; + pub fn deinit(document: *Document) void { c.pardes_pdf_close(document.handle); document.* = undefined; diff --git a/src/pdf_view.zig b/src/pdf_view.zig index f6d26eac..4ddbf299 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -84,6 +84,7 @@ pub const TintKey = if (enabled) struct { pub const Highlight = if (enabled) pdf.Highlight else void; pub const Highlights = if (enabled) struct { const Page = struct { page: usize, items: []const Highlight }; + pub const replica = .{ .len = .{.{ "pages", "len" }} }; pages: [3]Page = undefined, len: usize = 0, /// Repaint whenever the set changes, not only when a page's raster was @@ -619,6 +620,9 @@ pub const SectionsOutput = if (enabled) struct { pub const SelectionUpdate = enum { failed, stationary, unchanged, changed }; pub const State = if (enabled) struct { + /// What the replay digest leaves out (src/walk.zig): the pages' pixels, + /// rendered from the document, and the lanes rendering them. + pub const replica = .{ .derived = .{ "rasters", "rasters_len" }, .local = .{"lanes"} }; path: []u8, document: Document, page: usize = 0, diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 73bac520..37cf22d0 100644 --- a/src/selection_pipe.zig +++ b/src/selection_pipe.zig @@ -184,6 +184,7 @@ pub const Tasks = struct { future: std.Io.Future(void), }; + pub const replica = .{ .len = .{.{ "items", "len" }} }; items: [capacity]Task = undefined, len: usize = 0, diff --git a/src/tape.zig b/src/tape.zig new file mode 100644 index 00000000..eaf1a9fa --- /dev/null +++ b/src/tape.zig @@ -0,0 +1,741 @@ +//! The recorder and its tape (docs/next-daemon.md, phase 1). Recording, +//! everything the core consumes is appended in the order it consumed it, +//! which is the turn's order (a 9P request served while a step is out in a +//! syscall lands inside that syscall's record): +//! +//! - each call the host makes into the core (`Input`), with its arguments; +//! - each call the core makes out into the host -- a Host function, or a +//! syscall of its own (`Site`) -- and what it returned; +//! - each field the host wrote in the core between calls (`watched`); +//! - after every call the host made at the top, the session's digest. +//! +//! Replaying (`PARDES_REPLAY= pardes`, the same build), a core takes +//! its inputs off the tape instead: a host call answers what it answered, a +//! site returns what it returned, nothing is performed and no syscall is +//! made, and each digest is checked against the recorded one -- by two +//! replicas at different addresses and a third that never draws. +//! +//! The local path pays a null check a call: `on` is null unless +//! PARDES_RECORD names where to write, `.tape`. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const Pardes = pardes.Pardes; +const VTable = pardes.Host.VTable; +const libc = std.c; + +pub var on: ?*Tape = null; + +/// Every call into the core a host makes (a 9P connection task's too). +pub const Input = union(enum) { + init: pardes.Options, + init_from_dump: struct { opts: pardes.Options, bytes: []const u8 }, + restore: struct { bytes: []const u8, from: []const u8 }, + /// Whether it was the session's own core, or one a Restore replaced. + deinit: bool, + update: pardes.Event, + serve_fs: pardes.ctlfs.Req, + pump, + perform: pardes.Effect, + next_effect, + advance: u64, + render, + take_restore, + set_cwd: struct { id: usize, cwd: []const u8 }, + acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool }, + shell_failed: struct { id: u8, err: anyerror }, + restart_failed: struct { id: u8, err: anyerror }, + save_failed: struct { id: u8, path: []const u8, err: anyerror }, + dump_failed: struct { path: []const u8, err: anyerror }, + set_last_dump: []const u8, + set_message: struct { id: usize, text: []const u8 }, + set_status: struct { id: usize, text: []const u8 }, + report_error: struct { id: usize, operation: []const u8, err: anyerror }, + reload_watched_file: struct { id: u8, bytes: []const u8 }, + note_late_failure: usize, + invalidate, + acknowledge_panel_presentation: []const pardes.animation.Track, + run_closed_lines, + file_deleted: u8, +}; + +/// The core's own syscalls (each `turn.yield()`s around itself), numbered +/// after the Host functions on the tape. +pub const Site = enum(u16) { + // Out of the turn (`site`). + os_stat, + os_listing, + os_rw, + make_dirs, + resolve, + read_mount, + read_file, + dir_listing, + kind, + probe, + preview, + // Read in place (`value`). + clock, + budget, + wake, + env, + interpreter, + denied, + shell, + mtime, + cwd, + pointer, +}; + +/// Fields a host writes straight into the core between its calls. +const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" }; + +const Kind = enum(u8) { input, done, host, ret, set, digest }; +const host_fns = @typeInfo(VTable).@"struct".field_names; +/// The `set` that says which Host functions the recording host had. +const set_mask = 0xff; + +pub const Tape = struct { + gpa: std.mem.Allocator, + replay: bool, + /// The core whose digest is taken (a Restore replaces it). + core: ?*Pardes = null, + /// The host has the turn: a call into the core now is an input, and a + /// call out of it is the host's to answer. + host_side: bool = true, + depth: u32 = 0, + vtable: VTable = .{}, + // Recording. + fd: c_int = -1, + buf: std.ArrayList(u8) = .empty, + scratch: std.Io.Writer.Allocating, + real: pardes.Host = .{}, + seen: [watched.len]u64 = @splat(0), + // Replaying. + in: walk.Reader = .{ .bytes = &.{} }, + arena: std.heap.ArenaAllocator, + last_ret: walk.Reader = .{ .bytes = &.{} }, + mask: u64 = 0, + headless: bool = false, + digests: u64 = 0, + diverged: ?[]const u8 = null, + /// The input being applied, for a divergence's report. + applying: []const u8 = "", + /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too + /// (to find the step a divergence starts at), not only the host's last. + every: bool = false, + + fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { + t.scratch.clearRetainingCapacity(); + walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory"); + const payload = t.scratch.written(); + const len: u32 = @intCast(payload.len); + t.buf.append(t.gpa, @intFromEnum(kind)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory"); + if (t.buf.items.len > 1 << 20) t.flush(); + } + + fn flush(t: *Tape) void { + var at: usize = 0; + while (at < t.buf.items.len) { + const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at); + if (n <= 0) break; + at += @intCast(n); + } + t.buf.clearRetainingCapacity(); + } + + /// What the host wrote between calls, said before the core goes on. + fn noteWrites(t: *Tape) void { + const p = t.core orelse return; + inline for (watched, 0..) |name, i| { + const now = walk.digest(@FieldType(Pardes, name), &@field(p, name)); + if (now != t.seen[i]) { + t.seen[i] = now; + const set: struct { u8, @FieldType(Pardes, name) } = .{ i, @field(p, name) }; + t.record(.set, @TypeOf(set), &set); + } + } + } + + fn seeFields(t: *Tape) void { + const p = t.core orelse return; + inline for (watched, 0..) |name, i| t.seen[i] = walk.digest(@FieldType(Pardes, name), &@field(p, name)); + } + + /// The host's functions go through the tape while it records, whoever + /// set `p.host` last. + fn wrap(t: *Tape, p: *Pardes) void { + if (p.host.vtable == &t.vtable) return; + // The same host given again (each pump is given its host): only + // the wrapper put back. + if (p.host.vtable == t.real.vtable and p.host.ctx == t.real.ctx) { + p.host = .{ .ctx = t, .vtable = &t.vtable }; + return; + } + t.real = p.host; + var mask: u64 = 0; + inline for (host_fns, 0..) |f, i| if (@field(t.real.vtable.*, f) != null) { + mask |= @as(u64, 1) << i; + }; + p.host = t.hostOf(mask); + const set: struct { u8, u64 } = .{ set_mask, mask }; + t.record(.set, @TypeOf(set), &set); + } + + /// The tape as a host with these functions. + fn hostOf(t: *Tape, mask: u64) pardes.Host { + t.mask = mask; + t.vtable = .{}; + inline for (host_fns, 0..) |f, i| if (mask & (@as(u64, 1) << i) != 0 or + (t.headless and comptime std.mem.eql(u8, f, "watched"))) + { + @field(t.vtable, f) = &Wrap(i).f; + }; + return .{ .ctx = t, .vtable = &t.vtable }; + } + + fn enterInput(t: *Tape, p: ?*Pardes, input: *const Input) Entered { + if (!t.host_side) return .{}; + t.host_side = false; + t.depth += 1; + if (t.replay) return .{ .outer = true }; + if (p) |core| { + if (t.core == null) t.core = core; + t.noteWrites(); + t.wrap(core); + } + t.record(.input, Input, input); + return .{ .outer = true }; + } + + fn leaveInput(t: *Tape, p: ?*Pardes) void { + t.depth -= 1; + t.host_side = true; + if (t.replay) return; + if (p) |core| if (t.core == null) { + t.core = core; + t.wrap(core); + }; + t.record(.done, void, &{}); + if (t.depth == 0 or t.every) if (t.core) |core| { + var parts: Parts = undefined; + const d = digestParts(core, &parts); + t.record(.digest, []const u64, &d); + }; + if (t.depth == 0) t.flush(); + t.seeFields(); + } + + // ---- replaying ---- + + const Record = struct { kind: Kind, payload: walk.Reader }; + + fn next(t: *Tape) ?Record { + const head = t.in.take(5) catch return null; + const len = std.mem.bytesToValue(u32, head[1..5]); + const payload = t.in.take(len) catch return null; + return .{ .kind = @enumFromInt(head[0]), .payload = .{ .bytes = payload } }; + } + + fn peek(t: *const Tape) ?Kind { + if (t.in.bytes.len - t.in.at < 5) return null; + return @enumFromInt(t.in.bytes[t.in.at]); + } + + fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { + if (t.diverged != null) return; + t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why; + } + + /// A host call the headless replica does not make (it draws no frame, + /// so presents none): what the host told the core inside it (what it + /// presented, `acknowledge_panel_presentation`) is applied all the + /// same, the host's own calls below it stepped over. + fn skipHost(t: *Tape) void { + while (t.diverged == null) { + var rec = t.next() orelse return; + switch (rec.kind) { + .ret => return, + .input => t.apply(&rec.payload), + .set => t.applySet(&rec.payload), + .host => t.skipHost(), + .digest => t.check(&rec.payload), + .done => return t.diverge("a host call's records are out of order", .{}), + } + } + } + + /// Over a call the replica does not make (the headless one's present), + /// what was applied inside it too. + fn skip(t: *Tape) void { + var depth: usize = 1; + while (depth > 0) { + const rec = t.next() orelse return; + switch (rec.kind) { + .host, .input => depth += 1, + .ret, .done => depth -= 1, + else => {}, + } + } + } + + /// The records of an input up to its end: what was applied inside it, + /// as it was; the headless replica steps over the host calls it did + /// not make. + fn finishInput(t: *Tape) void { + while (t.diverged == null) { + const kind = t.peek() orelse return t.diverge("the tape ends inside an input", .{}); + var rec = t.next().?; + switch (kind) { + .done => return, + .set => t.applySet(&rec.payload), + .host => if (t.headless) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}), + .input => t.apply(&rec.payload), + .digest => t.check(&rec.payload), + .ret => return t.diverge("the core's input ended before the recorded one", .{}), + } + } + } + + /// The records inside a host call up to its return, whose result is + /// left in `last_ret`. + fn finishHost(t: *Tape) bool { + while (t.diverged == null) { + var rec = t.next() orelse { + t.diverge("the tape ends inside a host call", .{}); + return false; + }; + switch (rec.kind) { + .ret => { + t.last_ret = rec.payload; + return true; + }, + .input => t.apply(&rec.payload), + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .done => { + t.diverge("a host call's records are out of order", .{}); + return false; + }, + } + } + return false; + } + + fn expectHost(t: *Tape, id: u16, comptime what: []const u8) bool { + if (t.diverged != null) return false; + const rec = t.next() orelse { + t.diverge("the core called " ++ what ++ " past the tape's end", .{}); + return false; + }; + if (rec.kind != .host or std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) != id) { + const other: u16 = if (rec.kind == .host) std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) else 0; + t.diverge("the core called " ++ what ++ " where the tape has a {t} ({d})", .{ rec.kind, other }); + return false; + } + const outer = !t.host_side; + t.host_side = true; + defer t.host_side = !outer; + return t.finishHost(); + } + + /// A recorded digest against the replica's own. + fn check(t: *Tape, r: *walk.Reader) void { + const want = walk.decode([]const u64, r, t.arena.allocator()) catch return; + const p = t.core orelse return; + var parts: Parts = undefined; + const got = digestParts(p, &parts); + if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, partsDiffer(got, want, t.arena.allocator()) }); + t.digests += 1; + } + + fn applySet(t: *Tape, r: *walk.Reader) void { + const which = (r.take(1) catch return)[0]; + if (which == set_mask) { + const host = t.hostOf(std.mem.bytesToValue(u64, r.take(8) catch return)); + if (t.core) |p| p.host = host; + return; + } + const p = t.core orelse return; + inline for (watched, 0..) |name, i| if (i == which) { + @field(p, name) = walk.decode(@FieldType(Pardes, name), r, t.arena.allocator()) catch return; + }; + } + + /// One input, applied to the replica as the host applied it. + fn apply(t: *Tape, r: *walk.Reader) void { + // A session's options are kept by its core (`opts`): theirs for good. + const Tag = std.meta.Tag(Input); + const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{}))); + r.at -= @sizeOf(Tag); + const kept = tag == .init or tag == .init_from_dump or tag == .restore; + const input = walk.decode(Input, r, if (kept) t.gpa else t.arena.allocator()) catch return t.diverge("a short input", .{}); + const outer = t.applying; + t.applying = switch (input) { + .update => |ev| @tagName(ev), + else => @tagName(input), + }; + defer t.applying = outer; + t.host_side = true; + defer t.host_side = true; + switch (input) { + .init, .init_from_dump => { + var opts = switch (input) { + .init => |o| o, + .init_from_dump => |d| d.opts, + else => unreachable, + }; + opts.image_allocator = t.gpa; + opts.pdf_allocator = t.gpa; + opts.tree_sitter_allocator = t.gpa; + opts.frame_allocator = t.gpa; + t.core = switch (input) { + .init => Pardes.init(t.gpa, opts), + .init_from_dump => |d| pardes.dump.initFromDump(t.gpa, opts, d.bytes), + else => unreachable, + } catch |err| return t.diverge("init failed: {t}", .{err}); + t.core.?.host = t.hostOf(t.mask); + return t.finishInput(); + }, + else => {}, + } + const p = t.core orelse return t.diverge("an input before the core", .{}); + switch (input) { + .init, .init_from_dump => unreachable, + .restore => |rs| { + const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err}); + t.core = nc; + nc.host = t.hostOf(t.mask); + }, + // The session is over (or the core a Restore replaced is + // freed): nothing done now is compared. + .deinit => |own| { + if (own) t.core = null; + return t.skip(); + }, + .update => |ev| p.update(ev), + .serve_fs => |req| _ = p.serveFs(req), + .pump => p.pump(t.hostOf(t.mask)) catch {}, + .perform => |e| p.perform(e), + .next_effect => _ = p.nextEffect(), + .advance => |now| p.advance(now), + .render => if (t.headless) return t.skip() else { + _ = p.frame_arena.reset(.retain_capacity); + _ = p.render(p.frame_arena.allocator()) catch {}; + }, + .take_restore => _ = p.takeRestore(), + .set_cwd => |a| p.setCwd(a.id, a.cwd), + .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks), + .shell_failed => |a| p.shellFailed(a.id, a.err), + .restart_failed => |a| p.restartFailed(a.id, a.err), + .save_failed => |a| p.saveFailed(a.id, a.path, a.err), + .dump_failed => |a| p.dumpFailed(a.path, a.err), + .set_last_dump => |path| p.setLastDump(path), + .set_message => |a| p.setMessage(a.id, a.text), + .set_status => |a| p.setStatus(a.id, a.text), + .report_error => |a| p.reportError(a.id, a.operation, a.err), + .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes), + .note_late_failure => |id| p.noteLateFailure(id), + .invalidate => p.invalidate(), + .acknowledge_panel_presentation => |tracks| p.acknowledgePanelPresentation(tracks), + .run_closed_lines => pardes.ctlfs.runClosedLines(p), + .file_deleted => |id| pardes.panes.File.deleted(p, id), + } + t.finishInput(); + } +}; + +const pardes_fields = walk.fieldNames(Pardes); +const stream_fields = walk.fieldNames(pardes.Stream); +const pane_fields = walk.fieldNames(pardes.Pane); +const file_fields = walk.fieldNames(pardes.panes.File.State); +const places_fields = walk.fieldNames(pardes.panes.Places); +const Parts = [pardes_fields.len + stream_fields.len + pardes.MAX_PANES * (2 + pane_fields.len + file_fields.len + places_fields.len)]u64; + +/// The session's digest as one per field of the core's, its stream's, and +/// each pane's and its file's, so a divergence says where it is. +fn digestParts(p: *const Pardes, parts: *Parts) []const u64 { + var n = walk.fieldDigests(Pardes, p, parts[0..pardes_fields.len]).len; + n += walk.fieldDigests(pardes.Stream, &p.stream, parts[n..][0..stream_fields.len]).len; + for (p.panes, 0..) |slot, id| { + const pane = slot orelse continue; + parts[n] = id; + parts[n + 1] = @intFromBool(pane.file != null); + n += 2; + n += walk.fieldDigests(pardes.Pane, pane, parts[n..][0..pane_fields.len]).len; + if (pane.file) |*f| n += walk.fieldDigests(pardes.panes.File.State, f, parts[n..][0..file_fields.len]).len; + n += walk.fieldDigests(pardes.panes.Places, &pane.places, parts[n..][0..places_fields.len]).len; + } + return parts[0..n]; +} + +/// The fields whose digests differ, named. +fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []const u8 { + var said: std.ArrayList(u8) = .empty; + if (got.len != want.len) said.print(a, "the panes there are ({d} parts, {d} recorded); ", .{ got.len, want.len }) catch {}; + const n = @min(got.len, want.len); + var i: usize = 0; + var count: usize = 0; + while (i < pardes_fields.len + stream_fields.len and i < n) : (i += 1) { + if (got[i] == want[i]) continue; + const name = if (i < pardes_fields.len) pardes_fields[i] else stream_fields[i - pardes_fields.len]; + said.print(a, "{s}{s} ", .{ if (i < pardes_fields.len) "" else "stream.", name }) catch {}; + count += 1; + } + while (i + 2 <= n and count < 16) { + const id = got[i]; + const has_file = got[i + 1] != 0; + i += 2; + for (pane_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + } + if (has_file) for (file_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.file.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + }; + for (places_fields) |name| { + if (i >= n) break; + if (got[i] != want[i]) { + said.print(a, "pane {d}.places.{s} ", .{ id, name }) catch {}; + count += 1; + } + i += 1; + } + } + return said.items; +} + +/// A call into the core: the host's, and recorded, or the core's own. +pub const Entered = struct { outer: bool = false }; + +pub inline fn enter(p: ?*Pardes, input: Input) Entered { + const t = on orelse return .{}; + return t.enterInput(p, &input); +} + +pub inline fn leave(p: ?*Pardes, e: Entered) void { + if (e.outer) on.?.leaveInput(p); +} + +/// The core a Restore made is the session's from here. +pub fn adopt(p: *Pardes) void { + const t = on orelse return; + if (t.replay) return; + t.core = p; + t.wrap(p); +} + +/// Whether `p` is the core being recorded (a Restore's old one is not). +pub fn current(p: *const Pardes) bool { + const t = on orelse return true; + return t.core == null or t.core == p; +} + +/// The session's core is freed: no digest of it after. +pub fn forget(p: *Pardes) void { + const t = on orelse return; + if (t.core == p) t.core = null; +} + +/// The Host function `i`, through the tape. +fn Wrap(comptime i: usize) type { + const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child; + const info = @typeInfo(Fn).@"fn"; + const R = info.return_type.?; + const P = info.param_types; + return switch (P.len) { + 1 => struct { + fn f(ctx: ?*anyopaque) R { + return hostCall(i, R, ctx, .{}); + } + }, + 2 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?) R { + return hostCall(i, R, ctx, .{a}); + } + }, + 3 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?) R { + return hostCall(i, R, ctx, .{ a, b }); + } + }, + 4 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?) R { + return hostCall(i, R, ctx, .{ a, b, c }); + } + }, + 5 => struct { + fn f(ctx: ?*anyopaque, a: P[1].?, b: P[2].?, c: P[3].?, d: P[4].?) R { + return hostCall(i, R, ctx, .{ a, b, c, d }); + } + }, + else => @compileError("tape: a host function of more arguments"), + }; +} + +fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype) R { + const t: *Tape = @ptrCast(@alignCast(ctx.?)); + const name = host_fns[i]; + if (t.replay) { + // A replica that never draws is never watched: it asks no one. + if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false; + if (!t.expectHost(i, name)) return std.mem.zeroes(R); + var ret = t.last_ret; + if (R == ?[]const u8) { + // fg_name's answer is written into the caller's buffer. + const text = (walk.decode(R, &ret, t.arena.allocator()) catch return null) orelse return null; + const buf = args[args.len - 1]; + const n = @min(text.len, buf.len); + @memcpy(buf[0..n], text[0..n]); + return buf[0..n]; + } + return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R); + } + const real = @field(t.real.vtable.*, name).?; + const id: u16 = i; + t.record(.host, u16, &id); + t.seeFields(); + const outer = !t.host_side; + t.host_side = true; + const r = @call(.auto, real, .{t.real.ctx} ++ args); + t.host_side = !outer; + t.noteWrites(); + t.record(.ret, R, &r); + return r; +} + +/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P +/// request through a mount of this very session is answered meanwhile): +/// recorded with what it returned, or answered from the tape and never +/// made. A result with slices is decoded into `a` on replay, the allocator +/// `f` would have used. +pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return call(s, a, f, args, true); +} + +/// A read of the host's that is no syscall out of the turn (the clock, the +/// environment) or yields for itself: as `site`. +pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return call(s, null, f, args, false); +} + +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.?; + const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); + const id: u16 = @as(u16, host_fns.len) + @intFromEnum(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"); + } + var ret = t.last_ret; + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch @panic("tape: a short result"); + } + // The host's own (a shell's spawn asking the shell), or one inside a + // site's syscall: the outer call is the record. + if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + t.record(.host, u16, &id); + t.seeFields(); + t.host_side = true; + const r = if (yields) out(f, args) else @call(.auto, f, args); + t.host_side = false; + t.noteWrites(); + t.record(.ret, R, &r); + return r; +} + +/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent: +/// what the clock said is taped, so a replica stops its work where this did. +pub fn over(since_ns: u64, budget_ns: u64) bool { + return value(.budget, overNow, .{ since_ns, budget_ns }); +} + +fn overNow(since_ns: u64, budget_ns: u64) bool { + return pardes.FrameStats.nowNs() -| since_ns > budget_ns; +} + +fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + pardes.turn.yield(); + defer pardes.turn.back(); + return @call(.auto, f, args); +} + +/// Asked by `turn.yield`: a syscall a replica must never make. +pub fn replaying() bool { + return if (on) |t| t.replay else false; +} + +/// PARDES_RECORD set: the tape is written from the session's first call. +pub fn startFromEnv() void { + if (on != null) return; + const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return); + if (prefix.len == 0) return; + const gpa = std.heap.c_allocator; + var path_buf: [4096]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return; + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + const t = gpa.create(Tape) catch return; + t.* = .{ .gpa = gpa, .replay = false, .fd = fd, .scratch = .init(gpa), .arena = .init(gpa), .every = libc.getenv("PARDES_RECORD_EVERY") != null }; + on = t; +} + +/// `PARDES_REPLAY=`: the tape replayed into three replicas, each +/// digest checked; 0 when none diverged. +pub fn replayMain(io: std.Io, path: []const u8) u8 { + const gpa = std.heap.c_allocator; + // What a shell starts before its core (tty.zig's localSession). + pardes.image.start(io, gpa); + if (comptime pardes.pdf_enabled) pardes.pdf.start(gpa); + pardes.syntax.start(gpa); + const bytes = std.Io.Dir.cwd().readFileAlloc(std.Io.Threaded.global_single_threaded.io(), path, gpa, .unlimited) catch |err| { + std.debug.print("replay: cannot read {s}: {t}\n", .{ path, err }); + return 2; + }; + const runs = [_]struct { name: []const u8, headless: bool }{ + .{ .name = "first", .headless = false }, + // The first replica's memory is still held: this one's lies elsewhere. + .{ .name = "second", .headless = false }, + .{ .name = "headless", .headless = true }, + }; + var failed = false; + for (runs) |run| { + var t: Tape = .{ .gpa = gpa, .replay = true, .in = .{ .bytes = bytes }, .headless = run.headless, .scratch = .init(gpa), .arena = .init(gpa) }; + on = &t; + defer on = null; + while (t.diverged == null) { + const kind = t.peek() orelse break; + var rec = t.next().?; + switch (kind) { + .input => { + t.apply(&rec.payload); + _ = t.arena.reset(.retain_capacity); + }, + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}), + } + } + // A session killed mid-step leaves its last input cut short: the + // end of the tape, not a divergence. + if (t.diverged) |why| if (std.mem.startsWith(u8, why, "the tape ends")) { + t.diverged = null; + }; + if (t.diverged) |why| { + failed = true; + std.debug.print("replay {s}: {s} replica diverged after {d} digests: {s}\n", .{ path, run.name, t.digests, why }); + } else std.debug.print("replay {s}: {s} replica: {d} digests agree\n", .{ path, run.name, t.digests }); + } + return if (failed) 1 else 0; +} diff --git a/src/terminal.zig b/src/terminal.zig index ce41bd95..0afbe43a 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -159,6 +159,30 @@ pub const State = struct { mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {}, /// The kitty graphics its program placed, as drawn (TerminalImages.zig). images: TerminalImages = if (images_enabled) .{} else {}, + + /// The emulator as its program has made it, for the replay digest + /// (src/walk.zig): the active screen's text, how much history is above + /// it and where the view is, the cursor, the answers not yet written. + /// Not its pages (their addresses are its own), nor the history's text + /// (the screen's was all of it once, and a session's digest is taken + /// every step), nor the kitty images, whose media is read off the host: + /// a replica's terminal is the authority's screen state from phase 5 + /// on (docs/next-daemon.md). + pub fn digest(s: *const State, h: *pardes.walk.Hasher) void { + if (comptime !enabled) return; + const screen = s.vt.screens.active; + const sb = screen.pages.scrollbar(); + const cur = screen.cursor; + const facts = .{ sb.total, sb.offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; + inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); + h.update(s.reply[0..s.reply_len]); + var buf: [4096]u8 = undefined; + var text: std.Io.Writer.Hashing(pardes.walk.Hasher) = .initHasher(h.*, &buf); + if (screen.pages.getBottomRight(.active)) |br| + screen.dumpString(&text.writer, .{ .tl = screen.pages.getTopLeft(.active), .br = br, .unwrap = false }) catch {}; + text.writer.flush() catch {}; + h.* = text.hasher; + } }; const GColor = ghostty_vt.color; diff --git a/test/panes.zig b/test/panes.zig index 0379a03d..996accf1 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -587,6 +587,7 @@ const BodyHitTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 4); @@ -635,6 +636,7 @@ const LineNumberLookTests = struct { pane.scrollBy(30); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expect(layer.context_rows >= 2); @@ -693,6 +695,7 @@ const TreeContextTests = struct { pane.scrollBy(10); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); _ = try p.render(frame.allocator()); const layer = &p.surface.body_layers[0]; try std.testing.expectEqual(@as(u16, 4), layer.context_rows); @@ -747,9 +750,11 @@ const TreeContextTests = struct { var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); pane.scrollBy(7); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expect(p.executeBuiltinLine(0, "TreeContext")); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 4), pane.context_rows); for (0..4) |row| try std.testing.expectEqual(@as(i32, @intCast(row)), pane.wrapAt(@intCast(row)).line); @@ -803,6 +808,7 @@ const TreeContextTests = struct { pane.hscroll = 4; var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 3), pane.context_rows); const rect = p.rects[0]; @@ -822,6 +828,7 @@ const TreeContextTests = struct { // builtin above put up, so the body moved with it. try std.testing.expectEqual(p.bodyTop(pane, p.rects[0]), cursor.y); panes.File.setContent(p, &pane.file.?, try p.gpa.dupe(u8, std.mem.asBytes(&@as([18][28]u8, @splat("// no enclosing declaration\n".*))))); + p.sync(); _ = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 0), pane.context_rows); try std.testing.expectEqual(pane.scroll(), pane.wrapAt(0).line); @@ -840,6 +847,7 @@ const TreeContextTests = struct { pane.scrollBy(2); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); + p.sync(); const surface = try p.render(frame.allocator()); try std.testing.expectEqual(@as(u16, 1), pane.context_rows); try std.testing.expectEqual(@as(i32, 2), pane.wrapAt(1).line); @@ -2871,7 +2879,7 @@ const OutputTests = struct { if (w.line == 0 and w.pad > 0 and w.at < file.location_rows[0].location_end) break vr; } else return error.MissingWrappedLocation; const w = result.wrapAt(@intCast(wrapped)); - const shown = try panes.File.bodyText(frame.allocator(), result, file, true); + const shown = try panes.File.bodyText(frame.allocator(), result, file); const shown_line = modal.lineSlice(shown, wrapped); try std.testing.expect(std.mem.startsWith(u8, shown_line[panes.File.gutterWidth(result)..], "│ │ ")); const rect = p.rects[result_id]; @@ -4897,6 +4905,7 @@ test "Zig keywords keep every rendered byte colored across line-number widths" { p.settings.colors = true; p.settings.wrap = view.wrap; p.presentation.enabled = false; + p.sync(); var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const surface = try p.render(frame.allocator()); diff --git a/test/snapshots/tagnav.golden b/test/snapshots/tagnav.golden index e7c6eca4..8b6752bd 100644 --- a/test/snapshots/tagnav.golden +++ b/test/snapshots/tagnav.golden @@ -37,9 +37,11 @@ == snap topbar-exec grid=100x31 cursor=7,4 |0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn |4: 1 alpha Dump ls -== snap topbar-restore-word grid=100x31 cursor=78,0 +== snap topbar-restore-word grid=100x31 cursor=60,0 +|0: d Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-snap/tagnav/dump.zon |4: 1 alpha ls == snap topbar-restored grid=100x31 cursor=7,4 +|0: Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit Restore /tmp/pardes-sn |4: 1 alpha $ ls |6: 3 gamma $ |7: /tmp/pardes-snap/tagnav/cwd Tty+bash Save Mode F ── restored history ── -- cgit v1.3 From c2b4f235e78735102c691677f08bd1b2403168df Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 17:01:35 -0300 Subject: screen_state.zig: a terminal crosses to a replica as its screen's state, ghostty's own records by content address, a delta a screen of row hashes and the blobs the peer lacks, history fetched by absolute row Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 98 +++++ src/pardes.zig | 2 + src/screen_state.zig | 1102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1202 insertions(+) create mode 100644 src/screen_state.zig (limited to 'src/pardes.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index a39484b8..e3be6af8 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -648,3 +648,101 @@ Found and fixed 2026-10-29 (`src/tty/tty.zig`, `waitInput`). past its row's end ...`). A suite started as a shell's `&` job runs with SIGINT ignored, so four host_io tests whose Ctrl-C must stop a `sleep` fail there; started otherwise, they pass. + +## Screen-state terminal notes + +Built 2026-10-29 (`src/screen_state.zig`), a pure encode and apply pair +over byte buffers for the phase 5 stream; no transport yet. Only tests +reference it, so the tty and GUI binaries carry none of it: no symbol of +it is linked into the tty built from this tree. The local path's A/B, base +against that tty, is the Tty throughput notes' keystroke-to-present. + +- **Both kinds of blob are ghostty's own snapshot records,** not a cell + format of ours. The terminal-wide state (modes, colours and palette, + title, pwd, tab stops, mouse modes, scrolling region) is a TERMINAL + record. A row is the PAGE record of a one-row page the row is cloned + into, which carries its styles, graphemes, hyperlinks, wide cells, wrap + and prompt marks exactly. The replica decodes them with ghostty's + decoders and writes a row with `Screen.clonePartialRowGrowCapacity`. +- **A row's address is a hash of what it shows** (Wyhash of its flags, + its width and its cells by value), not of its blob: ghostty keeps the + source page's style ids when it clones a row, so equal rows give blobs + that differ. The width is in the address, because a blank row is the + same at every width. +- **A delta** is a header (cursor, kitty keyboard flags, the active + area's first row as an absolute row, the history's extent), the TERMINAL + record's address, an address for each active row, an address for each + row that scrolled off since the acknowledged state (the tail, up to a + screen of them), and the blobs for the addresses the peer does not hold. + An unchanged row costs 8 bytes. +- **One delta per peer is in flight,** against the state it acknowledged, + as FrameAck keeps one frame in flight for the tty. A slow peer skips the + states in between. The peer holds exactly the blobs of the state it + acknowledged, at most two screens of rows and the meta, so the authority + knows what it holds without asking. At 207 ms this means at most about 5 + deltas a second to the FreeBSD box; phase 6's predicted echo is meant to + cover typing there. +- **Absolute rows** come from a tracked pin at the last active top. The + rows scrolled since are the distance to the new top; if the pin's page + was pruned, the count jumps past every row the replica could hold. A + resize renumbers (an epoch), and the peer starts afresh. Scrollback is + fetched by absolute row (`encodeRows`, `applyRows`). +- **The replica writes into a ghostty Terminal,** a terminal pane's own, + so `src/terminal.zig` draws it unchanged. A test renders a replica's + terminal pane and its authority's in two cores: the bodies are equal, + cell for cell. +- **Gates.** + - The soak drives an authority with pty bytes and lands deltas at + random, both skipping states and delaying a delta while the authority + moves on. Resizes and fetches of holes also come at random. After each + delta it checks the replica's screen against the authority's as + encoded, and every history row the replica holds outside its holes + against the authority's row at that number. + - It passed with synthetic shell, TUI and `yes` streams at 4 seeds (in + the unit tests, with a 64 KB scrollback so it prunes). It also passed + with 197 recorded streams at 3 seeds each (the test that + PARDES_SCREEN_STATE= turns on): the 192 pane outputs of the snapshot scripts' + tapes, plus real vim, nvim, htop, less and btop sessions recorded + through a pty. Its corpus and recorder are in `.scratch/daemon/term`. +- **Numbers** (ReleaseFast: `PARDES_SCREEN_STATE= zig build core-test + -Dplatform=tty -Doptimize=ReleaseFast -Dtest-filter=screen_state`, which + runs under std.testing's checking allocator): + - Encode, 200x60, every row new: p50 250 us, p99 487 us, 29.8 KB. + - Encode, 200x60, nothing new: p50 71 us, p99 139 us, 534 bytes. That + is the 60 rows hashed and the TERMINAL record encoded to hash it. + - Under `yes` as fast as ghostty takes it in-process (9 to 12 MB/s), + with a delta every 16 ms: 62 deltas a second, 63 KB/s on the wire, + 1 KB a delta (60 rows and the tail's 60, all held). That is bounded by + the frame rate, not the output. + - Under varied text at 36 to 55 MB/s: 355 KB/s on the wire, 5.6 KB a + delta. + - The replica applies a `yes` delta in 2.4 ms, most of it pushing the + rows scrolled past into its history (a row at a time). A delta of + varied text applies in 0.24 ms under the C allocator and 0.95 ms + under the checking one. +- **What proved wrong in the design.** + - "Scrollback fetched on demand" alone loses rows. A row that changed + after the last delta and then scrolled off would sit stale in the + replica's history. The tail fixes the rows that scrolled within a + screen. Older ones, which the replica never saw or saw before they + last changed, are holes: rows it must fetch before it shows them. + - The address had to include the row's width, as above. The soak found + this: a replica reused an 80-column blank row after a resize to 10. + - ghostty narrows pages lazily, so an active row can be wider than its + terminal: only the terminal's columns are hashed and encoded. +- **Not done, or a gap.** + - Kitty images in terminals. + - pardes's own state derived from the stream is not carried: its + `MarkedHandler` prompt phase, command-output pins and the replies it + writes to the pty. ghostty's prompt marks on rows do cross. + - The view's scroll offset is per client and is not sent. + - A replica's core must not resize its pane's terminal itself; the + authority sizes it. + - The replica's history extent follows its own pruning under the + authority's limit, so it can be a page off, and its oldest rows can + be ones the authority has dropped. + - A fetch sends every row as a blob (phase 6's cache will send + addresses). No zstd. + - A replica pushes a flood's rows into its history one at a time. + - The integration into the stream and the cores (a replica pane's + State fed by `apply` instead of `.output`) is the next piece. diff --git a/src/pardes.zig b/src/pardes.zig index b5e729bb..228d97fa 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -63,6 +63,7 @@ pub const pdf = panes.Pdf.pdf; pub const memory = @import("memory.zig"); pub const tape = @import("tape.zig"); pub const walk = @import("walk.zig"); +pub const screen_state = if (terminal_panes) @import("screen_state.zig") else struct {}; pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -509,6 +510,7 @@ test { _ = @import("Tables.zig"); _ = @import("Handover.zig"); _ = @import("walk.zig"); + _ = screen_state; } pub const Builtin = builtins.registry.Builtin(); diff --git a/src/screen_state.zig b/src/screen_state.zig new file mode 100644 index 00000000..51c2ec30 --- /dev/null +++ b/src/screen_state.zig @@ -0,0 +1,1102 @@ +//! Screen-state terminals (docs/next-daemon.md, "Terminals cross as +//! authoritative screen state", phase 5). A replica is sent a terminal's +//! state, never its pty's bytes, so a `cat` of a gigabyte costs it screens. +//! +//! The authority encodes a delta for one peer: a header (the cursor, the +//! active area's first row as an absolute row, the history's extent), the +//! content address of the terminal-wide state, the content address of each +//! row of the active area, and the blobs behind the addresses the peer does +//! not hold. Both kinds of blob are ghostty's own snapshot records, so they +//! cross exactly: the terminal-wide state is a TERMINAL record (modes, +//! colours, title, tab stops...), and a row is the PAGE record of a one-row +//! page (its styles, graphemes, hyperlinks, wide cells, wrap and prompt +//! marks). A row's address is a hash of what the row shows, not of its blob, +//! whose style ids are its source page's. An unchanged row costs 8 bytes. +//! +//! One delta per peer is in flight, as FrameAck keeps one frame in flight +//! for the tty. The next delta is encoded against the state the peer +//! acknowledged, once it has, so a slow peer skips the states in between. +//! The peer holds the blobs of the state it acknowledged and no others. +//! +//! The replica writes each delta into a ghostty Terminal, which a replica's +//! terminal pane draws as a local one (src/terminal.zig reads only that). +//! Rows that scroll off its active area go into its history, made what the +//! authority's are by the delta's tail (their addresses, up to a screen of +//! them). Older ones it never saw, or saw before they last changed, are +//! holes there, fetched by absolute row on demand (`encodeRows`, +//! `applyRows`). +const std = @import("std"); +const vt = @import("ghostty-vt"); +const snapshot = vt.snapshot; +const Terminal = vt.Terminal; +const Page = vt.page.Page; +const Row = vt.page.Row; +const Pin = vt.Pin; +const Screen = vt.Screen; + +// ponytail: 64 bits: two of a million distinct rows collide about once in +// 4*10^7 sessions; phase 6's cache, shared across sessions, wants 128. +pub const Hash = u64; + +/// What a row shows, hashed: its wrap and prompt flags, and each of its +/// first `cols` cells up to the last one that is not blank, with its style, +/// grapheme and hyperlink by value. Not the page's style or hyperlink ids, +/// nor its derived flags. Its width is in it: a blob is a row that wide. +/// (A page can be wider than its terminal: ghostty narrows them lazily.) +pub fn rowHash(page: *Page, row: *Row, cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{rowFlags(row)}); + h.update(std.mem.asBytes(&cols)); + const all = page.getCells(row); + const cells = all[0..@min(cols, all.len)]; + var n = cells.len; + while (n > 0 and @as(u64, @bitCast(cells[n - 1])) == 0) n -= 1; + for (cells[0..n]) |*c| { + var word = c.*; + word.style_id = 0; + h.update(std.mem.asBytes(&@as(u64, @bitCast(word)))); + if (c.style_id != 0) h.update(std.mem.asBytes(&page.styles.get(page.memory, c.style_id).hash())); + if (c.hasGrapheme()) h.update(std.mem.sliceAsBytes(page.lookupGrapheme(c).?)); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| + h.update(std.mem.asBytes(&page.hyperlink_set.get(page.memory, id).hash(page.memory))); + } + return h.final(); +} + +fn rowFlags(row: *const Row) u8 { + return @as(u8, @intFromBool(row.wrap)) | @as(u8, @intFromBool(row.wrap_continuation)) << 1 | + @as(u8, @intFromEnum(row.semantic_prompt)) << 2; +} + +/// The authority's absolute row numbers for one terminal: its primary +/// screen's active area starts at `top`, and a row `d` above it in its +/// history is `top - d`. Kept by a tracked pin at the last active top, so +/// it counts the rows scrolled since. A resize renumbers (`epoch`). +pub const Rows = struct { + pin: ?*Pin = null, + top: u64 = 0, + epoch: u32 = 0, + cols: u16 = 0, + rows: u16 = 0, + + pub fn update(n: *Rows, t: *Terminal) !void { + const pages = &t.screens.get(.primary).?.pages; + const top = pages.getTopLeft(.active); + const y = pages.pointFromPin(.screen, top).?.screen.y; + if (n.pin) |p| { + if (t.cols != n.cols or t.rows != n.rows) n.epoch +%= 1; + // The last top was pruned away: every row the peer has is + // older than the oldest row here. + const was: ?u32 = if (p.garbage) null else if (pages.pointFromPin(.screen, p.*)) |pt| pt.screen.y else null; + n.top += if (was) |w| y -| w else @as(u64, y) + t.rows; + p.* = top; + } else { + n.pin = try pages.trackPin(top); + n.top = y; + } + n.cols = t.cols; + n.rows = t.rows; + } + + pub fn deinit(n: *Rows, t: *Terminal) void { + if (n.pin) |p| t.screens.get(.primary).?.pages.untrackPin(p); + n.* = .{}; + } + + /// The pin of absolute row `abs`, if the terminal still has it. + fn pinAt(n: *const Rows, t: *Terminal, abs: u64) ?Pin { + return pinOf(&t.screens.get(.primary).?.pages, n.top, abs); + } +}; + +fn pinOf(pages: anytype, top: u64, abs: u64) ?Pin { + if (abs >= top) return pages.pin(.{ .active = .{ .y = std.math.cast(u32, abs - top) orelse return null } }); + const d = std.math.cast(usize, top - abs) orelse return null; + return pages.getTopLeft(.active).up(d); +} + +/// The authority's scratch: a one-row page each blob is copied into +/// (fresh, so the blob holds that row's styles alone) and encoded from. +pub const Encoder = struct { + gpa: std.mem.Allocator, + scratch: ?Page = null, + meta: std.Io.Writer.Allocating, + pins: std.ArrayList(Pin) = .empty, + needed: std.ArrayList(struct { pin: Pin, hash: Hash }) = .empty, + + pub fn init(gpa: std.mem.Allocator) Encoder { + return .{ .gpa = gpa, .meta = .init(gpa) }; + } + + pub fn deinit(e: *Encoder) void { + if (e.scratch) |*s| s.deinit(); + e.meta.deinit(); + e.pins.deinit(e.gpa); + e.needed.deinit(e.gpa); + } + + /// The PAGE record of `row` alone, its first `cols` cells. + fn blob(e: *Encoder, page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + var cap: vt.page.Capacity = if (e.scratch) |s| s.capacity else .{ .cols = cols, .rows = 1, .styles = 64 }; + if (cap.cols != cols) cap = .{ .cols = cols, .rows = 1, .styles = 64 }; + for (0..16) |_| { + if (e.scratch) |*s| { + if (std.meta.eql(s.capacity, cap)) s.reinit() else { + s.deinit(); + e.scratch = null; + } + } + if (e.scratch == null) e.scratch = try Page.init(cap); + const s = &e.scratch.?; + s.cloneRowFrom(page, s.getRow(0), row) catch { + // Room for this row: everything doubled until it fits. + cap.styles *|= 2; + cap.grapheme_bytes *|= 2; + cap.hyperlink_bytes *|= 2; + cap.string_bytes *|= 2; + continue; + }; + break; + } else return error.RowTooBig; + var rw: snapshot.record.Writer = .init(e.gpa, w); + defer rw.deinit(); + try snapshot.page.encode(&e.scratch.?, &rw); + } +}; + +/// What the authority knows of one peer. +pub const Peer = struct { + seq: u32 = 0, + in_flight: bool = false, + /// Never sent anything, or sent before a renumbering: the next delta + /// starts the replica afresh. + fresh: bool = true, + epoch: u32 = 0, + /// The active area's first row in the state it acknowledged. + top: u64 = 0, + /// The blobs the peer holds: those of the state it acknowledged. + known: std.AutoHashMapUnmanaged(Hash, void) = .empty, + /// Those of the delta in flight, which become `known` on its ack. + sent: std.AutoHashMapUnmanaged(Hash, void) = .empty, + + pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { + peer.known.deinit(gpa); + peer.sent.deinit(gpa); + } + + pub fn ack(peer: *Peer, seq: u32) void { + if (!peer.in_flight or seq != peer.seq) return; + std.mem.swap(std.AutoHashMapUnmanaged(Hash, void), &peer.known, &peer.sent); + peer.in_flight = false; + } + + /// Start the peer afresh (it joined again, or lost its state). + pub fn reset(peer: *Peer) void { + peer.fresh = true; + peer.in_flight = false; + peer.known.clearRetainingCapacity(); + } +}; + +/// The delta's fixed header: u32 seq, u8 flags, u32 epoch, u64 meta, u16 +/// cursor x and y, u8 cursor style, u8 pending wrap, the kitty keyboard +/// flags, u64 top, u64 history rows; then u16 rows and a u64 per active +/// row, u16 and a u64 per row of the tail (those that scrolled off the +/// active area since the acknowledged state, up to a screen of them, oldest +/// first), and a u16 blob count and each blob as u64 hash, u32 length, bytes. +const flag_reset: u8 = 1; +const KittyFlags = @FieldType(Screen, "kitty_keyboard"); + +/// The delta for `peer` against what it acknowledged, onto `out`, or false +/// (nothing written) while its last delta is in flight. +pub fn encode(e: *Encoder, peer: *Peer, n: *Rows, t: *Terminal, out: *std.Io.Writer) !bool { + if (peer.in_flight) return false; + const gpa = e.gpa; + try n.update(t); + if (peer.epoch != n.epoch) peer.reset(); + const reset = peer.fresh; + peer.sent.clearRetainingCapacity(); + + e.meta.clearRetainingCapacity(); + { + var rw: snapshot.record.Writer = .init(gpa, &e.meta.writer); + defer rw.deinit(); + try snapshot.terminal.encode(t, &rw); + } + const meta = std.hash.Wyhash.hash(0, e.meta.written()); + try peer.sent.put(gpa, meta, {}); + + const screen = t.screens.active; + const pages = &t.screens.get(.primary).?.pages; + try out.writeInt(u32, peer.seq +% 1, .little); + try out.writeByte(if (reset) flag_reset else 0); + try out.writeInt(u32, n.epoch, .little); + try out.writeInt(u64, meta, .little); + try out.writeInt(u16, screen.cursor.x, .little); + try out.writeInt(u16, screen.cursor.y, .little); + try out.writeByte(@intFromEnum(screen.cursor.cursor_style)); + try out.writeByte(@intFromBool(screen.cursor.pending_wrap)); + // ponytail: raw bytes, as the hello refuses a peer of another build. + try out.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try out.writeInt(u64, n.top, .little); + const history = pages.total_rows - pages.rows; + try out.writeInt(u64, history, .little); + try out.writeInt(u16, t.rows, .little); + + // The active rows, then the tail: what scrolled off since the state + // acknowledged, which may have changed after it was sent. + const tail: u16 = if (reset) 0 else @intCast(@min(n.top -| peer.top, t.rows, history)); + e.pins.clearRetainingCapacity(); + var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| try e.pins.append(gpa, p); + for (0..tail) |j| try e.pins.append(gpa, pages.getTopLeft(.active).up(tail - j).?); + e.needed.clearRetainingCapacity(); + for (e.pins.items, 0..) |p, i| { + if (i == t.rows) try out.writeInt(u16, tail, .little); + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try out.writeInt(u64, h, .little); + // Every address listed is held once this is acknowledged; a blob + // goes only for one not held already, once. + if ((try peer.sent.getOrPut(gpa, h)).found_existing or peer.known.contains(h)) continue; + try e.needed.append(gpa, .{ .pin = p, .hash = h }); + } + if (tail == 0) try out.writeInt(u16, 0, .little); + const meta_new = !peer.known.contains(meta); + try out.writeInt(u16, @intCast(e.needed.items.len + @intFromBool(meta_new)), .little); + if (meta_new) { + try out.writeInt(u64, meta, .little); + try out.writeInt(u32, @intCast(e.meta.written().len), .little); + try out.writeAll(e.meta.written()); + } + var blob: std.Io.Writer.Allocating = .init(gpa); + defer blob.deinit(); + for (e.needed.items) |need| { + blob.clearRetainingCapacity(); + try e.blob(need.pin.node.page(), need.pin.rowAndCell().row, t.cols, &blob.writer); + try out.writeInt(u64, need.hash, .little); + try out.writeInt(u32, @intCast(blob.written().len), .little); + try out.writeAll(blob.written()); + } + peer.top = n.top; + peer.seq +%= 1; + peer.in_flight = true; + peer.fresh = false; + peer.epoch = n.epoch; + return true; +} + +/// The primary screen's rows `[lo, hi)` the authority still has, as u32 +/// epoch, u32 count, then each as u64 row, u32 length, blob: a replica's +/// fetch of history it holds as holes. +// ponytail: every row its blob, none by address: phase 6's content cache. +pub fn encodeRows(e: *Encoder, n: *Rows, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { + try n.update(t); + var blobs: std.Io.Writer.Allocating = .init(e.gpa); + defer blobs.deinit(); + var count: u32 = 0; + var abs = lo; + while (abs < hi) : (abs += 1) { + const p = n.pinAt(t, abs) orelse continue; + const start = blobs.written().len; + try blobs.writer.writeInt(u64, abs, .little); + try blobs.writer.writeInt(u32, 0, .little); + try e.blob(p.node.page(), p.rowAndCell().row, t.cols, &blobs.writer); + const len: u32 = @intCast(blobs.written().len - start - 12); + std.mem.writeInt(u32, blobs.written()[start + 8 ..][0..4], len, .little); + count += 1; + } + try out.writeInt(u32, n.epoch, .little); + try out.writeInt(u32, count, .little); + try out.writeAll(blobs.written()); +} + +/// A replica's terminal: what it was sent, written into a ghostty Terminal. +pub const Replica = struct { + seq: u32 = 0, + epoch: u32 = 0, + meta: Hash = 0, + top: u64 = 0, + /// The address of each active row as the Terminal holds it, a list per + /// screen (primary, alternate). + held: [2]std.ArrayList(Hash) = .{ .empty, .empty }, + /// The blobs of the last state applied: what the authority takes it to + /// hold. Owned copies. + blobs: std.AutoHashMapUnmanaged(Hash, []u8) = .empty, + /// History rows it cannot vouch for (written blank, or seen before + /// they last changed), `[lo, hi)` absolute, oldest first. + holes: std.ArrayList([2]u64) = .empty, + /// The one-row page each row blob is decoded into. + scratch: ?Page = null, + + pub fn deinit(r: *Replica, gpa: std.mem.Allocator) void { + for (&r.held) |*h| h.deinit(gpa); + var it = r.blobs.valueIterator(); + while (it.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.holes.deinit(gpa); + if (r.scratch) |*s| s.deinit(); + } + + /// Write one delta into `t`; the seq to acknowledge. + pub fn apply(r: *Replica, gpa: std.mem.Allocator, io: std.Io, t: *Terminal, bytes: []const u8) !u32 { + var in: std.Io.Reader = .fixed(bytes); + const seq = try in.takeInt(u32, .little); + const flags = try in.takeByte(); + const epoch = try in.takeInt(u32, .little); + const meta = try in.takeInt(u64, .little); + const cx = try in.takeInt(u16, .little); + const cy = try in.takeInt(u16, .little); + const style = try in.takeByte(); + const wrap = try in.takeByte(); + var kitty: KittyFlags = undefined; + @memcpy(std.mem.asBytes(&kitty), try in.take(@sizeOf(KittyFlags))); + const top = try in.takeInt(u64, .little); + const history = try in.takeInt(u64, .little); + const nrows = try in.takeInt(u16, .little); + const hashes = try in.take(@as(usize, nrows) * 8); + const tail = try in.takeInt(u16, .little); + const tails = try in.take(@as(usize, tail) * 8); + // The delta's blobs, borrowed from `bytes` while it applies. + var fresh: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer fresh.deinit(gpa); + for (0..try in.takeInt(u16, .little)) |_| { + const h = try in.takeInt(u64, .little); + try fresh.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } + const blobOf = struct { + fn of(rr: *Replica, f: *const std.AutoHashMapUnmanaged(Hash, []const u8), h: Hash) ![]const u8 { + if (f.get(h)) |b| return b; + if (rr.blobs.get(h)) |b| return b; + return error.MissingBlob; + } + }.of; + + if (flags & flag_reset != 0 or meta != r.meta) { + var reader: std.Io.Reader = .fixed(try blobOf(r, &fresh, meta)); + var next = try snapshot.terminal.decode(&reader, try snapshot.record.nextHeader(&reader), io, gpa); + if (flags & flag_reset != 0) { + std.mem.swap(Terminal, t, &next); + next.deinit(gpa); + for (&r.held) |*h| h.clearRetainingCapacity(); + r.holes.clearRetainingCapacity(); + r.top = top; + r.epoch = epoch; + // The authority's history, blank, to be fetched. + const primary = t.screens.get(.primary).?; + try scrollUp(primary, history); + if (history > 0) try r.holes.append(gpa, .{ top -| history, top }); + } else { + defer next.deinit(gpa); + try takeMeta(t, &next, io, gpa); + } + r.meta = meta; + } + if (epoch != r.epoch) return error.Renumbered; + + // The primary screen scrolled `top - r.top` rows since the last + // delta: as many go up into the history here (no more than it and + // the screen hold). + // ponytail: the history's extent then follows this terminal's own + // pruning, under the authority's limit: a page more or less than the + // authority's, and the oldest rows may be some it has dropped. The tail is what they are now; any older than + // the tail are holes, never seen or seen before they last changed. + const primary = t.screens.get(.primary).?; + if (top > r.top) { + const k = top - r.top; + try scrollUp(primary, @min(k, @as(u64, t.rows) + history + 1)); + const held = &r.held[0]; + if (k >= held.items.len) held.clearRetainingCapacity() else { + std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); + held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + } + if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); + r.top = top; + } + for (0..tail) |j| { + const p = pinOf(&primary.pages, top, top - tail + j) orelse continue; + const h = std.mem.readInt(u64, tails[j * 8 ..][0..8], .little); + if (rowHash(p.node.page(), p.rowAndCell().row, t.cols) != h) try r.writeRow(gpa, primary, p, try blobOf(r, &fresh, h)); + } + // The authority's history went (an ED3): so does this one's. + if (history == 0 and primary.pages.total_rows > primary.pages.rows) { + primary.pages.eraseHistory(null); + r.holes.clearRetainingCapacity(); + } + r.trimHoles(primary); + + const screen = t.screens.active; + const held = &r.held[@intFromEnum(t.screens.active_key)]; + const blank = blankHash(t.cols); + while (held.items.len < nrows) try held.append(gpa, blank); + held.shrinkRetainingCapacity(nrows); + for (0..nrows) |y| { + const h = std.mem.readInt(u64, hashes[y * 8 ..][0..8], .little); + if (h == held.items[y]) continue; + try r.writeRow(gpa, screen, screen.pages.pin(.{ .active = .{ .y = @intCast(y) } }).?, try blobOf(r, &fresh, h)); + held.items[y] = h; + } + screen.cursorReload(); + screen.cursorAbsolute(@min(cx, t.cols - 1), @min(cy, t.rows - 1)); + screen.cursor.cursor_style = @enumFromInt(style); + screen.cursor.pending_wrap = wrap != 0; + screen.kitty_keyboard = kitty; + + // Hold the blobs of this state, and no others. + var keep: std.AutoHashMapUnmanaged(Hash, []u8) = .empty; + errdefer keep.deinit(gpa); + try keep.ensureTotalCapacity(gpa, nrows + tail + 1); + for (0..nrows + tail + 1) |i| { + const h = if (i == nrows + tail) meta else if (i < nrows) + std.mem.readInt(u64, hashes[i * 8 ..][0..8], .little) + else + std.mem.readInt(u64, tails[(i - nrows) * 8 ..][0..8], .little); + if (keep.contains(h)) continue; + const b = if (r.blobs.fetchRemove(h)) |kv| kv.value else try gpa.dupe(u8, try blobOf(r, &fresh, h)); + keep.putAssumeCapacity(h, b); + } + var old = r.blobs.valueIterator(); + while (old.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.blobs = keep; + r.seq = seq; + return seq; + } + + /// The holes in `[lo, hi)`: the rows to fetch before showing them. + pub fn missing(r: *const Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void { + for (r.holes.items) |hole| { + const a = @max(lo, hole[0]); + const b = @min(hi, hole[1]); + if (a < b) try out.append(gpa, .{ a, b }); + } + } + + /// Write fetched history rows (`encodeRows`) into `t`. + pub fn applyRows(r: *Replica, gpa: std.mem.Allocator, t: *Terminal, bytes: []const u8) !void { + var in: std.Io.Reader = .fixed(bytes); + if (try in.takeInt(u32, .little) != r.epoch) return; // numbered before a resize + const primary = t.screens.get(.primary).?; + for (0..try in.takeInt(u32, .little)) |_| { + const abs = try in.takeInt(u64, .little); + const blob = try in.take(try in.takeInt(u32, .little)); + if (abs >= r.top) continue; + const p = pinOf(&primary.pages, r.top, abs) orelse continue; + try r.writeRow(gpa, primary, p, blob); + try r.fill(gpa, abs); + } + } + + fn inHole(r: *const Replica, abs: u64) bool { + for (r.holes.items) |hole| if (abs >= hole[0] and abs < hole[1]) return true; + return false; + } + + /// Row `abs` is written: out of the holes. + fn fill(r: *Replica, gpa: std.mem.Allocator, abs: u64) !void { + for (r.holes.items, 0..) |*hole, i| { + if (abs < hole[0] or abs >= hole[1]) continue; + if (abs == hole[0]) hole[0] += 1 else if (abs + 1 == hole[1]) hole[1] -= 1 else { + try r.holes.insert(gpa, i + 1, .{ abs + 1, hole[1] }); + r.holes.items[i][1] = abs; + } + if (r.holes.items[i][0] == r.holes.items[i][1]) _ = r.holes.orderedRemove(i); + return; + } + } + + /// Holes older than this history's oldest row are gone with it. + fn trimHoles(r: *Replica, primary: *Screen) void { + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + var i: usize = 0; + while (i < r.holes.items.len) { + const hole = &r.holes.items[i]; + hole[0] = @max(hole[0], oldest); + if (hole[0] >= hole[1]) _ = r.holes.orderedRemove(i) else i += 1; + } + } + + fn writeRow(r: *Replica, gpa: std.mem.Allocator, screen: *Screen, p: Pin, blob: []const u8) !void { + var reader: std.Io.Reader = .fixed(blob); + var dec: snapshot.page.Decoder = undefined; + try dec.init(&reader, try snapshot.record.nextHeader(&reader)); + const cap = dec.capacity(); + if (r.scratch) |*s| { + if (std.meta.eql(s.capacity, cap)) s.reinit() else { + s.deinit(); + r.scratch = null; + } + } + if (r.scratch == null) r.scratch = try Page.init(cap); + const src = &r.scratch.?; + try dec.decode(src, gpa); + if (src.size.cols != screen.pages.cols) return error.BadRow; + _ = screen.clonePartialRowGrowCapacity(p.node, p.y, src, src.getRow(0), 0, src.size.cols); + } +}; + +/// `k` blank rows onto the bottom of a screen's active area, its top `k` +/// going up into the history, as `k` linefeeds at the bottom would. +// ponytail: a row at a time, so a flood costs the replica up to its whole +// history's rows a delta (measured in `bench`); a bulk scroll if that shows. +fn scrollUp(screen: *Screen, k: u64) !void { + if (k == 0) return; + screen.cursorAbsolute(0, screen.pages.rows - 1); + for (0..@intCast(k)) |_| try screen.cursorDownScroll(); +} + +/// `rowHash` of a row never written. +fn blankHash(cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{0}); + h.update(std.mem.asBytes(&cols)); + return h.final(); +} + +/// The terminal-wide state of `next` (a decoded TERMINAL record) into `t`, +/// whose screens and pages stay: what `snapshot.terminal.decode` sets. +fn takeMeta(t: *Terminal, next: *Terminal, io: std.Io, gpa: std.mem.Allocator) !void { + if (t.cols != next.cols or t.rows != next.rows) try t.resize(gpa, .{ .cols = next.cols, .rows = next.rows }); + t.width_px = next.width_px; + t.height_px = next.height_px; + t.scrolling_region = next.scrolling_region; + t.status_display = next.status_display; + t.previous_char = next.previous_char; + t.cursor = next.cursor; + t.modes = next.modes; + t.flags.shell_redraws_prompt = next.flags.shell_redraws_prompt; + t.flags.modify_other_keys_2 = next.flags.modify_other_keys_2; + t.flags.mouse_event = next.flags.mouse_event; + t.flags.mouse_format = next.flags.mouse_format; + t.flags.mouse_shift_capture = next.flags.mouse_shift_capture; + t.flags.password_input = next.flags.password_input; + t.mouse_shape = next.mouse_shape; + t.colors = next.colors; + try t.setPwd(next.getPwd() orelse ""); + try t.setTitle(next.getTitle() orelse ""); + std.mem.swap(@TypeOf(t.tabstops), &t.tabstops, &next.tabstops); + const key = next.screens.active_key; + if (key != t.screens.active_key) { + if (t.screens.get(key) == null) _ = try t.screens.getInit(io, gpa, key, .{ .cols = t.cols, .rows = t.rows, .max_scrollback_bytes = 0 }); + t.screens.switchTo(key); + } +} + +// ---- checks: a replica against its authority, cell for cell ---- + +/// What a row shows, written out value by value (not `rowHash`'s walk): +/// its flags, then each cell's word without its page's ids, its style, its +/// grapheme and its hyperlink's URI. +fn dumpRow(page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + try w.print("{d}:", .{rowFlags(row)}); + const all = page.getCells(row); + for (all[0..@min(cols, all.len)]) |*c| { + var word = c.*; + word.style_id = 0; + try w.print(" {x}", .{@as(u64, @bitCast(word))}); + if (c.style_id != 0) try snapshot.style.encode(page.styles.get(page.memory, c.style_id).*, w); + if (c.hasGrapheme()) for (page.lookupGrapheme(c).?) |cp| try w.print("+{x}", .{cp}); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| try w.print("@{s}", .{page.hyperlink_set.get(page.memory, id).uri.slice(page.memory)}); + } + try w.writeByte('\n'); +} + +/// The state a replica must match: the terminal-wide state, the cursor, +/// the active screen's rows. +fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { + const screen = t.screens.active; + try w.print("{d}x{d} {s} cursor {d},{d} {s} {} title {s} pwd {s}\n", .{ + t.cols, t.rows, @tagName(t.screens.active_key), + screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style), + screen.cursor.pending_wrap, t.getTitle() orelse "", t.getPwd() orelse "", + }); + try w.writeAll(std.mem.asBytes(&t.modes.values)); + try w.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try w.print(" mouse {s} {s} {s}\n", .{ @tagName(t.flags.mouse_event), @tagName(t.flags.mouse_format), @tagName(t.mouse_shape) }); + for (t.colors.palette.current) |c| try w.print("{x}{x}{x}", .{ c.r, c.g, c.b }); + try w.writeByte('\n'); + var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| try dumpRow(p.node.page(), p.rowAndCell().row, t.cols, w); +} + +/// An authority and its replica, fed one stream of pty bytes, the deltas +/// and fetches between them at random: a delta encoded at a random step and +/// applied a random number of steps later (so the states between are +/// skipped), random resizes of the authority, random fetches of holes. +/// After each delta, the replica's screen is the authority's as it was +/// encoded, and every history row it holds outside its holes is what the +/// authority holds at that absolute row. +pub const Soak = struct { + gpa: std.mem.Allocator, + io: std.Io, + a: *Terminal, + stream: vt.TerminalStream, + rows: Rows = .{}, + enc: Encoder, + peer: Peer = .{}, + b: *Terminal, + rep: Replica = .{}, + prng: std.Random.DefaultPrng, + /// The delta in flight, the authority's dump when it was encoded, and + /// how many steps it waits before it lands. + delta: std.Io.Writer.Allocating, + expect: std.Io.Writer.Allocating, + got: std.Io.Writer.Allocating, + wait: u32 = 0, + resizes: bool = true, + stats: struct { deltas: u64 = 0, bytes: u64 = 0, fetched: u64 = 0, history_checked: u64 = 0 } = .{}, + + pub fn init(gpa: std.mem.Allocator, io: std.Io, cols: u16, rows: u16, scrollback: usize, seed: u64) !*Soak { + const s = try gpa.create(Soak); + errdefer gpa.destroy(s); + const a = try gpa.create(Terminal); + a.* = try Terminal.init(io, gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = scrollback }); + const b = try gpa.create(Terminal); + b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = scrollback }); + s.* = .{ + .gpa = gpa, + .io = io, + .a = a, + .stream = a.vtStream(), + .enc = .init(gpa), + .b = b, + .prng = .init(seed), + .delta = .init(gpa), + .expect = .init(gpa), + .got = .init(gpa), + }; + return s; + } + + pub fn deinit(s: *Soak) void { + const gpa = s.gpa; + s.stream.deinit(); + s.rows.deinit(s.a); + s.a.deinit(gpa); + gpa.destroy(s.a); + s.b.deinit(gpa); + gpa.destroy(s.b); + s.enc.deinit(); + s.peer.deinit(gpa); + s.rep.deinit(gpa); + s.delta.deinit(); + s.expect.deinit(); + s.got.deinit(); + gpa.destroy(s); + } + + /// `bytes` into the authority in random pieces, a step after each. + pub fn feed(s: *Soak, bytes: []const u8) !void { + var at: usize = 0; + const random = s.prng.random(); + while (at < bytes.len) { + const n = @min(bytes.len - at, 1 + random.uintLessThan(usize, 512)); + s.stream.nextSlice(bytes[at..][0..n]); + at += n; + try s.step(); + } + } + + pub fn step(s: *Soak) !void { + const random = s.prng.random(); + if (s.resizes and random.uintLessThan(u32, 200) == 0) { + try s.a.resize(s.gpa, .{ .cols = 10 + random.uintLessThan(u16, 200), .rows = 3 + random.uintLessThan(u16, 60) }); + } + if (!s.peer.in_flight) { + if (random.boolean()) return; // a state skipped + s.delta.clearRetainingCapacity(); + if (!try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer)) unreachable; + s.expect.clearRetainingCapacity(); + try dumpScreen(s.a, &s.expect.writer); + s.wait = random.uintLessThan(u32, 4); + s.stats.deltas += 1; + s.stats.bytes += s.delta.written().len; + return; + } + if (s.wait > 0) { + s.wait -= 1; + return; + } + try s.land(); + if (random.uintLessThan(u32, 8) == 0) try s.fetch(); + } + + /// The delta in flight applied and acknowledged, and checked. + pub fn land(s: *Soak) !void { + if (!s.peer.in_flight) return; + s.peer.ack(try s.rep.apply(s.gpa, s.io, s.b, s.delta.written())); + s.got.clearRetainingCapacity(); + try dumpScreen(s.b, &s.got.writer); + if (!std.mem.eql(u8, s.expect.written(), s.got.written())) { + reportDiff(s.expect.written(), s.got.written()); + return error.ReplicaDiffers; + } + try s.checkHistory(); + } + + /// Every history row the replica holds, outside its holes and inside + /// what the authority still has, is the authority's (rows in history + /// never change; a resize renumbers and starts the replica afresh). + fn checkHistory(s: *Soak) !void { + // Numbered as of now: the authority may have scrolled on since. + try s.rows.update(s.a); + if (s.rep.epoch != s.rows.epoch) return; + const pages = &s.b.screens.get(.primary).?.pages; + const have = pages.total_rows - pages.rows; + var ra: std.Io.Writer.Allocating = .init(s.gpa); + defer ra.deinit(); + var rb: std.Io.Writer.Allocating = .init(s.gpa); + defer rb.deinit(); + var d: u64 = 1; + while (d <= have) : (d += 1 + d / 8) { + const abs = s.rep.top - d; + if (s.rep.inHole(abs)) continue; + const pb = pinOf(pages, s.rep.top, abs) orelse continue; + const pa = s.rows.pinAt(s.a, abs) orelse continue; + ra.clearRetainingCapacity(); + rb.clearRetainingCapacity(); + try dumpRow(pa.node.page(), pa.rowAndCell().row, s.a.cols, &ra.writer); + try dumpRow(pb.node.page(), pb.rowAndCell().row, s.b.cols, &rb.writer); + if (!std.mem.eql(u8, ra.written(), rb.written())) { + std.debug.print("history row {d} ({d} above the top) differs\n", .{ abs, d }); + reportDiff(ra.written(), rb.written()); + return error.ReplicaHistoryDiffers; + } + s.stats.history_checked += 1; + } + } + + /// Some holes fetched from the authority, as a replica scrolled back. + fn fetch(s: *Soak) !void { + if (s.rep.holes.items.len == 0) return; + const random = s.prng.random(); + const hole = s.rep.holes.items[random.uintLessThan(usize, s.rep.holes.items.len)]; + const lo = hole[0] + random.uintLessThan(u64, hole[1] - hole[0]); + const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); + var out: std.Io.Writer.Allocating = .init(s.gpa); + defer out.deinit(); + try encodeRows(&s.enc, &s.rows, s.a, lo, hi, &out.writer); + try s.rep.applyRows(s.gpa, s.b, out.written()); + s.stats.fetched += hi - lo; + try s.checkHistory(); + } +}; + +fn reportDiff(want: []const u8, got: []const u8) void { + var wl = std.mem.splitScalar(u8, want, '\n'); + var gl = std.mem.splitScalar(u8, got, '\n'); + var line: usize = 0; + while (true) : (line += 1) { + const a = wl.next(); + const b = gl.next(); + if (a == null and b == null) return; + if (a != null and b != null and std.mem.eql(u8, a.?, b.?)) continue; + std.debug.print("line {d} differs:\n authority {s}\n replica {s}\n", .{ line, a orelse "(none)", b orelse "(none)" }); + return; + } +} + +/// Drive one stream through a soak and land a last delta of its end. +pub fn soak(gpa: std.mem.Allocator, io: std.Io, bytes: []const u8, cols: u16, rows: u16, scrollback: usize, seed: u64, resizes: bool) !@FieldType(Soak, "stats") { + const s = try Soak.init(gpa, io, cols, rows, scrollback, seed); + defer s.deinit(); + s.resizes = resizes; + try s.feed(bytes); + try s.land(); + s.delta.clearRetainingCapacity(); + _ = try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer); + s.expect.clearRetainingCapacity(); + try dumpScreen(s.a, &s.expect.writer); + try s.land(); + return s.stats; +} + +/// Every recorded pty stream in `dir` (a `-x` in its name sizes +/// it, 80x24 otherwise) through a soak at three seeds, then the encode's +/// cost and the bytes floods put on the wire: the test below, when +/// PARDES_SCREEN_STATE names the directory (numbers from a ReleaseFast +/// build: `zig build core-test -Doptimize=ReleaseFast -Dtest-filter=screen_state`). +fn perf(gpa: std.mem.Allocator, io: std.Io, dir_path: []const u8, w: *std.Io.Writer) !void { + var dir = try std.Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }); + defer dir.close(io); + var it = dir.iterate(); + var files: usize = 0; + var failed: usize = 0; + while (try it.next(io)) |entry| { + if (entry.kind != .file) continue; + const bytes = try dir.readFileAlloc(io, entry.name, gpa, .unlimited); + defer gpa.free(bytes); + var cols: u16 = 80; + var rows: u16 = 24; + if (std.mem.lastIndexOfScalar(u8, entry.name, '-')) |dash| { + const size = std.mem.sliceTo(entry.name[dash + 1 ..], '.'); + if (std.mem.indexOfScalar(u8, size, 'x')) |x| { + cols = std.fmt.parseInt(u16, size[0..x], 10) catch cols; + rows = std.fmt.parseInt(u16, size[x + 1 ..], 10) catch rows; + } + } + files += 1; + for (0..3) |seed| { + const st = soak(gpa, io, bytes, cols, rows, 128 * 1024, seed, seed != 0) catch |err| { + try w.print("screen-state {s} seed {d}: {s}\n", .{ entry.name, seed, @errorName(err) }); + failed += 1; + continue; + }; + if (seed == 0) try w.print("screen-state {s} {d}x{d}: {d} bytes, {d} deltas, {d} wire bytes, {d} history rows checked, {d} fetched\n", .{ entry.name, cols, rows, bytes.len, st.deltas, st.bytes, st.history_checked, st.fetched }); + } + } + try w.print("screen-state corpus: {d} streams x 3 seeds, {d} failed\n", .{ files, failed }); + try w.flush(); + try bench(gpa, io, w); +} + +fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { + const t = try gpa.create(Terminal); + defer gpa.destroy(t); + t.* = try Terminal.init(io, gpa, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer t.deinit(gpa); + var stream = t.vtStream(); + defer stream.deinit(); + var enc: Encoder = .init(gpa); + defer enc.deinit(); + var n: Rows = .{}; + defer n.deinit(t); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + var line: [1024]u8 = undefined; + // A full 200x60 screen, every row new to the peer: each a blob. + var full: [200]u64 = undefined; + var same: [200]u64 = undefined; + var full_bytes: usize = 0; + var same_bytes: usize = 0; + for (&full, &same, 0..) |*f, *u, rep| { + for (0..60) |y| { + var lw: std.Io.Writer = .fixed(&line); + try lw.print("\x1b[{d};1H\x1b[0;3{d};4{d}m{d} {d} ", .{ y + 1, rep % 8, y % 8, rep, y }); + while (lw.end < 190) try lw.writeAll("\x1b[1mbold\x1b[22m text "); + stream.nextSlice(lw.buffered()); + } + var peer: Peer = .{}; + defer peer.deinit(gpa); + out.clearRetainingCapacity(); + var t0 = std.Io.Clock.awake.now(io).nanoseconds; + _ = try encode(&enc, &peer, &n, t, &out.writer); + f.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); + full_bytes = out.written().len; + peer.ack(peer.seq); + out.clearRetainingCapacity(); + t0 = std.Io.Clock.awake.now(io).nanoseconds; + _ = try encode(&enc, &peer, &n, t, &out.writer); + u.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); + same_bytes = out.written().len; + } + std.mem.sort(u64, &full, {}, std.sort.asc(u64)); + std.mem.sort(u64, &same, {}, std.sort.asc(u64)); + try w.print("screen-state encode 200x60, every row new: p50 {d} us p99 {d} us, {d} bytes\n", .{ full[100] / 1000, full[198] / 1000, full_bytes }); + try w.print("screen-state encode 200x60, nothing new: p50 {d} us p99 {d} us, {d} bytes\n", .{ same[100] / 1000, same[198] / 1000, same_bytes }); + + // Floods: a second of output as fast as ghostty takes it, a delta every + // 16 ms when the last is acknowledged (by a replica applying it at once). + const yes = try yesStream(gpa, 1 << 16); + defer gpa.free(yes); + const shell = try shellStream(gpa); + defer gpa.free(shell); + for ([_]struct { name: []const u8, bytes: []const u8 }{ .{ .name = "yes", .bytes = yes }, .{ .name = "varied text", .bytes = shell } }) |flood| { + const s = try Soak.init(gpa, io, 200, 60, 16 << 20, 0); + defer s.deinit(); + const start = std.Io.Clock.awake.now(io).nanoseconds; + var next = start; + var fed: u64 = 0; + var deltas: u64 = 0; + var wire: u64 = 0; + var apply_ns: u64 = 0; + while (true) { + const now = std.Io.Clock.awake.now(io).nanoseconds; + if (now - start >= std.time.ns_per_s) break; + s.stream.nextSlice(flood.bytes); + fed += flood.bytes.len; + if (now < next) continue; + next += 16 * std.time.ns_per_ms; + s.delta.clearRetainingCapacity(); + _ = try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer); + const a0 = std.Io.Clock.awake.now(io).nanoseconds; + s.peer.ack(try s.rep.apply(gpa, io, s.b, s.delta.written())); + apply_ns += @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0); + deltas += 1; + wire += s.delta.written().len; + } + try w.print("screen-state flood of {s}: {d} MB/s of output, {d} deltas/s, {d} wire bytes/s ({d} a delta), replica apply {d} us a delta\n", .{ flood.name, fed / 1_000_000, deltas, wire, wire / @max(1, deltas), apply_ns / @max(1, deltas) / 1000 }); + } + try w.flush(); +} + +const testing = std.testing; + +test "recorded streams through replicas, and the encode's cost (PARDES_SCREEN_STATE=)" { + const dir = std.c.getenv("PARDES_SCREEN_STATE") orelse return error.SkipZigTest; + var buf: [4096]u8 = undefined; + // stderr: a test's stdout is the build runner's. + var out = std.Io.File.stderr().writer(testing.io, &buf); + try perf(testing.allocator, testing.io, std.mem.span(dir), &out.interface); +} + +/// A shell's: prompts marked (OSC 133), colours of the three kinds, wide +/// and combined graphemes, hyperlinks, tabs, a title and a directory, long +/// lines that wrap, and a clear (ED2, ED3) now and then. +fn shellStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("\x1b]0;a shell\x07\x1b]7;file://host/home/someone\x07"); + for (0..400) |i| { + try w.print("\x1b]133;A\x07$ \x1b]133;B\x07ls -l {d}\r\n\x1b]133;C\x07", .{i}); + try w.print("\x1b[1;31mred\x1b[0m \x1b[4;38;5;{d}m256\x1b[0m \x1b[38;2;{d};100;200;48;5;17mtrue\x1b[0m 漢字 e\u{301} 👩\u{200d}💻 ", .{ i % 256, i % 256 }); + try w.print("\x1b]8;;https://example.com/{d}\x07link\x1b]8;;\x07\tx\r\n", .{i}); + if (i % 7 == 0) { + for (0..12) |_| try w.writeAll("a long line that wraps "); + try w.writeAll("\r\n"); + } + try w.writeAll("\x1b]133;D;0\x07"); + if (i % 97 == 96) try w.writeAll("\x1b[H\x1b[2J\x1b[3J"); + if (i % 50 == 0) try w.print("\x1b]2;title {d}\x07\x1b]4;1;rgb:{x:0>2}/20/30\x07", .{ i, i % 256 }); + } + return out.toOwnedSlice(); +} + +/// A full-screen program's: the alternate screen and its modes, a status +/// line, a scrolling region scrolled both ways, lines inserted and +/// deleted, cursor shapes, the kitty keyboard pushed and popped. +fn tuiStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("before the program\r\n\x1b[?1049h\x1b[?25l\x1b[?1000h\x1b[?1006h\x1b[?2004h\x1b[?1h\x1b[>1u\x1b[4 q\x1b[H\x1b[2J"); + for (0..300) |f| { + try w.print("\x1b[1;1H\x1b[44;97m status {d} \x1b[K\x1b[0m", .{f}); + try w.print("\x1b[3;18r\x1b[18;1H\r\n\x1b[3{d}mline {d}\x1b[0m", .{ f % 8, f }); + if (f % 3 == 0) try w.writeAll("\x1b[3;1H\x1bM\x1b[2mreversed in\x1b[0m"); + if (f % 5 == 0) try w.writeAll("\x1b[6;1H\x1b[2L\x1b[9;1H\x1b[M"); + try w.writeAll("\x1b[r"); + try w.print("\x1b[{d};{d}H\x1b[7m*\x1b[0m\x1b[{d}X", .{ 2 + f % 20, 1 + (f * 7) % 70, f % 5 }); + if (f % 11 == 0) try w.writeAll("\x1b[?25h\x1b[6 q") else if (f % 11 == 5) try w.writeAll("\x1b[?25l\x1b[2 q"); + } + try w.writeAll("\x1b[ 10); + fetched += st.fetched; + history += st.history_checked; + } + } + // The holes were fetched, and history rows compared, at all. + try testing.expect(fetched > 0 and history > 0); +} + +test "under a flood a delta is a screen of hashes: an unchanged row costs its address alone" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 200, 60, 1 << 20, 1); + defer s.deinit(); + s.resizes = false; + const yes = try yesStream(gpa, 10_000); + defer gpa.free(yes); + for (0..3) |_| { + s.stream.nextSlice(yes); + s.delta.clearRetainingCapacity(); + try testing.expect(try encode(&s.enc, &s.peer, &s.rows, s.a, &s.delta.writer)); + s.expect.clearRetainingCapacity(); + try dumpScreen(s.a, &s.expect.writer); + try s.land(); + } + // The last: 60 rows and the tail's 60, of hashes the replica holds, no + // blob; the header and counts around them. + const d = s.delta.written(); + const at = 4 + 1 + 4 + 8 + 2 + 2 + 1 + 1 + @sizeOf(KittyFlags) + 8 + 8; + const nrows = std.mem.readInt(u16, d[at..][0..2], .little); + const tail = std.mem.readInt(u16, d[at + 2 + nrows * 8 ..][0..2], .little); + const blobs = std.mem.readInt(u16, d[at + 4 + (nrows + tail) * 8 ..][0..2], .little); + if (d.len >= (nrows + tail) * 8 + 128) std.debug.print("delta {d} bytes: {d} rows, {d} tail, {d} blobs\n", .{ d.len, nrows, tail, blobs }); + try testing.expectEqual(@as(u16, 0), blobs); + try testing.expect(d.len < (nrows + tail) * 8 + 128); + try testing.expect(s.rep.holes.items.len > 0); +} + +test "a replica's terminal pane draws as its authority's does" { + const pardes = @import("pardes.zig"); + const gpa = testing.allocator; + const a = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer a.deinit(); + const b = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer b.deinit(); + while (a.nextEffect()) |_| {} + while (b.nextEffect()) |_| {} + var fa = std.heap.ArenaAllocator.init(gpa); + defer fa.deinit(); + var fb = std.heap.ArenaAllocator.init(gpa); + defer fb.deinit(); + // Laid out once, so both panes have their size before the first delta. + _ = try a.render(fa.allocator()); + _ = try b.render(fb.allocator()); + const ta = &a.panes[0].?.terminal.?.vt; + const tb = &b.panes[0].?.terminal.?.vt; + var enc: Encoder = .init(gpa); + defer enc.deinit(); + var peer: Peer = .{}; + defer peer.deinit(gpa); + var rows: Rows = .{}; + defer rows.deinit(ta); + var rep: Replica = .{}; + defer rep.deinit(gpa); + var delta: std.Io.Writer.Allocating = .init(gpa); + defer delta.deinit(); + var line: [256]u8 = undefined; + for (0..60) |i| { + const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); + a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + delta.clearRetainingCapacity(); + try testing.expect(try encode(&enc, &peer, &rows, ta, &delta.writer)); + peer.ack(try rep.apply(gpa, terminalIoForTest(), tb, delta.written())); + b.needs_frame = true; + _ = fa.reset(.retain_capacity); + _ = fb.reset(.retain_capacity); + const sa = try a.render(fa.allocator()); + const sb = try b.render(fb.allocator()); + const r = a.rects[0]; + // The body: below the pane's tag row. + for (r.y + 1..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { + const ca = sa.at(@intCast(x), @intCast(y)); + const cb = sb.at(@intCast(x), @intCast(y)); + if (!ca.visuallyEqual(cb)) { + std.debug.print("step {d}: cell {d},{d}: '{s}' vs '{s}'\n", .{ i, x, y, ca.grapheme(), cb.grapheme() }); + return error.ReplicaDrawsDifferently; + } + }; + } +} + +fn terminalIoForTest() std.Io { + return std.Io.Threaded.global_single_threaded.io(); +} -- cgit v1.3 From 2cce341e2c94fb3150dc1f3e7911c26502a70487 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 17:01:35 -0300 Subject: views: several clients on one session, each its own layout, focus, cursors and scroll over the panes they share; a second `pardes --attach` draws on its own terminal beside the first, and `pardes-gui --attach` opens another window The core still sees one view, the live one, in the fields it always had (src/views.zig): a view's share of the session (`views.fields`: its screen, columns, rects, focus, pointer and gestures, jumps, key prefixes, column tags, presentation and animations, windowed and native_images) and of each pane (`PaneView`: its place in the layout, which text has the keyboard, the body's cursor, selections and mode, its scroll, a document's size, what the step measured of it) is copied out when another view goes live and back when it is live again. One view never parks: the single client pays a test of one byte a step. `toView`, `openView` and `closeView` are taped inputs; the pump draws each other view by going into it and back, which leaves every digested field as it was, so the replica that draws nothing skips it. A parked view is in no digest (its slot is written whenever it goes out); what drawing it made of its own (animations, presentation) is kept, whatever its unpark brought inside a changed text is done again when it is next live. Shared: the panes' text, terminals, tags, prompts, messages, registers (yanked in one view, put in another), the workspace tag and settings. A terminal shown at two sizes takes the size of the view that typed into it last (`Pane.sized_by`), and any view's once that one is gone; the others show it at that size. A text edited in one view moves the other views' cursors and scroll with the splice, as a Zerox twin's (File.ParkedPlaces). A pane closed in one view leaves the others' layouts (by its slot's serial); a pane focused in a view that does not show it is placed in its active column (views.show). tty: each terminal is a Screen of its own (its vaxis, input reader, frame answer, kitty images and shm probe, glide, trail and flash); its input reaches the loop as its view's (a `from` ahead of it when another terminal posted last, so one terminal posts none). A SIGWINCH asks every terminal its size; a SIGHUP asks each whether it hung up or lost its stub. Detach, or a terminal's end, with other views left closes only that view, its stub told `d`; the session's end tells every stub. daemon.zig: an attach names an attached session too (or the only one there is when none is detached). gui: the Gui holds the live window's share of itself (window_fields: the SDL window and its presenting, scroll, glide, scene and post chain, pointer, blink, captures); the others wait in Shell.windows, swapped in (`follow`) as the core's live view changes, and each SDL event first takes its window's view live (`route`). Closing a window while others stay closes its view. Harnesses: `start views=N` in a snapshot script gives the app N-1 more terminals (PARDES_TEST_VIEWS, opened as attached ones), `view ` types into and captures terminal k, every wait reads them all; `pardes-monkey --views=N` switches among N; `zig build replay` runs its seeds at three views too. views.snap pins two terminals: an edit seen in both, a scroll in one, a line yanked in one and put in the other, Newcol in one, Detach in one. Unit tests: own cursor, scroll, mode, layout and focus; edits shared; registers shared; terminal size by last typist; closing views; drawing the others leaves the digest; a view keeps its place in a text another edits. Co-Authored-By: Claude Opus 5.5 --- build/snap.zig | 11 +- src/File.zig | 51 +++- src/daemon.zig | 39 ++- src/gui/gui.zig | 225 ++++++++++++++- src/panes.zig | 16 +- src/pardes.zig | 70 ++++- src/tape.zig | 6 + src/tty/tty.zig | 666 ++++++++++++++++++++++++++++---------------- src/views.zig | 555 ++++++++++++++++++++++++++++++++++++ test/daemon.py | 32 +++ test/e2e_harness.zig | 90 +++++- test/monkey.zig | 34 ++- test/snapshot.zig | 92 ++++-- test/snapshots/views.golden | 123 ++++++++ test/snapshots/views.snap | 57 ++++ 15 files changed, 1763 insertions(+), 304 deletions(-) create mode 100644 src/views.zig create mode 100644 test/snapshots/views.golden create mode 100644 test/snapshots/views.snap (limited to 'src/pardes.zig') diff --git a/build/snap.zig b/build/snap.zig index 6982379c..d74767d3 100644 --- a/build/snap.zig +++ b/build/snap.zig @@ -86,7 +86,16 @@ pub fn addTty(b: *std.Build, opts: struct { run_monkey_replay.has_side_effects = true; // One pardes-snap run at a time (its lock): the scripts, then the seeds. run_monkey_replay.step.dependOn(&run_replay.step); - b.step("replay", "record every snapshot script's and -Dreplay-seeds monkey seeds' tapes and require each to replay without diverging").dependOn(&run_monkey_replay.step); + // The same seeds with three terminals on the session, each a view of + // it, typed into in turn (views.zig). + const run_views_replay = b.addRunArtifact(monkey); + run_views_replay.addArtifactArg(snap); + run_views_replay.addArtifactArg(opts.exe); + run_views_replay.addArgs(&.{ b.fmt("{d}", .{seeds}), "70", "--views=3", "--tape=" ++ tapes ++ "/monkey-views", "--out=" ++ tapes ++ "/monkey-views" }); + run_views_replay.setCwd(b.path(".")); + run_views_replay.has_side_effects = true; + run_views_replay.step.dependOn(&run_monkey_replay.step); + b.step("replay", "record every snapshot script's and -Dreplay-seeds monkey seeds' tapes (one terminal and three) and require each to replay without diverging").dependOn(&run_views_replay.step); const monkey_tests = b.addTest(.{ .root_module = monkey.root_module }); b.step("monkey-test", "test the monkey's generator and panic detector").dependOn(&b.addRunArtifact(monkey_tests).step); const run_driver_test = b.addRunArtifact(snap); diff --git a/src/File.zig b/src/File.zig index d795eaa1..dd6b2d34 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1208,7 +1208,12 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { // the caller did not say, is what the line index wants anyway. var twins: [pardes.MAX_PANES]usize = undefined; const ntwins = twinsOf(p, f, &twins); - const span: ?Span = span_in orelse if (ntwins > 0) spanOf(f.content, new) else null; + // The views not live keep their place in it as twins do (views.zig). + const viewed = pardes.views.several(p); + const span: ?Span = span_in orelse if (ntwins > 0 or viewed) spanOf(f.content, new) else null; + var parked: ParkedPlaces = .{}; + if (viewed) parked.before(p, f, span.?, new.len); + defer if (viewed) parked.after(p); var moved: [pardes.MAX_PANES]struct { cursor: usize, scroll: usize } = undefined; for (twins[0..ntwins], 0..) |id, i| { const t = p.panes[id].?; @@ -1301,6 +1306,50 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { } } +/// Each other view's cursor and scroll on a text being changed, as offsets +/// moved by the splice (`spliced`), put back as places in the new text once +/// it is in: a view not live keeps its place, as a twin does. Its +/// selections go, as a twin's do. +const ParkedPlaces = struct { + const max = pardes.MAX_PANES * pardes.views.max; + at: [max]struct { pane: *Pane, view: u8, cursor: usize, scroll: usize } = undefined, + n: usize = 0, + + fn before(pp: *ParkedPlaces, p: *Pardes, f: *State, span: Span, new_len: usize) void { + for (p.panes) |slot| { + const pane = slot orelse continue; + const tf = if (pane.file) |*file| file else continue; + if (tf != f and (f.twin == 0 or tf.twin != f.twin)) continue; + for (0..pardes.views.max) |k| { + if (k == p.view or !pardes.views.isOpen(p, @intCast(k)) or pp.n == max) continue; + const v = &pane.pv[k]; + const at = lineStart(p.gpa, tf, @intCast(@max(0, v.body.cur_row))) + @as(usize, @intCast(@max(0, v.body.cur_col))); + pp.at[pp.n] = .{ + .pane = pane, + .view = @intCast(k), + .cursor = spliced(@min(at, f.content.len), f.content.len, new_len, span), + .scroll = spliced(@min(lineStart(p.gpa, tf, v.scroll), f.content.len), f.content.len, new_len, span), + }; + pp.n += 1; + } + } + } + + fn after(pp: *ParkedPlaces, p: *Pardes) void { + for (pp.at[0..pp.n]) |a| { + const tf = &(a.pane.file orelse continue); + const v = &a.pane.pv[a.view]; + const cursor = positionIn(p.gpa, tf, @min(a.cursor, tf.content.len)); + v.body.cur_row = @intCast(cursor.row); + v.body.cur_col = @intCast(cursor.col); + v.body.vsel.active = false; + v.body.msel.active = false; + v.body.nsel = 0; + v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row; + } + } +}; + /// Where an offset into the old text is in the new, after the splice that /// kept `span` at either end: past it, moved by what it added; inside it, /// at its start, as acme moves a clone's q0 (textinsert, textdelete). diff --git a/src/daemon.zig b/src/daemon.zig index 0f770514..ac9e6eb4 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -530,21 +530,32 @@ fn hupOnStubDeath(on: bool) void { pub fn tellStubDetached() void { if (stub < 0) return; hupOnStubDeath(false); + tellDetached(stub); + stub = -1; +} + +/// The stub on `fd` told its terminal is detached (one of a session's +/// several, views.zig), and the pipe let go. +pub fn tellDetached(fd: c_int) void { + if (fd < 0) return; var buf: [160]u8 = undefined; const said = std.fmt.bufPrint(&buf, "d{s}", .{name()}) catch "d"; - _ = libc.write(stub, said.ptr, said.len); - _ = libc.close(stub); - stub = -1; + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); } /// The session's end, `code` its exit status: the stub is told (and goes). pub fn tellStubExit(code: u8) void { - if (stub < 0) return; + tellExit(stub, code); + stub = -1; +} + +pub fn tellExit(fd: c_int, code: u8) void { + if (fd < 0) return; var buf: [8]u8 = undefined; const said = std.fmt.bufPrint(&buf, "x{d}", .{code}) catch "x1"; - _ = libc.write(stub, said.ptr, said.len); - _ = libc.close(stub); - stub = -1; + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); } /// A terminal refused (an attach that came too late): its stub is told why. @@ -742,10 +753,7 @@ const Daemon = struct { _ = std.fmt.bufPrint(why, "no '{s}' is {s}={s}`", .{ wanted_name, other, wanted_name }) catch {}; return error.Said; } - if (!s.detached) { - _ = std.fmt.bufPrint(why, "no '{s}' is attached already, to another terminal or window", .{wanted_name}) catch {}; - return error.Said; - } + // Attached already: the session takes another view. return @intCast(i); }; _ = std.fmt.bufPrint(why, "no no session called '{s}'", .{wanted_name}) catch {}; @@ -764,6 +772,15 @@ const Daemon = struct { found = @intCast(i); }; if (count == 1) return found.?; + // None detached: the one session there is, as another view of it. + if (count == 0) { + for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind == k) { + count += 1; + found = @intCast(i); + }; + if (count == 1) return found.?; + count = 0; + } if (count == 0 and others == 0) { w.writeAll("no no detached session is running") catch {}; return error.Said; diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a1590b48..2d9124cc 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1550,6 +1550,68 @@ const Gui = struct { deck: deck.Deck = .{}, }; +/// The Gui's fields that are each window's own, one a view (views.zig): +/// the window and its presenting, its scroll and glide, its scene and post +/// chain, its pointer, its blink, its images as presented, its capture. The +/// device, the atlas, the font and the pipelines are every window's. The +/// Gui holds the live window's; the others wait in `Shell.windows`. +const window_fields = [_][:0]const u8{ + "window", "soft_renderer", "soft_texture", "soft_tex_w", "soft_tex_h", + "window_w", "window_h", "scroll_pane", "scroll_rect", "scroll_body_y", + "scroll_body_h", "scroll_held_px", "scroll_col", "scroll_row", "scroll_delta", + "scroll_lag", "scroll_edge", "scroll_edge_len", "scroll_notch", "program_wheel", + "glide", "pace_ns", "sel_glow", "hover_glow", "frame_arrival_ns", + "blink_due", "cursor_quad_alpha", "scene_tex", "scene_tex_w", "scene_tex_h", + "scene_failures", "scene_target_failed", "post", "pointer_present", "pointer_shape", + "pointer_mapped", "pointer_cell", "presented_images", "prepared_images", "touch", + "live_ctrl", "live_alt", "applied_window_opacity", "capture_tex", "capture_tex_w", + "capture_tex_h", "capture_xfer", "capture_xfer_size", "captured", "capture_dir", +}; + +const GuiWindow = blk: { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + var types: [window_fields.len]type = undefined; + var attrs: [window_fields.len]std.lang.Type.Struct.FieldAttributes = undefined; + for (window_fields, 0..) |name, i| { + const at = for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else @compileError("gui: Gui has no field " ++ name); + types[i] = st.field_types[at]; + attrs[i] = .{ .default_value_ptr = st.field_attrs[at].default_value_ptr }; + } + break :blk @Struct(.auto, null, &window_fields, &types, &attrs); +}; + +fn copyWindow(dst: anytype, src: anytype) void { + inline for (window_fields) |name| @field(dst, name) = @field(src, name); +} + +/// The live window's share of `g` as a window starts, but its SDL window +/// (reopenWindow makes that). +fn freshWindow(g: *Gui) void { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + inline for (window_fields) |name| { + const at = comptime for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else unreachable; + if (comptime st.field_attrs[at].defaultValue(st.field_types[at])) |d| @field(g, name) = d; + } +} + +/// What a window held of its own let go (closeWindow lets the window go). +fn releaseWindowParts(g: *Gui, gpa: std.mem.Allocator) void { + if (g.scene_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); + g.post.deinit(gpa, g.device); + g.glide.deinit(gpa); + gpa.free(g.scroll_edge); + g.presented_images.deinit(gpa); + g.prepared_images.deinit(gpa); +} + fn setGuiFontName(g: *Gui, fallback: []const u8) void { const name = if (c.ui_font_name(g.font)) |name_z| std.mem.span(name_z) else fallback; const len = @min(name.len, g.font_name.len); @@ -2247,7 +2309,9 @@ fn closeWindow(g: *Gui) void { g.soft_renderer = null; } else c.SDL_ReleaseWindowFromGPUDevice(g.device, g.window); c.SDL_DestroyWindow(g.window); - if (g.window_slot) |slot| slot.* = null; + if (g.window_slot) |slot| if (slot.* == g.window) { + slot.* = null; + }; } /// `pardes-gui --attach`: a window again, as the first was made, at the @@ -2399,6 +2463,14 @@ fn localSession( if (fs) |f| try f.watch(&queue, wakeFs); _ = c.SDL_StartTextInput(g.window); + // PARDES_TEST_VIEWS=N: N more windows from the start, each a view of the + // session (views.zig), captured under view-; view 0 live after. + if (test_mode) if (std.c.getenv("PARDES_TEST_VIEWS")) |n| { + for (0..std.fmt.parseInt(u8, std.mem.span(n), 10) catch 0) |_| shell.openAnotherWindow(); + core.toView(0); + shell.follow(); + }; + defer shell.closeOtherWindows(); if (test_mode) setStdinRaw() catch {}; shell.threads_ok = true; @@ -2431,6 +2503,10 @@ fn localSession( }; pipe_tasks.cancelAll(io); queue.discardCompletions(); + // ponytail: a restored session is one view, the live window's: + // the others close. + shell.closeOtherWindows(); + shell.gview = 0; shell.stopPtys(); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( inotify_fd, @@ -2635,6 +2711,24 @@ const StdinFeed = struct { // A mouse event at window pixels, built as SDL would and run // through dispatch, so a driver exercises the real pointer path // (pixel to cell, tag and body hits). SGR mouse carries only cells. + // ESC]777;view;BEL: what follows is window k's (k from 1), + // as an event of that window's makes its view live (route). + const view_prefix = "\x1b]777;view;"; + if (std.mem.startsWith(u8, f.buf[seq_start..len], view_prefix)) { + const body = f.buf[seq_start + view_prefix.len .. len]; + const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { + std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); + f.fill = len - seq_start; + break; + }; + const k = std.fmt.parseInt(u8, body[0..end], 10) catch 0; + if (in.shell) |sh| if (k >= 1 and pardes.views.isOpen(sh.core, k - 1)) { + sh.core.toView(k - 1); + sh.follow(); + }; + seq_start += view_prefix.len + end + 1; + continue; + } const mouse_prefix = "\x1b]777;mouse;"; if (std.mem.startsWith(u8, f.buf[seq_start..len], mouse_prefix)) { const body = f.buf[seq_start + mouse_prefix.len .. len]; @@ -2975,6 +3069,9 @@ test "the window's function and editing keys reach a terminal as its cursor keys const Input = struct { core: ?*pardes.Pardes = null, + /// The shell whose windows these are: a window closed while others + /// stay takes its view with it (views.zig). + shell: ?*Shell = null, quit: bool = false, fn post(in: *Input, ev: pardes.Event) void { @@ -2982,6 +3079,7 @@ const Input = struct { } fn close(in: *Input) void { + if (in.shell) |s| if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); if (in.core) |core| core.quit = true; in.quit = true; } @@ -3018,7 +3116,11 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { else => {}, } switch (sev.type) { - c.SDL_EVENT_QUIT, c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), + c.SDL_EVENT_QUIT => { + in.shell = null; + in.close(); + }, + c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), c.SDL_EVENT_WINDOW_FOCUS_GAINED => in.post(.{ .focus = true }), c.SDL_EVENT_WINDOW_FOCUS_LOST => in.post(.{ .focus = false }), c.SDL_EVENT_WINDOW_MOUSE_LEAVE => { @@ -3677,6 +3779,10 @@ const Shell = struct { /// The tick a paced frame is sampled at (waitInput): the clock the /// core is advanced to while anything moves. paced_ns: ?u64 = null, + /// Each other view's window, while another's is in the Gui (views.zig), + /// and the view whose window the Gui holds (`follow`). + windows: [pardes.views.max]GuiWindow = undefined, + gview: u8 = 0, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3766,10 +3872,92 @@ const Shell = struct { fn detachWindow(ctx: ?*anyopaque) void { const s = shellOf(ctx); 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 (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session; closing its window ends it"); s.closeTheWindow(); } + /// The Gui holds the window of the core's live view. + fn follow(s: *Shell) void { + const g = s.gui orelse return; + const k = s.core.view; + if (s.gview == k) return; + if (s.gview < s.windows.len) copyWindow(&s.windows[s.gview], g); + copyWindow(g, &s.windows[k]); + s.gview = k; + } + + /// An event of another view's window: that view goes live. + fn route(s: *Shell, sev: *const c.SDL_Event) void { + 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; + 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(); + }; + } + + /// `pardes-gui --attach` while a window is open: another window, a view + /// of its own (views.zig), opened as the first was. + fn openAnotherWindow(s: *Shell) void { + const g = s.gui orelse return; + s.follow(); + const k = s.core.openView() orelse return log.err("--attach: this session has as many windows as it takes", .{}); + const live = s.gview; + copyWindow(&s.windows[live], g); + freshWindow(g); + if (!reopenWindow(g)) { + log.err("--attach: no window: {s}", .{c.SDL_GetError()}); + copyWindow(g, &s.windows[live]); + return s.core.closeView(k); + } + if (s.test_mode) { + // Its own grid, its captures under view- (PARDES_TEST_VIEWS). + // ponytail: the path is kept for the session, a few bytes. + const cols: u32 = if (std.c.getenv("PARDES_TEST_COLS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 80 else 80; + const rows: u32 = if (std.c.getenv("PARDES_TEST_ROWS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 24 else 24; + _ = c.SDL_SetWindowSize(g.window, @intCast(cols * g.cell_w + g.capture_pad), @intCast(rows * g.cell_h + g.capture_pad)); + _ = c.SDL_SyncWindow(g.window); + g.capture = true; + g.capture_dir = std.fmt.allocPrint(s.gpa, "{s}/view-{d}", .{ s.windows[live].capture_dir, k }) catch ""; + if (g.capture_dir.len > 0) std.Io.Dir.cwd().createDirPath(s.io, g.capture_dir) catch {}; + } + s.gview = k; + s.core.toView(k); + s.core.needs_frame = 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; + s.core.toView(k); + s.follow(); + closeWindow(g); + releaseWindowParts(g, s.gpa); + s.gview = std.math.maxInt(u8); + s.core.closeView(k); + s.follow(); + s.core.needs_frame = true; + } + + /// The session's end: every window but the live one closed. + fn closeOtherWindows(s: *Shell) void { + const g = s.gui orelse return; + for (0..s.windows.len) |i| { + const k: u8 = @intCast(i); + if (k == s.gview or !pardes.views.isOpen(s.core, k)) continue; + var live: GuiWindow = undefined; + copyWindow(&live, g); + copyWindow(g, &s.windows[k]); + closeWindow(g); + releaseWindowParts(g, s.gpa); + copyWindow(g, &live); + } + } + fn closeTheWindow(s: *Shell) void { const g = s.gui orelse return; closeWindow(g); @@ -3975,7 +4163,7 @@ const Shell = struct { .files_changed => check_files = true, .fs_ready => {}, .window => { - s.openTheWindow(); + if (s.gui != null) s.openAnotherWindow() else s.openTheWindow(); s.saw_event = true; }, .adopted => |a| { @@ -4008,7 +4196,8 @@ fn shellOf(ctx: ?*anyopaque) *Shell { fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = shellOf(ctx); const core = s.core; - var in: Input = .{ .core = core }; + var in: Input = .{ .core = core, .shell = s }; + s.follow(); 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 @@ -4100,16 +4289,27 @@ 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 (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + if (sev.type == c.SDL_EVENT_MOUSE_MOTION and (motion == null or motion.?.motion.windowID == sev.motion.windowID)) { motion = sev; } else { - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } motion = null; - dispatch(g, &in, &sev); + if (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + motion = sev; + } else { + s.route(&sev); + dispatch(g, &in, &sev); + } } if (!c.SDL_PollEvent(&sev)) break; } - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } } runDemo(g, &in, core); // Paced: what came before the tick is in; wait out the rest of it, @@ -4133,8 +4333,12 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const more = c.SDL_PollEvent(&sev); pardes.turn.wake(); if (more) { + s.route(&sev); dispatch(g, &in, &sev); - while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev); + while (c.SDL_PollEvent(&sev)) { + s.route(&sev); + dispatch(g, &in, &sev); + } } else { // The events asked for every 100 µs, the core busy between. const until = c.SDL_GetTicksNS() + 100 * std.time.ns_per_us; @@ -4182,6 +4386,7 @@ fn waitDetached(core: *pardes.Pardes, timeout_ms: u32) void { fn pollFrame(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); s.reconcilePtys(); const core = s.core; if (s.fs) |f| if (f.tick().pending) s.queue.push(.fs_ready); @@ -4414,6 +4619,7 @@ test "background opacity acknowledges success and rolls back unsupported surface fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const s = shellOf(ctx); const core = s.core; + s.follow(); const g = s.gui orelse return; frame_started_ns = monoNs(); const frame = @constCast(surface); @@ -4447,6 +4653,7 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { fn postPresent(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); const g = s.gui orelse return; // No image (a minimized window) shows no tracks; time still passes and // the pump advances it all the same. diff --git a/src/panes.zig b/src/panes.zig index a782e3e0..71a04464 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -1,6 +1,7 @@ const std = @import("std"); const vaxis = @import("vaxis"); const pardes = @import("pardes.zig"); +const views = @import("views.zig"); const layout = @import("layout.zig"); const config = @import("config.zig"); const Pardes = pardes.Pardes; @@ -26,8 +27,9 @@ pub const EditText = struct { text: []u8, row0: i32 }; pub const Pane = struct { /// What the replay digest leaves out (src/walk.zig): the view's geometry, /// made again from the rest (File.geometry); the 9P server's record of - /// the pane's opens and readers. + /// the pane's opens and readers; the other views' share of it (views.zig). pub const replica = .{ + .local = .{ "pv", "pv_known" }, .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices" }, .authority = .{"fs"}, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, @@ -95,6 +97,7 @@ pub const Pane = struct { pub const Cwd = union(enum) { none, inherited: *Pane, owned: []u8 }; pub const Focus = enum { body, tag }; + pub const SessionView = struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 }; terminal: ?*terminal.State = null, gpa: std.mem.Allocator, serial: u32 = 0, @@ -263,7 +266,7 @@ pub const Pane = struct { geometry_key: u64 = 0, geometry_len: usize = 0, /// The session's part of the geometry, as of the last step (`sync`). - view: struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 } = .{}, + view: SessionView = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -297,6 +300,15 @@ pub const Pane = struct { /// body layer reserves exactly `len` rows for them, the way it already /// reserves rows for sticky context headers. notices: Notices = .{}, + /// Each other view's share of this pane, while it is not live + /// (views.zig): its cursor, scroll and place in that view's layout. + pv: [views.max]views.PaneView = undefined, + /// The views `pv` holds a share for (a pane retired before a view came + /// has none of it). + pv_known: u8 = 0, + /// The view that last typed into it: a terminal shown at two sizes takes + /// that view's (Pardes.sync). + sized_by: u8 = 0, pub const MessagePhase = enum { entering, shown, lingering, leaving }; /// One message line's animation: falling in, shown, lingering after the diff --git a/src/pardes.zig b/src/pardes.zig index 228d97fa..88d75fd1 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -64,6 +64,7 @@ pub const memory = @import("memory.zig"); pub const tape = @import("tape.zig"); pub const walk = @import("walk.zig"); pub const screen_state = if (terminal_panes) @import("screen_state.zig") else struct {}; +pub const views = @import("views.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -511,6 +512,7 @@ test { _ = @import("Handover.zig"); _ = @import("walk.zig"); _ = screen_state; + _ = @import("views.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -4699,6 +4701,12 @@ pub const Pardes = struct { restore_buf: [1024]u8 = undefined, surface: Surface = .{}, + /// The view the fields above are of: whose layout, focus, pointer and + /// panes' cursors and scroll (views.zig). `views_open` says which views + /// there are, one a window or terminal; each not live is in `parked`. + view: u8 = 0, + views_open: u8 = 1, + parked: [views.max]?*views.Parked = @splat(null), /// per-update scratch (paneCursorLines, selection text); reset each update scratch: std.heap.ArenaAllocator, /// The arena each frame's Surface text is built in, reset by `pump`. Hosts @@ -4725,6 +4733,9 @@ pub const Pardes = struct { // Drained by every pump before its digest; a replica's queues // are its own. "effects", "in_q", + // The views not live: written whenever one goes out, so the + // live one's slot is stale (views.zig). + "parked", }, .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, // The host's handles, the 9P server's fids and log, the store file @@ -5020,6 +5031,7 @@ pub const Pardes = struct { slot.* = null; }; const gpa = p.gpa; + views.deinit(p); for (&p.col_tags) |*t| t.deinit(gpa); p.global_tag.deinit(gpa); p.registers.deinit(gpa); @@ -5167,6 +5179,7 @@ pub const Pardes = struct { if (comptime pdf_enabled) if (pane.pdf) |*pv| panes.Pdf.deinitPane(p, pane, pv); if (pane.file) |*file| panes.File.deinit(p, pane, file); if (pane.ovl) |o| p.gpa.free(o.text); + views.tornDown(p, pane); pane.body.deinit(p.gpa); pane.tag.deinit(p.gpa); pane.input.deinit(p.gpa); @@ -5201,6 +5214,7 @@ pub const Pardes = struct { pane.serial = p.next_serial; p.rects[id] = .{}; p.panes[id] = pane; + views.installed(p, pane); panes.terminal.adoptSettings(p, pane); // /log hears of it once the update that made it ends, when it has // its file, name and place (`ctlfs.events.announce`). @@ -5569,6 +5583,33 @@ pub const Pardes = struct { /// Something changed the core without going through `update` -- a watched /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. + /// A view of its own for a new client (a second window, a terminal + /// `pardes --attach`ed), as the live one is laid out; null at + /// views.max. Not live until `toView`. + pub fn openView(p: *Pardes) ?u8 { + const taped = tape.enter(p, .open_view); + defer tape.leave(p, taped); + p.needs_frame = true; + return views.open(p); + } + + /// The client of view `k` gone: its view goes, the others stay. The + /// last one stays the session's own. + pub fn closeView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .close_view = k }); + defer tape.leave(p, taped); + views.close(p, k); + p.sync(); + } + + /// Into view `k`, before the input its client sent. + pub fn toView(p: *Pardes, k: u8) void { + if (k == p.view) return; + const taped = tape.enter(p, .{ .to_view = k }); + defer tape.leave(p, taped); + views.to(p, k); + } + pub fn invalidate(p: *Pardes) void { const taped = tape.enter(p, .invalidate); defer tape.leave(p, taped); @@ -6053,6 +6094,8 @@ pub const Pardes = struct { // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. if (v.watched) |f| if (!f(h.ctx)) return; + // The other views first, each into its own window or terminal. + if (views.several(p)) try views.drawOthers(p, h); _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); p.frames.mark_update = FrameStats.nowNs(); p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; @@ -6235,6 +6278,7 @@ pub const Pardes = struct { .key => |key| { p.can_ask = true; defer p.can_ask = false; + p.typedInto(); p.handleKey(key); }, .mouse => |m| { @@ -6254,7 +6298,10 @@ pub const Pardes = struct { } } }, - .paste => |bytes| edit.applyPaste(p, bytes), + .paste => |bytes| { + p.typedInto(); + edit.applyPaste(p, bytes); + }, .command => |line| _ = p.executeBuiltinLine(p.active, line), .pinch => |scale| p.ov_pinch_scale = scale, .touch_scroll => |delta| p.ov_touch_scroll_delta = delta, @@ -7672,6 +7719,7 @@ pub const Pardes = struct { tagline.exitHeader(p); } p.reapPanes(); + if (views.several(p)) views.show(p); layout.compute(p); p.presentation.sync(p); p.trackJump(); @@ -7759,7 +7807,9 @@ pub const Pardes = struct { // A terminal's pty follows its pane down to a row, whatever the // resize; only a folded one keeps the size it had, and one not // placed yet the grid it was made with. - if ((cols != pane.cols or rows != pane.rows) and (r.h > pane.tag_rows or (pane.isTerminal() and !pane.collapsed and r.h > 0))) { + if ((cols != pane.cols or rows != pane.rows) and (r.h > pane.tag_rows or (pane.isTerminal() and !pane.collapsed and r.h > 0)) and + (!pane.isTerminal() or p.sizes(pane))) + { // doc panes have no pty/emulator grid to reflow; just record // the size so bodyText renders the right number of rows if (pane.isTerminal()) { @@ -7798,6 +7848,22 @@ pub const Pardes = struct { tagline.revealHeader(p); } + /// The view typing goes into is the one its terminal is sized for + /// (`sizes`). + fn typedInto(p: *Pardes) void { + if (p.panes[p.active]) |pane| pane.sized_by = p.view; + } + + /// Whether the live view sizes terminal `pane`: a terminal shown at two + /// sizes takes the size of the view that typed into it last, and any + /// view's once that one is gone. The others show it at that size. + fn sizes(p: *Pardes, pane: *Pane) bool { + if (pane.sized_by == p.view) return true; + if (views.isOpen(p, pane.sized_by)) return false; + pane.sized_by = p.view; + return true; + } + pub fn theme(p: *const Pardes) *const Theme { if (p.custom_theme) |*custom| return custom; return &themes[p.settings.theme]; diff --git a/src/tape.zig b/src/tape.zig index eaf1a9fa..ec38deac 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -57,6 +57,9 @@ pub const Input = union(enum) { acknowledge_panel_presentation: []const pardes.animation.Track, run_closed_lines, file_deleted: u8, + open_view, + close_view: u8, + to_view: u8, }; /// The core's own syscalls (each `turn.yield()`s around itself), numbered @@ -442,6 +445,9 @@ pub const Tape = struct { .acknowledge_panel_presentation => |tracks| p.acknowledgePanelPresentation(tracks), .run_closed_lines => pardes.ctlfs.runClosedLines(p), .file_deleted => |id| pardes.panes.File.deleted(p, id), + .open_view => _ = p.openView(), + .close_view => |k| p.closeView(k), + .to_view => |k| p.toView(k), } t.finishInput(); } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 760c0692..f949d571 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -21,15 +21,45 @@ extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467)); -/// The terminal answered the shared-memory probe (`kittyShmProbe`): it can -/// read a raster from a POSIX shm object, so a page costs a name on the wire -/// instead of its whole base64 body. Set by the input reader, which sees the -/// reply before vaxis' parser swallows every graphics APC alike. -var kitty_shm = std.atomic.Value(bool).init(false); -/// Only a reply that arrives while the probe is out counts. The id is one -/// vaxis' own image numbering (1, 2, ...) never reaches, because a terminal -/// answers every transmission `i=;OK` and one of those must not pass. -var kitty_shm_probing = std.atomic.Value(bool).init(false); +/// What a terminal's input reader keeps, one a terminal (views.zig): its +/// graphemes, the frame it waits on the answer to, the shm probe, and the +/// view its input is. +const Feed = struct { + view: u8 = 0, + cache: vaxis.GraphemeCache = .{}, + ack: FrameAck = .{}, + /// The terminal answered the shared-memory probe (`kittyShmProbe`): it + /// can read a raster from a POSIX shm object, so a page costs a name on + /// the wire instead of its whole base64 body. Set by the input reader, + /// which sees the reply before vaxis' parser swallows every graphics APC + /// alike. + shm: std.atomic.Value(bool) = .init(false), + /// Only a reply that arrives while the probe is out counts. The id is + /// one vaxis' own image numbering (1, 2, ...) never reaches, because a + /// terminal answers every transmission `i=;OK` and one of those must + /// not pass. + shm_probing: std.atomic.Value(bool) = .init(false), +}; + +/// A terminal's input onto the loop's queue, as its view's: a `from` goes +/// ahead of it whenever another terminal posted last. With one terminal +/// none ever does. +const Poster = struct { + loop: *Loop, + view: u8, + var lock: std.Io.Mutex = .init; + var last: u8 = 0; + + pub fn postEvent(p: Poster, ev: @TypeOf(Command.value)) !void { + lock.lockUncancelable(p.loop.io); + defer lock.unlock(p.loop.io); + if (last != p.view) { + try p.loop.postEvent(.{ .from = p.view }); + last = p.view; + } + try p.loop.postEvent(ev); + } +}; const kitty_shm_probe_id = 0x7fff_fff1; var kitty_shm_serial: u32 = 0; /// Transmissions an unread shm segment outlives before it is removed. @@ -62,13 +92,13 @@ fn kittyShmWrite(name: [:0]const u8, bytes: []const u8) bool { /// Ask with a 1x1 RGB image in shm whether the terminal reads that medium. A /// remote terminal cannot see our shm and answers an error (or nothing). -fn kittyShmProbe(w: *std.Io.Writer) void { +fn kittyShmProbe(w: *std.Io.Writer, feed: *Feed) void { var buf: [64]u8 = undefined; const name = kittyShmName(&buf, 0) catch return; if (!kittyShmWrite(name, &.{ 0, 0, 0 })) return; var b64: [64]u8 = undefined; const enc = std.base64.standard.Encoder.encode(&b64, name); - kitty_shm_probing.store(true, .release); + feed.shm_probing.store(true, .release); w.print("\x1b_Gi={d},s=1,v=1,a=q,t=s,f=24;{s}\x1b\\", .{ kitty_shm_probe_id, enc }) catch return; w.flush() catch {}; } @@ -86,6 +116,12 @@ pub const Command = struct { adopted: struct { pid: posix.pid_t, fd: posix.fd_t }, /// A timed wait ran out: the core's next wake has come. wake, + /// The input after this is the terminal of this view's (Poster). + from: u8, + /// A SIGWINCH: a terminal changed size (Shell.winched). + winched, + /// A SIGHUP: the session's own terminal hung up. + hangup, key_press: vaxis.Key, pty_read: struct { id: usize, gen: u32, bytes: []u8 }, pty_eof: struct { id: usize, gen: u32 }, @@ -107,36 +143,36 @@ pub const Command = struct { }; const Loop = vaxis.Loop(@TypeOf(Command.value)); -fn startInput(loop: *Loop, cache: *vaxis.GraphemeCache) !void { +fn startInput(loop: *Loop, sc: *Screen) !void { if (comptime builtin.os.tag == .windows) return loop.start(); - if (loop.thread != null) return; - loop.thread = try loop.io.concurrent(inputThread, .{ loop, cache }); + if (sc.thread != null) return; + sc.thread = try loop.io.concurrent(inputThread, .{ loop, sc }); } -fn stopInput(loop: *Loop) void { - if (comptime builtin.os.tag == .windows) return loop.stop(); - if (loop.thread) |*thread| { - thread.cancel(loop.io); - loop.thread = null; +fn stopInput(io: std.Io, thread: *?std.Io.Future(void)) void { + if (thread.*) |*t| { + t.cancel(io); + thread.* = null; } } -fn inputThread(loop: *Loop, cache: *vaxis.GraphemeCache) void { +fn inputThread(loop: *Loop, sc: *Screen) void { // vaxis's test terminal reads nothing off Linux: it has no read at all. if (comptime !@hasDecl(vaxis.Tty, "read")) return; - inputReader(loop, loop.tty, cache); + inputReader(loop, &sc.tty, &sc.vx, &sc.feed); } -fn inputReader(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) void { - readInput(loop, tty, cache) catch |err| { +fn inputReader(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) void { + readInput(loop, tty, vx, feed) catch |err| { if (err == error.Canceled) return; std.log.err("terminal input: {s}", .{@errorName(err)}); }; - loop.postEvent(.quit) catch {}; + (Poster{ .loop = loop, .view = feed.view }).postEvent(.quit) catch {}; } -fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { - try loop.postEvent(.{ .winsize = try tty.getWinsize() }); +fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { + const post: Poster = .{ .loop = loop, .view = feed.view }; + try post.postEvent(.{ .winsize = try tty.getWinsize() }); var parser: vaxis.Parser = .{}; var buf: [1024]u8 = undefined; var carried: usize = 0; @@ -147,14 +183,14 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { // The terminal's answers to frames (FrameAck) come out first, // wherever they sit: never events, and never glued to a key (an // Escape pressed just before one would read as Alt on it). - const end = FrameAck.take(buf[0 .. carried + received], loop); + const end = feed.ack.take(buf[0 .. carried + received], loop); if (end == 0) { carried = 0; continue; } - if (kitty_shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| { - kitty_shm_probing.store(false, .release); - kitty_shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release); + if (feed.shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| { + feed.shm_probing.store(false, .release); + feed.shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release); // Answered either way: a terminal that could not read it left it. var name_buf: [64]u8 = undefined; if (kittyShmName(&name_buf, 0)) |name| _ = libc.shm_unlink(name) else |_| {} @@ -187,9 +223,9 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { std.mem.startsWith(u8, FrameAck.answer, rest)) break; if (!waited and parse_end == end and rest[0] == 0x1b and (rest.len == 1 or (rest.len == 2 and rest[1] == 0x1b))) { waited = true; - if (try inputFollows(tty, loop.io, if (loop.vaxis.caps.kitty_keyboard) 1000 else 50)) break; + if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) break; } - const result = try parser.parse(buf[consumed..parse_end], loop.vaxis.opts.system_clipboard_allocator); + const result = try parser.parse(buf[consumed..parse_end], vx.opts.system_clipboard_allocator); if (result.n == 0) break; consumed += result.n; // A modifier pressed on its own, which the kitty keyboard @@ -197,8 +233,8 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { // chord, and a shell pane was typed its private-use code. if (result.event) |event| if (event == .key_press and event.key_press.isModifier()) continue; if (result.event) |event| - vaxis.loop.handleEventGeneric(loop, loop.vaxis, cache, @TypeOf(Command.value), event, loop.vaxis.opts.system_clipboard_allocator) catch |err| { - if (event == .paste) if (loop.vaxis.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); + vaxis.loop.handleEventGeneric(post, vx, &feed.cache, @TypeOf(Command.value), event, vx.opts.system_clipboard_allocator) catch |err| { + if (event == .paste) if (vx.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); return err; }; // The last answer to the startup queries is no event of its @@ -226,14 +262,14 @@ const FrameAck = struct { const request = "\x1b[5n"; const answer = "\x1b[0n"; const timeout_ns: u64 = std.time.ns_per_s; - var waiting = std.atomic.Value(bool).init(false); - var off = std.atomic.Value(bool).init(false); + waiting: std.atomic.Value(bool) = .init(false), + off: std.atomic.Value(bool) = .init(false), /// When the request went out (the loop's thread only). - var sent_ns: u64 = 0; + sent_ns: u64 = 0, /// Take every answer out of `bytes`, closing up the rest; the new /// length. The loop is woken for the frame that waited on one. - fn take(bytes: []u8, loop: *Loop) usize { + fn take(a: *FrameAck, bytes: []u8, loop: *Loop) usize { var len = bytes.len; var found = false; while (std.mem.indexOf(u8, bytes[0..len], answer)) |at| { @@ -241,18 +277,18 @@ const FrameAck = struct { len -= answer.len; found = true; } - if (found and waiting.swap(false, .acq_rel)) loop.postEvent(.nop) catch {}; + if (found and a.waiting.swap(false, .acq_rel)) loop.postEvent(.nop) catch {}; return len; } /// Whether a new frame must wait (and the loop has a wake at the /// timeout); past the timeout the terminal is taken to never answer. - fn holds(now_ns: u64, io: std.Io) bool { - if (off.load(.acquire) or !waiting.load(.acquire)) return false; - const waited_ns = now_ns -| sent_ns; + fn holds(a: *FrameAck, now_ns: u64, io: std.Io) bool { + if (a.off.load(.acquire) or !a.waiting.load(.acquire)) return false; + const waited_ns = now_ns -| a.sent_ns; if (waited_ns >= timeout_ns) { - off.store(true, .release); - waiting.store(false, .release); + a.off.store(true, .release); + a.waiting.store(false, .release); return false; } requestWake(io, @intCast((timeout_ns - waited_ns + std.time.ns_per_ms - 1) / std.time.ns_per_ms)); @@ -260,14 +296,14 @@ const FrameAck = struct { } /// After a frame that wrote something: ask for the answer. - fn ask(w: *std.Io.Writer, now_ns: u64) void { - if (off.load(.acquire)) return; + fn ask(a: *FrameAck, w: *std.Io.Writer, now_ns: u64) void { + if (a.off.load(.acquire)) return; // Waiting before the request goes: a local terminal can answer // before the write returns, and an answer nobody waited for is lost. - sent_ns = now_ns; - waiting.store(true, .release); - w.writeAll(request) catch return waiting.store(false, .release); - w.flush() catch return waiting.store(false, .release); + a.sent_ns = now_ns; + a.waiting.store(true, .release); + w.writeAll(request) catch return a.waiting.store(false, .release); + w.flush() catch return a.waiting.store(false, .release); } }; @@ -299,7 +335,7 @@ test "terminal input preserves fragmented keys queries paste and text after EOF" vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -321,7 +357,7 @@ test "terminal input preserves fragmented keys queries paste and text after EOF" "plain \x1b[?62;", "4c\x1b[", "A\x1b[200", "~caf\xc3", "\xa9 \xe7", "\x95", "\x8c \xf0\x9f", "\x98\x80", "\x1b[201", "~\x1b]52;c;Y2", "xpcA==\x07tail", "\x1b", } }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(vx.queries_done.load(.unordered)); var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); @@ -384,7 +420,7 @@ test "terminal replies and malformed sequences never become typed text" { vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -427,7 +463,7 @@ test "terminal replies and malformed sequences never become typed text" { "c", }, }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(vx.queries_done.load(.unordered)); try std.testing.expect(vx.caps.kitty_graphics); var text: std.ArrayList(u8) = .empty; @@ -466,7 +502,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; // Two reads with `gap_ms` between them, as an ssh link delivers a // sequence cut across two packets. const Reader = struct { @@ -531,7 +567,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { const all = try std.mem.concat(gpa, u8, &.{ "x", case.bytes, "y" }); defer gpa.free(all); var one: Reader = .{ .parts = .{ all, "" }, .gap_ms = 0 }; - try readInput(&loop, &one, &cache); + try readInput(&loop, &one, &vx, &feed); try Reader.events(&loop, &whole); for (1..all.len) |cut| { // A cut just after an ESC is the one that has to wait, and a @@ -539,7 +575,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { // anywhere else is carried however long the gap. const after_esc = all[cut - 1] == 0x1b; var two: Reader = .{ .parts = .{ all[0..cut], all[cut..] }, .gap_ms = if (after_esc and !case.kitty) 40 else 60 }; - try readInput(&loop, &two, &cache); + try readInput(&loop, &two, &vx, &feed); try Reader.events(&loop, &split); std.testing.expectEqualStrings(whole.items, split.items) catch |err| { std.debug.print("case {f} cut at {d}\n", .{ std.ascii.hexEscape(case.bytes, .lower), cut }); @@ -562,7 +598,7 @@ test "an ESC read by itself is the Escape key at once" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; // More always follows at once: a reader that waited on the ESC would // take `j` for the rest of its sequence (Alt-j). const Reader = struct { @@ -587,7 +623,7 @@ test "an ESC read by itself is the Escape key at once" { for ([_]bool{ false, true }) |kitty| { vx.caps.kitty_keyboard = kitty; var reader: Reader = .{ .parts = &.{ "\x1b", "j" } }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(!reader.asked); try std.testing.expectEqual(.winsize, std.meta.activeTag((try loop.tryEvent()).?)); const escape = (try loop.tryEvent()).?.key_press; @@ -613,7 +649,7 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: [2][]const u8, next: usize = 0, @@ -635,12 +671,12 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { } }; const all = "x" ++ FrameAck.answer ++ "y"; - defer FrameAck.waiting.store(false, .release); + defer feed.ack.waiting.store(false, .release); for (0..all.len) |cut| { - FrameAck.waiting.store(true, .release); + feed.ack.waiting.store(true, .release); var reader: Reader = .{ .parts = .{ all[0..cut], all[cut..] } }; - try readInput(&loop, &reader, &cache); - try std.testing.expect(!FrameAck.waiting.load(.acquire)); + try readInput(&loop, &reader, &vx, &feed); + try std.testing.expect(!feed.ack.waiting.load(.acquire)); var typed: std.ArrayList(u8) = .empty; defer typed.deinit(gpa); var woke = false; @@ -658,9 +694,9 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { } // An Escape pressed just before an answer, in the same read, is the // Escape key, not Alt on the answer. - FrameAck.waiting.store(true, .release); + feed.ack.waiting.store(true, .release); var glued: Reader = .{ .parts = .{ "\x1b" ++ FrameAck.answer, "j" } }; - try readInput(&loop, &glued, &cache); + try readInput(&loop, &glued, &vx, &feed); try std.testing.expectEqual(.winsize, std.meta.activeTag((try loop.tryEvent()).?)); try std.testing.expectEqual(.nop, std.meta.activeTag((try loop.tryEvent()).?)); const escape = (try loop.tryEvent()).?.key_press; @@ -672,25 +708,21 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { test "a terminal that never answers a frame stops being asked after one timeout" { const io = std.testing.io; - defer { - FrameAck.off.store(false, .release); - FrameAck.waiting.store(false, .release); - FrameAck.sent_ns = 0; - } + var ack: FrameAck = .{}; var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); const t0: u64 = 5 * std.time.ns_per_s; - FrameAck.ask(&out.writer, t0); + ack.ask(&out.writer, t0); try std.testing.expectEqualStrings(FrameAck.request, out.written()); // Unanswered: the next frame waits, until the timeout. - try std.testing.expect(FrameAck.holds(t0 + 10 * std.time.ns_per_ms, io)); - try std.testing.expect(FrameAck.holds(t0 + 999 * std.time.ns_per_ms, io)); - try std.testing.expect(!FrameAck.holds(t0 + FrameAck.timeout_ns, io)); + try std.testing.expect(ack.holds(t0 + 10 * std.time.ns_per_ms, io)); + try std.testing.expect(ack.holds(t0 + 999 * std.time.ns_per_ms, io)); + try std.testing.expect(!ack.holds(t0 + FrameAck.timeout_ns, io)); // Then never again: no request, no wait. out.clearRetainingCapacity(); - FrameAck.ask(&out.writer, t0 + 2 * std.time.ns_per_s); + ack.ask(&out.writer, t0 + 2 * std.time.ns_per_s); try std.testing.expectEqual(@as(usize, 0), out.written().len); - try std.testing.expect(!FrameAck.holds(t0 + 2 * std.time.ns_per_s, io)); + try std.testing.expect(!ack.holds(t0 + 2 * std.time.ns_per_s, io)); } test "terminal input cancellation joins blocked reads and queued EOF" { @@ -724,8 +756,8 @@ test "terminal input cancellation joins blocked reads and queued EOF" { } return self.file.readStreaming(std.testing.io, &.{buf}); } - fn run(loop: *Loop, reader: *@This(), cache: *vaxis.GraphemeCache) void { - inputReader(loop, reader, cache); + fn run(loop: *Loop, reader: *@This(), vx_: *vaxis.Vaxis, feed: *Feed) void { + inputReader(loop, reader, vx_, feed); } }; const Case = enum { blocked_read, cancel_eof, deliver_eof }; @@ -738,9 +770,9 @@ test "terminal input cancellation joins blocked reads and queued EOF" { var reader: Reader = .{ .file = .{ .handle = fds[0], .flags = .{ .nonblocking = false } }, .eof = eof }; var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; - loop.thread = try io.concurrent(Reader.run, .{ &loop, &reader, &cache }); - defer stopInput(&loop); + var feed: Feed = .{}; + var thread: ?std.Io.Future(void) = try io.concurrent(Reader.run, .{ &loop, &reader, &vx, &feed }); + defer stopInput(io, &thread); try std.testing.expectEqual(.winsize, std.meta.activeTag(try loop.nextEvent())); try reader.entered.wait(io); if (eof) { @@ -750,10 +782,10 @@ test "terminal input cancellation joins blocked reads and queued EOF" { } if (case == .deliver_eof) { try std.testing.expectEqual(.nop, std.meta.activeTag(try loop.nextEvent())); - loop.thread.?.await(io); + thread.?.await(io); } - stopInput(&loop); - try std.testing.expect(loop.thread == null); + stopInput(io, &thread); + try std.testing.expect(thread == null); var count: usize = 0; while (try loop.tryEvent()) |event| { const expected: std.meta.Tag(@TypeOf(Command.value)) = if (case == .deliver_eof and count == 511) .quit else .nop; @@ -1194,7 +1226,8 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { 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 }; + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + defer screen.kitty.deinit(); switch (term) { .open => try screen.enter(try vaxis.Tty.init(io, &tty_buf)), .fd => |fd| try screen.enter(try Screen.ttyOn(io, &tty_buf, fd)), @@ -1202,27 +1235,19 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { } defer screen.leave(); - var kitty_handles = std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image).init(gpa); - defer { - if (screen.on) clearNativeImages(&kitty_handles, &screen.vx, &screen.tty); - kitty_handles.deinit(); - } var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); var loop: Loop = .init(io, &screen.tty, &screen.vx); - var input_cache: vaxis.GraphemeCache = .{}; - try localSession(init, opts, &screen, &loop, &input_cache, &kitty_handles, &paste_buf); + try localSession(init, opts, &screen, &loop, &paste_buf); } -/// Whether a terminal is there to ask its size (winchWatch): none while -/// detached, when the descriptor it had may be another's. -var terminal_on = std.atomic.Value(bool).init(false); - -/// The terminal a session draws on, while it has one: entered (raw, the +/// 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 -/// a reattach in between (daemon.zig). +/// a reattach in between (daemon.zig). One a view (views.zig): the session's +/// own (`primary`, its stub `daemon.stub`), and one more for each terminal +/// `pardes --attach`ed while it is attached, which keeps its own. const Screen = struct { io: std.Io, gpa: std.mem.Allocator, @@ -1231,6 +1256,24 @@ const Screen = struct { tty: vaxis.Tty = undefined, vx: vaxis.Vaxis = undefined, on: bool = false, + primary: bool = false, + /// The pipe to its stub, an attached one's (the primary's is daemon.stub). + stub: posix.fd_t = -1, + /// Its slot in Killed. + killed: ?usize = null, + feed: Feed = .{}, + thread: ?std.Io.Future(void) = null, + kitty: std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + caps_pending: bool = true, + /// Its size as last taken (`Shell.resized`). + size: ?vaxis.Winsize = null, + tracks: []const pardes.animation.Track = &.{}, + /// T2: the cursor's jump trail (JumpTrail). + trail: JumpTrail = .{}, + /// The view's glide, the GUI's motion drawn at whole rows (anim 12). + glide: pardes.glide.Glide = .{}, + /// T7: when each pane's view last moved (ThumbFlash). + flash: ThumbFlash = .{}, /// `fd` made raw, as vaxis.Tty.init makes /dev/tty: the terminal of a /// forked session is no controlling terminal of its, which /dev/tty is. @@ -1250,56 +1293,88 @@ const Screen = struct { try sc.vx.setMouseMode(sc.tty.writer(), true); try sc.vx.setBracketedPaste(sc.tty.writer(), true); if (comptime !builtin.is_test) { - Killed.arm(sc.tty.fd.handle, sc.tty.termios); + sc.killed = Killed.arm(sc.tty.fd.handle, sc.tty.termios); // What a panic puts back (vaxis.recover). - vaxis.tty.global_tty = sc.tty; + if (sc.primary) vaxis.tty.global_tty = sc.tty; } + const view = sc.feed.view; + sc.feed = .{ .view = view }; + sc.caps_pending = true; + sc.size = null; sc.on = true; - terminal_on.store(true, .release); } fn leave(sc: *Screen) void { if (!sc.on) return; sc.on = false; - terminal_on.store(false, .release); - Killed.disarm(); + if (sc.killed) |slot| Killed.disarm(slot); + sc.killed = null; + clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); sc.vx.exitAltScreen(sc.tty.writer()) catch {}; sc.vx.deinit(sc.gpa, sc.tty.writer()); - if (comptime !builtin.is_test) vaxis.tty.global_tty = null; + if (comptime !builtin.is_test) if (sc.primary) { + vaxis.tty.global_tty = null; + }; sc.tty.deinit(); // restore cooked termios LAST, after vx flushed its resets } + + /// Its stub told the terminal is detached (it ends, and its shell has + /// the terminal back). + fn sayDetached(sc: *Screen) void { + if (sc.primary) return daemon.tellStubDetached(); + daemon.tellDetached(sc.stub); + sc.stub = -1; + } + + /// Its terminal hung up, or the stub that waits on it is gone. + fn hungUp(sc: *const Screen) bool { + const stub = if (sc.primary) daemon.stub else sc.stub; + var fds = [_]posix.pollfd{ + .{ .fd = sc.tty.fd.handle, .events = 0, .revents = 0 }, + .{ .fd = stub, .events = 0, .revents = 0 }, + }; + _ = posix.poll(fds[0..if (stub >= 0) 2 else 1], 0) catch return false; + const gone = posix.POLL.HUP | posix.POLL.ERR; + return fds[0].revents & gone != 0 or (stub >= 0 and fds[1].revents & gone != 0); + } + + fn setStub(sc: *Screen, fd: posix.fd_t) void { + if (sc.primary) daemon.stub = fd else sc.stub = fd; + } }; -/// SIGTERM or SIGHUP: the terminal put back as a clean exit leaves it -- +/// SIGTERM or SIGHUP: each terminal put back as a clean exit leaves it -- /// the main screen, no mouse, paste or keyboard modes, the cursor shown, /// cooked termios -- and then the signal's own death. A nested editor /// killed in a pane must not leave that pane's terminal wedged. /// Async-signal-safe: write, tcsetattr, sigaction, raise. const Killed = struct { - var fd: posix.fd_t = -1; - var cooked: posix.termios = undefined; + var fds: [pardes.views.max]posix.fd_t = @splat(-1); + var cooked: [pardes.views.max]posix.termios = undefined; const reset = "\x1b[= 0) { + for (fds, &cooked) |fd, *termios| if (fd >= 0) { _ = std.c.write(fd, reset, reset.len); - _ = std.c.tcsetattr(fd, .FLUSH, &cooked); - } + _ = std.c.tcsetattr(fd, .FLUSH, termios); + }; // The 9P socket and private prompt files go too: a killed editor // leaves none behind. Its shells too, the daemon's dups let go. ninep_io.unlinkOwnSocket(); @@ -1315,14 +1390,10 @@ fn localSession( opts: pardes.Options, screen: *Screen, loop: *Loop, - input_cache: *vaxis.GraphemeCache, - kitty_handles: *std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), paste_buf: *std.Io.Writer.Allocating, ) !void { const io = init.io; const gpa = init.gpa; - const vx = &screen.vx; - const tty = &screen.tty; const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); @@ -1367,20 +1438,23 @@ fn localSession( .core = core, .prompt_rcs = &prompt_rcs, .loop = loop, - .vx = vx, - .tty = tty, - .screen = screen, - .input_cache = input_cache, - .kitty = kitty_handles, .frame = &frame_arena, .paste_buf = paste_buf, .inotify_fd = file_watch.init(false), .fs = fs, .test_clock = host_io.testClock(), }; + sh.terms[0] = screen; pardes.lsp.setStatusSink(&sh, lspStatusSink); host_io.setExitWake(loop, wakeForExit); - defer sh.glide.deinit(gpa); + defer screen.glide.deinit(gpa); + // The terminals of the other views end with the session: each put back, + // its stub told. + defer for (&sh.terms) |*slot| if (slot.*) |sc| if (!sc.primary) { + stopInput(io, &sc.thread); + sh.dropScreen(sc, .exit); + slot.* = null; + }; defer { host_io.setExitWake(null, null); pardes.lsp.setStatusSink(null, null); @@ -1421,7 +1495,7 @@ fn localSession( .search_rows => |b| b.deinit(gpa), .attach => |a| { _ = libc.close(a.tty); - _ = libc.close(a.stub); + if (a.stub >= 0) _ = libc.close(a.stub); }, .adopted => |a| _ = libc.close(a.fd), else => {}, @@ -1434,14 +1508,26 @@ fn localSession( core.unattended = !screen.on; while (core.nextEffect()) |effect| core.perform(effect); - if (screen.on) try startInput(loop, input_cache); - defer stopInput(loop); - (try std.Thread.spawn(.{}, winchWatch, .{ loop, vx, tty })).detach(); + if (screen.on) try startInput(loop, screen); + defer stopInput(io, &screen.thread); + (try std.Thread.spawn(.{}, winchWatch, .{loop})).detach(); (try std.Thread.spawn(.{}, wakeWatch, .{ io, loop })).detach(); if (fs) |f| try f.watch(loop, wakeFs); - if (screen.on) try vx.queryTerminalSend(tty.writer()); + if (screen.on) try screen.vx.queryTerminalSend(screen.tty.writer()); // Detached from the start, nothing else may wake the first wait. if (!screen.on) daemon.watch(loop, deliverNote); + // PARDES_TEST_VIEWS: the terminals at these paths are views of the + // session too, from its start, as `pardes --attach`ed ones are (the + // snapshot harness's `start ... views=N`). + if (init.environ_map.get("PARDES_TEST_VIEWS")) |paths| { + var it = std.mem.tokenizeScalar(u8, paths, ' '); + while (it.next()) |path| { + var path_buf: [256]u8 = undefined; + const z = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0) catch continue; + const fd = libc.open(z, .{ .ACCMODE = .RDWR, .NOCTTY = true, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (fd >= 0) sh.attachTerminal(fd, -1); + } + } sh.threads_ok = true; for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { @@ -1497,15 +1583,29 @@ fn localSession( .{ .text = 0 }, ); _ = file_watch.applyThemeEffect(core, gpa, sh.inotify_fd, &sh.watches, 0, false, false); - if (screen.on) clearNativeImages(kitty_handles, vx, tty); - nc.native_images = screen.on and vx.caps.kitty_graphics; - nc.unattended = !screen.on; + // ponytail: a restored session is one view, the live one's + // terminal: the others are let go, as a detach of each. + const live = sh.term().?; + for (&sh.terms) |*slot| if (slot.*) |sc| if (sc != live) { + stopInput(io, &sc.thread); + sh.dropScreen(sc, .detach); + slot.* = null; + }; + sh.terms = @splat(null); + sh.terms[0] = live; + stopInput(io, &live.thread); + live.feed.view = 0; + sh.from = 0; + if (live.on) startInput(loop, live) catch {}; + 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; if (fs) |f| f.reset(nc); core.deinit(); core = nc; sh.core = nc; - if (comptime pardes.pdf_enabled) if (screen.on) { - updateCoreTerminalSize(core, vx.screen.width, vx.screen.height, vx.screen.width_pix, vx.screen.height_pix); + if (comptime pardes.pdf_enabled) if (live.on) { + updateCoreTerminalSize(core, live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix); }; } } @@ -1554,12 +1654,11 @@ const Shell = struct { core: *pardes.Pardes, prompt_rcs: *const host_io.Shell.PromptFiles, loop: *Loop, - vx: *vaxis.Vaxis, - tty: *vaxis.Tty, - /// The terminal, while there is one (Screen.on); null in tests. - screen: ?*Screen = null, - input_cache: ?*vaxis.GraphemeCache = null, - kitty: *std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + /// Each view's terminal, by view (views.zig): the session's own first; + /// none in tests. + terms: [pardes.views.max]?*Screen = @splat(null), + /// The view the input taken now is from (`Command.value.from`). + from: u8 = 0, frame: *std.heap.ArenaAllocator, paste_buf: *std.Io.Writer.Allocating, inotify_fd: c_int, @@ -1576,16 +1675,8 @@ const Shell = struct { lsp_task: ?host_io.Lsp.Task = null, pipe_tasks: PipeTasks = .{}, threads_ok: bool = false, - caps_pending: bool = true, check_files: bool = false, in_paste: bool = false, - tracks: []const pardes.animation.Track = &.{}, - /// T2: the cursor's jump trail (JumpTrail). - trail: JumpTrail = .{}, - /// The view's glide, the GUI's motion drawn at whole rows (anim 12). - glide: pardes.glide.Glide = .{}, - /// T7: when each pane's view last moved (ThumbFlash). - flash: ThumbFlash = .{}, /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the /// running-out wait moves it: the core's next wake. test_clock: ?u64 = null, @@ -1602,6 +1693,8 @@ const Shell = struct { input_ns: u64 = 0, focused_ns: u64 = 0, + const Leaving = enum { detach, exit }; + fn of(ctx: ?*anyopaque) *Shell { return @ptrCast(@alignCast(ctx.?)); } @@ -1653,8 +1746,13 @@ const Shell = struct { return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } + /// The live view's terminal (null in tests). + fn term(s: *const Shell) ?*Screen { + return s.terms[s.core.view]; + } + fn attached(s: *const Shell) bool { - const screen = s.screen orelse return true; + const screen = s.term() orelse return true; return screen.on; } @@ -1668,14 +1766,46 @@ const Shell = struct { fn detach(ctx: ?*anyopaque) void { const s = of(ctx); 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); if (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session (it is its terminal's job)"); s.leaveTerminal(); } + /// View `k`'s terminal let go (detached, or gone) while others stay: the + /// view goes with it. + fn dropView(s: *Shell, k: u8, how: Leaving) void { + const sc = s.terms[k] orelse return; + stopInput(s.io, &sc.thread); + s.terms[k] = null; + s.core.closeView(k); + s.dropScreen(sc, how); + } + + /// A terminal put back and its stub told; an attached one's Screen freed + /// (the session's own stays, a view of none). + fn dropScreen(s: *Shell, sc: *Screen, how: Leaving) void { + if (sc.primary) { + sc.leave(); + return sc.sayDetached(); + } + sc.leave(); + switch (how) { + .detach => sc.sayDetached(), + .exit => { + daemon.tellExit(sc.stub, 0); + sc.stub = -1; + }, + } + sc.glide.deinit(s.gpa); + sc.kitty.deinit(); + s.gpa.free(sc.buf); + s.gpa.destroy(sc); + } + fn leaveTerminal(s: *Shell) void { - const screen = s.screen orelse return; - stopInput(s.loop); - clearNativeImages(s.kitty, s.vx, s.tty); + const screen = s.term() orelse return; + stopInput(s.io, &screen.thread); screen.leave(); s.core.native_images = false; s.core.unattended = true; @@ -1685,39 +1815,65 @@ const Shell = struct { for (0..3) |i| _ = libc.dup2(null_fd, @intCast(i)); if (null_fd > 2) _ = libc.close(null_fd); } - daemon.tellStubDetached(); + screen.sayDetached(); daemon.setState(true); } - /// `pardes --attach`'s terminal, for this detached session: entered as - /// the first one was, sized, asked what it can do, and drawn whole. + /// `pardes --attach`'s terminal: for a detached session its own again, + /// entered as the first one was; for one attached already, a view of + /// its own (views.zig). Sized, asked what it can do, and drawn whole. fn attachTerminal(s: *Shell, fd: posix.fd_t, stub: posix.fd_t) void { - const screen = s.screen orelse return; - if (screen.on) { + const live = s.term() orelse return; + const screen = if (!live.on) live else extra: { + const sc = s.gpa.create(Screen) catch break :extra null; + const buf = s.gpa.alloc(u8, 0x10000) catch { + s.gpa.destroy(sc); + break :extra null; + }; + sc.* = .{ .io = s.io, .gpa = s.gpa, .env = live.env, .buf = buf, .kitty = .init(s.gpa) }; + break :extra sc; + } orelse { _ = libc.close(fd); - return daemon.refuseStub(stub, "that session has a terminal already"); - } + return refuse(stub, "that session has no room for another terminal"); + }; + const extra = screen != live; const tty = Screen.ttyOn(s.io, screen.buf, fd) catch { _ = libc.close(fd); - daemon.setState(true); - return daemon.refuseStub(stub, "that terminal would not go raw"); + if (extra) s.freeScreen(screen) else daemon.setState(true); + return refuse(stub, "that terminal would not go raw"); }; screen.enter(tty) catch { - daemon.setState(true); - return daemon.refuseStub(stub, "that terminal would not take the screen"); + if (extra) s.freeScreen(screen) else daemon.setState(true); + return refuse(stub, "that terminal would not take the screen"); }; - daemon.stub = stub; - FrameAck.waiting.store(false, .release); - FrameAck.off.store(false, .release); - kitty_shm.store(false, .release); - s.caps_pending = true; + if (extra) { + const k = s.core.openView() orelse { + screen.leave(); + s.freeScreen(screen); + return refuse(stub, "that session has as many terminals as it takes"); + }; + screen.feed.view = k; + s.terms[k] = screen; + } + screen.setStub(stub); s.core.unattended = false; - startInput(s.loop, s.input_cache.?) catch {}; - s.vx.queryTerminalSend(s.tty.writer()) catch {}; + startInput(s.loop, screen) catch {}; + screen.vx.queryTerminalSend(screen.tty.writer()) catch {}; s.core.needs_frame = true; daemon.setState(false); } + fn freeScreen(s: *Shell, sc: *Screen) void { + sc.kitty.deinit(); + s.gpa.free(sc.buf); + s.gpa.destroy(sc); + } + + /// A terminal refused: its stub told why (a test's has none). + fn refuse(stub: posix.fd_t, why: []const u8) void { + if (stub >= 0) daemon.refuseStub(stub, why); + } + /// Adopt: the daemon is asked for the shells of sessions that died /// holding them; each comes back (`adopted`) as a terminal pane here. fn adopt(ctx: ?*anyopaque, pane: u8) void { @@ -1880,12 +2036,38 @@ const Shell = struct { else => {}, } if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + // 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 (event == .paste) s.gpa.free(@constCast(event.paste)); + return false; + } + if (s.term() != null) core.toView(s.from); + }, + else => {}, + } switch (event) { .nop => {}, + .from => |k| s.from = k, + .winched => s.winched(), + // A terminal hung up (its stub passes the hangup on) or its stub + // died (a forked session's death signal): that one's input has + // ended. A HUP says not whose: each is asked. + .hangup => for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on and sc.hungUp()) { + s.from = @intCast(k); + if (s.apply(.quit)) return true; + }, .wake => if (s.test_clock) |*virtual| { virtual.* = @max(virtual.*, s.wake_to); }, .quit => { + // One of several: its view goes with it. + if (pardes.views.count(core) > 1) { + s.dropView(core.view, .detach); + return true; + } // The terminal went: a session the daemon keeps carries on // without it, to be attached again. Gone before the first // frame, the reader that takes the daemon's `ok` (kept @@ -1905,13 +2087,7 @@ const Shell = struct { core.update(.pointer_leave); core.update(.{ .focus = false }); }, - .winsize => |ws| if (s.attached()) { - s.vx.resize(s.gpa, s.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 } }); - }, + .winsize => |ws| if (s.attached()) s.resized(ws), .pty_read => |pr| { tookRead(s.io, pr.id); s.output_seen = true; @@ -2008,6 +2184,32 @@ 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| { + 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 } }); + } + + /// A SIGWINCH: each terminal whose size it changed is resized, in its + /// view (a terminal reporting in band says so as input). + fn winched(s: *Shell) void { + for (s.terms, 0..) |slot, k| { + const sc = slot orelse continue; + 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); + } + } + fn reloadWatched(s: *Shell) void { if (!s.check_files) return; s.check_files = false; @@ -2020,13 +2222,12 @@ const Shell = struct { /// notches are); `wheeled` from the mouse event if it should glide. fn stepGlide(ctx: ?*anyopaque) void { const s = of(ctx); - s.glide.step(s.core, s.gpa, null, null); + if (s.term()) |sc| sc.glide.step(s.core, s.gpa, null, null); } fn pollFrame(ctx: ?*anyopaque) void { const s = of(ctx); stepGlide(ctx); - s.core.native_image_patches = kitty_shm.load(.acquire); if (s.fs) |f| if (f.tick().pending) { _ = s.loop.tryPostEvent(.fs_ready) catch {}; }; @@ -2035,23 +2236,26 @@ const Shell = struct { if (host_io.ttyCwd(pt.pid, pt.file.handle, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(id, cwd); }; - if (s.caps_pending and s.attached() and s.vx.queries_done.load(.unordered)) { - s.caps_pending = false; - s.vx.enableDetectedFeatures(s.tty.writer()) catch {}; - s.core.native_images = s.vx.caps.kitty_graphics; - if (s.vx.caps.kitty_graphics) kittyShmProbe(s.tty.writer()); - s.vx.queueRefresh(); + const sc = s.term() orelse return; + s.core.native_image_patches = sc.feed.shm.load(.acquire); + if (sc.caps_pending and sc.on and sc.vx.queries_done.load(.unordered)) { + sc.caps_pending = false; + sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; + s.core.native_images = sc.vx.caps.kitty_graphics; + if (sc.vx.caps.kitty_graphics) kittyShmProbe(sc.tty.writer(), &sc.feed); + sc.vx.queueRefresh(); s.core.needs_frame = true; } } fn present(ctx: ?*anyopaque, canonical: *const pardes.Surface) void { const s = of(ctx); - const vx = s.vx; - s.tracks = canonical.panelTracks(); + const sc = s.term() orelse return; + const vx = &sc.vx; + sc.tracks = canonical.panelTracks(); // The last frame is not answered yet: this one stays owed, and the // answer's wake draws the state as it is then. - if (FrameAck.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { + if (sc.feed.ack.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { s.core.present_skipped = true; return; } @@ -2059,21 +2263,21 @@ const Shell = struct { const surface = panel_compositor.compose( s.frame.allocator(), @constCast(canonical), - s.tracks, + sc.tracks, s.core.theme().bg, ) catch return; const tz_cells = tracy.zone(@src(), "surface->vaxis"); const win = vx.window(); const truecolor = vx.caps.rgb or colortermTruecolor(); // Each of the shell's own motions below asks for frames while it runs. - if (s.glide.pending) s.glide.measure(s.gpa, canonical); - if (s.glide.moving()) s.glide.advance(s.core.now_ns, s.glide.motion); - s.core.shell_continuous = s.glide.moving(); - s.core.frames.scroll = @round(s.glide.follow.offset); - const glided = pardes.glide.shifted(&s.glide, s.frame.allocator(), surface) catch surface; + if (sc.glide.pending) sc.glide.measure(s.gpa, canonical); + if (sc.glide.moving()) sc.glide.advance(s.core.now_ns, sc.glide.motion); + s.core.shell_continuous = sc.glide.moving(); + s.core.frames.scroll = @round(sc.glide.follow.offset); + const glided = pardes.glide.shifted(&sc.glide, s.frame.allocator(), surface) catch surface; const shadowed = chipShadows(s.frame.allocator(), glided, s.core, truecolor) catch glided; - const flashed = s.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; - const trailed = s.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; + 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); tz_cells.end(); for (surface.images[0..surface.nimages]) |maybe| { @@ -2083,7 +2287,7 @@ const Shell = struct { if (!kittyImageRepresentable(place)) continue; } const key = place.cacheKey(); - if (!s.kitty.contains(key) and vx.caps.kitty_graphics and kitty_shm.load(.acquire)) shm: { + if (!sc.kitty.contains(key) and vx.caps.kitty_graphics and sc.feed.shm.load(.acquire)) shm: { var buf: [64]u8 = undefined; kitty_shm_serial +%= 1; // Bounded: a segment this many transmissions old was never @@ -2095,21 +2299,21 @@ const Shell = struct { } const name = kittyShmName(&buf, kitty_shm_serial) catch break :shm; if (!kittyShmWrite(name, place.rgba)) break :shm; - if (vx.transmitLocalImagePath(s.gpa, s.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| - s.kitty.put(key, handle) catch vx.freeImage(s.tty.writer(), handle.id) + if (vx.transmitLocalImagePath(s.gpa, sc.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| + sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id) else _ = libc.shm_unlink(name); } - if (!s.kitty.contains(key) and vx.caps.kitty_graphics) { + if (!sc.kitty.contains(key) and vx.caps.kitty_graphics) { const enc = std.base64.standard.Encoder; if (s.gpa.alloc(u8, enc.calcSize(place.rgba.len))) |b64| { defer s.gpa.free(b64); _ = enc.encode(b64, place.rgba); - if (vx.transmitPreEncodedImage(s.tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null) |handle| - s.kitty.put(key, handle) catch vx.freeImage(s.tty.writer(), handle.id); + if (vx.transmitPreEncodedImage(sc.tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null) |handle| + sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id); } else |_| {} } - if (s.kitty.get(key)) |cached| { + if (sc.kitty.get(key)) |cached| { if (comptime !pardes.pdf_enabled) { const child = win.child(.{ .x_off = place.x, .y_off = place.y, .width = place.w, .height = place.h }); cached.draw(child, .{ .scale = .contain }) catch {}; @@ -2150,32 +2354,32 @@ const Shell = struct { while (true) { var stale: [pardes.MAX_PANES]pardes.ImageCacheKey = undefined; var stale_len: usize = 0; - var image_iterator = s.kitty.iterator(); + var image_iterator = sc.kitty.iterator(); while (image_iterator.next()) |entry| { if (surfaceHasKittyKey(surface, entry.key_ptr.*)) continue; stale[stale_len] = entry.key_ptr.*; stale_len += 1; if (stale_len == stale.len) break; } - for (stale[0..stale_len]) |key| if (s.kitty.fetchRemove(key)) |removed| - vx.freeImage(s.tty.writer(), removed.value.id); + for (stale[0..stale_len]) |key| if (sc.kitty.fetchRemove(key)) |removed| + vx.freeImage(sc.tty.writer(), removed.value.id); if (stale_len < stale.len) break; } if (glided.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar); const tz_render = tracy.zone(@src(), "vx.render"); - const before = s.tty.tty_writer.pos + s.tty.writer().end; - vx.render(s.tty.writer()) catch {}; + const before = sc.tty.tty_writer.pos + sc.tty.writer().end; + vx.render(sc.tty.writer()) catch {}; // The frame's cost to the terminal (FrameStats.Frame.bytes): what // vaxis's diff of the cells wrote. - s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), s.tty.tty_writer.pos + s.tty.writer().end -| before)); - if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); + s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), sc.tty.tty_writer.pos + sc.tty.writer().end -| before)); + if (sc.tty.tty_writer.pos + sc.tty.writer().end != before) sc.feed.ack.ask(sc.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); s.drawn_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); tz_render.end(); } fn postPresent(ctx: ?*anyopaque) void { const s = of(ctx); - s.core.acknowledgePanelPresentation(s.tracks); + if (s.term()) |sc| s.core.acknowledgePanelPresentation(sc.tracks); tracy.frameMark(); } @@ -2320,14 +2524,16 @@ const Shell = struct { fn setClipboard(ctx: ?*anyopaque, text: []const u8) void { const s = of(ctx); // Detached, the session keeps it (a 9P read, a paste here). - if (!s.attached()) return s.core.fallback.setClipboard(text); - copyToClipboard(s.vx, s.tty, s.gpa, text); + const sc = s.term() orelse return s.core.fallback.setClipboard(text); + if (!sc.on) return s.core.fallback.setClipboard(text); + copyToClipboard(&sc.vx, &sc.tty, s.gpa, text); } fn readClipboard(ctx: ?*anyopaque) void { const s = of(ctx); - if (!s.attached()) return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); - requestClipboard(s.vx, s.tty); + const sc = s.term() orelse return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); + if (!sc.on) return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); + requestClipboard(&sc.vx, &sc.tty); } fn openLink(_: ?*anyopaque, url: []const u8) void { @@ -2391,9 +2597,6 @@ test "TTY queued results reject old PTY generations and LSP request IDs" { .core = core, .prompt_rcs = undefined, .loop = undefined, - .vx = undefined, - .tty = undefined, - .kitty = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, @@ -2439,9 +2642,6 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .core = core, .prompt_rcs = undefined, .loop = undefined, - .vx = undefined, - .tty = undefined, - .kitty = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, @@ -2507,24 +2707,16 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { fn keepSignal(_: posix.SIG) callconv(.c) void {} -fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { +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); while (true) { var sig: c_int = 0; if (libc.sigwait(&set, &sig) != 0) continue; - // The terminal hung up (its stub passes the hangup on): as its - // input's end. - if (sig == @backingInt(posix.SIG.HUP)) { - loop.postEvent(.quit) catch {}; - continue; - } - // Detached, the descriptor the terminal had may be another's now. - if (!terminal_on.load(.acquire)) continue; - if (vx.state.in_band_resize) continue; // terminal reports via CSI 48 - const ws = tty.getWinsize() catch continue; - loop.postEvent(.{ .winsize = ws }) catch {}; + // 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 {}; } } @@ -2665,7 +2857,7 @@ test "a terminal pane gets function keys as their sequences and lone modifiers n vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -2698,7 +2890,7 @@ test "a terminal pane gets function keys as their sequences and lone modifiers n "\x1b[104;;104u", }, }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); core.panes[0].?.body.mode = .tty; diff --git a/src/views.zig b/src/views.zig new file mode 100644 index 00000000..ae2b338d --- /dev/null +++ b/src/views.zig @@ -0,0 +1,555 @@ +//! Views: several clients on one session, kakoune's way (docs/next-daemon.md, +//! "Phase 4 notes"). Each view has its own layout, focus, selections and +//! scroll over the panes every view shares: their text, terminals, tags, +//! registers and messages. +//! +//! The core still sees one view, the live one, in the fields it always had: +//! a view's share of the session (`fields`) and of each pane (`PaneView`) is +//! copied out when another view goes live (`park`) and back when it is live +//! again (`unpark`). No line of the core reads a view through an +//! indirection, and one view never parks: today's single client runs +//! exactly as before. +//! +//! A parked view is in no digest (Pardes.replica's `parked`, Pane's `pv`): +//! a slot is written whenever its view goes out, so the live view's slot is +//! stale by design, and a parked view that diverged shows when it is live +//! again. The pump draws the other views by going into each and back +//! (`drawOthers`), which leaves every digested field as it was; a replica +//! that draws nothing skips it. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const Text = pardes.panes.Text; +const File = pardes.panes.File; +const layout = pardes.layout; +const MAX_PANES = pardes.MAX_PANES; + +/// Views at once: a window or terminal each. +pub const max = 4; + +/// The session's fields that are each view's own: its screen and layout, +/// its focus, its pointer and gestures, its jumps, its key prefixes, and +/// what drawing it keeps (animations, the presentation of its panels). +pub const fields = [_][:0]const u8{ + "screen_w", "screen_h", "cell_pixels", "row_metrics", + "ncol", "col_weight", "col_panes", "col_n", + "col_tags", "col_serial", "finder", "rects", + "col_x", "col_w", "presentation", "active", + "jumps", "njumps", "jcur", "jump_force", + "active_column", "lifts", "lift_serials", "cursor_glide", + "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous", + "input_ns", "native_images", "native_image_patches", "windowed", + "drag", "mouse_grab", "window_focused", "focus_told", + "hover_col", "hover_row", "pointer_raw_col", "pointer_raw_row", + "pointer_body_hit", "pointer_tag_hit", "raw_hover_intent", "pointer_inside", + "last_click", "click_as", "chord_hold", "wheel_guard", + "ctrl_w_pending", "register", "leader_on", "window_pick", + "leader_keys", "leader_n", "header_focus", "header_column", + "header_drag", "header_sweep", "header_scroll", "header_top", + "ov_pinch_scale", "ov_touch_scroll_delta", "chord_arg", "clip_pending", +}; + +/// A pane's fields that are each view's own: where it is in the view's +/// layout, which of its texts has the keyboard, how far it is scrolled, and +/// what the view's step measured of it (its tag's rows, its bands). +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices" }; + +/// The body's: its cursor, selections and mode. Its undo stays the pane's. +const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "msel", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; + +pub const View = Part(Pardes, &fields); + +/// Named field by field, not taken from Pane's type: a pane holds its own. +pub const PaneView = struct { + focus: Pane.Focus = .body, + vweight: f32 = 1, + collapsed: bool = false, + attached_to: ?u32 = null, + hscroll: i32 = 0, + tag_scroll: u16 = 0, + tag_top: u16 = 0, + tag_rows: u16 = 1, + view: Pane.SessionView = .{}, + notices: Pane.Notices = .{}, + body: Part(Text, &body_fields) = .{}, + /// A document's scroll and size; a terminal's size is its pty's, which + /// every view shows (sized by its last typist, `Pardes.sync`). + scroll: usize = 0, + cols: u16 = 1, + rows: u16 = 1, +}; + +/// A view not live: its share of the session, and the serial of each pane +/// slot when it went out, by which the panes closed since leave its layout. +pub const Parked = struct { + view: View, + serials: [MAX_PANES]u32 = @splat(0), +}; + +/// The fields of `T` named, as a struct of their own, each of its type and +/// default. +fn Part(comptime T: type, comptime names: []const [:0]const u8) type { + @setEvalBranchQuota(100_000); + const s = @typeInfo(T).@"struct"; + var types: [names.len]type = undefined; + var attrs: [names.len]std.lang.Type.Struct.FieldAttributes = undefined; + for (names, 0..) |name, i| { + const at = for (s.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else @compileError("views: " ++ @typeName(T) ++ " has no field " ++ name); + types[i] = s.field_types[at]; + attrs[i] = .{ .default_value_ptr = s.field_attrs[at].default_value_ptr }; + } + return @Struct(.auto, null, names, &types, &attrs); +} + +fn copy(comptime names: []const [:0]const u8, dst: anytype, src: anytype) void { + inline for (names) |name| @field(dst, name) = @field(src, name); +} + +pub fn isOpen(p: *const Pardes, k: u8) bool { + return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; +} + +pub fn count(p: *const Pardes) usize { + return @popCount(p.views_open); +} + +/// More than one view: the one test the single client's path pays. +pub fn several(p: *const Pardes) bool { + return p.views_open & (p.views_open -% 1) != 0; +} + +/// Pane `pane`'s share of the live view. +fn extract(pane: *const Pane) PaneView { + var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; + copy(&pane_fields, &v, pane); + copy(&body_fields, &v.body, &pane.body); + if (pane.file) |f| v.scroll = f.scroll; + return v; +} + +/// Its share of view `v` put back. +fn apply(pane: *Pane, v: *const PaneView) void { + copy(&pane_fields, pane, v); + copy(&body_fields, &pane.body, &v.body); + if (!pane.isTerminal()) { + pane.cols = v.cols; + pane.rows = v.rows; + } + const f = if (pane.file) |*file| file else return; + // The text may have changed while the view was out: what it held is + // brought back inside it. + const lines = File.nlines(pane.gpa, f); + f.scroll = @min(v.scroll, lines -| 1); + if (pane.body.cur_row >= lines) { + pane.body.vsel.active = false; + pane.body.msel.active = false; + pane.body.nsel = 0; + } + _ = pardes.edit.clampFileCursor(&pane.body); +} + +/// A copy of the live view's share for another view, owning nothing of the +/// live one's (its extra selections stay behind). +fn fresh(pane: *const Pane) PaneView { + var v = extract(pane); + v.body.sels = &.{}; + v.body.nsel = 0; + return v; +} + +/// A new pane is in each other view as it was made, there to be shown. +pub fn installed(p: *Pardes, pane: *Pane) void { + pane.sized_by = p.view; + if (!several(p)) return; + for (0..max) |k| if (k != p.view and isOpen(p, @intCast(k))) { + pane.pv[k] = fresh(pane); + }; + pane.pv_known = p.views_open; +} + +/// A pane going: the other views' selections of it go too. +pub fn tornDown(p: *Pardes, pane: *Pane) void { + for (0..max) |k| if (k != p.view and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { + const sels = pane.pv[k].body.sels; + if (sels.len > 0) p.gpa.free(sels); + pane.pv[k].body.sels = &.{}; + }; +} + +fn park(p: *Pardes) void { + const slot = p.parked[p.view].?; + copy(&fields, &slot.view, p); + for (p.panes, 0..) |s, id| { + const pane = s orelse { + slot.serials[id] = 0; + continue; + }; + slot.serials[id] = pane.serial; + pane.pv[p.view] = extract(pane); + } +} + +fn unpark(p: *Pardes, k: u8) void { + const slot = p.parked[k].?; + copy(&fields, p, &slot.view); + p.view = k; + for (p.panes) |s| if (s) |pane| apply(pane, &pane.pv[k]); + // The panes closed while it was out (a slot empty or another pane's + // now) leave its layout, and its keyboard leaves them. + for (0..p.ncol) |c| { + var n: usize = 0; + for (p.col_panes[c][0..p.col_n[c]]) |id| { + const pane = p.panes[id] orelse continue; + if (pane.serial != slot.serials[id]) continue; + p.col_panes[c][n] = id; + n += 1; + } + p.col_n[c] = n; + } + if (layout.findPane(p, p.active) == null) { + for (0..p.ncol) |c| if (p.col_n[c] > 0) { + p.active = p.col_panes[c][0]; + break; + }; + } + layout.compute(p); +} + +/// The pane with the keyboard is one the live view shows: focused where its +/// layout has it not (a Look at a file another view opened, a jump, a 9P +/// focus), it goes into the view's active column, as kakoune shows a buffer +/// in the window that asked for it. +pub fn show(p: *Pardes) void { + if (p.panes[p.active] == null or layout.findPane(p, p.active) != null) return; + const c = layout.firstColumn(p) orelse layout.columnBySerial(p, p.active_column) orelse p.ncol - 1; + layout.acmePlace(p, c, null, p.active); +} + +/// Into view `k`: the live one parked, `k`'s put in its place. +pub fn to(p: *Pardes, k: u8) void { + std.debug.assert(isOpen(p, k)); + if (k == p.view) return; + park(p); + unpark(p, k); +} + +/// A new view, as the live one is laid out and focused, with its panes +/// where they are scrolled and its cursors where they are; the rest of it +/// (column tags, gestures, animations) as a session starts. Null when there +/// are `max` already. +pub fn open(p: *Pardes) ?u8 { + const k: u8 = for (0..max) |i| { + if (!isOpen(p, @intCast(i))) break @intCast(i); + } else return null; + // Each live view parks too: its slot is made with the first other. + for ([_]u8{ p.view, k }) |i| if (p.parked[i] == null) { + const slot = p.gpa.create(Parked) catch return null; + slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; + p.parked[i] = slot; + }; + 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| + @field(slot.view, name) = @field(p, name); + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); + for (p.panes, 0..) |s, id| { + const pane = s orelse continue; + slot.serials[id] = pane.serial; + pane.pv[k] = fresh(pane); + pane.pv_known |= @as(u8, 1) << @intCast(k); + } + p.views_open |= @as(u8, 1) << @intCast(k); + return 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 { + if (!isOpen(p, k) or count(p) == 1) return; + if (k == p.view) { + const other: u8 = for (0..max) |i| { + if (i != k and isOpen(p, @intCast(i))) break @intCast(i); + } else unreachable; + park(p); + unpark(p, other); + } + const v = &p.parked[k].?.view; + for (&v.col_tags) |*t| t.deinit(p.gpa); + if (v.chord_arg) |a| p.gpa.free(a); + v.presentation.deinit(p.gpa); + for (p.panes) |s| if (s) |pane| { + const sels = pane.pv[k].body.sels; + if (sels.len > 0) p.gpa.free(sels); + pane.pv[k].body.sels = &.{}; + pane.pv_known &= ~(@as(u8, 1) << @intCast(k)); + }; + p.views_open &= ~(@as(u8, 1) << @intCast(k)); +} + +/// The session's end: what each view not live holds. +pub fn deinit(p: *Pardes) void { + for (0..max) |i| { + const k: u8 = @intCast(i); + const slot = p.parked[k] orelse continue; + if (k != p.view and isOpen(p, k)) { + const v = &slot.view; + for (&v.col_tags) |*t| t.deinit(p.gpa); + if (v.chord_arg) |a| p.gpa.free(a); + v.presentation.deinit(p.gpa); + } + p.gpa.destroy(slot); + } +} + +/// What drawing a view keeps of its own (its animations, its panels as +/// presented), kept from a frame drawn while it was out. +const drawn_fields = [_][:0]const u8{ "presentation", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous" }; + +/// Each view but the live one drawn and presented, the live one put back +/// after exactly as it was: no digested field moves, so a replica that +/// draws nothing (and skips this) stays the same. A view drawn keeps only +/// what its drawing made (`drawn_fields`); whatever its unpark brought +/// inside a changed text is not kept, and is done again when it is next +/// live. Before the live one's frame: the surface after the pump is its. +pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { + const live = p.view; + const v = h.vtable; + park(p); + defer { + copy(&fields, p, &p.parked[live].?.view); + p.view = live; + for (p.panes) |s| if (s) |pane| { + const pv = &pane.pv[live]; + copy(&pane_fields, pane, pv); + copy(&body_fields, &pane.body, &pv.body); + if (!pane.isTerminal()) { + pane.cols = pv.cols; + pane.rows = pv.rows; + } + if (pane.file) |*f| f.scroll = pv.scroll; + }; + } + for (0..max) |i| { + const k: u8 = @intCast(i); + if (k == live or !isOpen(p, k)) continue; + unpark(p, k); + _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); + const surface = try p.render(p.frame_arena.allocator()); + if (v.present) |f| f(h.ctx, surface); + if (v.post_present) |f| f(h.ctx); + copy(&drawn_fields, &p.parked[k].?.view, p); + } +} + +// ---- tests ---- + +const testing = std.testing; +const Key = pardes.Key; + +fn typeKeys(p: *Pardes, text: []const u8) void { + for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); +} + +fn escape(p: *Pardes) void { + p.update(.{ .key = .{ .cp = Key.escape } }); +} + +fn numbered(gpa: std.mem.Allocator, n: usize) ![]u8 { + var out: std.ArrayList(u8) = .empty; + for (1..n + 1) |i| try out.print(gpa, "line {d}\n", .{i}); + return out.toOwnedSlice(gpa); +} + +/// The cursor put on `row`, as a jump puts it, and the step after. +fn cursorTo(p: *Pardes, pane: *Pane, row: i32) void { + pane.body.vsel.active = false; + pane.body.nsel = 0; + pane.body.cur_row = row; + pane.body.cur_col = 0; + pane.body.cur_pinned = true; + pane.ensureCursorVisible(); + p.sync(); +} + +test "each view has its own cursor and scroll, and an edit in one is in the other" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 200); + defer gpa.free(text); + const pane = try p.setTestFile(text); + try testing.expectEqual(@as(?u8, 1), p.openView()); + cursorTo(p, pane, 119); + const scrolled = pane.file.?.scroll; + try testing.expect(scrolled > 0); + // The other view is where the session was when it came. + p.toView(1); + try testing.expectEqual(@as(i32, 0), pane.body.cur_row); + try testing.expectEqual(@as(usize, 0), pane.file.?.scroll); + typeKeys(p, "ix"); + escape(p); + try testing.expectEqual(Text.Mode.normal, pane.body.mode); + p.toView(0); + try testing.expectEqual(@as(i32, 119), pane.body.cur_row); + try testing.expectEqual(scrolled, pane.file.?.scroll); + try testing.expect(std.mem.startsWith(u8, pane.file.?.content, "xline 1\n")); + // Its mode is its own: insert here leaves the other in normal. + typeKeys(p, "i"); + try testing.expectEqual(Text.Mode.insert, pane.body.mode); + p.toView(1); + try testing.expectEqual(Text.Mode.normal, pane.body.mode); +} + +test "each view has its own layout and focus over the same panes" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + const k = p.openView().?; + const ncol = p.ncol; + p.toView(k); + p.update(.{ .command = "Newcol" }); + p.update(.{ .command = "New" }); + const made = p.active; + try testing.expect(made != 0); + try testing.expectEqual(ncol + 1, p.ncol); + p.toView(0); + try testing.expectEqual(ncol, p.ncol); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expect(layout.findPane(p, made) == null); + try testing.expectEqual(pardes.Rect{}, p.rects[made]); + // The pane is the session's: focused here, this view shows it too. + p.active = made; + p.sync(); + try testing.expect(layout.findPane(p, made) != null); + try testing.expect(p.rects[made].h > 0); + // Closed here, it leaves the other view's layout, and its keyboard. + try p.removePane(made, null); + p.sync(); + p.toView(k); + try testing.expect(layout.findPane(p, made) == null); + try testing.expect(p.panes[p.active] != null); + try testing.expect(layout.findPane(p, p.active) != null); +} + +test "registers are the session's: yanked in one view, put in another" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha\nbeta\n"); + const k = p.openView().?; + typeKeys(p, "xy"); + p.toView(k); + cursorTo(p, pane, 1); + typeKeys(p, "p"); + try testing.expectEqualStrings("alpha\nbeta\nalpha\n", pane.file.?.content); +} + +test "a terminal shown at two sizes takes the size of the view that typed into it last" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const term = p.panes[p.active].?; + try testing.expect(term.isTerminal()); + const small = .{ term.cols, term.rows }; + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); + // Shown bigger here, it keeps the size of the view that typed last. + try testing.expect(p.rects[p.active].w > 80); + try testing.expectEqual(small, .{ term.cols, term.rows }); + p.update(.{ .key = .{ .cp = Key.right } }); + try testing.expect(term.cols > small[0] and term.rows > small[1]); + const big = .{ term.cols, term.rows }; + p.toView(0); + p.sync(); + try testing.expectEqual(big, .{ term.cols, term.rows }); + p.update(.{ .key = .{ .cp = Key.right } }); + try testing.expectEqual(small, .{ term.cols, term.rows }); + // That view gone, the one left sizes it. + p.toView(k); + p.closeView(0); + try testing.expectEqual(k, p.view); + try testing.expectEqual(big, .{ term.cols, term.rows }); +} + +test "a view closing leaves the others as they were" { + 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 a = p.openView().?; + const b = p.openView().?; + try testing.expectEqual(@as(usize, 3), count(p)); + p.toView(b); + p.update(.{ .command = "Newcol" }); + cursorTo(p, pane, 40); + typeKeys(p, "xy"); + p.toView(a); + cursorTo(p, pane, 70); + p.toView(0); + cursorTo(p, pane, 10); + const ncol = p.ncol; + // The live one closing: the session goes into another. + p.toView(b); + p.closeView(b); + try testing.expect(p.view != b); + try testing.expectEqual(@as(usize, 2), count(p)); + p.toView(a); + try testing.expectEqual(@as(i32, 70), pane.body.cur_row); + p.closeView(0); + try testing.expectEqual(a, p.view); + try testing.expectEqual(ncol, p.ncol); + // The last stays. + p.closeView(a); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expectEqual(@as(i32, 70), 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 }); + 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); + cursorTo(p, pane, 60); + typeKeys(p, "dd"); + p.toView(0); + cursorTo(p, pane, 95); + const before = pardes.walk.digest(Pardes, p); + try drawOthers(p, .{}); + try testing.expectEqual(@as(u8, 0), p.view); + try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); + p.toView(k); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); +} + +test "a view not live keeps its place in a text another view edits" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 200); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + cursorTo(p, pane, 100); + const scrolled = pane.file.?.scroll; + p.toView(0); + // Three lines put above it, one taken below it. + typeKeys(p, "xyppp"); + cursorTo(p, pane, 150); + typeKeys(p, "xd"); + p.toView(k); + try testing.expectEqual(@as(i32, 103), pane.body.cur_row); + try testing.expectEqual(scrolled + 3, pane.file.?.scroll); + try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101")); +} diff --git a/test/daemon.py b/test/daemon.py index 9e9ea600..04455f0f 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -208,6 +208,38 @@ def main(binary, cycles): assert now == baseline, (cycle, now, baseline) print(f'daemon: {cycles} detach/attach cycles, fds/sockets/processes level: {baseline}') + # Another terminal while one is attached: a view of its own + # (src/views.zig), drawn beside the first. Detached from inside, its + # stub ends and the first draws on; closed outright, the same. + for how in ('detach', 'close'): + term2 = Term(20, 60) + stub2 = term2.spawn([binary, '--attach=work'], env) + started.append(stub2.pid) + wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) + wait_for('the second terminal\'s view live', lambda: client.screen()['cols'] == 60) + seen = len(term.text()) + run(client, shell, f'echo both-drawn-{how}\n'.encode()) + wait_for('the first terminal drawn', lambda: f'both-drawn-{how}'.encode() in term.text()[seen:]) + wait_for('the second terminal drawn', lambda: f'both-drawn-{how}'.encode() in term2.text()) + assert stub.poll() is None and stub2.poll() is None + if how == 'detach': + # Its view is the live one (its terminal spoke last). + client.write(f'/pane/{shell}/exec', b'Detach\n') + assert stub2.wait(timeout=10) == 0, stub2.returncode + assert b'detached from \'work\'' in term2.text(), term2.text()[-400:] + term2.close() + else: + term2.close() + assert stub2.wait(timeout=10) != 0 or True + wait_for('the first terminal\'s view live again', lambda: client.screen()['cols'] == 100) + seen = len(term.text()) + run(client, shell, f'echo first-on-{how}\n'.encode()) + wait_for('the first terminal drawn on', lambda: f'first-on-{how}'.encode() in term.text()[seen:]) + assert stub.poll() is None, 'the first terminal stays attached' + now = (fds(session), fds(daemon), len(list(root.iterdir())), len([p for p in processes(root) if alive(p)])) + assert now[:3] == baseline[:3], (how, now, baseline) + print('daemon: a second terminal on an attached session drawn beside the first, then detached and closed; the first drew on') + # Killed outright: its shells live on, their masters the daemon's. os.kill(session, signal.SIGKILL) stub.wait(timeout=10) diff --git a/test/e2e_harness.zig b/test/e2e_harness.zig index 7e29476b..6011ba16 100644 --- a/test/e2e_harness.zig +++ b/test/e2e_harness.zig @@ -12,6 +12,14 @@ extern "c" fn forkpty( termp: ?*const anyopaque, winp: ?*const posix.winsize, ) c_int; +extern "c" fn openpty( + amaster: *c_int, + aslave: *c_int, + name: ?[*]u8, + termp: ?*const anyopaque, + winp: ?*const posix.winsize, +) c_int; +extern "c" fn ttyname(fd: c_int) ?[*:0]const u8; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn _exit(status: c_int) noreturn; extern "c" fn unsetenv(name: [*:0]const u8) c_int; @@ -72,6 +80,12 @@ pub const Harness = struct { /// clipboard writes). Capture is bounded explicitly so a runaway child cannot /// consume unbounded test-runner memory. raw: *RawCapture, + /// Another view's terminal of the app this harness started (`view`): + /// it neither started the app nor kills it. + view: bool = false, + /// Every terminal of the app's, this one's too: each wait reads them + /// all, or the app would block writing a frame to one nobody reads. + views: []const *Harness = &.{}, pub fn init(gpa: std.mem.Allocator, exe: [*:0]const u8, rows: u16, cols: u16) !Harness { return initArgs(gpa, exe, rows, cols, null); @@ -129,8 +143,59 @@ pub const Harness = struct { }; } + /// A terminal for another view of the app `pid` (views.zig): a pty of + /// its own, whose path the app is given (PARDES_TEST_VIEWS) and opens. + pub fn initView(gpa: std.mem.Allocator, rows: u16, cols: u16, path: *[64]u8) !Harness { + var master: c_int = undefined; + var slave: c_int = undefined; + const ws = posix.winsize{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + if (openpty(&master, &slave, null, null, &ws) != 0) return error.NoPty; + defer _ = libc.close(slave); + const name = std.mem.span(ttyname(slave) orelse return error.NoPty); + if (name.len >= path.len) return error.NoPty; + @memcpy(path[0..name.len], name); + path[name.len] = 0; + const term = try gpa.create(ghostty_vt.Terminal); + errdefer gpa.destroy(term); + term.* = try ghostty_vt.Terminal.init(std.Io.Threaded.global_single_threaded.io(), gpa, .{ + .cols = cols, + .rows = rows, + .max_scrollback_bytes = 1024 * 1024, + }); + errdefer term.deinit(gpa); + const raw = try gpa.create(RawCapture); + raw.* = .{}; + return .{ .gpa = gpa, .master = master, .pid = 0, .term = term, .stream = term.vtStream(), .rows = rows, .cols = cols, .raw = raw, .view = true }; + } + + /// What the app wrote to its other terminals, read without waiting. + fn drainOthers(self: *Harness) void { + for (self.views) |other| if (other != self) while (other.pumpRead(0)) {}; + } + + fn pumpRead(self: *Harness, ms: i64) bool { + var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; + const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; + if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; + var buf: [4096]u8 = undefined; + const n = posix.read(self.master, &buf) catch return false; + if (n == 0) return false; + self.recordRaw(buf[0..n]); + self.feed(buf[0..n]); + return true; + } + + /// Bytes in every terminal of the app's (`pending`). + pub fn pendingAll(self: *Harness) usize { + var n = self.pending(); + for (self.views) |other| if (other != self) { + n += other.pending(); + }; + return n; + } + pub fn deinit(self: *Harness) void { - posix.kill(self.pid, posix.SIG.KILL) catch {}; + if (!self.view) posix.kill(self.pid, posix.SIG.KILL) catch {}; _ = libc.close(self.master); self.stream.deinit(); self.gpa.destroy(self.raw); @@ -175,8 +240,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; @@ -193,15 +259,14 @@ pub const Harness = struct { /// SETTLED grid sleeps in poll until the app actually writes, instead of /// burning a fixed window per check (see snapshot.zig's waitStable). pub fn pumpOnce(self: *Harness, ms: i64) !bool { - var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - const ready = posix.poll(&fds, @intCast(@max(0, ms))) catch 0; - if (ready == 0 or (fds[0].revents & posix.POLL.IN) == 0) return false; - var buf: [4096]u8 = undefined; - const n = posix.read(self.master, &buf) catch return false; - if (n == 0) return false; - self.recordRaw(buf[0..n]); - self.feed(buf[0..n]); - return true; + if (self.views.len <= 1) return self.pumpRead(ms); + // Several terminals: each looked at every few milliseconds. + const deadline = nowMs() + ms; + while (true) { + self.drainOthers(); + if (self.pumpRead(@min(5, @max(0, deadline - nowMs())))) return true; + if (nowMs() >= deadline) return false; + } } /// Bytes sitting in the pty that nobody has picked up — keystrokes the app @@ -261,8 +326,9 @@ pub const Harness = struct { const deadline = nowMs() + ms; var buf: [4096]u8 = undefined; while (nowMs() < deadline) { + self.drainOthers(); var fds = [_]posix.pollfd{.{ .fd = self.master, .events = posix.POLL.IN, .revents = 0 }}; - _ = posix.poll(&fds, 50) catch {}; + _ = posix.poll(&fds, if (self.views.len > 1) 5 else 50) catch {}; if ((fds[0].revents & posix.POLL.IN) != 0) { const n = posix.read(self.master, &buf) catch break; if (n == 0) break; diff --git a/test/monkey.zig b/test/monkey.zig index 7c9761ff..d35599ff 100644 --- a/test/monkey.zig +++ b/test/monkey.zig @@ -1,6 +1,6 @@ // pardes-monkey: random snapshot scripts against the tty, hunting panics. // -// zig build monkey -Dplatform=tty -- [steps] [--from=N] [--out=DIR] [--keep] +// zig build monkey -Dplatform=tty -- [steps] [--from=N] [--out=DIR] [--keep] [--views=N] // // Each seed makes one script (a fixed seed makes the same script, so a hit // replays): keys and leader bursts, typing, mouse presses, drags and wheel, @@ -9,7 +9,9 @@ // /tmp/pardes-monkey), and a panic is a record in the app's crash file // (src/crash.zig, in the script's fake home: found even when the crash cut // the script short) or a `.zig:N:N: 0x` stack frame in the report or the -// grid it captured (a trace renders INTO the screen). A hit +// grid it captured (a trace renders INTO the screen). --views=N gives the +// session N terminals, each a view of it (views.zig), and types into and +// sizes each in turn, a `view` switch among the steps. A hit // keeps DIR/seed-N.snap, its captures and its report; the rest are deleted. // A panic's trace can scroll off a small grid: replay the kept script, or // run the app in a larger pty. Every hit gets a fix and a pinned regression @@ -26,6 +28,9 @@ const Script = struct { gpa: std.mem.Allocator, rows: u16 = 24, cols: u16 = 80, + /// Each terminal's size, and the one typed into (`--views`). + sizes: [4][2]u16 = @splat(.{ 24, 80 }), + view: usize = 0, fn line(s: *Script, comptime fmt: []const u8, args: anytype) !void { try s.out.print(s.gpa, fmt ++ "\n", args); @@ -38,18 +43,24 @@ const commands = [_][]const u8{ "Newcol", "Find", "Grep", "Joincol", "Help", "Du const leader_keys = "abcdefghijklmnopqrstuvwxyz0123456789$^"; const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; -fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize) ![]u8 { +fn generate(gpa: std.mem.Allocator, seed: u64, steps: usize, views: usize) ![]u8 { var prng = std.Random.DefaultPrng.init(seed); const random = prng.random(); var s: Script = .{ .gpa = gpa }; - try s.line("# pardes-monkey seed {d}, {d} steps", .{ seed, steps }); + try s.line("# pardes-monkey seed {d}, {d} steps, {d} views", .{ seed, steps, views }); try s.line("file bad.txt hello \\xff\\xfe world\\nlatin-1 caf\\xe9 tail\\ntruncated \\xe6\\x97 here\\nend\\n", .{}); try s.line("file wide.txt {s}tail\\nsecond line\\n", .{std.mem.asBytes(&@as([8][37]u8, @splat("\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaa\\x9e ".*)))}); try s.line("lines long.txt 400 some words after the number, long enough to wrap in a narrow pane", .{}); - try s.line("start 24 80 bad.txt", .{}); + if (views > 1) try s.line("start 24 80 views={d} bad.txt", .{views}) else try s.line("start 24 80 bad.txt", .{}); try s.line("wait 8000 Newcol Joincol", .{}); try s.line("stable 300 5000", .{}); for (0..steps) |step| { + if (views > 1 and random.uintLessThan(u8, 4) == 0) { + s.sizes[s.view] = .{ s.rows, s.cols }; + s.view = random.uintLessThan(usize, views); + s.rows, s.cols = s.sizes[s.view]; + try s.line("view {d}", .{s.view + 1}); + } const wide = s.cols >= 70 and s.rows >= 6; switch (random.uintLessThan(u8, 12)) { // A workspace word, run with a middle click: new columns make @@ -150,9 +161,9 @@ test "a panic trace is found, and ordinary colons are not" { } test "a seed makes the same script every time" { - const a = try generate(std.testing.allocator, 42, 50); + const a = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(a); - const b = try generate(std.testing.allocator, 42, 50); + const b = try generate(std.testing.allocator, 42, 50, 3); defer std.testing.allocator.free(b); try std.testing.expectEqualStrings(a, b); } @@ -166,7 +177,7 @@ pub fn main(init: std.process.Init) !void { const io = init.io; const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len < 4) { - std.debug.print("usage: pardes-monkey [steps] [--from=N] [--out=DIR] [--keep] [--tape=DIR]\n", .{}); + std.debug.print("usage: pardes-monkey [steps] [--from=N] [--out=DIR] [--keep] [--tape=DIR] [--views=N]\n", .{}); std.process.exit(2); } const snap = args[1]; @@ -180,9 +191,12 @@ pub fn main(init: std.process.Init) !void { // --tape=DIR: each seed's sessions record their tapes there and the // seed is a hit if one does not replay (pardes-snap --tape). var tape: ?[]const u8 = null; + var views: usize = 1; var positional: usize = 0; for (args[3..]) |a| { - if (std.mem.startsWith(u8, a, "--from=")) { + if (std.mem.startsWith(u8, a, "--views=")) { + views = std.math.clamp(try std.fmt.parseInt(usize, a["--views=".len..], 10), 1, 4); + } else if (std.mem.startsWith(u8, a, "--from=")) { from = try std.fmt.parseInt(u64, a["--from=".len..], 10); } else if (std.mem.eql(u8, a, "--keep")) { keep = true; @@ -203,7 +217,7 @@ pub fn main(init: std.process.Init) !void { var hits: usize = 0; var seed = from; while (seed < from + seeds) : (seed += 1) { - const script = try generate(gpa, seed, steps); + const script = try generate(gpa, seed, steps, views); defer gpa.free(script); var path_buf: [4096]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/seed-{d}.snap", .{ out, seed }); diff --git a/test/snapshot.zig b/test/snapshot.zig index 255f55bf..8a0583e3 100644 --- a/test/snapshot.zig +++ b/test/snapshot.zig @@ -29,7 +29,11 @@ // lines [tail] create file with n numbered lines, tail on each // dirmk create a subdirectory // run run a command in the script's cwd (NOT in a pane) -// start [arg] fork the app in a pty (optional extra CLI arg) +// start [views=N] [arg] fork the app in a pty (optional +// extra CLI arg); views=N gives it N-1 more +// terminals, each a view of the session +// view type into and capture terminal k (1 is the +// app's own; a resize there SIGWINCHes the app) // wait pump until needle appears on the grid (fails hard) // settle pump for a fixed duration // stable pump until the grid stops changing @@ -74,6 +78,7 @@ const Client = @import("9p_io").Client; pub const std_options: std.Options = .{ .log_level = .err }; extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; +extern "c" fn unsetenv(name: [*:0]const u8) c_int; extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn system(cmd: [*:0]const u8) c_int; extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; @@ -652,6 +657,14 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const var chunk_len: usize = 0; var h: ?eh.Harness = null; defer if (h) |*hp| hp.deinit(); + // `views=N`: the app's other terminals, and the one driven now. + var others: [3]eh.Harness = undefined; + var nothers: usize = 0; + var group: [4]*eh.Harness = undefined; + var cur: usize = 0; + defer for (others[0..nothers]) |*o| o.deinit(); + // A capture is a delta against the previous one of its terminal's. + var prevs: [4]Prev = @splat(.{}); // captures are deltas against the previous one of the same kind (see Prev) var prev: Prev = .{}; @@ -717,15 +730,46 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); var extra: [6]?[*:0]const u8 = @splat(null); var nextra: usize = 0; - while (tok.next()) |a| : (nextra += 1) { + var nviews: usize = 1; + while (tok.next()) |a| { + if (std.mem.startsWith(u8, a, "views=")) { + nviews = try std.fmt.parseInt(usize, a["views=".len..], 10); + if (nviews < 1 or nviews > 4) return error.BadScript; + continue; + } if (nextra >= extra.len) return error.BadScript; extra[nextra] = (try arena.dupeSentinel(u8, a, 0)).ptr; + nextra += 1; } if (h) |*hp| hp.deinit(); // scripts may restart (e.g. dump then -l) + for (others[0..nothers]) |*o| o.deinit(); + nothers = 0; + cur = 0; // a new process is a new screen: capture it whole, or its first // frame would be a delta against a dead instance's rows prev = .{}; + prevs = @splat(.{}); + var paths: std.ArrayList(u8) = .empty; + while (nothers + 1 < nviews) : (nothers += 1) { + var path: [64]u8 = undefined; + others[nothers] = try eh.Harness.initView(gpa, rows, cols, &path); + try paths.print(arena, "{s}{s}", .{ if (paths.items.len > 0) " " else "", std.mem.sliceTo(&path, 0) }); + } + if (nothers > 0) _ = setenv("PARDES_TEST_VIEWS", try arena.dupeSentinel(u8, paths.items, 0), 1); h = try eh.Harness.initArgv(gpa, exe_z.ptr, rows, cols, extra[0..nextra]); + _ = unsetenv("PARDES_TEST_VIEWS"); + group[0] = &h.?; + for (others[0..nothers], 1..) |*o, i| { + o.pid = h.?.pid; + group[i] = o; + } + for (group[0 .. nothers + 1]) |v| v.views = group[0 .. nothers + 1]; + } else if (std.mem.eql(u8, cmd, "view")) { + const k = try std.fmt.parseInt(usize, tok.next() orelse return error.BadScript, 10); + if (k < 1 or k > nothers + 1) return error.BadScript; + prevs[cur] = prev; + cur = k - 1; + prev = prevs[cur]; } else if (std.mem.eql(u8, cmd, "snapfile")) { const label = tok.next() orelse return error.BadScript; const contents = try readFileAlloc(arena, tok.rest()); @@ -735,49 +779,52 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const try appendChunk(chunks, &chunk_len, try snapFile(arena, label, contents)); } else if (std.mem.eql(u8, cmd, "wait")) { const ms = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try live(&h).expectWaitFor(tok.rest(), ms, "snapshot wait"); + try on(&h, &group, nothers, cur).expectWaitFor(tok.rest(), ms, "snapshot wait"); } else if (std.mem.eql(u8, cmd, "settle")) { - try live(&h).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); + try on(&h, &group, nothers, cur).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10)); } else if (std.mem.eql(u8, cmd, "stable")) { const quiet = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); const timeout = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10); - try waitStable(live(&h), quiet, timeout); + try waitStable(on(&h, &group, nothers, cur), quiet, timeout); } else if (std.mem.eql(u8, cmd, "text")) { - try live(&h).send(tok.rest()); + try on(&h, &group, nothers, cur).send(tok.rest()); } else if (std.mem.eql(u8, cmd, "send")) { - try live(&h).send(try unescape(arena, tok.rest())); + try on(&h, &group, nothers, cur).send(try unescape(arena, tok.rest())); } else if (std.mem.eql(u8, cmd, "key")) { - while (tok.next()) |name| try live(&h).send(keyBytes(name) orelse return error.BadKey); + while (tok.next()) |name| try on(&h, &group, nothers, cur).send(keyBytes(name) orelse return error.BadKey); } else if (std.mem.eql(u8, cmd, "press") or std.mem.eql(u8, cmd, "release") or std.mem.eql(u8, cmd, "drag")) { const btn = buttonCode(tok.next() orelse return error.BadScript) orelse return error.BadScript; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); const b: u16 = if (std.mem.eql(u8, cmd, "drag")) btn + 32 else btn; const fin: u8 = if (std.mem.eql(u8, cmd, "release")) 'm' else 'M'; - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin })); } else if (std.mem.eql(u8, cmd, "motion")) { const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row })); } else if (std.mem.eql(u8, cmd, "wheel")) { const dir = tok.next() orelse return error.BadScript; const b: u16 = if (std.mem.eql(u8, dir, "up")) 64 else 65; const col_spec = tok.next() orelse return error.BadScript; const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - const col = try resolveCol(live(&h), col_spec, row); - try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); + const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row); + try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row })); } else if (std.mem.eql(u8, cmd, "resize")) { const rows = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10); - try live(&h).resize(rows, cols); + try on(&h, &group, nothers, cur).resize(rows, cols); + // Another view's terminal is no one's controlling one: the app + // is told it changed, as a stub tells it of its own. + if (cur > 0) std.posix.kill(live(&h).pid, std.posix.SIG.WINCH) catch {}; } else if (std.mem.eql(u8, cmd, "snap")) { - try appendChunk(chunks, &chunk_len, try snapText(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapText(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snapstyle")) { - try appendChunk(chunks, &chunk_len, try snapStyle(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snapStyle(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else if (std.mem.eql(u8, cmd, "snap9p")) { - try appendChunk(chunks, &chunk_len, try snap9p(arena, live(&h), tok.rest(), &prev)); + try appendChunk(chunks, &chunk_len, try snap9p(arena, on(&h, &group, nothers, cur), tok.rest(), &prev)); } else { std.debug.print("unknown command {s}\n", .{cmd}); return error.BadScript; @@ -891,6 +938,13 @@ fn snapFile(arena: std.mem.Allocator, label: []const u8, contents: []const u8) ! return out; } +/// The terminal the script drives now (`view`). +fn on(h: *?eh.Harness, group: *[4]*eh.Harness, nothers: usize, cur: usize) *eh.Harness { + const app = live(h); + if (cur == 0 or cur > nothers) return app; + return group[cur]; +} + fn live(h: *?eh.Harness) *eh.Harness { if (h.*) |*v| return v; fatal("script used the app before `start`", .{}); @@ -928,7 +982,7 @@ fn waitStable(h: *eh.Harness, quiet: i64, timeout: i64) !void { // and nothing in its process tree is runnable. Held across two probes, // because a pane's shell can write and sleep a scheduler tick before // the app wakes to read it. - if (since >= probe_after_ms and h.pending() == 0 and treeAsleep(h.pid)) { + if (since >= probe_after_ms and h.pendingAll() == 0 and treeAsleep(h.pid)) { if (asleep_since == 0) { asleep_since = now; } else if (now - asleep_since >= probe_hold_ms) { diff --git a/test/snapshots/views.golden b/test/snapshots/views.golden new file mode 100644 index 00000000..40f6a294 --- /dev/null +++ b/test/snapshots/views.golden @@ -0,0 +1,123 @@ +== snap one grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap two grid=80x24 cursor=7,3 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +| /tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +| 1 line 1 +| 2 line 2 +| 3 line 3 +| 4 line 4 +| 5 line 5 +| 6 line 6 +| 7 line 7 +| 8 line 8 +| 9 line 9 +| 10 line 10 +| 11 line 11 +| 12 line 12 +| 13 line 13 +| 14 line 14 +| 15 line 15 +| 16 line 16 +| 17 line 17 +| 18 line 18 +| 19 line 19 +| 20 line 20 +| 21 line 21 +== snap one-edit grid=80x24 cursor=15,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-sees grid=80x24 cursor=7,3 +|2: */tmp/pardes-snap/views/cwd/v.txt Save Tty Collapse Del +|3: 1 EDIT-ONEline 1 +== snap two-down grid=80x24 cursor=7,6 +|3: 22 line 22 +|4: 23 line 23 +|5: 24 line 24 +|6: 25 line 25 +|7: 26 line 26 +|8: 27 line 27 +|9: 28 line 28 +|10: 29 line 29 +|11: 30 line 30 +|12: 31 line 31 +|13: 32 line 32 +|14: 33 line 33 +|15: 34 line 34 +|16: 35 line 35 +|17: 36 line 36 +|18: 37 line 37 +|19: 38 line 38 +|20: 39 line 39 +|21: 40 line 40 +|22: 41 line 41 +|23: 42 line 42 +== snap one-top grid=80x24 cursor=15,3 +== snap two-put grid=80x24 cursor=21,7 +|7: 26 EDIT-ONEline 1 +|8: 27 line 26 +|9: 28 line 27 +|10: 29 line 28 +|11: 30 line 29 +|12: 31 line 30 +|13: 32 line 31 +|14: 33 line 32 +|15: 34 line 33 +|16: 35 line 34 +|17: 36 line 35 +|18: 37 line 36 +|19: 38 line 37 +|20: 39 line 38 +|21: 40 line 39 +|22: 41 line 40 +|23: 42 line 41 +== snap two-col grid=80x24 cursor=42,1 +|1: New Tty Find Grep Joincol Delcol New Tty Find Grep Joincol Delcol +|2: */tmp/pardes-snap/views/cwd/v.txt Save +|3: Tty Collapse Del +|4: 22 line 22 +|5: 23 line 23 +|6: 24 line 24 +|7: 25 line 25 +|8: 26 EDIT-ONEline 1 +|9: 27 line 26 +|10: 28 line 27 +|11: 29 line 28 +|12: 30 line 29 +|13: 31 line 30 +|14: 32 line 31 +|15: 33 line 32 +|16: 34 line 33 +|17: 35 line 34 +|18: 36 line 35 +|19: 37 line 36 +|20: 38 line 37 +|21: 39 line 38 +|22: 40 line 39 +|23: 41 line 40 +== snap one-col grid=80x24 cursor=21,3 +== snap one-after grid=80x24 cursor=13,4 diff --git a/test/snapshots/views.snap b/test/snapshots/views.snap new file mode 100644 index 00000000..eba26bae --- /dev/null +++ b/test/snapshots/views.snap @@ -0,0 +1,57 @@ +# Two terminals on one session, each a view of it (src/views.zig): each its +# own layout, focus, cursor and scroll over the same panes. An edit typed in +# one is in the other; a line yanked in one is put in the other (registers +# are the session's); Newcol in one leaves the other's columns; Detach in +# one lets that terminal go and leaves the other drawing. +config Verbose off +lines v.txt 120 +start 24 80 views=2 v.txt +wait 8000 line 1 +stable 500 10000 +snap one +view 2 +wait 8000 line 1 +stable 500 10000 +snap two +# Typed in view 1: in view 2 too. +view 1 +key i +text EDIT-ONE +key esc +settle 100 +stable 400 8000 +snap one-edit +view 2 +stable 400 8000 +snap two-sees +# Scrolled in view 2: view 1 stays at the top. +key pgdn +stable 400 8000 +snap two-down +view 1 +stable 400 8000 +snap one-top +# Yanked in view 1, put in view 2. +key x y +stable 400 8000 +view 2 +key p +stable 400 8000 +snap two-put +# A column in view 2 only. +key space +text cn +stable 400 8000 +snap two-col +view 1 +stable 400 8000 +snap one-col +# Detached in view 2: its terminal let go, view 1 drawing on. +view 2 +key space +text sD +stable 400 8000 +view 1 +key j +stable 400 8000 +snap one-after -- cgit v1.3 From 3a5db21504a653cc5e97b98edad59a6262a3b8e9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 17:48:21 -0300 Subject: views: a view drawn while another is live takes no post_present (its acknowledgement moved the live session's pointer and hover, and a replica that draws nothing applied it to another view); daemon.py attaches a second terminal and a second window beside the first; views-term.snap pins a terminal shown at two sizes Found by `pardes-monkey --views=3 --tape`: 4 of 6 seeds diverged at acknowledge_panel_presentation inside the pump, the recording having moved the look-hover of the view it drew and the headless replica that of the live one. A view's panels are acknowledged when it is live and drawn. With it, 6 of 6 seeds replay in all three replicas. test/daemon.py: with the first terminal attached, a second `pardes --attach` gets a view of its own; both are drawn (a line run in the shell shows in each), then it is detached from inside (its stub ends 0, the first draws on) and, again, its terminal closed outright; fds of the session and the daemon level. --gui: a second `pardes-gui --attach` while the window is open opens another (its captures under view-1), and Detach closes only it. views-term.snap: a terminal typed into from the big view is sized for it and shown at that size in the small one, then the other way round. ponytail noted at Pardes.sizes: a view smaller than the terminal shows its top rows. Co-Authored-By: Claude Opus 5.5 --- src/pardes.zig | 3 +++ src/views.zig | 4 +++- test/daemon.py | 19 ++++++++++++++--- test/snapshots/views-term.golden | 46 ++++++++++++++++++++++++++++++++++++++++ test/snapshots/views-term.snap | 27 +++++++++++++++++++++++ 5 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 test/snapshots/views-term.golden create mode 100644 test/snapshots/views-term.snap (limited to 'src/pardes.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 88d75fd1..59233347 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7857,6 +7857,9 @@ pub const Pardes = struct { /// Whether the live view sizes terminal `pane`: a terminal shown at two /// sizes takes the size of the view that typed into it last, and any /// view's once that one is gone. The others show it at that size. + // ponytail: a view smaller than the terminal shows its top rows, the + // rest cut; panning to its cursor, as tmux does, when a small view must + // follow a busy shell it never types into. fn sizes(p: *Pardes, pane: *Pane) bool { if (pane.sized_by == p.view) return true; if (views.isOpen(p, pane.sized_by)) return false; diff --git a/src/views.zig b/src/views.zig index ae2b338d..e3ba5b4e 100644 --- a/src/views.zig +++ b/src/views.zig @@ -339,7 +339,9 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); const surface = try p.render(p.frame_arena.allocator()); if (v.present) |f| f(h.ctx, surface); - if (v.post_present) |f| f(h.ctx); + // No post_present: what a shell acknowledges there moves the + // session's pointer and hover, which are the live view's to move; + // a view's panels are acknowledged when it is live and drawn. copy(&drawn_fields, &p.parked[k].?.view, p); } } diff --git a/test/daemon.py b/test/daemon.py index 04455f0f..41cfbaf7 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -212,11 +212,13 @@ def main(binary, cycles): # (src/views.zig), drawn beside the first. Detached from inside, its # stub ends and the first draws on; closed outright, the same. for how in ('detach', 'close'): - term2 = Term(20, 60) + # Bigger than the first: the shell, sized for the first (its + # last typist), shows whole in it. + term2 = Term(40, 120) stub2 = term2.spawn([binary, '--attach=work'], env) started.append(stub2.pid) wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) - wait_for('the second terminal\'s view live', lambda: client.screen()['cols'] == 60) + wait_for('the second terminal\'s view live', lambda: client.screen()['cols'] == 120) seen = len(term.text()) run(client, shell, f'echo both-drawn-{how}\n'.encode()) wait_for('the first terminal drawn', lambda: f'both-drawn-{how}'.encode() in term.text()[seen:]) @@ -230,7 +232,7 @@ def main(binary, cycles): term2.close() else: term2.close() - assert stub2.wait(timeout=10) != 0 or True + stub2.wait(timeout=10) wait_for('the first terminal\'s view live again', lambda: client.screen()['cols'] == 100) seen = len(term.text()) run(client, shell, f'echo first-on-{how}\n'.encode()) @@ -359,6 +361,17 @@ def gui(binary, cycles): baseline = baseline or now assert now == baseline, (cycle, now, baseline) print(f'daemon: {cycles} window detach/attach cycles, fds/processes level: {baseline}') + # Attached already: another window, a view of its own (its test + # captures under view-1); Detach in it closes only it. + attach = subprocess.run([binary, '--attach=win'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert attach.returncode == 0, attach.stderr + wait_for('the second window drawn', lambda: (root / 'caps' / 'view-1' / 'latest.ppm').exists()) + client.write(f'/pane/{shell}/exec', b'Detach\n') + time.sleep(.5) + assert not sized(client), 'the first window stays' + assert proc.poll() is None + print('daemon: a second window on an attached window session, detached alone') # Another kind is refused in words, and so is a name nobody has. client.write(f'/pane/{shell}/exec', b'Detach\n') wait_for('the window closed', lambda: sized(client)) diff --git a/test/snapshots/views-term.golden b/test/snapshots/views-term.golden new file mode 100644 index 00000000..d3cff726 --- /dev/null +++ b/test/snapshots/views-term.golden @@ -0,0 +1,46 @@ +== snap small grid=50x14 cursor=4,4 +| Newcol Joincol Find Grep Help Changelog Tutor Du +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-term/cwd Tty+bash Save Mo +| de Filter Collapse Del +| $ +| +| +| +| +| +| /tmp/pardes-snap/views-term/cwd/+New Save Tty Co +| llapse Del +| 1 +== snap big grid=80x24 cursor=4,5 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-term/cwd Tty+bash Save Mode Filter Collapse Del +| $ echo from-the-big-one +| from-the-big-one +| $ +| +| +| +| +| +| +| +| +| +| +| +| /tmp/pardes-snap/views-term/cwd/+New Save Tty Collapse Del +| 1 +== snap small-sees grid=50x14 cursor=4,6 +|4: $ echo from-the-big-one +|5: from-the-big-one +|6: $ +== snap small-typed grid=50x14 cursor=4,8 +|6: $ echo from-the-small-one +|7: from-the-small-one +|8: $ +== snap big-sees grid=80x24 cursor=4,7 +|5: $ echo from-the-small-one +|6: from-the-small-one +|7: $ diff --git a/test/snapshots/views-term.snap b/test/snapshots/views-term.snap new file mode 100644 index 00000000..bbd1c368 --- /dev/null +++ b/test/snapshots/views-term.snap @@ -0,0 +1,27 @@ +# A terminal shown in two views at two sizes (src/views.zig): it takes the +# size of the view that typed into it last; the other shows it at that size. +config Verbose off +start 24 80 views=2 +wait 8000 $ +stable 700 20000 +view 2 +resize 14 50 +stable 500 10000 +snap small +# Typed in the big view: sized for it, shown so in the small one. +view 1 +text echo from-the-big-one +key enter +stable 500 10000 +snap big +view 2 +stable 500 10000 +snap small-sees +# Typed in the small view: sized for it now, the big one shows it so. +text echo from-the-small-one +key enter +stable 500 10000 +snap small-typed +view 1 +stable 500 10000 +snap big-sees -- cgit v1.3 From 32b80ad0393321d44f929e501e5515bbf6e51c12 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 19:52:55 -0300 Subject: views: a terminal shown in a view shorter than it is passed down to its cursor, a shell's prompt never out of sight, an arrow in the gutter where rows are above; a name drafted in a tag is the view's that opened it A terminal takes the size of the view that typed into it last; a shorter view used to show its top rows, the prompt below them cut. It now passes over as many as keep the cursor's row in sight, as tmux pans a bigger window in a smaller client: `Pane.pan` (per view, derived) is made in the step's sync and again when a view is drawn out of turn (views.unpark), by terminal.panFor (0 in the view that sizes it, in one tall enough, or folded), and read where the body's rows start: `Pane.scroll`, the body walk in tty mode, the visible cursor. One view never pans: the view that sizes a terminal returns 0 at its first test. A `.name` prompt (a file's name drafted in its tag) is the view's that opened it (`Pane.prompt_view`): another view's step, where that pane has not the keyboard, no longer drops it; its view closing does. views-pan.snap: a shell sized for a 24-row view, its last lines and prompt in a 12-row one under the arrow, then typed into from the short view and sized for it. A unit test: the short view's frame has the cursor's row, and the line on it, inside the pane. docs: the notes, the reference. Co-Authored-By: Claude Opus 5.5 --- docs/next-daemon.md | 41 +++++++++++++++++++------------- docs/typ/reference.typ | 4 ++-- src/draw.zig | 3 +++ src/panes.zig | 11 +++++++-- src/pardes.zig | 11 +++++---- src/tagline.zig | 1 + src/terminal.zig | 24 ++++++++++++++++--- src/views.zig | 49 +++++++++++++++++++++++++++++++++++++- test/snapshots/views-pan.golden | 52 +++++++++++++++++++++++++++++++++++++++++ test/snapshots/views-pan.snap | 27 +++++++++++++++++++++ 10 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 test/snapshots/views-pan.golden create mode 100644 test/snapshots/views-pan.snap (limited to 'src/pardes.zig') diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 3d4c8a08..e7ddcf5f 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -314,15 +314,23 @@ the ponytail lens: workspace tag, prompts, messages, registers (decided), the macro being recorded, look-hover (one pointer at a time), mouse sweeps, query panes, search state, settings and theme, the effect and input queues, - `needs_frame` (any change draws every view). + `needs_frame` (any change draws every view). A `.name` prompt (a + file's name drafted in its tag) is the view's that opened it + (`Pane.prompt_view`): only that view's focus leaving the pane drops it, + and its closing. - **A terminal shown at two sizes** takes the size of the view that typed into it last (`Pane.sized_by`, set by a key or a paste; a new pane's is its maker's; a closed view's goes to the next to sync it). The others show it at that size: `sync` resizes no pty for them, and `pane.cols/rows` stay the terminal's own, so the terminal code's invariant (pane size is - the emulator's) holds. ponytail: a smaller view sees its top rows, cut; - panning to the cursor as tmux does is the upgrade, if a small view must - follow a shell it never types into. + the emulator's) holds. A view shorter than the terminal is passed down + its top rows as far as keeps its cursor's row in sight, as tmux shows a + bigger window in a smaller client (`Pane.pan`, terminal.panFor: per view, + made in the step and again when a view is drawn out of turn, read where + the body's rows start, `Pane.scroll` and the body walk), an arrow in its + gutter's first row saying rows are above; a shell's prompt, at the + bottom, is never out of sight. ponytail: a panned view's mouse reports to + the terminal's program and its kitty images are placed as unpanned. - **Panes come and go under parked views.** A view keeps the serial of each slot when it goes out; unparked, the ids whose serial changed leave its layout and its keyboard moves to its first pane left. A pane focused @@ -390,8 +398,6 @@ the ponytail lens: named in a path yet (a script's focus or layout is that view's). - Every change draws every view (`needs_frame` is the session's); a view's own dirtiness waits for a measurement that wants it. - - Prompts are the pane's: a `.name` prompt drafted in one view is - closed by another view's step, where that pane has not the keyboard. - Dump, Restore and Handover keep one view, the live one. - **What proved wrong in the design.** "Views" in the design were a struct the core reads through; the swap made them cheaper and kept every @@ -433,21 +439,24 @@ Two views (not gated): every frame draws both, so a change costs two renders; a view switch copies ~60 fields and each pane's share, a few microseconds. -Gates, on the line rebased onto main's tty throughput fix (qzsouslm): +Gates, on the line rebased onto main's screen-state terminals (mnlwlpvk): -- `zig build replay -Dplatform=tty`: 112 snapshot scripts pass (the 110, - views.snap and views-term.snap), their 130 tapes replay in all three - replicas (390 runs, 29,355 digests); 20 monkey seeds at one view (60 - runs, 14,010 digests) and 20 at three views (60 runs, 9,720 digests); - no divergence, no panic. views.snap: 75 digests a replica, - views-term.snap 35. -- Unit tests: tty 1478 of 1498 pass, 20 skipped; GUI 1549 of 1560, 9 +- `zig build replay -Dplatform=tty`: 113 snapshot scripts pass (the 110, + views.snap, views-term.snap and views-pan.snap), their 131 tapes replay + in all three replicas (393 runs, 29,436 digests); 20 monkey seeds at one + view (60 runs, 14,004 digests) and 20 at three views (60 runs, 9,666 + digests); no divergence, no panic. views.snap: 75 digests a replica, + views-term.snap 35. core-test: 1117 of 1135, 18 skipped. +- Unit tests (on qzsouslm): tty 1478 of 1498 pass, 20 skipped; GUI 1549 of 1560, 9 skipped and 2 failed, the same two that fail on phase 1's line (`tagline pointer mapping retains physical grips...` and `a tag caret past its - row's end glides...`). The views' own: 7 (own cursor, scroll, mode, + row's end glides...`). The views' own: 8 (own cursor, scroll, mode, layout and focus; an edit and a register shared; a terminal sized by its last typist; views closing; the other views drawn leave the digest; a - view keeps its place in a text another edits). + view keeps its place in a text another edits; a short view keeps a tall + terminal's cursor in sight). views-pan.snap: a shell sized for the tall + view, its prompt and last lines in the short one under an arrow, then + typed into from the short one and sized for it. - test/fs.py passes (with a TMPDIR short enough for the 9P sockets' paths). test/daemon.py: 20 detach and attach cycles, then a second terminal on the attached session, both drawn, detached from inside and diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index 190adba7..ce2a45f1 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -1018,8 +1018,8 @@ to its end before the write is answered, and one that finds nothing fails, each terminal or window its own layout, focus, cursors, selections, modes, scroll and jumps, over the panes, texts, tags, terminals, messages and registers they share. A terminal pane shown in two takes the - size of the view that typed into it last; a smaller view shows its top - rows. #word("Detach"), or a terminal closing, lets that view go and + size of the view that typed into it last; a shorter view shows the + rows down to its cursor, an arrow in the gutter when rows are above. #word("Detach"), or a terminal closing, lets that view go and leaves the others. With none attached, `size C R` sets the screen, messages clear by the clock, and a pty reports its size in pixels at the last cell size (8×16 before any), diff --git a/src/draw.zig b/src/draw.zig index 4f9c6c87..0cc5c80d 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -958,6 +958,9 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); + // A terminal panned to its cursor in a view shorter than it: an arrow + // in the gutter's first body row says rows are above (views.zig). + if (pane.pan > 0) s.set(r.x, p.bodyTop(pane, r), "↑", .{ .fg = .{ .rgb = chrome.box } }); // A query's places' ticks, each in the track's cell of its row: chrome, // painted with the rail (a pixel shell draws its own from the same list). for (s.rail_ticks[0..s.nrail_ticks]) |tick| { diff --git a/src/panes.zig b/src/panes.zig index 71a04464..89f24eb6 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -30,7 +30,7 @@ pub const Pane = struct { /// the pane's opens and readers; the other views' share of it (views.zig). pub const replica = .{ .local = .{ "pv", "pv_known" }, - .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices" }, + .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "view", "notices", "pan" }, .authority = .{"fs"}, .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, }; @@ -309,6 +309,13 @@ pub const Pane = struct { /// The view that last typed into it: a terminal shown at two sizes takes /// that view's (Pardes.sync). sized_by: u8 = 0, + /// A terminal shown in a view shorter than it (one that does not size + /// it): the rows its top is passed over by, so its cursor stays in + /// sight (terminal.panFor); 0 everywhere else. + pan: u16 = 0, + /// The view whose step a `.name` prompt is (tagline.draftName): only + /// that view's focus leaving the pane drops it. + prompt_view: u8 = 0, pub const MessagePhase = enum { entering, shown, lingering, leaving }; /// One message line's animation: falling in, shown, lingering after the @@ -543,7 +550,7 @@ pub const Pane = struct { pub fn scroll(pane: *Pane) i32 { if (pane.file) |f| return @intCast(f.scroll); if (comptime Pdf.enabled) if (pane.pdf) |pv| return @intCast(pv.text_scroll); - return pane.surfRow(terminal.gridOffset(pane)); + return pane.surfRow(terminal.gridOffset(pane) + pane.pan); } pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } { diff --git a/src/pardes.zig b/src/pardes.zig index 59233347..75e91ee9 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -7729,7 +7729,7 @@ pub const Pardes = struct { // hands it back to its body, and a name drafted there is dropped. if (id != p.active or p.header_focus) { pane.focus = .body; - if (pane.prompt == .name) pane.closePrompt(); + if (pane.prompt == .name and pane.prompt_view == p.view) pane.closePrompt(); } // A sweep over the tag is the tag's while it has the keyboard. const swept = pane.sel[sel_slot]; @@ -7822,6 +7822,7 @@ pub const Pardes = struct { pane.rows = rows; if (pane.file) |*file| file.syntax_dirty = true; } + pane.pan = panes.terminal.panFor(p, pane, r); // A directory pane's entries follow its width, as acme's // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols); @@ -7856,10 +7857,10 @@ pub const Pardes = struct { /// Whether the live view sizes terminal `pane`: a terminal shown at two /// sizes takes the size of the view that typed into it last, and any - /// view's once that one is gone. The others show it at that size. - // ponytail: a view smaller than the terminal shows its top rows, the - // rest cut; panning to its cursor, as tmux does, when a small view must - // follow a busy shell it never types into. + /// view's once that one is gone. The others show it at that size, a + /// shorter one passed down to its cursor (`Pane.pan`, terminal.panFor). + // ponytail: a panned view's pointer reports to a terminal's program and + // its kitty images are placed as the unpanned rows are. fn sizes(p: *Pardes, pane: *Pane) bool { if (pane.sized_by == p.view) return true; if (views.isOpen(p, pane.sized_by)) return false; diff --git a/src/tagline.zig b/src/tagline.zig index 79c2c09b..f669e98b 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -361,6 +361,7 @@ pub fn draftName(p: *Pardes, pane: *Pane, col: usize) void { const name = p.gpa.dupe(u8, f.path) catch return; pane.closePrompt(); pane.prompt = .name; + pane.prompt_view = p.view; pane.input = .{ .what = .input, .own = name, .mode = .insert, .cur_pinned = true, .cur_col = @intCast(@min(name.len, panes.File.rawAtDisplay(name, col))) }; } diff --git a/src/terminal.zig b/src/terminal.zig index 0afbe43a..e737a831 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -395,8 +395,26 @@ pub fn visibleCursor(pane: *const Pane) ?GridCursor { const cur = state.vt.screens.active.cursor; const sb = scrollbar(pane); const row = sb.total - sb.len + cur.y; - if (row < sb.offset or row - sb.offset >= sb.len) return null; - return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset) }; + if (row < sb.offset + pane.pan or row - sb.offset >= sb.len) return null; + return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset - pane.pan) }; +} + +/// The rows terminal `pane`'s top is passed over by in a view whose rect +/// `r` is shorter than it, one that does not size it (Pardes.sizes): as +/// many as keep its cursor's row in sight, as tmux pans a bigger window in +/// a smaller client. 0 in the view that sizes it, and in one tall enough. +pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { + if (comptime !enabled) return 0; + if (pane.sized_by == p.view or pane.collapsed or !pane.isTerminal()) return 0; + const shown = r.h -| pane.tag_rows; + if (shown == 0 or shown >= pane.rows) return 0; + const state = pane.terminal orelse return 0; + const sb = scrollbar(pane); + // The cursor's row in the viewport; past its end (scrolled back), its + // last row. + const row = sb.total - sb.len + state.vt.screens.active.cursor.y; + const at: usize = if (row < sb.offset) 0 else @min(row - sb.offset, pane.rows - 1); + return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); } /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). @@ -1886,7 +1904,7 @@ const BodyWalk = struct { return .{ .pane = pane, .goff = goff, - .g = if (pane.body.mode == .tty) goff else pane.gridRow(off), + .g = if (pane.body.mode == .tty) goff + pane.pan else pane.gridRow(off), .skip = if (pane.ovl) |o| @intCast(@max(0, off - pane.surfRow(o.row))) else 0, }; } diff --git a/src/views.zig b/src/views.zig index e3ba5b4e..c847b987 100644 --- a/src/views.zig +++ b/src/views.zig @@ -53,7 +53,7 @@ pub const fields = [_][:0]const u8{ /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled, and /// what the view's step measured of it (its tag's rows, its bands). -const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices" }; +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "msel", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; @@ -72,6 +72,7 @@ pub const PaneView = struct { tag_rows: u16 = 1, view: Pane.SessionView = .{}, notices: Pane.Notices = .{}, + pan: u16 = 0, body: Part(Text, &body_fields) = .{}, /// A document's scroll and size; a terminal's size is its pty's, which /// every view shows (sized by its last typist, `Pardes.sync`). @@ -216,6 +217,10 @@ fn unpark(p: *Pardes, k: u8) void { }; } layout.compute(p); + // Its terminals' cursors moved while it was out: each pan follows. + for (p.panes, 0..) |s, id| if (s) |pane| { + pane.pan = pardes.panes.terminal.panFor(p, pane, p.rects[id]); + }; } /// The pane with the keyboard is one the live view shows: focused where its @@ -285,6 +290,8 @@ pub fn close(p: *Pardes, k: u8) void { if (sels.len > 0) p.gpa.free(sels); pane.pv[k].body.sels = &.{}; pane.pv_known &= ~(@as(u8, 1) << @intCast(k)); + // A name it was drafting goes with it. + if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); }; p.views_open &= ~(@as(u8, 1) << @intCast(k)); } @@ -555,3 +562,43 @@ test "a view not live keeps its place in a text another view edits" { try testing.expectEqual(scrolled + 3, pane.file.?.scroll); try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101")); } + +test "a terminal shown in a view shorter than it keeps its cursor in sight" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 20 }); + defer p.deinit(); + const id = p.active; + const term = p.panes[id].?; + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 100, .rows = 50 } }); + p.update(.{ .key = .{ .cp = Key.right } }); + const rows = term.rows; + try testing.expect(rows > 30); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); + for (1..rows) |i| try out.print(gpa, "row-{d}\r\n", .{i}); + try out.appendSlice(gpa, "LAST$ "); + p.update(.{ .output = .{ .pane = @intCast(id), .bytes = out.items } }); + // Sized for the tall view, it fits there whole: nothing is passed over. + try testing.expectEqual(@as(u16, 0), term.pan); + p.toView(0); + p.sync(); + try testing.expectEqual(rows, term.rows); + try testing.expect(term.pan > 0); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const r = p.rects[id]; + // The cursor's row, the last, is on screen in the short view, inside + // the pane, and so is the line it is on. + const cursor = s.cursor.?; + try testing.expect(cursor.y >= r.y and cursor.y < r.y + r.h); + var row_text: std.ArrayList(u8) = .empty; + defer row_text.deinit(gpa); + for (0..s.cols) |x| try row_text.appendSlice(gpa, s.cells[@as(usize, cursor.y) * s.cols + x].grapheme()); + try testing.expect(std.mem.indexOf(u8, row_text.items, "LAST$") != null); + // Back in the tall view, its top is where it was. + p.toView(k); + try testing.expectEqual(@as(u16, 0), term.pan); +} diff --git a/test/snapshots/views-pan.golden b/test/snapshots/views-pan.golden new file mode 100644 index 00000000..db63d29c --- /dev/null +++ b/test/snapshots/views-pan.golden @@ -0,0 +1,52 @@ +== snap tall grid=80x24 cursor=4,16 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes Config Debug Exit +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-pan/cwd Tty+bash Save Mode Filter Collapse Del +| 18 +| 19 +| 20 +| 21 +| 22 +| 23 +| 24 +| 25 +| 26 +| 27 +| 28 +| 29 +| 30 +| $ +| /tmp/pardes-snap/views-pan/cwd/+New Save Tty Collapse Del +| 1 +== snap short-pans grid=60x12 cursor=4,8 +| Newcol Joincol Find Grep Help Changelog Tutor Dump Themes +| New Tty Find Grep Joincol Delcol +|$ /tmp/pardes-snap/views-pan/cwd Tty+bash Save Mode Filter C +| ollapse Del +|↑ 27 +| 28 +| 29 +| 30 +| $ +| /tmp/pardes-snap/views-pan/cwd/+New Save Tty Collapse Del +| 1 +== snap short-typed grid=60x12 cursor=4,8 +|4: 29 +|5: 30 +|6: $ echo typed-in-short +|7: typed-in-short +== snap tall-sees grid=80x24 cursor=4,7 +|3: 29 +|4: 30 +|5: $ echo typed-in-short +|6: typed-in-short +|7: $ +|8: +|9: +|10: +|11: +|12: +|13: +|14: +|15: +|16: diff --git a/test/snapshots/views-pan.snap b/test/snapshots/views-pan.snap new file mode 100644 index 00000000..3ba1ac5b --- /dev/null +++ b/test/snapshots/views-pan.snap @@ -0,0 +1,27 @@ +# A terminal shown in a view shorter than it (src/views.zig): sized for the +# tall view that typed into it, the short one passes over its top rows so +# its cursor and prompt stay in sight, an arrow in the gutter saying rows +# are above. +config Verbose off +start 24 80 views=2 +wait 8000 $ +stable 700 20000 +view 2 +resize 12 60 +stable 500 10000 +view 1 +text seq 1 30 +key enter +wait 8000 30 +stable 500 10000 +snap tall +view 2 +stable 500 10000 +snap short-pans +text echo typed-in-short +key enter +stable 500 10000 +snap short-typed +view 1 +stable 500 10000 +snap tall-sees -- cgit v1.3 From 3b57a72becd6dd6700d996c9c5c000e1cb53e076 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 18:19:13 -0300 Subject: phase 5 part A: the speculation and rollback engine, in one process: a walker copy put back where it was, replicas that apply their own input at once and take the authority's tape as the truth, and a harness of typists over delayed links src/rollback.zig: the replica core's allocator (`Epoch`) knows every allocation, and while a copy is held puts off frees of what was there before and refuses to grow it in place, so what the core held stays where it was. The copy (`Copy`) is the digest's walk saving the bytes of every allocation it reaches, field by field (the derived caches and this process's own too: a safe build's free writes over what it frees, and the frame arena points at what the epoch then frees), but the queues a step drains and the frame statistics; a file's text, its line index and its undo snapshots are pinned instead (made anew at each edit, never written; now freed raw everywhere), checked unchanged in a safe build. Put back, each allocation's bytes go back over it, the epoch's allocations are freed and its put-off frees forgotten; let go, they are made. Tree-sitter's trees, the C heap's, are held the same way (syntax.held_trees). src/replica.zig: an `Authority` records its core's every input to a tape in memory and hands each client's input back as a unit (origin, sequence number, its tape); a `Replica` applies its own input at once, as the authority will (update, then pump), recorded by its own tape (tape.zig's speculation mode), and for each unit in order drops its pending head when the authority recorded the same (inputs, host calls and answers: the fast path), else puts the copy back, replays the units since and this one (every digest checked) and applies its pending inputs again. A host call or a syscall stops a speculation: it is answered harmlessly, undone when it returns, and the input waits for the authority's answer; a clock or environment read is a guess the comparison checks; an emulator is the authority's (terminal.zig's writers stop a speculation; a safe build asserts none changed). The view's events (a resize, the pointer) wait. Messages are bytes: no transport yet. Each client has a view of its own (src/views.zig): the authority goes into it before the client's input (`toView`, taped), the replica's speculation the same, and the copy walks the views not live too (`parked`, each pane's `pv`). test/replicas.zig (`zig build replicas`): one authority and N replicas, a view each (`--shared` puts them in one), each client typing monkey.zig's mix at 50-200 ms over its own link (half the RTT plus 10% jitter, in order), the authority's pty echoing 2 ms later; gates: no replica diverges, every replica's digest is the authority's at quiescence; reported per RTT: inputs applied at once, stopped, waiting, fast paths, rollbacks (mispredicted, foreign), and what a speculative apply, a copy and a rollback cost. 3 clients x 100 inputs at 30, 80 and 207 ms: no divergence over 12 Debug seeds and 20 ReleaseFast ones with a filled heap; ReleaseFast p99 a speculative apply under 0.2 ms, a rollback under 1.6 ms. `zig build rollback-bench` times the real copy now (take, release, an edit, restore). What the replicas found, fixed: a release build reads what the heap had where Debug's fill made two cores agree: `col_panes` and `restore_buf` digested undefined (zeroed now), `terminal.State.reply_dropped` never set at create, `Text.sels` and `PendingPipe.ranges` digested past their length (a walker `len` cuts a slice too). PARDES_REPLICA_FILL fills a replica's allocations to show what is left. Co-Authored-By: Claude Opus 5.5 --- build.zig | 10 + src/File.zig | 14 +- src/Pipe.zig | 2 + src/Text.zig | 6 +- src/memory.zig | 9 + src/pardes.zig | 11 +- src/replica.zig | 578 ++++++++++++++++++++++++++++++++++++++++++++++++ src/rollback.zig | 496 +++++++++++++++++++++++++++++++++++++++++ src/syntax.zig | 24 ++ src/tape.zig | 236 ++++++++++++++++++-- src/terminal.zig | 17 ++ src/walk.zig | 24 +- test/replicas.zig | 327 +++++++++++++++++++++++++++ test/rollback_bench.zig | 120 ++++------ 14 files changed, 1763 insertions(+), 111 deletions(-) create mode 100644 src/replica.zig create mode 100644 src/rollback.zig create mode 100644 test/replicas.zig (limited to 'src/pardes.zig') diff --git a/build.zig b/build.zig index 13cb8ebc..1a5dd200 100644 --- a/build.zig +++ b/build.zig @@ -1504,6 +1504,16 @@ pub fn build(b: *std.Build) void { run_rollback_bench.has_side_effects = true; b.step("rollback-bench", "a replica's rollback copy (walked state, pinned blobs, walk time) for a typical and a big session, and a fork of the big one (-Doptimize=ReleaseFast)").dependOn(&run_rollback_bench.step); + const replicas = b.addExecutable(.{ + .name = "pardes-replicas", + .root_module = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("test/replicas.zig"), .link_libc = true }), + }); + replicas.root_module.addImport("pardes", core_module); + const run_replicas = b.addRunArtifact(replicas); + run_replicas.addPassthruArgs(); + run_replicas.has_side_effects = true; + b.step("replicas", "one authority and several replicas in process, each client typing over a delayed link: digests at quiescence, rollback rate and costs per RTT (-- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207])").dependOn(&run_replicas.step); + const locations_bench_step = b.step("locations-bench", "measure cold/warm result formatting (-Doptimize=ReleaseFast; CSV on stderr)"); if (optimize != .fast) { locations_bench_step.dependOn(&b.addFail("locations-bench requires -Doptimize=ReleaseFast").step); diff --git a/src/File.zig b/src/File.zig index dd6b2d34..84222191 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1159,10 +1159,12 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (p.panes[id].?.file.?.line_starts.ptr == file.line_starts.ptr) break true; } else false; p.gpa.free(file.path); - if (ntwins == 0) p.gpa.free(file.content); + // Raw, as every text is freed (a replica's rollback copy pins them, + // src/rollback.zig, and a safe build's free writes over what it frees). + if (ntwins == 0) pardes.memory.freeUnfilled(p.gpa, file.content); if (file.listing) |listing| p.gpa.free(listing.names); if (file.mini) |*mini| mini.deinit(p.gpa); - if (file.line_starts.len > 0 and !index_shared) p.gpa.free(file.line_starts); + if (file.line_starts.len > 0 and !index_shared) pardes.memory.freeRaw(p.gpa, file.line_starts); if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); @@ -1264,7 +1266,7 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { }; // The clock the session that recorded read (src/tape.zig). f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{}); - if (f.line_starts.len > 0) p.gpa.free(f.line_starts); + if (f.line_starts.len > 0) pardes.memory.freeRaw(p.gpa, f.line_starts); f.line_starts = starts; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); f.highlights = &.{}; @@ -1276,7 +1278,7 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { const tf = &t.file.?; // Its own index, built while the shared one was empty, goes; the // shared one, freed above, is replaced. - if (tf.line_starts.len > 0 and tf.line_starts.ptr != old_starts) p.gpa.free(tf.line_starts); + if (tf.line_starts.len > 0 and tf.line_starts.ptr != old_starts) pardes.memory.freeRaw(p.gpa, tf.line_starts); tf.content = f.content; tf.line_starts = f.line_starts; tf.revision = f.revision; @@ -1498,8 +1500,8 @@ pub fn relink(p: *Pardes, of: usize, id: usize, dot: ?[2]u32) void { sf.twin = p.next_twin; p.next_twin +%= 1; } - p.gpa.free(f.content); - if (f.line_starts.len > 0) p.gpa.free(f.line_starts); + pardes.memory.freeUnfilled(p.gpa, f.content); + if (f.line_starts.len > 0) pardes.memory.freeRaw(p.gpa, f.line_starts); f.history.destroy(p.gpa); f.content = sf.content; f.line_starts = sf.line_starts; diff --git a/src/Pipe.zig b/src/Pipe.zig index 850f9c64..239a94e4 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -52,6 +52,8 @@ pub fn pipeMarker(how: modal.Normal.PipeBehavior) []const u8 { } pub const PendingPipe = struct { + /// The digest's walk (src/walk.zig) of the ranges, as far as they go. + pub const replica = .{ .len = .{.{ "ranges", "nranges" }} }; id: u32, pane: usize, serial: u32, diff --git a/src/Text.zig b/src/Text.zig index 3f107542..da4be590 100644 --- a/src/Text.zig +++ b/src/Text.zig @@ -49,9 +49,9 @@ pub const SelRange = struct { sticky: i32 = -1, }; -/// The replay digest's walk (src/walk.zig) of the histories: as far as -/// they go. -pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" } } }; +/// The replay digest's walk (src/walk.zig) of the histories and the ranges: +/// as far as they go (the ranges' room is made whole and used `nsel` deep). +pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" }, .{ "sels", "nsel" } } }; what: What = .body, /// The characters of a text that holds its own (gpa-owned): everything but diff --git a/src/memory.zig b/src/memory.zig index 47bd755d..1d082964 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -421,6 +421,15 @@ pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); } +/// `freeUnfilled` for a slice of any element: a text's line index, made +/// anew at every edit too (and pinned by a replica's rollback copy, +/// src/rollback.zig, which a safe build's overwrite would spoil). +pub fn freeRaw(allocator: Allocator, items: anytype) void { + const E = @typeInfo(@TypeOf(items)).pointer.child; + const bytes = std.mem.sliceAsBytes(items); + if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); +} + /// Gives free memory back to the system, the arenas' and libc's, inside its /// heap too: after a big result is formatted or a big pane torn down, the /// tree-sitter arena kept 73 MiB of emptied buckets resident and glibc what diff --git a/src/pardes.zig b/src/pardes.zig index 75e91ee9..5d889c86 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -65,6 +65,8 @@ pub const tape = @import("tape.zig"); pub const walk = @import("walk.zig"); pub const screen_state = if (terminal_panes) @import("screen_state.zig") else struct {}; pub const views = @import("views.zig"); +pub const rollback = @import("rollback.zig"); +pub const replica = @import("replica.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -474,6 +476,8 @@ pub const Loc = struct { }; test { + _ = @import("rollback.zig"); + _ = @import("replica.zig"); _ = @import("modal.zig"); _ = @import("tagline.zig"); _ = @import("hunks.zig"); @@ -4412,7 +4416,10 @@ pub const Pardes = struct { // layout: columns own x by weight; panes own y by vweight within a column. ncol: usize = 0, col_weight: [MAX_COLS]u64 = @splat(column_weight_unit), - col_panes: [MAX_COLS][MAX_PANES]usize = undefined, + // Zeroed, not undefined: a digest takes the whole of it, a release + // build's heap leaves its unused slots whatever was there before, and + // two replicas must agree on them (src/replica.zig). + col_panes: [MAX_COLS][MAX_PANES]usize = @splat(@splat(0)), col_n: [MAX_COLS]usize = @splat(0), /// Each column's tag and the workspace's: Texts like a pane's tag, with /// no prefix, showing a default until someone edits them. @@ -4698,7 +4705,7 @@ pub const Pardes = struct { /// a Restore builtin wants this dump loaded into the live session; the /// shell consumes it via takeRestore each frame (restore contents stay host-fed) restore_req: ?[]const u8 = null, - restore_buf: [1024]u8 = undefined, + restore_buf: [1024]u8 = @splat(0), surface: Surface = .{}, /// The view the fields above are of: whose layout, focus, pointer and diff --git a/src/replica.zig b/src/replica.zig new file mode 100644 index 00000000..42d94503 --- /dev/null +++ b/src/replica.zig @@ -0,0 +1,578 @@ +//! A session's replicas (docs/next-daemon.md, phase 5 part A): the authority +//! orders every input and records what its core consumed (src/tape.zig); +//! each replica runs a core of its own, applies its own inputs at once, and +//! takes the authority's stream as the truth. +//! +//! A replica's state is the confirmed position plus its own pending inputs, +//! applied on top (a speculation, recorded as the authority would record +//! it). For each unit of the authority's stream, in order: +//! - its own pending head, recorded the same (inputs, host calls and +//! answers): the head goes, nothing is redone (the fast path); +//! - else the rollback copy is put back (src/rollback.zig), the confirmed +//! units since it are replayed, then this one, and the pending inputs +//! are applied again. +//! A speculation that reaches a host call or a syscall (a Host function, +//! a `tape.site`) or an emulator (the authority's) stops: the copy is put +//! back and the input waits for the authority's answer, one RTT late but +//! never wrong. Clock and environment reads (`tape.value`) are made here, +//! and the comparison catches a guess the authority did not share. +//! +//! No transport: messages are bytes in and out. A client's input is its +//! id, a sequence number and the event as detached/wire.zig frames it; a +//! unit is the input's origin (0 for the authority's own: a pty's output, a +//! tick), its sequence number and the authority's tape of it. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const tape = @import("tape.zig"); +const rollback = @import("rollback.zig"); +const walk = @import("walk.zig"); +const wire = @import("detached/wire.zig"); +const Allocator = std.mem.Allocator; +const Pardes = pardes.Pardes; + +const checked = builtin.mode == .debug or builtin.mode == .safe; +const head_len = 6; // origin or client: u16le, seq: u32le +/// Confirmed units fast-pathed under one copy before it is taken afresh: +/// a rollback replays them all. +const rebase_after = 32; + +fn header(buf: []u8, who: u16, seq: u32) void { + std.mem.writeInt(u16, buf[0..2], who, .little); + std.mem.writeInt(u32, buf[2..6], seq, .little); +} + +pub const Unit = struct { origin: u16, seq: u32, tape: []const u8 }; + +pub fn parseUnit(msg: []const u8) ?Unit { + if (msg.len < head_len) return null; + return .{ .origin = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .tape = msg[head_len..] }; +} + +pub const Input = struct { client: u16, seq: u32, event: pardes.Event }; + +/// A client's input message; its event borrows `msg`. +pub fn parseInput(msg: []const u8) ?Input { + if (msg.len < head_len) return null; + const f = (wire.framed(msg[head_len..]) catch return null) orelse return null; + const ev = switch (wire.decodeClient(f.tag, f.payload) catch return null) { + .event => |ev| ev, + else => return null, + }; + return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev }; +} + +/// The core whose tape is `t`, stepped: tape.on is process-wide, and so +/// are syntax.zig's held trees, so one process can step several. +const Stepping = struct { + tape: ?*tape.Tape, + trees: ?*pardes.syntax.HeldTrees, + + fn begin(t: *tape.Tape, epoch: ?*rollback.Epoch) Stepping { + const was: Stepping = .{ .tape = tape.on, .trees = pardes.syntax.held_trees }; + tape.on = t; + rollback.Epoch.select(epoch); + return was; + } + + fn end(was: Stepping) void { + tape.on = was.tape; + pardes.syntax.held_trees = was.trees; + } +}; + +fn nowNs() u64 { + return pardes.FrameStats.nowNs(); +} + +// ---- the authority ---- + +pub const Authority = struct { + gpa: Allocator, + t: tape.Tape, + core: *Pardes, + host: pardes.Host, + out: std.ArrayListUnmanaged(u8) = .empty, + seq: u32 = 0, + /// Each client's view (src/views.zig): its own layout, focus and + /// cursors over the session's text. The first client has the session's. + view_of: [64]u8 = @splat(0), + + /// A session booted with `opts`; `unit` is its first, which a replica + /// is made from. + pub fn init(gpa: Allocator, options: pardes.Options, host: pardes.Host) !*Authority { + // A replay gives its core every allocator (tape.zig), and a digest + // tells an allocator from none. + var opts = options; + opts.image_allocator = opts.image_allocator orelse gpa; + opts.pdf_allocator = opts.pdf_allocator orelse gpa; + opts.tree_sitter_allocator = opts.tree_sitter_allocator orelse gpa; + opts.frame_allocator = opts.frame_allocator orelse gpa; + const a = try gpa.create(Authority); + errdefer gpa.destroy(a); + a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + const was = Stepping.begin(&a.t, null); + defer was.end(); + a.core = try Pardes.init(gpa, opts); + a.core.host = host; + a.core.pump(host) catch {}; + return a; + } + + pub fn deinit(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.deinit(); + was.end(); + tape.release(&a.t); + a.out.deinit(a.gpa); + a.gpa.destroy(a); + } + + /// What was recorded since the last unit, as one (the boot's, a setup's + /// done straight on the core). + pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { + a.out.clearRetainingCapacity(); + try a.out.appendNTimes(a.gpa, 0, head_len); + header(a.out.items, origin, seq); + try a.out.appendSlice(a.gpa, tape.taken(&a.t)); + tape.clearTaken(&a.t); + return a.out.items; + } + + /// An input of the authority's own (a pty's output, a tick, a setup's + /// command), recorded into the unit being made. + pub fn apply(a: *Authority, ev: pardes.Event) void { + const was = Stepping.begin(&a.t, null); + a.core.update(ev); + a.core.pump(a.host) catch {}; + was.end(); + } + + /// One of the authority's own inputs, as its unit. + pub fn local(a: *Authority, ev: pardes.Event) ![]const u8 { + a.apply(ev); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// A view of its own for `client`, as its unit; which it is. + pub fn openView(a: *Authority, client: u16) !struct { view: u8, unit: []const u8 } { + const was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse { + was.end(); + return error.TooManyViews; + }; + a.core.pump(a.host) catch {}; + was.end(); + a.view_of[client] = k; + a.seq += 1; + return .{ .view = k, .unit = try a.unit(0, a.seq) }; + } + + /// One client's input, applied in the order it came, in its view: the + /// unit every replica takes. + pub fn take(a: *Authority, msg: []const u8) ![]const u8 { + const in = parseInput(msg) orelse return error.BadInput; + const was = Stepping.begin(&a.t, null); + a.core.toView(a.view_of[in.client]); + a.core.update(in.event); + a.core.pump(a.host) catch {}; + was.end(); + return a.unit(in.client, in.seq); + } +}; + +// ---- a replica ---- + +pub const Stats = struct { + inputs: u32 = 0, + /// Applied at once and clean. + speculated: u32 = 0, + /// Stopped at a host call, a syscall or an emulator: waits one RTT. + stopped: u32 = 0, + /// Not tried: an event of the view's (a resize, the pointer), phase 4's. + unspeculated: u32 = 0, + /// The authority's unit was the pending head, recorded the same. + fast: u32 = 0, + /// The copy put back: the head recorded otherwise (a guess of the clock + /// the authority did not share), or another's unit first. + mispredicted: u32 = 0, + foreign: u32 = 0, + rebases: u32 = 0, + units: u32 = 0, + /// Nanoseconds: one speculative apply, a copy taken, a rollback (the + /// copy put back, the confirmed units replayed, the pending reapplied). + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_bytes: u64 = 0, + copies: u32 = 0, + + pub fn deinit(s: *Stats, gpa: Allocator) void { + s.spec_ns.deinit(gpa); + s.copy_ns.deinit(gpa); + s.rollback_ns.deinit(gpa); + } +}; + +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null }; + +pub const Replica = struct { + gpa: Allocator, + id: u16, + /// Its own view of the session (Authority.openView's). + view: u8 = 0, + /// The core's allocator; its tapes and the engine's own lists are not. + epoch: rollback.Epoch = .{}, + t: tape.Tape, + copy: rollback.Copy = .{}, + pending: std.ArrayListUnmanaged(Pending) = .empty, + /// pending[0..applied] are applied on top of the confirmed position. + applied: usize = 0, + /// The pending input that waits for the authority (it stopped at a host + /// call, or is the view's), and those after it with it. + blocked: ?u32 = null, + /// Confirmed units fast-pathed since the copy was taken. + since: std.ArrayListUnmanaged([]u8) = .empty, + /// Pending inputs to apply again (after a rollback). + dirty: bool = false, + next_seq: u32 = 1, + stats: Stats = .{}, + diverged: ?[]const u8 = null, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds of the rollback being made, until its reapply. + recovering: ?u64 = null, + + /// A replica of the session whose first unit is `first`. + pub fn init(gpa: Allocator, id: u16, first: []const u8) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa) }; + r.t.core_gpa = r.epoch.allocator(); + r.confirm(first); + if (r.t.core == null) { + r.deinit(); + return error.NoCore; + } + return r; + } + + pub fn deinit(r: *Replica) void { + if (r.t.core) |c| { + const was = Stepping.begin(&r.t, &r.epoch); + if (r.copy.held) r.copy.restore(); + c.deinit(); + was.end(); + } + r.copy.deinit(); + for (r.pending.items) |p| r.freePending(p); + r.pending.deinit(r.gpa); + for (r.since.items) |u| r.gpa.free(u); + r.since.deinit(r.gpa); + r.out.deinit(r.gpa); + r.stats.deinit(r.gpa); + tape.release(&r.t); + if (r.diverged) |why| r.gpa.free(why); + r.epoch.deinit(); + r.gpa.destroy(r); + } + + pub fn core(r: *const Replica) *Pardes { + return r.t.core.?; + } + + fn freePending(r: *Replica, p: Pending) void { + r.gpa.free(p.msg); + if (p.spec) |s| r.gpa.free(s); + } + + /// The replica's own input: applied at once when it can be, and the + /// message for the authority returned (valid until the next call). + pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 { + r.stats.inputs += 1; + const seq = r.next_seq; + r.next_seq += 1; + const room = try r.gpa.alloc(u8, head_len + wire.clientBound(.{ .event = ev })); + defer r.gpa.free(room); + const n = (try wire.encodeClient(room[head_len..], .{ .event = ev })).len; + header(room, r.id, seq); + const msg = try r.gpa.dupe(u8, room[0 .. head_len + n]); + try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg }); + r.dirty = true; + r.settle(); + r.out.clearRetainingCapacity(); + try r.out.appendSlice(r.gpa, msg); + return r.out.items; + } + + /// One unit of the authority's stream, in its order. + pub fn confirm(r: *Replica, msg: []const u8) void { + if (r.diverged != null) return; + const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); + r.stats.units += 1; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and + tape.sameRecords(r.pending.items[0].spec.?, u.tape)) + { + r.stats.fast += 1; + r.freePending(r.pending.orderedRemove(0)); + r.applied -= 1; + if (r.applied == 0) { + // Nothing speculative on top any more: the state is the + // confirmed one. + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + return; + } + const kept = r.gpa.dupe(u8, u.tape) catch return r.fail("out of memory"); + r.since.append(r.gpa, kept) catch return r.fail("out of memory"); + if (r.since.items.len >= rebase_after) { + r.stats.rebases += 1; + r.toConfirmed(); + r.dirty = true; + } + return; + } + if (r.applied > 0) { + if (u.origin == r.id) r.stats.mispredicted += 1 else r.stats.foreign += 1; + if (u.origin == r.id and std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }) catch {}; + tape.describe(r.pending.items[0].spec.?, &w.writer); + w.writer.print("and the authority recorded:\n", .{}) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + r.recovering = nowNs(); + r.toConfirmed(); + } + if (tape.replayUnit(&r.t, u.tape)) |why| { + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + return r.fail(why); + } + if (u.origin == r.id) { + if (r.pending.items.len == 0 or r.pending.items[0].seq != u.seq) return r.fail("the authority confirmed an input this replica did not send next"); + if (r.blocked == u.seq) r.blocked = null; + r.freePending(r.pending.orderedRemove(0)); + } + r.dirty = true; + } + + fn fail(r: *Replica, why: []const u8) void { + if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return; + } + + fn clearSince(r: *Replica) void { + for (r.since.items) |u| r.gpa.free(u); + r.since.clearRetainingCapacity(); + } + + /// Back to the confirmed position: the copy put back, the units fast- + /// pathed since replayed. + fn toConfirmed(r: *Replica) void { + if (r.copy.held) { + r.copy.restore(); + rollback.Epoch.select(null); + } + r.applied = 0; + for (r.since.items) |u| if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); + r.clearSince(); + for (r.pending.items) |*p| if (p.spec) |s| { + r.gpa.free(s); + p.spec = null; + }; + } + + /// The pending inputs applied again, as far as they go. + pub fn settle(r: *Replica) void { + if (!r.dirty or r.diverged != null) return; + r.dirty = false; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + while (r.applied < r.pending.items.len and r.blocked != r.pending.items[r.applied].seq) if (!r.speculate()) break; + if (r.recovering) |t0| { + r.stats.rollback_ns.append(r.gpa, nowNs() - t0) catch {}; + r.recovering = null; + } + } + + /// pending[applied] applied on top: whether it was. + fn speculate(r: *Replica) bool { + const p = &r.pending.items[r.applied]; + const in = parseInput(p.msg) orelse return false; + switch (in.event) { + .key, .paste, .command => {}, + else => { + // The view's (a resize, the pointer): the authority's for + // now, phase 4's views to come. + r.stats.unspeculated += 1; + r.blocked = p.seq; + return false; + }, + } + const c = r.core(); + if (!r.copy.held) { + const t0 = nowNs(); + r.copy.take(&r.epoch, c) catch return false; + rollback.Epoch.select(&r.epoch); + r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + var terminals: [pardes.MAX_PANES]u64 = undefined; + if (checked) terminalDigests(c, &terminals); + const t0 = nowNs(); + tape.beginSpeculation(&r.t); + c.toView(r.view); + c.update(in.event); + c.pump(c.host) catch {}; + const clean = tape.endSpeculation(&r.t); + const dt = nowNs() - t0; + // Where a blob was written, should one be: the input that did. + if (checked and std.c.getenv("PARDES_REPLICA_TRACE") != null) r.copy.check(); + if (!clean) { + // Undone, and the earlier ones applied again on the confirmed + // position: this one waits for the authority. + r.stats.stopped += 1; + r.blocked = p.seq; + const upto = r.applied; + r.toConfirmed(); + while (r.applied < upto) if (!r.speculate()) break; + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + } + return false; + } + if (checked) { + var after: [pardes.MAX_PANES]u64 = undefined; + terminalDigests(c, &after); + if (!std.mem.eql(u64, &terminals, &after)) @panic("replica: a speculation wrote a terminal's emulator"); + } + r.stats.speculated += 1; + r.stats.spec_ns.append(r.gpa, dt) catch {}; + p.spec = r.gpa.dupe(u8, tape.taken(&r.t)) catch return false; + tape.clearTaken(&r.t); + r.applied += 1; + return true; + } + + /// Nothing pending and nothing applied: the replica is the authority's + /// confirmed state, which `digest` says. + pub fn quiet(r: *const Replica) bool { + return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held; + } + + pub fn digest(r: *Replica) u64 { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + return tape.digestOf(r.core()); + } +}; + +fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { + for (p.panes, out) |slot, *d| { + d.* = 0; + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + var h = walk.Hasher.init(0); + state.digest(&h); + d.* = h.final(); + } +} + +pub fn digestOf(a: *Authority) u64 { + const was = Stepping.begin(&a.t, null); + defer was.end(); + return tape.digestOf(a.core); +} + +const TestHost = struct { + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { + const text = [_]u8{@intCast(cp & 0x7f)}; + const msg = try r.input(.{ .key = .{ .cp = cp, .text = if (cp < 0x80 and cp >= 0x20) &text else "" } }); + try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); +} + +test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + for ("oabc") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + try std.testing.expectEqual(@as(usize, 5), r.applied); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(@as(u32, 5), r.stats.fast); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "another client's input first: a replica rolls back, replays it and applies its own again" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first); + defer r2.deinit(); + var s1: std.ArrayListUnmanaged([]u8) = .empty; + var s2: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (s1.items) |m| gpa.free(m); + for (s2.items) |m| gpa.free(m); + s1.deinit(gpa); + s2.deinit(gpa); + } + for ("oone") |k| try key(r1, k, &s1); + for ("Otwo") |k| try key(r2, k, &s2); + // The authority hears r2 first, then r1; both hear the units in that + // order. + for ([_]*std.ArrayListUnmanaged([]u8){ &s2, &s1 }) |list| for (list.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + r2.confirm(u); + r2.settle(); + }; + for ([_]*Replica{ r1, r2 }) |r| { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + } + try std.testing.expect(r1.stats.foreign >= 1); + try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); +} diff --git a/src/rollback.zig b/src/rollback.zig new file mode 100644 index 00000000..9137b6f6 --- /dev/null +++ b/src/rollback.zig @@ -0,0 +1,496 @@ +//! A replica's rollback copy (docs/next-daemon.md, phase 5): the state a +//! speculation can write, saved before it writes, and put back where it was +//! when the authority's order differs. +//! +//! Two halves: +//! - `Epoch`, the replica core's allocator. It knows every allocation, and +//! while a copy is held it frees nothing that was there before (a free +//! is put off) and grows nothing of then in place. What is allocated +//! meanwhile is the epoch's. So whatever the core held when the copy was +//! taken is still where it was when the copy is put back. +//! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a +//! digest covers, so views, buffers, registers, counters and terminal +//! metadata), saving the bytes of every allocation it reaches, the core +//! struct's own included. A file's text, its line index and its undo +//! snapshots are pinned instead: an edit puts a new buffer in their +//! place (File.setContentSpan) and never writes one, so the copy keeps +//! a reference and the put-off free keeps it alive. +//! +//! Putting the copy back writes each saved allocation's bytes over it, so +//! every pointer in them points where it did, at what was there; then the +//! epoch's allocations are freed and its put-off frees forgotten. Letting +//! the copy go (the speculation was right) makes the put-off frees. +//! +//! ponytail: an allocation reached only through a field the walk leaves +//! out (a derived cache's own heap, say) is not saved; that cache is the +//! epoch's as it was written, which is right for one remade from the text +//! it was made of and wrong for one changed in place. The replica harness +//! (test/replicas.zig) checks a digest at every confirmed step. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const Allocator = std.mem.Allocator; +const Alignment = std.mem.Alignment; +const Pardes = pardes.Pardes; + +/// Blobs this long or longer (`pinned`) are pinned, not copied. Checked +/// unchanged in a safe build when the copy goes. +const blob_min = 4096; +const checked = builtin.mode == .debug or builtin.mode == .safe; + +pub const Epoch = struct { + child: Allocator = std.heap.c_allocator, + /// Its own bookkeeping, never the epoch's. + meta: Allocator = std.heap.c_allocator, + live: std.AutoHashMapUnmanaged(usize, Entry) = .empty, + on: bool = false, + id: u32 = 0, + born: std.ArrayListUnmanaged(usize) = .empty, + deferred: std.ArrayListUnmanaged(usize) = .empty, + trees: pardes.syntax.HeldTrees = .{ .meta = std.heap.c_allocator }, + + const Entry = struct { len: usize, alignment: Alignment, epoch: u32, freed: bool = false }; + + pub fn allocator(e: *Epoch) Allocator { + return .{ .ptr = e, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + pub fn deinit(e: *Epoch) void { + var it = e.live.iterator(); + while (it.next()) |kv| e.child.rawFree(@as([*]u8, @ptrFromInt(kv.key_ptr.*))[0..kv.value_ptr.len], kv.value_ptr.alignment, @returnAddress()); + e.live.deinit(e.meta); + e.born.deinit(e.meta); + e.deferred.deinit(e.meta); + e.trees.born.deinit(e.meta); + e.trees.freed.deinit(e.meta); + } + + fn mine(e: *const Epoch, entry: Entry) bool { + return e.on and entry.epoch == e.id; + } + + /// The live allocation that starts at `addr`. + pub fn owns(e: *const Epoch, addr: usize) ?Entry { + const entry = e.live.get(addr) orelse return null; + return if (entry.freed) null else entry; + } + + fn note(e: *Epoch, addr: usize, len: usize, alignment: Alignment) bool { + e.live.put(e.meta, addr, .{ .len = len, .alignment = alignment, .epoch = if (e.on) e.id else 0 }) catch return false; + if (e.on) e.born.append(e.meta, addr) catch {}; + return true; + } + + /// Every new allocation filled with this: a core reading memory it never + /// wrote shows as a replica's divergence (test/replicas.zig, a release + /// build's heap is otherwise whatever it was). + pub var fill: ?u8 = null; + + fn alloc(ctx: *anyopaque, len: usize, alignment: Alignment, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const ptr = e.child.rawAlloc(len, alignment, ra) orelse return null; + if (fill) |b| @memset(ptr[0..len], b); + if (!e.note(@intFromPtr(ptr), len, alignment)) { + e.child.rawFree(ptr[0..len], alignment, ra); + return null; + } + return ptr; + } + + fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) bool { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const entry = e.live.getPtr(@intFromPtr(memory.ptr)) orelse return false; + // What the copy saved stays its size: the core moves instead. + if (e.on and !e.mine(entry.*)) return false; + if (!e.child.rawResize(memory, alignment, new_len, ra)) return false; + entry.len = new_len; + return true; + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const old = @intFromPtr(memory.ptr); + const entry = e.live.get(old) orelse return null; + if (e.on and !e.mine(entry)) return null; + const ptr = e.child.rawRemap(memory, alignment, new_len, ra) orelse return null; + _ = e.live.remove(old); + e.live.put(e.meta, @intFromPtr(ptr), .{ .len = new_len, .alignment = alignment, .epoch = entry.epoch }) catch {}; + if (e.mine(entry) and @intFromPtr(ptr) != old) e.born.append(e.meta, @intFromPtr(ptr)) catch {}; + return ptr; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: Alignment, ra: usize) void { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const addr = @intFromPtr(memory.ptr); + const entry = e.live.getPtr(addr) orelse return e.child.rawFree(memory, alignment, ra); + if (e.on and !e.mine(entry.*)) { + // Held by the copy until it is put back or let go. + if (!entry.freed) e.deferred.append(e.meta, addr) catch {}; + entry.freed = true; + return; + } + e.child.rawFree(memory.ptr[0..entry.len], entry.alignment, ra); + _ = e.live.remove(addr); + } + + fn reallyFree(e: *Epoch, addr: usize) void { + const entry = e.live.get(addr) orelse return; + e.child.rawFree(@as([*]u8, @ptrFromInt(addr))[0..entry.len], entry.alignment, @returnAddress()); + _ = e.live.remove(addr); + } + + fn begin(e: *Epoch) void { + std.debug.assert(!e.on); + e.id +%= 1; + if (e.id == 0) e.id = 1; + e.on = true; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + pardes.syntax.held_trees = &e.trees; + } + + /// The copy put back: what the epoch made goes, what it freed stays. + fn discard(e: *Epoch) void { + for (e.born.items) |addr| if (e.live.get(addr)) |entry| if (e.mine(entry)) e.reallyFree(addr); + for (e.deferred.items) |addr| if (e.live.getPtr(addr)) |entry| { + entry.freed = false; + }; + for (e.trees.born.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + /// The copy let go: what the epoch freed goes now. + fn keep(e: *Epoch) void { + for (e.deferred.items) |addr| e.reallyFree(addr); + for (e.trees.freed.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + fn end(e: *Epoch) void { + e.on = false; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + if (pardes.syntax.held_trees == &e.trees) pardes.syntax.held_trees = null; + } + + /// The trees of whichever core is stepped next: one in-process harness + /// steps several (syntax.held_trees is process-wide). + pub fn select(e: ?*Epoch) void { + pardes.syntax.held_trees = if (e) |x| (if (x.on) &x.trees else null) else null; + } +}; + +pub const Copy = struct { + meta: Allocator = std.heap.c_allocator, + regions: std.ArrayListUnmanaged(Region) = .empty, + bytes: std.ArrayListUnmanaged(u8) = .empty, + seen: std.AutoHashMapUnmanaged(Seen, void) = .empty, + pins: std.ArrayListUnmanaged(Pin) = .empty, + held: bool = false, + epoch: ?*Epoch = null, + /// The field being walked, for a pin's report, and whether it is one + /// of the blobs: a file's text or an undo snapshot's. + where: []const u8 = "", + pinnable: bool = false, + + const Region = struct { addr: usize, off: usize, len: usize }; + const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; + const Seen = struct { addr: usize, ty: usize }; + pub const Error = Allocator.Error; + + pub fn deinit(c: *Copy) void { + c.regions.deinit(c.meta); + c.bytes.deinit(c.meta); + c.seen.deinit(c.meta); + c.pins.deinit(c.meta); + } + + pub fn savedBytes(c: *const Copy) usize { + return c.bytes.items.len; + } + + pub fn pinnedBytes(c: *const Copy) usize { + var n: usize = 0; + for (c.pins.items) |pin| n += pin.len; + return n; + } + + /// The copy of `p`, whose allocator is `e`'s, taken: from now on what + /// it held stays where it is until the copy is put back or let go. + pub fn take(c: *Copy, e: *Epoch, p: *const Pardes) Error!void { + std.debug.assert(!c.held); + c.regions.clearRetainingCapacity(); + c.bytes.clearRetainingCapacity(); + c.seen.clearRetainingCapacity(); + c.pins.clearRetainingCapacity(); + c.epoch = e; + // A step drains these before it ends: between steps they hold + // nothing, and a copy is taken between steps. + std.debug.assert(p.effects_len == 0 and p.in_len == 0); + if (c.epoch.?.owns(@intFromPtr(p)) == null) @panic("rollback: the core is not its epoch's"); + try c.object(Pardes, p); + e.begin(); + c.held = true; + } + + /// The core as it was when the copy was taken. + pub fn restore(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + for (c.regions.items) |r| @memcpy(@as([*]u8, @ptrFromInt(r.addr))[0..r.len], c.bytes.items[r.off..][0..r.len]); + c.epoch.?.discard(); + c.held = false; + } + + /// The speculation was right: the copy is not wanted. + pub fn release(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + c.epoch.?.keep(); + c.held = false; + } + + pub fn check(c: *const Copy) void { + if (comptime !checked) return; + for (c.pins.items) |pin| if (std.hash.Wyhash.hash(0, @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]) != pin.hash) { + const now = @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]; + const entry = c.epoch.?.live.get(pin.addr); + std.debug.print("rollback: {s}, {d} bytes at {x}, pinned, was written in place: now {any}; entry {any}, epoch {d}\n", .{ pin.where, pin.len, pin.addr, now[0..32], entry, c.epoch.?.id }); + @panic("rollback: a pinned blob was written in place"); + }; + } + + fn save(c: *Copy, addr: usize, len: usize) Error!void { + if (len == 0) return; + const off = c.bytes.items.len; + try c.bytes.appendSlice(c.meta, @as([*]const u8, @ptrFromInt(addr))[0..len]); + try c.regions.append(c.meta, .{ .addr = addr, .off = off, .len = len }); + } + + /// Whether the value at `addr` of type `T` was reached before. + fn reached(c: *Copy, comptime T: type, addr: usize) Error!bool { + const key: Seen = .{ .addr = addr, .ty = @intFromPtr(@typeName(T).ptr) }; + const got = try c.seen.getOrPut(c.meta, key); + return got.found_existing; + } + + /// The allocation at `addr`, saved once, whatever reaches it. + fn allocation(c: *Copy, addr: usize) Error!?usize { + const entry = c.epoch.?.owns(addr) orelse return null; + if (try c.reached(void, addr)) return entry.len; + try c.save(addr, entry.len); + return entry.len; + } + + fn visit(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime !heapIn(T, 0)) return; + switch (@typeInfo(T)) { + .optional => |o| if (v.*) |*payload| try c.visit(o.child, payload), + .array => |a| if (comptime heapIn(a.child, 0)) for (v) |*e| try c.visit(a.child, e), + .pointer => |p| switch (p.size) { + .one => try c.object(p.child, v.*), + .slice => try c.slice(p.child, v.*), + // No length to walk by: its keeper's (a C handle's buffer). + else => {}, + }, + .@"struct" => |s| { + if (comptime walk.machinery(T)) return; + // A terminal: the authority's (its emulator is never written + // by a speculation: terminal.zig's guards). + if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return; + if (s.layout == .@"packed") return; + if (comptime @hasField(T, "items") and @hasField(T, "capacity") and s.field_names.len == 2) { + if (v.capacity == 0) return; + _ = try c.allocation(@intFromPtr(v.items.ptr)); + const E = @typeInfo(@FieldType(T, "items")).pointer.child; + if (comptime heapIn(E, 0)) for (v.items) |*e| try c.visit(E, e); + return; + } + if (comptime @hasDecl(T, "KV") and @hasDecl(T, "Hash") and @hasField(T, "metadata")) { + const m = v.metadata orelse return; + // std's header sits before the metadata, at the start + // of the one allocation. + const header = std.mem.alignForward(usize, 2 * @sizeOf(usize) + @sizeOf(u32), @alignOf(usize)); + _ = try c.allocation(@intFromPtr(m) - header); + var it = v.iterator(); + while (it.next()) |entry| { + try c.visit(@TypeOf(entry.key_ptr.*), entry.key_ptr); + try c.visit(@TypeOf(entry.value_ptr.*), entry.value_ptr); + } + return; + } + if (comptime @hasField(T, "raw") and s.field_names.len == 1 and @hasDecl(T, "load")) return; + if (comptime @hasField(T, "any_future")) return; + if (comptime @hasField(T, "bit_length") and @hasField(T, "masks") and s.field_names.len == 2) { + if (v.bit_length == 0) return; + _ = try c.allocation(@intFromPtr(v.masks) - @sizeOf(@typeInfo(@FieldType(T, "masks")).pointer.child)); + return; + } + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + // What a digest covers, the caches made from it and this + // process's own (the views not live are among it): a + // safe build's free writes over what it frees, so what + // the speculation freed would come back as that. Not the + // queues a step drains (undefined between steps), nor the + // authority's. + if (attr.@"comptime" or comptime (annotated(T, "authority", name) or (annotated(T, "local", name) and unsaved(name)))) continue; + if (comptime opaqueTo(F)) continue; + c.where = @typeName(T) ++ "." ++ name; + c.pinnable = comptime pinned(T, name); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + const len = comptime walk.lengthOf(T, name).?; + if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); + } else try c.visit(F, &@field(v, name)); + } + }, + .@"union" => switch (v.*) { + inline else => |*payload| try c.visit(@TypeOf(payload.*), payload), + }, + .error_union => |e| if (v.*) |*payload| try c.visit(e.payload, payload) else |_| {}, + else => {}, + } + } + + fn object(c: *Copy, comptime P: type, ptr: *const P) Error!void { + if (comptime walk.machinery(P)) return; + const addr = @intFromPtr(ptr); + if (try c.reached(P, addr)) return; + if (comptime @hasDecl(P, "digest") and @TypeOf(P.digest) == fn (*const P, *walk.Hasher) void) return; + if (c.epoch.?.owns(addr) != null) { + _ = try c.reached(void, addr); + try c.fields(P, ptr); + } + try c.visit(P, ptr); + } + + /// A struct's bytes, field by field where it says what its fields are: + /// all of them (this process's own too, which the epoch's frees would + /// leave pointing at nothing), but the queues a step drains and the + /// frame statistics (numbers, megabytes of them), and an array only as + /// far as its length says. + fn fields(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime @typeInfo(T) != .@"struct" or !@hasDecl(T, "replica")) return c.save(@intFromPtr(v), @sizeOf(T)); + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime") continue; + if (comptime annotated(T, "local", name) and unsaved(name)) continue; + const at = @intFromPtr(&@field(v, name)); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + try c.save(at, walk.lengthIn(v, comptime walk.lengthOf(T, name).?) * @sizeOf(@typeInfo(F).array.child)); + } else if (comptime annotatedStruct(F)) { + try c.fields(F, &@field(v, name)); + } else if (comptime @typeInfo(F) == .array and annotatedStruct(@typeInfo(F).array.child)) { + for (&@field(v, name)) |*e| try c.fields(@typeInfo(F).array.child, e); + } else try c.save(at, @sizeOf(F)); + } + } + + fn slice(c: *Copy, comptime E: type, s: []const E) Error!void { + if (s.len == 0) return; + const addr = @intFromPtr(s.ptr); + if (try c.reached([]const E, addr)) return; + if (comptime !heapIn(E, 0)) if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + const bytes = std.mem.sliceAsBytes(s); + try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); + return; + }; + _ = try c.allocation(addr); + if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e); + } +}; + +/// The blobs: what an edit puts in place of its predecessor, never writes +/// (File.setContentSpan), and frees raw (memory.freeUnfilled, freeRaw). +fn pinned(comptime T: type, comptime name: []const u8) bool { + const File = pardes.panes.File; + return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or + (T == File.Snapshot and std.mem.eql(u8, name, "content")); +} + +fn annotatedStruct(comptime T: type) bool { + return @typeInfo(T) == .@"struct" and @hasDecl(T, "replica"); +} + +/// The queues every step drains (empty between steps, asserted when a copy +/// is taken: what is in their slots is nothing), and the frame statistics +/// (numbers, and a static name). +fn unsaved(comptime name: []const u8) bool { + return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames"); +} + +fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), kind)) return false; + inline for (@field(T.replica, kind)) |n| if (comptime std.mem.eql(u8, n, name)) return true; + return false; +} + +/// A field no walk sees into (a C handle, a function): its own keeper's. +fn opaqueTo(comptime F: type) bool { + return switch (@typeInfo(F)) { + .optional => |o| opaqueTo(o.child), + .pointer => |p| p.size == .many or p.size == .c or switch (@typeInfo(p.child)) { + .@"opaque", .@"fn" => true, + else => false, + }, + else => false, + }; +} + +/// Whether a `T` can hold a pointer to the heap at all (else a walk of it +/// finds nothing to save). Deep types count as yes. +fn heapIn(comptime T: type, comptime depth: u32) bool { + if (depth > 12) return true; + return switch (@typeInfo(T)) { + .void, .null, .undefined, .bool, .int, .float, .@"enum", .error_set, .comptime_int, .comptime_float, .enum_literal, .type => false, + .optional => |o| heapIn(o.child, depth + 1), + .array => |a| a.len > 0 and heapIn(a.child, depth + 1), + .pointer => true, + .error_union => |e| heapIn(e.payload, depth + 1), + .@"struct" => |s| blk: { + if (s.layout == .@"packed") break :blk false; + for (s.field_types, s.field_attrs) |F, attr| if (!attr.@"comptime" and heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + .@"union" => |u| blk: { + for (u.field_types) |F| if (heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + else => true, + }; +} + +test "a copy put back is the core as it was; one let go keeps what was done" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const before = walk.digest(Pardes, p); + var c: Copy = .{}; + defer c.deinit(); + const typed = "ox y z"; + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + const edited = walk.digest(Pardes, p); + try std.testing.expect(edited != before); + c.restore(); + try std.testing.expectEqual(before, walk.digest(Pardes, p)); + // The same again, let go this time: what was typed stays. + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + c.release(); + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} diff --git a/src/syntax.zig b/src/syntax.zig index 985a9888..858ef6a2 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -381,10 +381,34 @@ pub fn parseTree(path: []const u8, content: []const u8) ?*anyopaque { if (!enabled) return null; const selected = (forExt(std.fs.path.extension(path)) catch return null) orelse return null; const tree = selected.parser.parseString(content, null) orelse return null; + if (held_trees) |h| h.born.append(h.meta, @ptrCast(tree)) catch {}; return @ptrCast(tree); } +/// While a replica's rollback copy is held (src/rollback.zig) a tree parsed +/// is the speculation's, and one freed is put off: trees live on the C +/// heap, which the core's allocator cannot see. Null otherwise. +pub var held_trees: ?*HeldTrees = null; +pub const HeldTrees = struct { + meta: std.mem.Allocator, + born: std.ArrayListUnmanaged(*anyopaque) = .empty, + freed: std.ArrayListUnmanaged(*anyopaque) = .empty, +}; + pub fn freeTree(tree: *anyopaque) void { + if (!enabled) return; + if (held_trees) |h| { + for (h.born.items, 0..) |t, i| if (t == tree) { + _ = h.born.swapRemove(i); + return destroyTree(tree); + }; + h.freed.append(h.meta, tree) catch destroyTree(tree); + return; + } + destroyTree(tree); +} + +pub fn destroyTree(tree: *anyopaque) void { if (!enabled) return; const t: *ts.Tree = @ptrCast(@alignCast(tree)); t.destroy(); diff --git a/src/tape.zig b/src/tape.zig index ec38deac..70609858 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -127,19 +127,40 @@ pub const Tape = struct { /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too /// (to find the step a divergence starts at), not only the host's last. every: bool = false, + /// A replica applying its own input before the authority has ordered it + /// (src/replica.zig): recorded as the authority would record it, so the + /// two can be compared, but nothing is performed. A host call or a + /// syscall stops it (`poisoned`): what the call returns is the + /// authority's to say, and the input waits for it. A read of the clock + /// or the environment (`value`) is made here, a guess the comparison + /// checks. + speculating: bool = false, + poisoned: bool = false, + /// The allocator of the core a replay makes (a replica's rollback epoch, + /// src/rollback.zig), when it is not the tape's own. + core_gpa: ?std.mem.Allocator = null, + /// Where a replay keeps what a core keeps of an input (its options), + /// for a tape that is let go; else the tape's allocator, for good. + kept_arena: ?std.heap.ArenaAllocator = null, + /// No digests recorded (a speculation's: a replica compares what it + /// was given and what it asked, from a state the authority shares). + no_digest: bool = false, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); walk.encode(T, v, &t.scratch.writer) catch @panic("tape: out of memory"); const payload = t.scratch.written(); const len: u32 = @intCast(payload.len); - t.buf.append(t.gpa, @intFromEnum(kind)) catch @panic("tape: out of memory"); + t.buf.append(t.gpa, @backingInt(kind)) catch @panic("tape: out of memory"); t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory"); t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory"); if (t.buf.items.len > 1 << 20) t.flush(); } fn flush(t: *Tape) void { + // A tape in memory (a replica's, an authority's stream) is taken + // by whoever holds it. + if (t.fd < 0) return; var at: usize = 0; while (at < t.buf.items.len) { const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at); @@ -222,7 +243,7 @@ pub const Tape = struct { t.wrap(core); }; t.record(.done, void, &{}); - if (t.depth == 0 or t.every) if (t.core) |core| { + if ((t.depth == 0 or t.every) and !t.no_digest) if (t.core) |core| { var parts: Parts = undefined; const d = digestParts(core, &parts); t.record(.digest, []const u64, &d); @@ -239,12 +260,12 @@ pub const Tape = struct { const head = t.in.take(5) catch return null; const len = std.mem.bytesToValue(u32, head[1..5]); const payload = t.in.take(len) catch return null; - return .{ .kind = @enumFromInt(head[0]), .payload = .{ .bytes = payload } }; + return .{ .kind = @fromBackingInt(@intCast(head[0])), .payload = .{ .bytes = payload } }; } fn peek(t: *const Tape) ?Kind { if (t.in.bytes.len - t.in.at < 5) return null; - return @enumFromInt(t.in.bytes[t.in.at]); + return @fromBackingInt(@intCast(t.in.bytes[t.in.at])); } fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { @@ -374,7 +395,8 @@ pub const Tape = struct { const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{}))); r.at -= @sizeOf(Tag); const kept = tag == .init or tag == .init_from_dump or tag == .restore; - const input = walk.decode(Input, r, if (kept) t.gpa else t.arena.allocator()) catch return t.diverge("a short input", .{}); + const keep = if (t.kept_arena) |*k| k.allocator() else t.gpa; + const input = walk.decode(Input, r, if (kept) keep else t.arena.allocator()) catch return t.diverge("a short input", .{}); const outer = t.applying; t.applying = switch (input) { .update => |ev| @tagName(ev), @@ -390,13 +412,14 @@ pub const Tape = struct { .init_from_dump => |d| d.opts, else => unreachable, }; - opts.image_allocator = t.gpa; - opts.pdf_allocator = t.gpa; - opts.tree_sitter_allocator = t.gpa; - opts.frame_allocator = t.gpa; + const gpa = t.core_gpa orelse t.gpa; + opts.image_allocator = gpa; + opts.pdf_allocator = gpa; + opts.tree_sitter_allocator = gpa; + opts.frame_allocator = gpa; t.core = switch (input) { - .init => Pardes.init(t.gpa, opts), - .init_from_dump => |d| pardes.dump.initFromDump(t.gpa, opts, d.bytes), + .init => Pardes.init(gpa, opts), + .init_from_dump => |d| pardes.dump.initFromDump(gpa, opts, d.bytes), else => unreachable, } catch |err| return t.diverge("init failed: {t}", .{err}); t.core.?.host = t.hostOf(t.mask); @@ -608,8 +631,21 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype } return walk.decode(R, &ret, t.arena.allocator()) catch std.mem.zeroes(R); } - const real = @field(t.real.vtable.*, name).?; const id: u16 = i; + if (t.speculating) { + // Nobody watches a replica's step: its frames are its own, drawn + // outside it (and the authority's host says so too, or the + // comparison fails). + if (comptime std.mem.eql(u8, name, "watched")) { + const r = false; + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + } + t.poisoned = true; + return poisonOf(R); + } + const real = @field(t.real.vtable.*, name).?; t.record(.host, u16, &id); t.seeFields(); const outer = !t.host_side; @@ -639,7 +675,7 @@ 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.?; const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); - const id: u16 = @as(u16, host_fns.len) + @intFromEnum(s); + 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 "?"}); @@ -651,6 +687,12 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt // The host's own (a shell's spawn asking the shell), or one inside a // site's syscall: the outer call is the record. if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + // A syscall a speculating replica must not make: its input waits for + // the authority's answer. + if (yields and t.speculating) { + t.poisoned = true; + return poisonOf(R); + } t.record(.host, u16, &id); t.seeFields(); t.host_side = true; @@ -679,7 +721,173 @@ fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_ty /// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying() bool { - return if (on) |t| t.replay else false; + return if (on) |t| t.replay or t.speculating else false; +} + +/// 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 { + return if (on) |t| t.speculating else false; +} + +/// What a speculation reached is the authority's: the input is undone and +/// waits for the authority's order. +pub fn poison() void { + if (on) |t| if (t.speculating) { + t.poisoned = true; + }; +} + +/// A harmless stand-in for what a host call or a syscall would have +/// returned to a speculation that is thrown away once it returns: an +/// error where one can be said, else zeroes. +fn poisonOf(comptime R: type) R { + return switch (@typeInfo(R)) { + .error_union => |e| if (@typeInfo(e.error_set).error_set.error_names) |errors| + (if (errors.len > 0) @field(e.error_set, errors[0]) else std.mem.zeroes(R)) + else + error.Unexpected, + .optional => null, + .void => {}, + .@"union" => |u| inline for (u.field_names, u.field_types) |name, F| { + if (comptime zeroable(F)) break @unionInit(R, name, std.mem.zeroes(F)); + } else @compileError("tape: no harmless value of " ++ @typeName(R)), + else => std.mem.zeroes(R), + }; +} + +fn zeroable(comptime T: type) bool { + return switch (@typeInfo(T)) { + .int, .float, .bool, .optional, .void => true, + .pointer => |p| p.size == .slice, + .array => |a| zeroable(a.child), + .@"struct" => |s| blk: { + for (s.field_types) |F| if (!zeroable(F)) break :blk false; + break :blk true; + }, + else => false, + }; +} + +// ---- a replica's tapes in memory (src/replica.zig) ---- + +/// A tape recorded in memory: an authority's stream, taken unit by unit. +pub fn memory(gpa: std.mem.Allocator) Tape { + return .{ .gpa = gpa, .replay = false, .scratch = .init(gpa), .arena = .init(gpa), .kept_arena = .init(gpa) }; +} + +/// What a tape in memory holds, given back (its core is gone already). +pub fn release(t: *Tape) void { + t.buf.deinit(t.gpa); + t.scratch.deinit(); + t.arena.deinit(); + if (t.kept_arena) |*k| k.deinit(); +} + +/// What was recorded since the last take; the tape's to reuse after. +pub fn taken(t: *Tape) []const u8 { + return t.buf.items; +} + +pub fn clearTaken(t: *Tape) void { + t.buf.clearRetainingCapacity(); +} + +/// The records of `bytes`, top-level inputs and all, applied to the +/// tape's core as the authority applied them, each digest checked; why +/// they diverged, or null. +pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 { + t.replay = true; + t.speculating = false; + defer t.replay = false; + t.in = .{ .bytes = bytes }; + while (t.diverged == null) { + const kind = t.peek() orelse break; + var rec = t.next().?; + switch (kind) { + .input => { + t.apply(&rec.payload); + _ = t.arena.reset(.retain_capacity); + }, + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}), + } + } + return t.diverged; +} + +/// Starts recording `t`'s core as a speculation: the host's watched +/// fields as they are now, nothing recorded yet. +pub fn beginSpeculation(t: *Tape) void { + t.replay = false; + t.speculating = true; + t.poisoned = false; + t.no_digest = true; + t.buf.clearRetainingCapacity(); + t.seeFields(); +} + +/// Whether it stayed clean (no host call, no syscall, no authority state). +pub fn endSpeculation(t: *Tape) bool { + t.speculating = false; + t.no_digest = false; + return !t.poisoned; +} + +/// Whether two tapes record the same inputs, host calls and answers, +/// their digests aside. +pub fn sameRecords(a: []const u8, b: []const u8) bool { + var x: walk.Reader = .{ .bytes = a }; + var y: walk.Reader = .{ .bytes = b }; + while (true) { + const ra = nextRecord(&x); + const rb = nextRecord(&y); + if (ra == null or rb == null) return ra == null and rb == null; + if (ra.?.kind != rb.?.kind or !std.mem.eql(u8, ra.?.payload, rb.?.payload)) return false; + } +} + +fn nextRecord(r: *walk.Reader) ?struct { kind: Kind, payload: []const u8 } { + while (true) { + const head = r.take(5) catch return null; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return null; + const kind: Kind = @fromBackingInt(@intCast(head[0])); + if (kind != .digest) return .{ .kind = kind, .payload = payload }; + } +} + +/// A tape's records, one a line (kind; an input's or a host call's name), +/// for a divergence's report. +pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { + var r: walk.Reader = .{ .bytes = bytes }; + var depth: usize = 0; + while (true) { + const head = r.take(5) catch return; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return; + const kind: Kind = @fromBackingInt(@intCast(head[0])); + if (kind == .done or kind == .ret) depth -|= 1; + w.splatByteAll(' ', depth * 2) catch {}; + switch (kind) { + .input => { + 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 {}; + }, + .host => { + const id = std.mem.bytesToValue(u16, payload[0..2]); + w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {}; + }, + else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {}, + } + w.writeByte('\n') catch {}; + if (kind == .input or kind == .host) depth += 1; + } +} + +/// The digest a replica and its authority must agree on. +pub fn digestOf(p: *const Pardes) u64 { + return walk.digest(Pardes, p); } /// PARDES_RECORD set: the tape is written from the session's first call. diff --git a/src/terminal.zig b/src/terminal.zig index e737a831..81fae5db 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -417,9 +417,18 @@ pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); } +/// A replica's speculation leaves an emulator be: it is the authority's +/// (docs/next-daemon.md, phase 5), so the input waits for its order. +fn authorityOnly() bool { + if (!pardes.tape.speculating()) return false; + pardes.tape.poison(); + return true; +} + /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). pub fn scrollGrid(pane: *Pane, delta: i32) void { if (comptime !enabled) return; + if (authorityOnly()) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); } @@ -434,6 +443,7 @@ pub fn atBottom(pane: *const Pane) bool { /// Snap the viewport back onto live output. pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; + if (authorityOnly()) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.active); } @@ -442,6 +452,7 @@ pub fn followOutput(pane: *Pane) void { /// a scrollback; the next resize retries with the same numbers. pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) void { if (comptime !enabled) return; + if (authorityOnly()) return; const state = pane.terminal orelse return; if (cols != pane.cols) for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); @@ -643,6 +654,7 @@ pub fn screenTextLen(pane: *Pane) usize { /// Release the emulator's heap. The Pane allocation itself is the core's. pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { if (comptime !enabled) return; + if (authorityOnly()) return; const state = pane.terminal orelse return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); @@ -654,6 +666,7 @@ pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { /// Allocate the live emulator half of a terminal pane. Slot ownership, serial /// assignment, and spawn effects remain core lifecycle invariants. pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { + if (authorityOnly()) return error.OutOfMemory; const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); if (comptime !enabled) return pane; @@ -674,6 +687,9 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; + // Every field its default (create leaves them what the heap had, which + // a digest then reads: two replicas must agree, src/replica.zig). + state.reply_dropped = 0; state.mouse_cell = null; state.images = if (images_enabled) .{} else {}; if (comptime images_enabled) TerminalImages.installPngDecoder(); @@ -900,6 +916,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. if (comptime !enabled) return; if (pane.terminal == null) return; + if (authorityOnly()) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; diff --git a/src/walk.zig b/src/walk.zig index dc1664cb..3e482e9a 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -42,7 +42,7 @@ pub fn fieldDigests(comptime T: type, v: *const T, out: []u64) []u64 { if (attr.@"comptime" or comptime excluded(T, name)) continue; var h = Hasher.init(0); if (comptime lengthOf(T, name)) |len| { - hashSlice(@typeInfo(F).array.child, @field(v, name)[0..lengthIn(v, len)], &h); + hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], &h); } else hash(F, &@field(v, name), &h); out[n] = h.final(); n += 1; @@ -63,7 +63,7 @@ pub fn fieldNames(comptime T: type) []const []const u8 { } /// Types that are machinery, never state: an allocator, the Io, a lock. -fn machinery(comptime T: type) bool { +pub fn machinery(comptime T: type) bool { inline for (.{ std.mem.Allocator, std.Io, std.Io.Mutex, std.Io.Condition, std.Thread, std.heap.ArenaAllocator }) |M| if (T == M) return true; return false; } @@ -92,7 +92,7 @@ fn opaqueTo(comptime F: type) bool { }; } -fn excluded(comptime T: type, comptime name: []const u8) bool { +pub fn excluded(comptime T: type, comptime name: []const u8) bool { @setEvalBranchQuota(100_000); if (!@hasDecl(T, "replica")) return false; inline for (.{ "local", "derived", "authority" }) |kind| { @@ -104,12 +104,22 @@ fn excluded(comptime T: type, comptime name: []const u8) bool { } /// How far the field `len` names says its array is written (null: none). -fn lengthIn(v: anytype, comptime len: []const u8) usize { +/// What a `len`-cut field holds: a fixed array's, or a slice's room (a +/// selection's ranges, made once and used `nsel` deep). +fn Element(comptime F: type) type { + return switch (@typeInfo(F)) { + .array => |a| a.child, + .pointer => |p| p.child, + else => @compileError("walk: a `len` cuts an array or a slice, not " ++ @typeName(F)), + }; +} + +pub fn lengthIn(v: anytype, comptime len: []const u8) usize { const n = @field(v, len); return if (@typeInfo(@TypeOf(n)) == .optional) (if (n) |k| k else 0) else n; } -fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 { +pub fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 { @setEvalBranchQuota(100_000); if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "len")) return null; inline for (T.replica.len) |pair| if (comptime std.mem.eql(u8, pair[0], name)) return pair[1]; @@ -134,7 +144,7 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { .bool => h.update(&.{@intFromBool(v.*)}), .int => hashInt(T, v.*, h), .float => h.update(std.mem.asBytes(v)), - .@"enum" => |e| hashInt(e.tag_type, @intFromEnum(v.*), h), + .@"enum" => |e| hashInt(e.tag_type, @backingInt(v.*), h), .optional => |o| if (v.*) |*payload| { h.update(&.{1}); hash(o.child, payload, h); @@ -167,7 +177,7 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { if (attr.@"comptime" or comptime excluded(T, name)) continue; if (comptime opaqueTo(F)) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is " ++ @typeName(F) ++ ", which no walk sees into: say what it is (`replica`)"); if (comptime lengthOf(T, name)) |len| { - hashSlice(@typeInfo(F).array.child, @field(v, name)[0..lengthIn(v, len)], h); + hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], h); } else hash(F, &@field(v, name), h); } }, diff --git a/test/replicas.zig b/test/replicas.zig new file mode 100644 index 00000000..cdb161e8 --- /dev/null +++ b/test/replicas.zig @@ -0,0 +1,327 @@ +//! pardes-replicas: one authority and several replicas of a session in one +//! process (src/replica.zig), each client typing at once over its own +//! delayed, jittered link, as monkey.zig's scripts type at a tty, each in a +//! view of its own (src/views.zig) over the text they share; `--shared` +//! puts them all in one, where they clash at every key: a stress of +//! correctness, not a picture of a session. +//! +//! zig build replicas -Dplatform=tty -- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207] [--first K] [--shared] +//! +//! Gates, per seed and RTT: no replica diverges (every confirmed unit's +//! digests agree as it is replayed), and at quiescence every replica's +//! digest is the authority's. Reported per RTT: how inputs went (applied at +//! once, stopped at a host call, left to the authority), how units were +//! taken (fast path, rollback for a misprediction or another's input), and +//! what a speculative apply, a copy and a rollback cost against a frame. +const std = @import("std"); +const libc = std.c; +const pardes = @import("pardes"); +const replica = pardes.replica; +const Key = pardes.Key; + +const gpa = std.heap.c_allocator; +var trace = false; +const frame_us = 16_667; + +/// The authority's host: no frames drawn (a replica draws its own), and a +/// pty that echoes what it is written, 2 ms later, as a shell's. +const Host = struct { + echo: std.ArrayListUnmanaged(struct { pane: u8, bytes: []u8 }) = .empty, + + fn watched(_: ?*anyopaque) bool { + return false; + } + + fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { + const h: *Host = @ptrCast(@alignCast(ctx.?)); + h.echo.append(gpa, .{ .pane = pane, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; + } + + fn ignore(_: ?*anyopaque, _: []const u8) void {} + + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite, .set_clipboard = ignore }; + + fn host(h: *Host) pardes.Host { + return .{ .ctx = h, .vtable = &vtable }; + } +}; + +const Kind = enum { input, to_authority, to_replica, echo }; + +const Msg = struct { + at: u64, + n: u64, + kind: Kind, + who: u16, + bytes: []u8, + + fn order(_: void, a: Msg, b: Msg) std.math.Order { + if (a.at != b.at) return std.math.order(a.at, b.at); + return std.math.order(a.n, b.n); + } +}; + +const Link = struct { + last: u64 = 0, + /// When a message sent at `now` arrives: half the RTT, jitter on top, + /// and never before the one sent ahead of it (a stream keeps order). + fn arrive(l: *Link, random: std.Random, now: u64, one_way: u64, jitter: u64) u64 { + const at = @max(l.last + 1, now + one_way + random.uintLessThan(u64, jitter + 1)); + l.last = at; + return at; + } +}; + +const Totals = struct { + inputs: u64 = 0, + speculated: u64 = 0, + stopped: u64 = 0, + unspeculated: u64 = 0, + units: u64 = 0, + fast: u64 = 0, + mispredicted: u64 = 0, + foreign: u64 = 0, + rebases: u64 = 0, + copies: u64 = 0, + copy_bytes: u64 = 0, + divergences: u64 = 0, + unequal: u64 = 0, + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + + fn add(t: *Totals, s: *const replica.Stats) !void { + t.inputs += s.inputs; + t.speculated += s.speculated; + t.stopped += s.stopped; + t.unspeculated += s.unspeculated; + t.units += s.units; + t.fast += s.fast; + t.mispredicted += s.mispredicted; + t.foreign += s.foreign; + t.rebases += s.rebases; + t.copies += s.copies; + t.copy_bytes += s.copy_bytes; + try t.spec_ns.appendSlice(gpa, s.spec_ns.items); + try t.copy_ns.appendSlice(gpa, s.copy_ns.items); + try t.rollback_ns.appendSlice(gpa, s.rollback_ns.items); + } +}; + +/// One client's next input: monkey.zig's mix, as events. +fn nextEvent(random: std.Random, buf: *[64]u8) pardes.Event { + const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; + const normal = "hjklwbe0$xpuyoOiaAIdcv.nN%sC"; + switch (random.uintLessThan(u8, 40)) { + 0...15 => { + buf[0] = typed[random.uintLessThan(usize, typed.len)]; + return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; + }, + 16...27 => { + buf[0] = normal[random.uintLessThan(usize, normal.len)]; + return .{ .key = .{ .cp = buf[0], .text = buf[0..1] } }; + }, + 28...31 => return .{ .key = .{ .cp = Key.escape } }, + 32 => return .{ .key = .{ .cp = Key.enter } }, + 33 => return .{ .key = .{ .cp = Key.backspace } }, + 34 => return .{ .key = .{ .cp = switch (random.uintLessThan(u8, 4)) { + 0 => Key.up, + 1 => Key.down, + 2 => Key.left, + else => Key.right, + } } }, + 35 => { + const n = random.intRangeAtMost(usize, 1, 24); + for (buf[0..n]) |*c| c.* = typed[random.uintLessThan(usize, typed.len)]; + return .{ .paste = buf[0..n] }; + }, + 36 => return .{ .command = switch (random.uintLessThan(u8, 4)) { + 0 => "Newcol", + 1 => "Zerox", + 2 => "Undo", + else => "Redo", + } }, + 37 => return .{ .resize = .{ .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) } }, + 38 => return .{ .mouse = .{ .button = .left, .kind = .press, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + else => return .{ .mouse = .{ .button = .left, .kind = .release, .col = random.uintLessThan(u16, 80), .row = random.uintLessThan(u16, 24) } }, + } +} + +fn writeFile(path: [:0]const u8, lines: usize) !void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = libc.close(fd); + var line: [80]u8 = undefined; + for (0..lines) |n| { + const text = try std.fmt.bufPrint(&line, "const value_{d:0>4} = {d}; // a line to edit\n", .{ n, n *% 7919 }); + if (libc.write(fd, text.ptr, text.len) != @as(isize, @intCast(text.len))) return error.WriteFailed; + } +} + +const Run = struct { seed: u64, rtt_ms: u64, steps: usize, clients: u16, shared: bool }; + +fn run(r: Run, dir: []const u8, totals: *Totals) !void { + if (trace) std.debug.print("seed {d} rtt {d} ms\n", .{ r.seed, r.rtt_ms }); + var prng = std.Random.DefaultPrng.init(r.seed *% 0x9e3779b97f4a7c15 +% r.rtt_ms); + const random = prng.random(); + var host: Host = .{}; + defer { + for (host.echo.items) |e| gpa.free(e.bytes); + host.echo.deinit(gpa); + } + var paths: [3][256:0]u8 = undefined; + for (&paths, 0..) |*p, i| { + const path = try std.fmt.bufPrintSentinel(p, "{s}/f{d}.zig", .{ dir, i }, 0); + try writeFile(path, 60 + 40 * i); + } + // The tty's own boot (a raw shell, a text pane under it), then three + // files looked at: keys go to the last, a pane's shell echoes. + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, host.host()); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + var reps: [8]*replica.Replica = undefined; + for (reps[0..r.clients], 1..) |*rp, id| rp.* = try replica.Replica.init(gpa, @intCast(id), first); + defer for (reps[0..r.clients]) |rp| rp.deinit(); + for (&paths) |*path| { + var line: [300]u8 = undefined; + const unit = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{std.mem.sliceTo(path, 0)}) }); + for (reps[0..r.clients]) |rp| rp.confirm(unit); + } + // Each client a view of its own (src/views.zig), the first the + // session's; or, --shared, every client in the one. + if (!r.shared) for (reps[1..r.clients], 2..) |rp, id| { + const opened = try a.openView(@intCast(id)); + rp.view = opened.view; + for (reps[0..r.clients]) |each| each.confirm(opened.unit); + }; + var queue: std.PriorityQueue(Msg, void, Msg.order) = .empty; + defer queue.deinit(gpa); + var n: u64 = 0; + const one_way = r.rtt_ms * 1000 / 2; + const jitter = one_way / 10; + var up: [8]Link = @splat(.{}); + var down: [8]Link = @splat(.{}); + var left: [8]usize = @splat(r.steps); + // Each client's first input, soon. + for (0..r.clients) |c| { + try queue.push(gpa, .{ .at = random.uintLessThan(u64, 200_000), .n = n, .kind = .input, .who = @intCast(c + 1), .bytes = &.{} }); + n += 1; + } + while (queue.pop()) |m| { + defer if (m.bytes.len > 0) gpa.free(m.bytes); + switch (m.kind) { + .input => { + const c = m.who - 1; + var buf: [64]u8 = undefined; + const ev = nextEvent(random, &buf); + const before = reps[c].stats; + const msg = try gpa.dupe(u8, try reps[c].input(ev)); + if (trace) std.debug.print("{d} ms client {d}: {t} {s} -> {s}\n", .{ m.at / 1000, m.who, ev, switch (ev) { + .key => |k| k.text, + .paste => |t| t, + .command => |t| t, + else => "", + }, if (reps[c].stats.speculated > before.speculated) "applied" else if (reps[c].stats.stopped > before.stopped) "STOPPED" else if (reps[c].stats.unspeculated > before.unspeculated) "view" else "waits" }); + try queue.push(gpa, .{ .at = up[c].arrive(random, m.at, one_way, jitter), .n = n, .kind = .to_authority, .who = m.who, .bytes = msg }); + n += 1; + left[c] -= 1; + if (left[c] > 0) { + // A typist: keys 50-200 ms apart, a pause now and then. + const gap: u64 = if (random.uintLessThan(u8, 12) == 0) random.intRangeAtMost(u64, 300_000, 1_500_000) else random.intRangeAtMost(u64, 50_000, 200_000); + try queue.push(gpa, .{ .at = m.at + gap, .n = n, .kind = .input, .who = m.who, .bytes = &.{} }); + n += 1; + } + }, + .to_authority, .echo => { + const unit = if (m.kind == .echo) + try a.local(.{ .output = .{ .pane = @intCast(m.who), .bytes = m.bytes } }) + else + try a.take(m.bytes); + for (0..r.clients) |c| { + const copy = try gpa.dupe(u8, unit); + try queue.push(gpa, .{ .at = down[c].arrive(random, m.at, one_way, jitter), .n = n, .kind = .to_replica, .who = @intCast(c + 1), .bytes = copy }); + n += 1; + } + for (host.echo.items) |e| { + try queue.push(gpa, .{ .at = m.at + 2_000, .n = n, .kind = .echo, .who = e.pane, .bytes = e.bytes }); + n += 1; + } + host.echo.clearRetainingCapacity(); + }, + .to_replica => { + const rp = reps[m.who - 1]; + rp.confirm(m.bytes); + rp.settle(); + }, + } + } + const want = replica.digestOf(a); + for (reps[0..r.clients], 1..) |rp, id| { + try totals.add(&rp.stats); + if (rp.diverged) |why| { + totals.divergences += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d} diverged: {s}\n", .{ r.seed, r.rtt_ms, id, why }); + } else if (!rp.quiet() or rp.digest() != want) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: replica {d} at quiescence: {s}\n", .{ r.seed, r.rtt_ms, id, if (rp.quiet()) "another digest" else "inputs still pending" }); + } + } +} + +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)]; +} + +pub fn main(init: std.process.Init) !void { + const args = try init.minimal.args.toSlice(init.arena.allocator()); + var seeds: u64 = 4; + var first: u64 = 0; + var steps: usize = 120; + var clients: u16 = 3; + var rtts: []const u64 = &.{ 30, 80, 207 }; + var shared = false; + var rtt_buf: [8]u64 = undefined; + var i: usize = 1; + while (i < args.len and std.mem.eql(u8, args[i], "--shared")) : (i += 1) shared = true; + while (i + 1 < args.len) : (i += 2) { + while (i < args.len and std.mem.eql(u8, args[i], "--shared")) : (i += 1) shared = true; + if (i + 1 >= args.len) break; + const name = args[i]; + const value = args[i + 1]; + if (std.mem.eql(u8, name, "--seeds")) seeds = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--first")) first = try std.fmt.parseInt(u64, value, 10) else if (std.mem.eql(u8, name, "--steps")) steps = try std.fmt.parseInt(usize, value, 10) else if (std.mem.eql(u8, name, "--clients")) clients = @min(8, try std.fmt.parseInt(u16, value, 10)) else if (std.mem.eql(u8, name, "--rtt")) { + var it = std.mem.tokenizeScalar(u8, value, ','); + var k: usize = 0; + while (it.next()) |v| : (k += 1) rtt_buf[k] = try std.fmt.parseInt(u64, v, 10); + rtts = rtt_buf[0..k]; + } else return error.UnknownOption; + } + trace = libc.getenv("PARDES_REPLICA_TRACE") != null; + if (libc.getenv("PARDES_REPLICA_FILL")) |b| pardes.rollback.Epoch.fill = std.fmt.parseInt(u8, std.mem.span(b), 0) catch 0xff; + var dir_buf: [256]u8 = undefined; + const tmp = std.mem.span(libc.getenv("TMPDIR") orelse "/tmp"); + const dir = try std.fmt.bufPrint(&dir_buf, "{s}/replicas-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) }); + try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); + defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; + var failed = false; + std.debug.print("{d} seeds x {d} inputs from each of {d} clients, {s}; costs in us (frame {d})\n", .{ seeds, steps, clients, if (shared) "sharing one view" else "a view each", frame_us }); + std.debug.print(" rtt inputs at-once stopped view units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); + for (rtts) |rtt| { + var t: Totals = .{}; + for (first..first + seeds) |seed| try run(.{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .shared = shared }, dir, &t); + const rollbacks = t.mispredicted + t.foreign; + std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>4} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ + rtt, t.inputs, t.speculated, + t.stopped, t.unspeculated, t.units, + t.fast, rollbacks, t.mispredicted, + t.foreign, @as(f64, @floatFromInt(rollbacks)) * 100 / @as(f64, @floatFromInt(@max(t.inputs, 1))), t.rebases, + pct(t.spec_ns.items, 50) / 1000, pct(t.spec_ns.items, 99) / 1000, pct(t.copy_ns.items, 50) / 1000, + pct(t.copy_ns.items, 99) / 1000, t.copy_bytes / @max(t.copies, 1) / 1024, pct(t.rollback_ns.items, 50) / 1000, + pct(t.rollback_ns.items, 99) / 1000, t.divergences, t.unequal, + }); + if (t.divergences > 0 or t.unequal > 0) failed = true; + } + if (failed) std.process.exit(1); +} diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig index 721c6f13..38fe011d 100644 --- a/test/rollback_bench.zig +++ b/test/rollback_bench.zig @@ -1,14 +1,11 @@ -//! What a replica's rollback copy would cost (docs/next-daemon.md, open -//! point 3), and what a fork of the authority stalls it for (open point 5). -//! A typical session and a big one are built in process; for each: -//! - the state the walker visits (src/walk.zig's digest walk, which is -//! what speculation can write: views, buffers, registers, counters, -//! terminal metadata), in bytes; -//! - the part of it that is pinned blobs: a file's text and its undo -//! snapshots, which an edit replaces rather than changes -//! (File.setContentSpan), so a copy keeps a reference; -//! - the walk's time, the blobs' share of it, and a memcpy of the rest: -//! a walker copy costs about the walk plus that write. +//! What a replica's rollback copy costs (docs/next-daemon.md, open point +//! 3, src/rollback.zig), and what a fork of the authority stalls it for +//! (open point 5). A typical session and a big one are built in process, on +//! a rollback epoch's allocator as a replica's core is; for each: +//! - the copy taken and let go: its time, the bytes it saves, the bytes +//! it pins (a file's text, its undo snapshots); +//! - a key that edits a file (`x`) applied on top, as a speculation, and +//! the copy put back: the apply's time and the restore's. //! Then a fork of the big one, child gone at once, and the authority's //! copy-on-write after it. //! @@ -69,8 +66,8 @@ fn writeFile(path: []const u8, lines: usize) !void { } } -fn build(s: Session, dir: []const u8) !*pardes.Pardes { - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); +fn build(s: Session, dir: []const u8, a: std.mem.Allocator) !*pardes.Pardes { + const core = try pardes.Pardes.init(a, .{ .tty_only = true, .image_allocator = a, .pdf_allocator = a, .tree_sitter_allocator = a, .frame_allocator = a }); core.update(.{ .resize = .{ .cols = 200, .rows = 60 } }); pump(core); // The shell: pane 0 of a tty boot, its output fed as a pty's. @@ -112,36 +109,6 @@ fn build(s: Session, dir: []const u8) !*pardes.Pardes { return core; } -/// Where a file's text and its undo snapshots are held: what a copy pins. -fn blobs(core: *pardes.Pardes, out: *std.ArrayList(*[]u8)) !usize { - var total: usize = 0; - for (core.panes) |slot| if (slot) |pane| if (pane.file) |*f| { - try out.append(gpa, &f.content); - for (f.history.undo[0..f.history.undo_len]) |*snap| try out.append(gpa, &snap.content); - for (f.history.redo[0..f.history.redo_len]) |*snap| try out.append(gpa, &snap.content); - }; - for (out.items) |b| total += b.len; - return total; -} - -fn walkNs(core: *const pardes.Pardes) u64 { - var t: [reps]u64 = undefined; - var sink: u64 = 0; - for (&t) |*x| { - const t0 = nowNs(); - sink +%= walk.digest(pardes.Pardes, core); - x.* = nowNs() - t0; - } - std.mem.doNotOptimizeAway(sink); - return median(&t); -} - -fn walked(core: *const pardes.Pardes) usize { - var h = walk.Hasher.init(0); - walk.hash(pardes.Pardes, core, &h); - return h.total_len; -} - fn median(samples: []u64) u64 { std.mem.sort(u64, samples, {}, std.sort.asc(u64)); return samples[samples.len / 2]; @@ -161,49 +128,44 @@ pub fn main(init: std.process.Init) !void { const dir = try std.fmt.bufPrint(&dir_buf, "{s}/rollback-bench-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) }); try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir); defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {}; - std.debug.print("session panes walked MB blobs MB copy KB walk ms walk ms, blobs out memcpy ms copy ms (est.)\n", .{}); + std.debug.print("session panes copy KB pinned MB take ms release ms edit ms restore ms\n", .{}); var big: ?*pardes.Pardes = null; - for (sessions) |s| { - const core = try build(s, dir); - var list: std.ArrayList(*[]u8) = .empty; - defer list.deinit(gpa); - const blob_bytes = try blobs(core, &list); - const all = walked(core); - const walk_ns = walkNs(core); - // The blobs taken out, as a copy pins them: the walk of what a copy - // writes, then put back. - const kept = try gpa.alloc([]u8, list.items.len); - defer gpa.free(kept); - for (list.items, kept) |b, *k| { - k.* = b.*; - b.* = &.{}; + var epochs: [sessions.len]*pardes.rollback.Epoch = undefined; + for (sessions, &epochs) |s, *e| { + e.* = try gpa.create(pardes.rollback.Epoch); + e.*.* = .{}; + const core = try build(s, dir, e.*.allocator()); + var c: pardes.rollback.Copy = .{}; + defer c.deinit(); + var take: [reps]u64 = undefined; + var release: [reps]u64 = undefined; + for (&take, &release) |*x, *y| { + const t0 = nowNs(); + try c.take(e.*, core); + const t1 = nowNs(); + c.release(); + x.* = t1 - t0; + y.* = nowNs() - t1; } - const copy_bytes = walked(core); - const copy_walk_ns = walkNs(core); - for (list.items, kept) |b, k| b.* = k; - const src = try gpa.alloc(u8, @max(copy_bytes, 1)); - defer gpa.free(src); - const dst = try gpa.alloc(u8, @max(copy_bytes, 1)); - defer gpa.free(dst); - @memset(src, 1); - @memset(dst, 0); - var t: [reps]u64 = undefined; - for (&t) |*x| { + var edit: [reps]u64 = undefined; + var restore: [reps]u64 = undefined; + for (&edit, &restore) |*x, *y| { + try c.take(e.*, core); const t0 = nowNs(); - @memcpy(dst, src); - std.mem.doNotOptimizeAway(dst.ptr); - x.* = nowNs() - t0; + key(core, 'x', "x"); + const t1 = nowNs(); + c.restore(); + x.* = t1 - t0; + y.* = nowNs() - t1; } - const memcpy_ns = median(&t); var panes: usize = 0; for (core.panes) |slot| panes += @intFromBool(slot != null); const ms = 1e-6; - std.debug.print("{s:<8} {d:>5} {d:>9.1} {d:>8.1} {d:>7.1} {d:>7.3} {d:>18.3} {d:>9.3} {d:>14.3}\n", .{ - s.name, panes, - @as(f64, @floatFromInt(all)) / 1e6, @as(f64, @floatFromInt(blob_bytes)) / 1e6, - @as(f64, @floatFromInt(copy_bytes)) / 1e3, @as(f64, @floatFromInt(walk_ns)) * ms, - @as(f64, @floatFromInt(copy_walk_ns)) * ms, @as(f64, @floatFromInt(memcpy_ns)) * ms, - @as(f64, @floatFromInt(copy_walk_ns + memcpy_ns)) * ms, + std.debug.print("{s:<8} {d:>5} {d:>7.1} {d:>9.1} {d:>7.3} {d:>10.3} {d:>7.3} {d:>10.3}\n", .{ + s.name, panes, + @as(f64, @floatFromInt(c.savedBytes())) / 1e3, @as(f64, @floatFromInt(c.pinnedBytes())) / 1e6, + @as(f64, @floatFromInt(median(&take))) * ms, @as(f64, @floatFromInt(median(&release))) * ms, + @as(f64, @floatFromInt(median(&edit))) * ms, @as(f64, @floatFromInt(median(&restore))) * ms, }); if (big) |b| b.deinit(); big = core; -- cgit v1.3 From 349790339897466f323ff28748916aa48aa344f7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:00:18 -0300 Subject: keys: Esc in a column or the workspace tag in normal mode gives the keyboard back to a pane, as Shift-Esc does, instead of running Last underneath it Co-Authored-By: Claude Opus 5.5 --- src/normal.zig | 1 + src/pardes.zig | 15 +++++++++++++++ 2 files changed, 16 insertions(+) (limited to 'src/pardes.zig') diff --git a/src/normal.zig b/src/normal.zig index 25e2deec..5bc24b08 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -659,6 +659,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void t.select = false; // typed again by a macro, Esc is only helix's if (p.macro.replaying > 0) return; + tagline.exitHeader(p); // out of a header too, as Shift-Esc return exec.runBuiltin(p, .Last, p.active, "", null); }, .goto => |go| switch (go.target) { diff --git a/src/pardes.zig b/src/pardes.zig index 5d889c86..eb09b1f2 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -8851,6 +8851,21 @@ test "Shift-Esc backs out to the previous pane in every mode, never into raw" { try std.testing.expect(p.active != 0); } +test "Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { + if (platform == .web) return; + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); + defer p.deinit(); + p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a pane to go back to + for ([_]?usize{ 0, null }) |column| { + tagline.enterHeader(p, column); + try std.testing.expect(tagline.focusedHeader(p) != null); + const was = p.active; + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(tagline.focusedHeader(p) == null); + try std.testing.expect(p.active != was); + } +} + test "window mode moves an outline and focuses it on Enter, Esc or a label" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 30 }); defer p.deinit(); -- cgit v1.3 From 70de5ec5ff3dad01afcc45244ad5e5a481486da6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 17:01:35 -0300 Subject: dump: one walker dump of the whole session, by name across builds, for Restore, -l, Handover and the store; dump.zig's hand-written schema gone A dump is walk.zig's walk of the core with a table of every type it reaches (fields, enum and union names, number widths), read back by name; terminals are ghostty's snapshot (screens, history, kitty images); a failed load frees what it made. Co-Authored-By: Claude Opus 5.5 --- build.zig | 3 + build/snap.zig | 10 +- docs/typ/building.typ | 4 +- docs/typ/reference.typ | 35 +- src/File.zig | 387 --------------- src/Handover.zig | 112 +++-- src/Output.zig | 34 +- src/Pipe.zig | 4 + src/Query.zig | 9 +- src/Store.zig | 57 +-- src/builtins.zig | 2 +- src/dump.zig | 1176 +++++++++++----------------------------------- src/file_watch.zig | 57 +-- src/image.zig | 63 --- src/ninep/ctl.zig | 64 +-- src/ninep/events.zig | 24 +- src/panes.zig | 4 +- src/pardes.zig | 55 +-- src/pdf.zig | 4 +- src/pdf_view.zig | 14 + src/tagline.zig | 8 - src/tape.zig | 40 +- src/terminal.zig | 316 ++++++------- src/walk.zig | 1120 ++++++++++++++++++++++++++++++++++++++++++- test/fs.py | 5 +- test/fs_namespace.zig | 44 +- test/kitty.zig | 6 +- test/monkey.zig | 9 +- test/output.zig | 17 - test/panes.zig | 61 +-- test/pdf.zig | 39 -- test/snapshot.zig | 9 +- test/snapshots/dump.snap | 9 +- 33 files changed, 1851 insertions(+), 1950 deletions(-) (limited to 'src/pardes.zig') diff --git a/build.zig b/build.zig index 1a5dd200..962b6322 100644 --- a/build.zig +++ b/build.zig @@ -1950,6 +1950,9 @@ pub fn build(b: *std.Build) void { run_shell_test.has_side_effects = true; unit_step.dependOn(&run_shell_test.step); if (unit_match) |check| check.addArtifactArg(shell_test); + // The native shell's tests alone, without the perf gate's + // ReleaseFast build: what a -Dtest-filter iteration wants. + b.step("shell-test", "run the native shell's unit tests (src/main.zig's)").dependOn(&run_shell_test.step); const run_profile = b.addRunArtifact(unit_profile); run_profile.addArtifactArg(shell_test); run_profile.addPassthruArgs(); diff --git a/build/snap.zig b/build/snap.zig index d74767d3..c1a26e64 100644 --- a/build/snap.zig +++ b/build/snap.zig @@ -69,19 +69,21 @@ pub fn addTty(b: *std.Build, opts: struct { // The replay-divergence gate (src/tape.zig, docs/next-daemon.md): every // snapshot script and `replay-seeds` monkey seeds with each session's // tape recorded, each replayed into two replicas at different - // addresses and one that never draws, every digest checked. + // addresses and one that never draws, every digest checked; and at each + // session's end its dump loaded, which must be that session, digest for + // digest, and dump again to the same bytes (phase 2). const tapes = ".zig-cache/tapes"; const run_replay = b.addRunArtifact(snap); run_replay.addArtifactArg(opts.exe); run_replay.addPrefixedFileArg("--lspmock=", lspmock.getEmittedBin()); - run_replay.addArg("--tape=" ++ tapes); + run_replay.addArgs(&.{ "--tape=" ++ tapes, "--dump" }); run_replay.setCwd(b.path(".")); run_replay.has_side_effects = true; const seeds = b.option(u32, "replay-seeds", "monkey seeds `zig build replay` records and replays (default 20)") orelse 20; const run_monkey_replay = b.addRunArtifact(monkey); run_monkey_replay.addArtifactArg(snap); run_monkey_replay.addArtifactArg(opts.exe); - run_monkey_replay.addArgs(&.{ b.fmt("{d}", .{seeds}), "70", "--tape=" ++ tapes ++ "/monkey", "--out=" ++ tapes ++ "/monkey" }); + run_monkey_replay.addArgs(&.{ b.fmt("{d}", .{seeds}), "70", "--tape=" ++ tapes ++ "/monkey", "--dump", "--out=" ++ tapes ++ "/monkey" }); run_monkey_replay.setCwd(b.path(".")); run_monkey_replay.has_side_effects = true; // One pardes-snap run at a time (its lock): the scripts, then the seeds. @@ -95,7 +97,7 @@ pub fn addTty(b: *std.Build, opts: struct { run_views_replay.setCwd(b.path(".")); run_views_replay.has_side_effects = true; run_views_replay.step.dependOn(&run_monkey_replay.step); - b.step("replay", "record every snapshot script's and -Dreplay-seeds monkey seeds' tapes (one terminal and three) and require each to replay without diverging").dependOn(&run_views_replay.step); + b.step("replay", "record every snapshot script's and -Dreplay-seeds monkey seeds' tapes (one terminal and three) and require each to replay without diverging, and each session's dump to load to its digest").dependOn(&run_views_replay.step); const monkey_tests = b.addTest(.{ .root_module = monkey.root_module }); b.step("monkey-test", "test the monkey's generator and panic detector").dependOn(&b.addRunArtifact(monkey_tests).step); const run_driver_test = b.addRunArtifact(snap); diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 00088d7b..e42702c6 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -19,7 +19,7 @@ builds MuPDF and is on by default (`-Dmupdf=false` drops it). 9P over QUIC [`zig build`], [the terminal shell and the SDL window together, *installed into `~/.local/bin`* (`pardes`, `pardes-gui`, and `pardes-v9fs`, the #word("Tty9p") helper)], [`zig build -Dplatform=tty`], [the terminal shell alone], [`zig build -Dplatform=gui`], [the SDL3 window alone], - [`zig build web -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=`], [a freestanding wasm core plus vanilla JavaScript (`docs/web.md`)], + [`zig build web -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=`], [a freestanding wasm core plus vanilla JavaScript (`docs/web.md`)], [`zig build -Dplatform=macos`], [an AppKit and CoreText app over a static `libpardes.a` (`docs/macos.md`)], [`zig build -Dplatform=esp32p4`], [a freestanding riscv32 editor object for the ESP32-P4, with a 384 KiB heap], ) @@ -59,7 +59,7 @@ there; its namespace is in `src/esp32p4_gpio.zig`. [`-Dprebuilt-shaders`], [bool: embed the committed SPIR-V; on for a bare `zig build`, off with `-Dplatform`; `zig build shaders` refreshes it with glslc], [`-Dtracy`], [path to a Tracy checkout; off], [`-Dmacos-identity`], [codesigning identity for `pardes.app`; `-` (ad hoc)], - [`-Ddump`], [a `dump.zon` to embed in the web shell], + [`-Ddump`], [a Dump to embed in the web shell], [`-Dtest-filter`], [run only tests whose name contains it; a filter that matches nothing fails], [`-Dtest-rebuild`], [bool: fresh Zig test compilation], [`-Dhelix-harness`], [reference executable for live differential tests; `HX_HARNESS`, else `hx-harness` on `PATH`], diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index ce2a45f1..2e395cc6 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -368,22 +368,20 @@ Writes take settings and session builtins (`scope = .session` in #word("Delcol") and a pane's `get` refuse the same way with their own word, except that #word("Delcol") opens no `+Unsaved` pane: it names the panes only in its `unsaved` records and its notice. -- #word("Dump") writes `pardes--