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/config.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/config.zig') diff --git a/src/config.zig b/src/config.zig index 988f634d..66b11c72 100644 --- a/src/config.zig +++ b/src/config.zig @@ -217,8 +217,8 @@ pub const leader_path = paths: { table.set(.Unmount, null); } if (pardes.can_attach) { - table.set(.Attach, "sa"); table.set(.Detach, "sD"); + table.set(.Adopt, null); } if (builtins.Peek.enabled) { table.set(.Peek, null); @@ -2306,6 +2306,8 @@ test "docs/typ names only words, keys and default tags that exist" { var dir = try std.Io.Dir.cwd().openDir(io, @import("doc_typ").dir, .{ .iterate = true }); defer dir.close(io); var found: [3]std.ArrayList([]const u8) = @splat(.empty); + // The arena's: gone with this test, so no later test reads it. + defer doc_typ.other_build = ""; for ([_][]const u8{ "builtins-tty.json", "builtins-gui.json" }) |f| { const text = dir.readFileAlloc(io, f, gpa, .limited(1 << 20)) catch continue; doc_typ.other_build = try std.mem.concat(gpa, u8, &.{ doc_typ.other_build, text }); -- cgit v1.3 From 7e6834ba5eff5c0e61891ec1935e5e0ca8291c2f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 12:32:23 -0300 Subject: tests on FreeBSD: Linux's /proc/1 fixtures, the builtins lists, daemon.py and fs.py without /proc - The refusing directory some tests name is Linux's /proc/1/root: that part skips elsewhere. The committed builtins-*.json are Linux builds' (Tty9p): their check skips, and a key the docs give a word this build leaves out is found in the other's list. - kitty: a shared memory object is sized before it is written (FreeBSD's write does not grow it); a FIFO in /dev/shm is Linux's case. - daemon.py asks ps and procstat where there is no /proc, and finds bash on PATH. fs.py runs printenv once a name, and skips two permission checks as root, who searches and writes anywhere. Co-Authored-By: Claude Opus 5.5 --- src/builtins_json.zig | 4 +++- src/config.zig | 5 ++++- src/fs.zig | 3 ++- src/ninep/ctl.zig | 15 ++++++++++----- test/daemon.py | 19 ++++++++++++++++++- test/fs.py | 40 ++++++++++++++++++++++------------------ test/kitty.zig | 4 ++++ 7 files changed, 63 insertions(+), 27 deletions(-) (limited to 'src/config.zig') diff --git a/src/builtins_json.zig b/src/builtins_json.zig index 4440130d..3598a3e2 100644 --- a/src/builtins_json.zig +++ b/src/builtins_json.zig @@ -66,7 +66,9 @@ pub fn write(w: *std.Io.Writer) !void { } test "docs/typ/builtins-.json is what the registry says now" { - // a terminal build checks its file, a window build its own + // a terminal build checks its file, a window build its own; both are + // Linux builds' lists (Tty9p is Linux's) + if (comptime @import("builtin").os.tag != .linux) return error.SkipZigTest; const file = switch (pardes.platform) { .tty => "builtins-tty.json", .gui => "builtins-gui.json", diff --git a/src/config.zig b/src/config.zig index 66b11c72..f0023193 100644 --- a/src/config.zig +++ b/src/config.zig @@ -2277,7 +2277,10 @@ const doc_typ = struct { for (std.enums.values(Builtin)) |b| if (leader_path.get(b)) |p| if (std.mem.eql(u8, p, path[0..n])) return true; for (leader_args) |la| if (std.mem.eql(u8, la.path, path[0..n])) return true; - return false; + // A word this build leaves out (Tty9p off Linux) keeps its leader in the other's. + var buf: [96]u8 = undefined; + const needle = std.fmt.bufPrint(&buf, "\"leader\": \"{s}\"", .{k}) catch return false; + return std.mem.indexOf(u8, other_build, needle) != null; } toks.reset(); while (toks.next()) |t| if (!bound(chord(t) orelse return false)) return false; diff --git a/src/fs.zig b/src/fs.zig index 08379697..9104c93f 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -708,7 +708,8 @@ pub fn deniedAbove(dir: []const u8) bool { } test "a directory under one that may not be searched or written is denied, one merely missing is not" { - try std.testing.expect(deniedAbove("/proc/1/root/x/y")); + // The refusing directory is Linux's /proc/1/root. + if (comptime @import("builtin").os.tag == .linux) try std.testing.expect(deniedAbove("/proc/1/root/x/y")); try std.testing.expect(!deniedAbove("/tmp/pardes-surely-missing-dir/a/b")); } diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 9cb96667..3176ebc8 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -4676,8 +4676,8 @@ test "pager answers its +Pager on the open that asked, whatever another client e try testing.expectEqualStrings("/tmp/+Pager", second_pane.file.?.path); try testing.expect(p.paneBySerial(paged) != null); _ = call(p, .{ .tag = 14, .op = .release, .node = pager, .handle = resolved, .opened = true }); - // One that may not be written is refused, permission denied. - try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno()); + // One that may not be written is refused, permission denied (Linux's /proc/1). + if (comptime @import("builtin").os.tag == .linux) try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno()); // One directory a write, absolute, and there. try testing.expectEqual(E.INVAL, wr(p, pager, "/a\n/b\n").errno()); // One directory an open: one refused is not followed by another, as a @@ -4786,8 +4786,11 @@ test "a command pane an exec open was answered is leased to it while it stays op test "a Save whose directory is missing because one above refuses says permission denied, not no such directory" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); - p.saveFailed(0, "/proc/1/root/x/f.txt", error.FileNotFound); - try testing.expect(th.logHas(p, "Save /proc/1/root/x/f.txt: permission denied")); + // The refusing directory is Linux's /proc/1/root. + if (comptime @import("builtin").os.tag == .linux) { + p.saveFailed(0, "/proc/1/root/x/f.txt", error.FileNotFound); + try testing.expect(th.logHas(p, "Save /proc/1/root/x/f.txt: permission denied")); + } p.saveFailed(0, "/tmp/pardes-surely-missing-dir/f.txt", error.FileNotFound); try testing.expect(th.logHas(p, "no such directory")); } @@ -5036,8 +5039,10 @@ test "DumpDir refuses a relative directory and one that may not be written, up f defer p.deinit(); const root_ctl = @backingInt(tree.TopFile.ctl); try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir dumps\n").errno()); - try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir /proc/1/root/dumps\n").errno()); try testing.expectEqual(Status.ok, wr(p, root_ctl, "DumpDir /tmp/pardes-dumps-test\n").reply.status); + // The refusing directory is Linux's /proc/1/root. + if (comptime @import("builtin").os.tag != .linux) return; + try testing.expectEqual(E.INVAL, wr(p, root_ctl, "DumpDir /proc/1/root/dumps\n").errno()); p.fs.late_failure_len = 0; p.dumpFailed("/proc/1/root/x/pardes.dump.zon", error.FileNotFound); try testing.expect(std.mem.endsWith(u8, p.fs.late_failure[0..p.fs.late_failure_len], ": permission denied")); diff --git a/test/daemon.py b/test/daemon.py index bf3d237c..f4f46286 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -94,10 +94,21 @@ def run(client, serial, line): client.close(fid) +# FreeBSD has no /proc: ps and procstat answer instead. +procfs = os.path.isdir('/proc/self') + + +def ps(*args): + return subprocess.run(['ps', *args], capture_output=True).stdout + + def processes(root): """The pids whose environment is this run's (its runtime directory).""" found = [] mark = f'XDG_RUNTIME_DIR={root}'.encode() + if not procfs: # ps -e prints each environment before its command + return [int(row.split()[0]) for row in ps('-axeww', '-o', 'pid=', '-o', 'command=').splitlines() + if mark in row.split()] for entry in os.listdir('/proc'): if not entry.isdigit(): continue @@ -111,6 +122,8 @@ def processes(root): def cmdline(pid): + if not procfs: + return ps('-ww', '-o', 'args=', '-p', str(pid)).split() try: return open(f'/proc/{pid}/cmdline', 'rb').read().split(b'\0') except OSError: @@ -122,6 +135,8 @@ def alive(pid): os.kill(pid, 0) except ProcessLookupError: return False + if not procfs: + return not ps('-o', 'state=', '-p', str(pid)).startswith(b'Z') try: state = open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[0] except OSError: @@ -130,6 +145,8 @@ def alive(pid): def fds(pid): + if not procfs: + return len(subprocess.run(['procstat', '-f', str(pid)], capture_output=True).stdout.splitlines()) - 1 try: return len(os.listdir(f'/proc/{pid}/fd')) except OSError: @@ -146,7 +163,7 @@ def main(binary, cycles): root = Path(tempfile.mkdtemp(prefix='rt-', dir=scratch)) env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), - TERM='xterm-256color', SHELL='/bin/bash', PARDES_NOTIME='1') + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') (root / 'config' / 'pardes').mkdir(parents=True) (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') daemon_sock = root / 'pardes-daemon.sock' diff --git a/test/fs.py b/test/fs.py index 74c958ce..b69ecf57 100644 --- a/test/fs.py +++ b/test/fs.py @@ -520,7 +520,7 @@ def run_file(binary): client.remove(f'/pane/{pager[0]}') # systemd's tools page through their own variable: set too, its # secure mode off, the path unquoted where it needs none. - systemd_pager = run(client, term, b'printenv SYSTEMD_PAGER SYSTEMD_PAGERSECURE\n') + systemd_pager = run(client, term, b'printenv SYSTEMD_PAGER; printenv SYSTEMD_PAGERSECURE\n') assert systemd_pager.startswith(b'exit 0\n') and systemd_pager.rstrip().endswith(b' -\n0'), systemd_pager if shutil.which('journalctl'): assert run(client, term, b'journalctl -n 3 --no-hostname\n').startswith(b'exit ') @@ -551,8 +551,9 @@ def run_file(binary): time.sleep(.05) locked.chmod(0o600) try: - client.write(f'/pane/{term}/pty/ctl', b'exec\n') - raise AssertionError('pty/ctl exec of a shell that cannot start was taken') + if os.geteuid() != 0: # root enters any directory + client.write(f'/pane/{term}/pty/ctl', b'exec\n') + raise AssertionError('pty/ctl exec of a shell that cannot start was taken') except OSError as refused: assert 'shell' in str(refused), refused finally: @@ -1663,15 +1664,17 @@ def test(binary, quic=False): client.remove(f'/pane/{dashed}') # A name under a directory that may not be searched is refused, # permission denied, exit 1, no pane: its Save could never be. - (root / 'noperm').mkdir() - (root / 'noperm').chmod(0) - before = serials() - try: - denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) - finally: - (root / 'noperm').chmod(0o755) - assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied - assert serials() == before + # (Root searches any directory.) + if os.geteuid() != 0: + (root / 'noperm').mkdir() + (root / 'noperm').chmod(0) + before = serials() + try: + denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) + finally: + (root / 'noperm').chmod(0o755) + assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied + assert serials() == before # --wait waits on the pane its look answered, on the look's own # open: a name /index shows escaped (a backslash is `\\` there) # is still the pane waited on, not one already gone. @@ -1824,16 +1827,17 @@ def test(binary, quic=False): stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) assert fed.returncode == 0 and serials() == before, (fed, serials()) # A new name in a directory there but not writable: refused now, - # not a pane that only fails at Save. + # not a pane that only fails at Save. (Root writes anywhere.) shut = root / 'shut-dir' shut.mkdir() shut.chmod(0o500) try: - refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'may not be read or written' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) + if os.geteuid() != 0: + refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) + assert refused.returncode == 1, refused + assert b'may not be read or written' in refused.stderr, refused.stderr + assert serials() == before, (serials(), before) finally: shut.chmod(0o755) for word, said in (('@p999:1', b'pardes: @p999:1: this session has no such pane'), diff --git a/test/kitty.zig b/test/kitty.zig index de4dd605..07e89cf9 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -299,6 +299,8 @@ test "kitty: file, temporary-file and shared-memory media load, and the terminal const fd = shm.shm_open(name, @bitCast(std.c.O{ .ACCMODE = .RDWR, .CREAT = true, .TRUNC = true }), 0o600); if (fd < 0) return error.ShmOpenFailed; defer _ = shm.shm_unlink(name); + // Sized first: FreeBSD's write does not grow a shared memory object. + if (ftruncate(fd, rgb_raw.len) != 0) return error.WriteFailed; if (std.c.write(fd, &rgb_raw, rgb_raw.len) != rgb_raw.len) return error.WriteFailed; _ = std.c.close(fd); r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=t,t=s,i=43,f=24,s=4,v=2,S={d};{s}\x1b\\", .{ rgb_raw.len, try b64(arena, name) })); @@ -847,6 +849,8 @@ test "kitty: a FIFO named by t=f, t=t or t=s never blocks pardes" { .{ .medium = 's', .fifo = shm_path, .name = shm_path["/dev/shm".len..] }, }; for (cases) |c| { + // Shared memory is a directory only on Linux (/dev/shm). + if (c.medium == 's' and comptime @import("builtin").os.tag != .linux) continue; _ = std.c.unlink(c.fifo); if (mkfifo(c.fifo, 0o600) != 0) return error.MkfifoFailed; defer _ = std.c.unlink(c.fifo); -- 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/config.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 ddcfd860ef05c37f5536038d8828cc81c0c4125c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 22:18:34 -0300 Subject: upgrade: the Upgrade word (SPC s U, a tag, 9P) moves a running session onto the pardes installed where it started, in place, by exec at a step boundary: its dump in the DumpDir, its pty masters, terminals, stubs and daemon link kept open and named on the new command line, every view and running shell its own again; the daemon re-execs too, its registry on its command line; a build that cannot load the dump falls back to the old binary, or starts afresh with the shells adopted and the dump kept (test/daemon.py --upgrade, --upgrade --gui) Co-Authored-By: Claude Opus 5.5 --- docs/typ/building.typ | 13 ++ docs/typ/builtins-gui.json | 1 + docs/typ/builtins-tty.json | 1 + docs/typ/guide.typ | 7 +- src/9p_io.zig | 33 ++-- src/builtins.zig | 13 ++ src/config.zig | 1 + src/daemon.zig | 145 ++++++++++++++++- src/dump.zig | 71 +++++++-- src/gui/gui.zig | 120 +++++++++++++- src/host_io.zig | 3 + src/main.zig | 42 +++-- src/pardes.zig | 8 + src/tty/tty.zig | 220 ++++++++++++++++++++++--- src/upgrade.zig | 389 +++++++++++++++++++++++++++++++++++++++++++++ test/daemon.py | 315 +++++++++++++++++++++++++++++++++++- 16 files changed, 1316 insertions(+), 66 deletions(-) create mode 100644 src/upgrade.zig (limited to 'src/config.zig') diff --git a/docs/typ/building.typ b/docs/typ/building.typ index e42702c6..d8c089f6 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -236,6 +236,19 @@ hands an accepted connection to its session (`SCM_RIGHTS`), which welcomes it and from then on is the one it talks to. Today the session answers pings and nothing more. +#word("Upgrade") (`src/upgrade.zig`) execs the path the session was started +from, at a step boundary, after asking it `--version`: the session's dump +goes to the DumpDir (`pardes-upgrade-.dump`), and its pty masters, +terminals, stubs and daemon link stay open across the exec, named in a +record on the new command line (`--upgraded=`) ahead of the arguments it +was started with. The pid stays, so the shells stay its children; the new +process loads the dump with every view and takes the fds back. One that +cannot load it execs the old binary again (kept open from +`/proc/self/exe`), or with none starts afresh with the shells adopted and +the dump left for a Restore. The daemon re-execs the same way, its +registry on its command line. `python3 -B test/daemon.py --upgrade +` (and `--upgrade --gui`) is the end-to-end test. + = 9P The wire format, the client and server connections, the file-server engine diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 9db9e396..e731b75e 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -152,6 +152,7 @@ {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, + {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, {"name": "Verbose", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A builtin says its own name on the message row as it runs, on or off."}, {"name": "Vignette", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off, 0-3", "doc": "The window's corners fall into shade: a level, 0 (off) through 3."}, {"name": "Window", "leader": "SPC w", "also": ["C-w"], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Window mode: h/j/k/l move an outline between the panes; Enter or Esc focuses the outlined pane, `:` its tag, the letter shown on a pane that one; d is Del on it (then k or j between two), c Collapse."}, diff --git a/docs/typ/builtins-tty.json b/docs/typ/builtins-tty.json index 151c04a6..fe81b3fa 100644 --- a/docs/typ/builtins-tty.json +++ b/docs/typ/builtins-tty.json @@ -135,6 +135,7 @@ {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, + {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, {"name": "Verbose", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A builtin says its own name on the message row as it runs, on or off."}, {"name": "Window", "leader": "SPC w", "also": ["C-w"], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Window mode: h/j/k/l move an outline between the panes; Enter or Esc focuses the outlined pane, `:` its tag, the letter shown on a pane that one; d is Del on it (then k or j between two), c Collapse."}, {"name": "Wrap", "leader": "SPC t w", "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "Long lines wrap onto the next row, on or off."}, diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index 0b721658..4430276f 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -358,7 +358,12 @@ one lets only that one go. A session is named by `--9p=` or `--detach=`, else its pid. A session killed outright leaves its shells running; #word("Adopt") in another takes them in as terminal panes, what they showed gone. One `pardes --daemon` per user keeps all of it, and -leaves once nothing is kept. +leaves once nothing is kept. After a rebuild, #word("Upgrade") +(#key("SPC s U")) moves a running session onto the pardes now installed +where it was started from, in place: its text and undo, its views, its +terminals with their running shells, and its terminals and windows come +along, and the daemon moves too. A build that cannot read the session +hands it back to the one that ran it. = Config diff --git a/src/9p_io.zig b/src/9p_io.zig index 9bcb2827..ef6385fc 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -902,6 +902,25 @@ pub const Listener = struct { l.posted_len = 0; } + /// What the connections have still to send let go, 200 ms at most, + /// before they are cut (a quit, an Upgrade's exec: its write's answer). + pub fn flushOut(l: *Listener) void { + if (comptime !supported) return; + const deadline = nowMs() +| 200; + while (nowMs() < deadline) { + const pending = for (&l.runner.conns) |*conn| { + if (!conn.live()) continue; + conn.lock(); + const n = conn.engine.output().len; + conn.unlock(); + if (n != 0) break true; + } else false; + if (!pending) break; + Client.nap(1); + } + Client.nap(1); // the last bytes leave the stream's own buffer + } + pub fn deinit(l: *Listener, gpa: std.mem.Allocator) void { l.stopping.store(true, .release); l.unpostFromRegistry(); @@ -922,19 +941,7 @@ pub const Listener = struct { l.edit_watchers.cancel(l.io); // An answer given as the editor quit (a Kill written to /ctl) // may still be on its way out: let it go before the cut. - const deadline = nowMs() +| 200; - while (nowMs() < deadline) { - const pending = for (&l.runner.conns) |*conn| { - if (!conn.live()) continue; - conn.lock(); - const n = conn.engine.output().len; - conn.unlock(); - if (n != 0) break true; - } else false; - if (!pending) break; - Client.nap(1); - } - Client.nap(1); // the last bytes leave the stream's own buffer + l.flushOut(); l.runner.stop(); pardes.turn.wake(); } diff --git a/src/builtins.zig b/src/builtins.zig index 0ad59fd4..1dc428da 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -762,6 +762,19 @@ pub const Adopt = struct { } }; +/// Move this session onto the pardes installed since it started, in place: +/// its text, undo, views, terminals with their running shells, and its +/// terminals and windows come along. The pardes daemon moves too. +pub const Upgrade = struct { + pub const scope: Scope = .session; + pub const enabled = pardes.can_attach; + pub fn run(c: Ctx) void { + if (comptime !enabled) unreachable; + const upgrade = c.p.host.vtable.upgrade orelse return c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); + upgrade(c.p.host.ctx, @intCast(c.id)); + } +}; + /// Mount another pardes's 9P tree under /n/, given its dial address. pub const Mount = struct { pub const scope: Scope = .session; diff --git a/src/config.zig b/src/config.zig index 13404461..742e9010 100644 --- a/src/config.zig +++ b/src/config.zig @@ -219,6 +219,7 @@ pub const leader_path = paths: { if (pardes.can_attach) { table.set(.Detach, "sD"); table.set(.Adopt, null); + table.set(.Upgrade, "sU"); } if (builtins.Peek.enabled) { table.set(.Peek, null); diff --git a/src/daemon.zig b/src/daemon.zig index 702d11b1..43f15c6c 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -27,6 +27,7 @@ const libc = std.c; const posix = std.posix; const ninep_io = @import("9p_io.zig"); const wire = @import("detached/wire.zig"); +const upgrade = @import("upgrade.zig"); const log = std.log.scoped(.replica); pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; @@ -355,6 +356,33 @@ pub fn adopt() void { tell("adopt", &.{}); } +/// This session is about to exec another build (upgrade.zig): the daemon +/// is asked to move too, and every replica told why it is let go. +/// ponytail: a pong its thread writes at the same moment may split the +/// words; the client reconnects either way. +pub fn upgraded() void { + tell("upgrade", &.{}); + var out: [600]u8 = undefined; + const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return; + for (&replica_fds) |*slot| { + const fd = slot.load(.acquire); + if (fd < 0) continue; + _ = writeAll(fd, said); + _ = libc.shutdown(fd, 2); + } +} + +/// An upgraded session's link and stub as its old process left them: its +/// build's hello said again on the link (the daemon keeps its masters as +/// they were), and the stub's death a HUP again. +pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, detached: bool) void { + if (comptime !supported) return; + stub = stub_fd; + if (stub >= 0) hupOnStubDeath(true); + setName(named); + if (fd >= 0 and hello(fd, k, libc.getpid(), name(), detached)) link = fd else if (fd >= 0) _ = libc.close(fd); +} + pub const Note = union(enum) { /// A terminal for a detached tty session (`pardes --attach`), and the /// stub's pipe; for a window session, both -1: open a window. @@ -422,6 +450,8 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } +var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1)); + /// A replica the daemon handed over, its hello accepted: welcomed, its pings /// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it /// a replica (the log, the dump, terminal state); this stub is what times a @@ -429,6 +459,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { /// than its buffer ends it. fn serveReplica(fd: c_int) void { defer _ = libc.close(fd); + // Where an upgrade finds it to say why it ends (`upgraded`). + const slot = for (&replica_fds) |*r| { + if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r; + } else null; + defer if (slot) |r| r.store(-1, .release); log.info("a replica connected to '{s}'", .{name()}); defer log.info("a replica of '{s}' left", .{name()}); var out: [wire.hello_max]u8 = undefined; @@ -698,6 +733,8 @@ const Daemon = struct { conns: [max_conns]Conn = @splat(.{}), held: [max_held]Held = @splat(.{}), replicas: [max_replicas]Replica = @splat(.{}), + /// A session asked it to move onto the build installed since (`reexec`). + upgrade_asked: bool = false, fn idle(d: *const Daemon) bool { for (d.conns) |c| if (c.fd >= 0) return false; @@ -776,6 +813,10 @@ const Daemon = struct { for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) d.drop(h); return; } + if (std.mem.eql(u8, verb, "upgrade")) { + d.upgrade_asked = true; + return; + } if (std.mem.eql(u8, verb, "adopt")) { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); @@ -920,6 +961,93 @@ const Daemon = struct { _ = libc.close(fd); } + /// Upgrade: this daemon exec'd as the pardes now at the path it was + /// started from, on the same listeners and lock, every session's link + /// and every master kept open, its registry on the new command line + /// (`restore`). A replica whose hello is still on its way is let go: it + /// dials again. Returns when it cannot be done, as it was. + fn reexec(d: *Daemon, replicas: c_int) void { + const path = upgrade.exe() orelse return; + var why: [700]u8 = undefined; + switch (upgrade.check(path, &why)) { + .no => |w| return log.warn("{s}", .{w}), + .ok => {}, + } + var text: [flag_room]u8 = undefined; + var w: std.Io.Writer = .fixed(&text); + w.print("{s}replicas {d}\n", .{ upgrade.flag, replicas }) catch return; + for (&d.conns, 0..) |*c, i| if (c.fd >= 0) { + const k: []const u8 = if (c.kind) |k| @tagName(k) else "-"; + w.print("c {d} {d} {s} {d} {d} {x} {s}\n", .{ i, c.fd, k, c.pid, @intFromBool(c.detached), c.semantics, c.named() }) catch return; + }; + for (&d.held) |*h| if (h.fd >= 0) { + w.print("h {d} {d} {d}\n", .{ h.fd, h.pid, h.owner orelse 255 }) catch return; + }; + w.writeByte(0) catch return; + for (&d.replicas) |*r| if (r.fd >= 0) { + _ = libc.close(r.fd); + r.* = .{}; + }; + const keep = struct { + fn all(dd: *Daemon, rep: c_int, on: bool) void { + const v: c_int = if (on) 0 else 1; + for ([_]c_int{ 3, 4, rep }) |fd| if (fd >= 0) { + _ = libc.fcntl(fd, libc.F.SETFD, v); + }; + for (&dd.conns) |*c| if (c.fd >= 0) { + _ = libc.fcntl(c.fd, libc.F.SETFD, v); + }; + for (&dd.held) |*h| if (h.fd >= 0) { + _ = libc.fcntl(h.fd, libc.F.SETFD, v); + }; + } + }.all; + const argv = [_:null]?[*:0]const u8{ path, "--daemon", @ptrCast(&text), null }; + keep(d, replicas, true); + _ = libc.execve(path, &argv, @ptrCast(libc.environ)); + keep(d, replicas, false); + log.warn("upgrade: {s} would not start", .{path}); + } + + /// The registry an upgraded daemon was exec'd with (`reexec`); the + /// replicas' listener, its fd, or -1. + fn restore(d: *Daemon, text: []const u8) c_int { + var replicas: c_int = -1; + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + const what = words.next() orelse continue; + if (std.mem.eql(u8, what, "replicas")) { + replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; + if (replicas >= 0) ninep_io.setCloexec(replicas); + } else if (std.mem.eql(u8, what, "c")) { + const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + if (i >= max_conns) continue; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const detached = std.mem.eql(u8, words.next() orelse "", "1"); + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + ninep_io.setCloexec(fd); + const c = &d.conns[i]; + c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + } else if (std.mem.eql(u8, what, "h")) { + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; + ninep_io.setCloexec(fd); + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; + break; + }; + } + } + return replicas; + } + fn accept(d: *Daemon, listener: c_int) void { const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); if (fd < 0) return; @@ -946,16 +1074,23 @@ fn listenReplicas() c_int { return -1; } +/// Room for an upgrade's registry on a command line (`reexec`): every +/// connection and master, each a line. +const flag_room = 64 * 1024; + /// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the -/// lock (fd 4). Returns only to exit, idle. -pub fn serve() noreturn { +/// lock (fd 4), or (`state`) an upgrade of one, its registry. Returns only +/// to exit, idle. +pub fn serve(state: ?[]const u8) noreturn { const listener: c_int = 3; ninep_io.setCloexec(3); ninep_io.setCloexec(4); const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null); - const replicas = listenReplicas(); + upgrade.remember(&.{}); var d: Daemon = .{}; + const given = if (state) |text| d.restore(text) else -1; + const replicas = if (given >= 0) given else listenReplicas(); const slots = 2 + max_conns + max_replicas + max_held; var fds: [slots]libc.pollfd = undefined; var conn_of: [slots]u8 = undefined; @@ -1025,6 +1160,10 @@ pub fn serve() noreturn { if (pfd.revents == 0 or d.held[i].fd != pfd.fd) continue; d.drop(&d.held[i]); } + if (d.upgrade_asked) { + d.upgrade_asked = false; + d.reexec(replicas); + } } var sock_buf: [sun_path_len:0]u8 = undefined; _ = libc.close(listener); diff --git a/src/dump.zig b/src/dump.zig index 2cef46e2..cb639568 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -321,7 +321,7 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { var opts = p.opts; opts.cols = p.screen_w; opts.rows = p.screen_h; - const replacement = try loadAndRevive(p.gpa, opts, bytes, p); + const replacement = try loadAndRevive(p.gpa, opts, bytes, p, null); replacement.pointRestoreAt(from); // The tape follows the replacement from here (it is the session). pardes.tape.adopt(replacement); @@ -332,7 +332,7 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { pardes.tape.startFromEnv(); const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = bytes } }); - const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null)) catch |err| { + const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null, null)) catch |err| { pardes.tape.leave(null, taped); return err; }; @@ -346,18 +346,39 @@ fn carriedWindow(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Par const p = try Pardes.create(gpa, opts); errdefer p.deinit(); _ = try Handover.adopt(p, bytes); - try reviveSession(p, null); + try reviveSession(p, null, false); return p; } -fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes) !*Pardes { +/// An upgraded session's dump, loaded as the session it was (upgrade.zig): +/// every view kept, its settings the dump's (the startup config is not run +/// again), and the terminals whose shells run on (`kept`) not revived: the +/// host gives each its pty back. Untaped: a recording ends at an upgrade. +pub fn resumeFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, kept: *const [MAX_PANES]bool) !*Pardes { + return loadAndRevive(gpa, opts, bytes, null, kept); +} + +/// Where an upgrade leaves its dump: the DumpDir (a relative Restore finds +/// it there by name), as `pardes-upgrade-.dump`; never $PARDES_DUMP, +/// which is a Dump's. +pub fn upgradePath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { + var dir_buf: [900]u8 = undefined; + const dir = directory(&dir_buf, dir_setting) orelse return null; + _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {}; + return std.fmt.bufPrintSentinel(buf, "{s}/pardes-upgrade-{d}.dump", .{ dir, std.c.getpid() }, 0) catch null; +} + +fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes, kept: ?*const [MAX_PANES]bool) !*Pardes { var notes: std.ArrayList(u8) = .empty; defer notes.deinit(gpa); var on_disk: OnDisk = @splat(false); const p = try loadKnowing(gpa, opts, bytes, ¬es, &on_disk); errdefer p.deinit(); - for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| revivePane(p, i, pane, was_on_disk); - try reviveSession(p, previous); + for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| { + if (kept) |k| if (k[i] and pane.terminal != null) continue; + revivePane(p, i, pane, was_on_disk); + }; + try reviveSession(p, previous, kept != null); // What another build's dump could not give, said where the user looks // (stderr is a tty session's screen). if (notes.items.len > 0) { @@ -370,11 +391,12 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ /// A loaded core given to this host (with `revivePane` for each of its /// panes): the store and Recent read (a Restore's carried from the session -/// it replaces) and its ThemeFile asked for again. -fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void { +/// it replaces) and its ThemeFile asked for again. An upgrade's (`resumed`) +/// keeps its views, whose clients come back with it, and its settings. +fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void { // ponytail: one client loads it, so it is one view, the live one's; a // replica joining (phase 5) takes `load`'s, every view. - views.only(p); + if (!resumed) views.only(p); if (previous) |old| { // Never a serial or a request number the old session gave out. p.next_serial = @max(p.next_serial, old.next_serial); @@ -403,7 +425,7 @@ fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void { // The dump's screen until this host says its own (a resize): two // reflows would lose where a terminal's view was. p.sync(); - exec.applyStartupConfig(p); + if (!resumed) exec.applyStartupConfig(p); colors.finishThemeInitialization(p); p.sync(); p.presentation.enabled = true; @@ -640,6 +662,35 @@ test "a loaded terminal's history stays above its banner, the cursor where it wa try std.testing.expect(std.mem.indexOf(u8, text, "two") .? < std.mem.indexOf(u8, text, "restored history").?); } +test "an upgraded session's dump keeps its views and its running terminal as they were, and settings the startup config would change" { + if (comptime !pardes.panes.terminal.enabled or isolated) return error.SkipZigTest; + const gpa = std.testing.allocator; + const term = pardes.panes.terminal; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + term.feedOutput(p, p.panes[0].?, "one\r\n\x1b[?1049h$ vim"); + try std.testing.expect(p.executeBuiltinLine(p.active, "Verbose on")); + _ = p.openView().?; + const bytes = try dumpBytes(p); + defer gpa.free(bytes); + var kept: [MAX_PANES]bool = @splat(false); + kept[0] = true; + const q = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" }, bytes, &kept); + defer q.deinit(); + try std.testing.expectEqual(@as(usize, 2), views.count(q)); + try std.testing.expect(q.settings.verbose); + // The program's own screen, no banner, and no shell asked for. + try std.testing.expectEqual(term.gridCursor(p.panes[0].?), term.gridCursor(q.panes[0].?)); + try std.testing.expect(q.panes[0].?.terminal.?.vt.screens.active_key == .alternate); + while (q.nextEffect()) |e| try std.testing.expect(e != .spawn); + // Not kept, the same terminal is a new shell's under its history. + kept[0] = false; + const r = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, bytes, &kept); + defer r.deinit(); + try std.testing.expect(r.panes[0].?.terminal.?.vt.screens.active_key == .primary); +} + test "a session read by name, field by field as another build reads it, is the same session" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c550aa4c..24061251 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -23,6 +23,7 @@ const c_heap = @import("c_heap"); const ninep_io = @import("../9p_io.zig"); const daemon = @import("../daemon.zig"); +const upgrade = @import("../upgrade.zig"); const tracy = @import("../tracy.zig"); pub const c = @import("gui_c"); // SDL3 and font.h, translated by build.zig @@ -2376,7 +2377,9 @@ fn localSession( opts.cols = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(pw, 1))), g.cell_w))); opts.rows = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(ph, 1))), g.cell_h))); } - var core = if (opts.load_path) |lp| blk: { + var core = if (upgrade.resumed) |*rec| + try upgrade.load(gpa, allocs.pardes, opts, rec) + else if (opts.load_path) |lp| blk: { const bytes = try @import("../fs.zig").readFile(gpa, lp); defer gpa.free(bytes); break :blk try pardes.dump.initFromDump(allocs.pardes, opts, bytes); @@ -2457,6 +2460,12 @@ fn localSession( core.host = host; while (core.nextEffect()) |e| core.perform(e); + // An upgraded session's shells, once a fresh session's own has a + // directory to adopt them in. + if (upgrade.resumed) |*rec| { + shell.resumePtys(rec); + while (core.nextEffect()) |e| core.perform(e); + } for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| { pt.reader = try queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), gens[id]); }; @@ -2467,10 +2476,12 @@ fn localSession( if (fs) |f| try f.watch(&queue, wakeFs); _ = c.SDL_StartTextInput(g.window); + // An upgraded session's views, a window each again. + if (upgrade.resumed) |*rec| if (!rec.fresh and !headless) shell.resumeWindows(); // 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(); + if (test_mode and upgrade.resumed == null) if (std.c.getenv("PARDES_TEST_VIEWS")) |n| { + for (0..std.fmt.parseInt(u8, std.mem.span(n), 10) catch 0) |_| shell.openAnotherWindow(null); core.toView(0); shell.follow(); }; @@ -2491,6 +2502,8 @@ fn localSession( while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Asked in the step (the word), done between steps: the session whole. + if (shell.upgrade_asked) |pane| shell.upgradeNow(pane); // A window asked for again, or an adopted shell, from the daemon's // thread through the queue; started after the first frame. daemon.watch(&queue, deliverNote); @@ -3787,6 +3800,8 @@ const Shell = struct { /// and the view whose window the Gui holds (`follow`). windows: [pardes.views.max]GuiWindow = undefined, gview: u8 = 0, + /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). + upgrade_asked: ?u8 = null, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3905,11 +3920,12 @@ const Shell = struct { } /// `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 { + /// of its own (views.zig), opened as the first was; or (`existing`) an + /// upgraded session's view's window again. + fn openAnotherWindow(s: *Shell, existing: ?u8) 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 k = existing orelse 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); @@ -3998,6 +4014,94 @@ const Shell = struct { daemon.adopt(); } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + const s = shellOf(ctx); + if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); + s.upgrade_asked = pane; + } + + /// Upgrade, between steps: this process exec'd as the pardes now at the + /// path it was started from, the session in its dump and its ptys and + /// daemon link kept open across (upgrade.zig); the new process opens + /// its windows again. Returns only when it could not be, as it was. + fn upgradeNow(s: *Shell, pane: u8) void { + s.upgrade_asked = null; + const core = s.core; + var why: [800]u8 = undefined; + const path = upgrade.exe() orelse return core.reportFailure(pane, "Upgrade: this process does not know the path it was started from"); + // 9P is served while the new binary is asked, and not after. + pardes.turn.rest(); + const checked = upgrade.check(path, &why); + pardes.turn.wake(); + switch (checked) { + .no => |w| return core.reportFailure(pane, w), + .ok => {}, + } + // Nothing more read from a pty, and what was read goes in: drained, + // the readers stopped (a reader waiting for room has it then), and + // what they pushed meanwhile drained. + s.drainQueue(); + for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.reader) |*r| { + r.cancel(s.queue.io); + pt.reader = null; + }; + s.drainQueue(); + core.settle(); + var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; + for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); + var at_buf: [1024:0]u8 = undefined; + const failed: []const u8 = failed: { + const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is"; + const bytes = pardes.dump.dumpBytes(core) catch |err| { + var words: [128]u8 = undefined; + break :failed std.fmt.bufPrint(&why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; + }; + defer s.gpa.free(bytes); + if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written"; + rec.dump = at; + rec.old = upgrade.openSelf(); + host_io.Shell.unlinkOwnedRcs(); + daemon.upgraded(); + if (s.fs) |f| f.flushOut(); + const err_words = upgrade.exec(s.gpa, path, -1, &rec); + // Still here: the exec failed. + _ = libc.unlink(at); + if (rec.old >= 0) _ = libc.close(rec.old); + break :failed std.fmt.bufPrint(&why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; + }; + // As it was: its ptys read on. + for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) { + pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), s.gens[id]) catch null; + }; + core.reportFailure(pane, failed); + } + + /// An upgraded session's shells, running on: each its pane's pty again, + /// or, when its dump would not load (`fresh`), adopted as new panes. + fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { + for (rec.ptys[0..rec.nptys]) |k| { + const pane = if (rec.fresh) null else s.core.panes[k.pane]; + if (pane == null or !pane.?.isTerminal()) { + s.adoptShell(k.pid, k.fd); + continue; + } + s.ptys[k.pane] = .{ .fd = k.fd, .pid = k.pid, .serial = pane.?.serial, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; + } + } + + /// An upgraded session's views, a window each again: the Gui's is the + /// live view's, the others open as `--attach` opens one. + fn resumeWindows(s: *Shell) void { + const live = s.core.view; + s.gview = live; + for (0..pardes.views.max) |i| { + const k: u8 = @intCast(i); + if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + } + s.core.toView(live); + s.follow(); + } + /// One shell of a dead session (`pid` 0: the last of them was told): /// a terminal pane made for it, as the Tty word makes one, whose spawn /// takes this pty (spawnPane). @@ -4038,6 +4142,7 @@ const Shell = struct { .watched = watched, .detach = detachWindow, .adopt = adopt, + .upgrade = upgradeAsked, .spawn = spawnPane, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -4072,6 +4177,7 @@ const Shell = struct { v.watched = null; v.detach = null; v.adopt = null; + v.upgrade = null; break :vt v; }; @@ -4167,7 +4273,7 @@ const Shell = struct { .files_changed => check_files = true, .fs_ready => {}, .window => { - if (s.gui != null) s.openAnotherWindow() else s.openTheWindow(); + if (s.gui != null) s.openAnotherWindow(null) else s.openTheWindow(); s.saw_event = true; }, .adopted => |a| { diff --git a/src/host_io.zig b/src/host_io.zig index 7b0fe9a2..46ee9afc 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -29,6 +29,9 @@ pub const Host = struct { detach: ?*const fn (ctx: ?*anyopaque) void = null, /// Adopt: the shells a dead session left, asked of the daemon. adopt: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + /// Upgrade: this process exec'd as the pardes installed since, at + /// the step's end (upgrade.zig); `pane` hears why if it cannot be. + upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null, pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null, diff --git a/src/main.zig b/src/main.zig index ac2226ae..a6144475 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const pardes = @import("pardes.zig"); const ninep_io = @import("9p_io.zig"); const daemon = @import("daemon.zig"); +const upgrade = @import("upgrade.zig"); const is_emscripten = builtin.os.tag == .emscripten; @@ -346,8 +347,18 @@ fn nativeMain(std_init: std.process.Init) !void { var mounts_len: usize = 0; const arena = init.arena.allocator(); const args = try init.minimal.args.toSlice(arena); - // The per-user daemon a session started (daemon.zig): nothing else. - if (comptime daemon.supported) if (args.len == 2 and std.mem.eql(u8, args[1], "--daemon")) daemon.serve(); + // The per-user daemon a session started (daemon.zig), or an upgrade of + // one with its registry: nothing else. + if (comptime daemon.supported) if (args.len >= 2 and args.len <= 3 and std.mem.eql(u8, args[1], "--daemon")) + daemon.serve(if (args.len == 3 and std.mem.startsWith(u8, args[2], upgrade.flag)) args[2][upgrade.flag.len..] else null); + // An upgraded session's new process (upgrade.zig): its record, then the + // arguments it was started with, which configure it as before; its + // file, stdin, directory and daemon come from the record instead. + const upgraded = comptime upgrade.supported and (pardes.platform == .tty or pardes.platform == .gui); + if (upgraded) if (args.len > 1 and std.mem.startsWith(u8, args[1], upgrade.flag)) { + upgrade.resumed = upgrade.Record.decode(args[1][upgrade.flag.len..]) catch badArgs(init.io, "--upgraded: a record this build cannot read", .{}); + }; + const resuming = upgrade.resumed != null; var session_only = true; var new_session = false; var explicit_tty = false; @@ -355,7 +366,7 @@ fn nativeMain(std_init: std.process.Init) !void { var attach: ?[]const u8 = null; var positional: ?[:0]const u8 = null; var wait = false; - var i: usize = 1; + var i: usize = if (resuming) 2 else 1; while (i < args.len) : (i += 1) { const a = args[i]; if (std.mem.eql(u8, a, "--tty")) { @@ -496,7 +507,7 @@ fn nativeMain(std_init: std.process.Init) !void { badArgs(init.io, "--9p-quic opens a session's own listener, and --attach has none of its own", .{}); if (opts.mounts.len != 0 and attach != null) badArgs(init.io, "--mount configures a session's own core, and --attach has none of its own", .{}); - if (!new_session and !opts.nested and detach == null and attach == null) forwarding: { + if (!new_session and !opts.nested and detach == null and attach == null and !resuming) forwarding: { // Am I inside a pardes at all? That is $PARDES_PID and nothing else — // one variable, exported by every session that means to take its // children's Looks, so a session that could not open a socket is still @@ -677,6 +688,7 @@ fn nativeMain(std_init: std.process.Init) !void { if (wait) waitForDel(init.io, arena, parent.dial, path, made_serial); return; } + if (resuming) positional = null; // `pardes -` outside a session: an editor whose first pane holds stdin, // as `vim -` does; the terminal is /dev/tty, never stdin. if (positional) |a| if (std.mem.eql(u8, a, "-")) { @@ -726,11 +738,11 @@ fn nativeMain(std_init: std.process.Init) !void { opts.config_dir = found.dir; opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); - if (attach) |name| attachTo(init.io, name); + if (attach) |name| if (!resuming) attachTo(init.io, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's // socket holds already (a plain session is named by its pid too). - if (name.len != 0 and ninep_io.taken(name)) { + if (name.len != 0 and !resuming and ninep_io.taken(name)) { var said: [512]u8 = undefined; std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&said, "pardes: a session named {s} is already running\n", .{name}) catch "pardes: a session of that name is already running\n") catch {}; std.process.exit(1); @@ -743,7 +755,19 @@ fn nativeMain(std_init: std.process.Init) !void { } const kind: daemon.Kind = if (pardes.platform == .gui) .gui else .tty; var term: @import("tty/tty.zig").Term = if (detach != null) .none else .open; - if (daemon.wanted()) { + var headless = detach != null; + upgrade.remember(args); + if (upgrade.resumed) |*rec| { + // Close-on-exec again: a shell forked from here inherits none. + rec.closeOnExec(true); + opts.load_path = rec.dump; + term = .none; + for (rec.ttys[0..rec.nttys]) |t| if (t.primary) { + term = .{ .fd = .{ .fd = t.fd, .termios = t.termios } }; + }; + headless = if (pardes.platform == .gui) !rec.window else rec.nttys == 0; + daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, headless); + } else if (daemon.wanted()) { if (detach != null or pardes.platform == .gui) { _ = daemon.register(null, kind, opts.ninep_name, detach != null); } else if (pardes.platform == .tty) fork: { @@ -770,7 +794,7 @@ fn nativeMain(std_init: std.process.Init) !void { break :fork; }; _ = daemon.register(conn, .tty, opts.ninep_name, false); - term = .{ .fd = fd }; + term = .{ .fd = .{ .fd = fd } }; } } defer daemon.quit(); @@ -785,7 +809,7 @@ fn nativeMain(std_init: std.process.Init) !void { }, else => return err, }, - .gui => try @import("gui/gui.zig").run(init, opts, detach != null), + .gui => try @import("gui/gui.zig").run(init, opts, headless), .web, .macos, .esp32p4 => unreachable, } daemon.tellStubExit(0); diff --git a/src/pardes.zig b/src/pardes.zig index 0806cc78..081861ac 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -501,6 +501,7 @@ test { _ = @import("body_layer.zig"); _ = @import("draw.zig"); _ = @import("dump.zig"); // its tests ran nowhere before + _ = @import("upgrade.zig"); _ = @import("recent.zig"); _ = @import("Store.zig"); _ = @import("Presentation.zig"); @@ -6048,6 +6049,13 @@ pub const Pardes = struct { } } + /// What a step leaves queued, its inputs and its effects, taken now: + /// the session at a step boundary (an Upgrade's dump, upgrade.zig). + pub fn settle(p: *Pardes) void { + while (p.nextQueued()) |ev| p.update(ev); + while (p.nextEffect()) |e| p.perform(e); + } + pub fn pump(p: *Pardes, host: Host) !void { p.host = host; const taped = tape.enter(p, .pump); diff --git a/src/tty/tty.zig b/src/tty/tty.zig index a7048645..ef7688bc 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -16,6 +16,7 @@ const ninep_io = @import("../9p_io.zig"); const panel_compositor = @import("panel_compositor.zig"); const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); +const upgrade = @import("../upgrade.zig"); extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; @@ -69,6 +70,16 @@ fn kittyShmName(buf: []u8, n: u32) ![:0]const u8 { return std.fmt.bufPrintSentinel(buf, "/pardes-{d}-{d}", .{ libc.getpid(), n }, 0); } +/// A terminal unlinks what it reads; whatever it never read (the probe +/// unanswered, a transmission lost with the terminal) is ours to remove. +fn unlinkKittyShm() void { + var name_buf: [64]u8 = undefined; + var n: u32 = 0; + while (n <= kitty_shm_serial) : (n += 1) { + if (kittyShmName(&name_buf, n)) |name| _ = libc.shm_unlink(name) else |_| {} + } +} + /// Write `bytes` to a fresh shm object; the terminal unlinks it after reading. fn kittyShmWrite(name: [:0]const u8, bytes: []const u8) bool { const fd = libc.shm_open(name, @bitCast(libc.O{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true }), @as(c_uint, 0o600)); @@ -1208,7 +1219,9 @@ fn nativePdfWheelTarget(core: *const pardes.Pardes, mouse: vaxis.Mouse) ?PdfWhee /// Where a session's terminal comes from: /dev/tty, as a plain `pardes` /// opens it; one open already (a session forked off its stub, /// daemon.forkSession); or none, until one is attached (`--detach`). -pub const Term = union(enum) { open, fd: posix.fd_t, none }; +/// The terminal a session starts on: /dev/tty, one its stub opened (an +/// upgraded session's is raw already, `termios` the cooked one), or none. +pub const Term = union(enum) { open, fd: struct { fd: posix.fd_t, termios: ?posix.termios = null }, none }; pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { const io = init.io; @@ -1230,7 +1243,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { 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)), + .fd => |t| try screen.enter(try Screen.ttyOn(io, &tty_buf, t.fd, t.termios)), .none => {}, } defer screen.leave(); @@ -1277,9 +1290,10 @@ const Screen = struct { /// `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. - fn ttyOn(io: std.Io, buf: []u8, fd: posix.fd_t) !vaxis.Tty { + /// An upgraded session's is raw already, `cooked` what it goes back to. + fn ttyOn(io: std.Io, buf: []u8, fd: posix.fd_t, cooked: ?posix.termios) !vaxis.Tty { if (comptime builtin.is_test) unreachable; // vaxis.Tty is its TestTty - const termios = try vaxis.Tty.makeRaw(fd); + const termios = cooked orelse try vaxis.Tty.makeRaw(fd); const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } }; return .{ .io = io, .fd = file, .termios = termios, .tty_writer = file.writerStreaming(io, buf) }; } @@ -1416,7 +1430,9 @@ fn localSession( pardes.syntax.stop(); } - var core = if (options.load_path) |lp| blk: { + var core = if (upgrade.resumed) |*rec| + try upgrade.load(gpa, allocs.pardes, options, rec) + else if (options.load_path) |lp| blk: { const bytes = try filesystem.readFile(gpa, lp); defer gpa.free(bytes); break :blk try pardes.dump.initFromDump(allocs.pardes, options, bytes); @@ -1505,9 +1521,17 @@ fn localSession( const host = sh.host(); core.host = host; + // An upgraded session's terminals, its own again. + if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec, screen); // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig). - core.unattended = !screen.on; + core.unattended = !sh.attached(); while (core.nextEffect()) |effect| core.perform(effect); + // ...and its shells, once a fresh session's own has a directory to + // adopt them in. + if (upgrade.resumed) |*rec| { + sh.resumePtys(rec); + while (core.nextEffect()) |effect| core.perform(effect); + } if (screen.on) try startInput(loop, screen); defer stopInput(io, &screen.thread); @@ -1520,15 +1544,15 @@ fn localSession( // 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| { + if (upgrade.resumed == null) 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); + if (fd >= 0) sh.attachTerminal(fd, -1, null); } - } + }; sh.threads_ok = true; for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { @@ -1536,18 +1560,12 @@ fn localSession( }; if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null; - // A terminal unlinks what it reads; whatever it never read (the probe - // unanswered, a transmission lost with the terminal) is ours to remove. - defer { - var name_buf: [64]u8 = undefined; - var n: u32 = 0; - while (n <= kitty_shm_serial) : (n += 1) { - if (kittyShmName(&name_buf, n)) |name| _ = libc.shm_unlink(name) else |_| {} - } - } + defer unlinkKittyShm(); while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Asked in the step (the word), done between steps: the session whole. + if (sh.upgrade_asked) |pane| sh.upgradeNow(pane); // A terminal (`pardes --attach`) or an adopted shell, handed on by // the daemon's thread to this loop; started after the first frame, // off a startup's way. @@ -1695,6 +1713,10 @@ const Shell = struct { drawn_ns: u64 = 0, input_ns: u64 = 0, focused_ns: u64 = 0, + /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). + upgrade_asked: ?u8 = null, + /// Its pty readers stopped for it: their end of file is not a shell's. + upgrading: bool = false, const Leaving = enum { detach, exit }; @@ -1723,6 +1745,7 @@ const Shell = struct { .watched = watched, .detach = detach, .adopt = adopt, + .upgrade = upgradeAsked, .spawn = spawn, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -1825,7 +1848,7 @@ const Shell = struct { /// `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 { + fn attachTerminal(s: *Shell, fd: posix.fd_t, stub: posix.fd_t, cooked: ?posix.termios) void { const live = s.term() orelse return; const screen = if (!live.on) live else extra: { const sc = s.gpa.create(Screen) catch break :extra null; @@ -1840,7 +1863,7 @@ const Shell = struct { 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 { + const tty = Screen.ttyOn(s.io, screen.buf, fd, cooked) catch { _ = libc.close(fd); if (extra) s.freeScreen(screen) else daemon.setState(true); return refuse(stub, "that terminal would not go raw"); @@ -1886,6 +1909,159 @@ const Shell = struct { daemon.adopt(); } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + const s = of(ctx); + if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); + s.upgrade_asked = pane; + } + + /// Upgrade, between steps: this process exec'd as the pardes now at the + /// path it was started from, the session in its dump, and its ptys, + /// terminals, stubs and daemon link kept open across (upgrade.zig). + /// Returns only when it could not be, the session going on as it was. + fn upgradeNow(s: *Shell, pane: u8) void { + if (comptime builtin.is_test) unreachable; // vaxis.Tty is its TestTty + s.upgrade_asked = null; + const core = s.core; + var why: [800]u8 = undefined; + const path = upgrade.exe() orelse return core.reportFailure(pane, "Upgrade: this process does not know the path it was started from"); + // 9P is served while the new binary is asked (its clients' clunks + // and reads after the word are answered), and not after. + pardes.turn.rest(); + const checked = upgrade.check(path, &why); + pardes.turn.wake(); + switch (checked) { + .no => |w| return core.reportFailure(pane, w), + .ok => {}, + } + // Nothing more read from a pty or a terminal, and what was read + // goes in; a reader's cancel posts an end of file, passed over. + s.upgrading = true; + defer s.upgrading = false; + for (&s.ptys) |*slot| if (slot.*) |*pt| pt.reader.cancel(s.io) catch {}; + for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread); + while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); + core.settle(); + var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; + for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); + for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ + .view = @intCast(k), + .fd = sc.tty.fd.handle, + .stub = if (sc.primary) daemon.stub else sc.stub, + .primary = sc.primary, + .termios = sc.tty.termios, + }); + var popped = false; + var at_buf: [1024:0]u8 = undefined; + const failed: []const u8 = failed: { + const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is"; + const bytes = pardes.dump.dumpBytes(core) catch |err| { + var words: [128]u8 = undefined; + break :failed std.fmt.bufPrint(&why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; + }; + defer s.gpa.free(bytes); + if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written"; + rec.dump = at; + rec.old = upgrade.openSelf(); + // What this process put in its terminals that the next puts in + // again: its images, and its keyboard mode, a stack. + for (s.terms) |slot| if (slot) |sc| if (sc.on) { + clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); + if (sc.vx.state.kitty_keyboard) sc.tty.writer().writeAll("\x1b[= 0) _ = libc.close(rec.old); + break :failed std.fmt.bufPrint(&why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; + }; + // As it was: its ptys read on, its terminals' keys as they were. + for (&s.ptys, 0..) |*slot, id| if (slot.*) |*pt| { + pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, id, s.gens[id], s.loop }) catch pt.reader; + }; + for (s.terms) |slot| if (slot) |sc| if (sc.on) { + if (popped) sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; + startInput(s.loop, sc) catch {}; + }; + core.reportFailure(pane, failed); + } + + /// An upgraded session's shells, running on: each its pane's pty again, + /// or, when its dump would not load (`fresh`), adopted as new panes. + fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { + for (rec.ptys[0..rec.nptys]) |k| { + const pane = if (rec.fresh) null else s.core.panes[k.pane]; + if (pane == null or !pane.?.isTerminal()) { + s.adoptShell(k.pid, k.fd); + continue; + } + const file: std.Io.File = .{ .handle = k.fd, .flags = .{ .nonblocking = false } }; + s.ptys[k.pane] = .{ .file = file, .pid = k.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; + } + } + + /// An upgraded session's terminals, each its view's again (`primary`, + /// run's, entered already); in a fresh session each but the primary a + /// view of its own, as `--attach` makes one. A view left with none + /// closes. + fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { + if (comptime builtin.is_test) unreachable; + const core = s.core; + if (rec.fresh) { + for (rec.ttys[0..rec.nttys]) |t| if (!t.primary) s.attachTerminal(t.fd, t.stub, t.termios); + return; + } + s.terms = @splat(null); + if (rec.nttys == 0) { + pardes.views.only(core); + s.terms[core.view] = primary; + return; + } + for (rec.ttys[0..rec.nttys]) |t| { + if (!pardes.views.isOpen(core, t.view) or s.terms[t.view] != null) { + if (t.primary) continue; + _ = libc.close(t.fd); + if (t.stub >= 0) daemon.tellDetached(t.stub); + continue; + } + const sc = if (t.primary) primary 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 = primary.env, .buf = buf, .kitty = .init(s.gpa) }; + const tty = Screen.ttyOn(s.io, sc.buf, t.fd, t.termios) catch { + s.freeScreen(sc); + break :extra null; + }; + sc.enter(tty) catch { + s.freeScreen(sc); + break :extra null; + }; + sc.setStub(t.stub); + startInput(s.loop, sc) catch {}; + sc.vx.queryTerminalSend(sc.tty.writer()) catch {}; + break :extra sc; + } orelse { + _ = libc.close(t.fd); + if (t.stub >= 0) daemon.tellDetached(t.stub); + continue; + }; + sc.feed.view = t.view; + s.terms[t.view] = sc; + } + for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null) core.closeView(@intCast(k)); + if (s.terms[core.view] == null) s.terms[core.view] = primary; + } + /// One shell of a dead session (`pid` 0: the last of them was told). fn adoptShell(s: *Shell, pid: posix.pid_t, fd: posix.fd_t) void { const core = s.core; @@ -2083,7 +2259,7 @@ const Shell = struct { core.quit = true; return true; }, - .attach => |a| s.attachTerminal(a.tty, a.stub), + .attach => |a| s.attachTerminal(a.tty, a.stub, null), .adopted => |a| s.adoptShell(a.pid, a.fd), .focus_in => core.update(.{ .focus = true }), .focus_out => { @@ -2102,7 +2278,7 @@ const Shell = struct { s.takeExits(); return true; }, - .pty_eof => |e| if (s.gens[e.id] == e.gen) { + .pty_eof => |e| if (s.gens[e.id] == e.gen and !s.upgrading) { if (s.ptys[e.id]) |*pt| { pt.reader.await(s.io) catch {}; // reader just finished; join it or its future leaks // A command's pty stays open until its child has exited: diff --git a/src/upgrade.zig b/src/upgrade.zig new file mode 100644 index 00000000..c49d2370 --- /dev/null +++ b/src/upgrade.zig @@ -0,0 +1,389 @@ +//! Upgrade (docs/next-daemon.md, "Phase 6: upgrade notes"): a running +//! session, and the daemon, moved onto the pardes installed since they +//! started, in place. +//! +//! At a step boundary a session writes its dump (phase 2) and execs the +//! path it was started from. Its pty masters, terminals, stubs and daemon +//! link stay open across the exec, named in a record on the new command +//! line: `--upgraded=` ahead of the arguments it was started with. +//! The pid stays, so its shells stay its children; the new process loads +//! the dump and takes those fds back (each host's `resume`). A new build +//! that cannot load the dump execs the old one again, from the copy this +//! process left open (Linux), and with none it starts afresh with the +//! shells adopted, the dump left for a Restore by hand. +const std = @import("std"); +const builtin = @import("builtin"); +const libc = std.c; +const posix = std.posix; +const pardes = @import("pardes.zig"); + +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; +pub const flag = "--upgraded="; + +/// What this build says it is (`pardes --version`'s first line). +pub const version_line = if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")" else "pardes " ++ pardes.version; + +// ---- the path it was started from ---- + +/// The path this process was started from, what Upgrade execs: a reinstall +/// puts the new build there, while /proc/self/exe names the old file, +/// deleted by then. +var exe_buf: [4096:0]u8 = undefined; +var exe_len: usize = 0; +/// The arguments it was started with (an upgrade's record left out). +var given: []const [:0]const u8 = &.{}; + +/// Its path asked of the system now, at its start (one readlink), and its +/// arguments kept; an upgraded process takes the path its record names +/// (the system would name a fallback's old binary, deleted). +pub fn remember(args: []const [:0]const u8) void { + given = if (args.len > 1 and std.mem.startsWith(u8, args[1], flag)) args[2..] else if (args.len > 0) args[1..] else &.{}; + if (resumed) |*rec| if (rec.exe.len > 0 and rec.exe.len < exe_buf.len) { + @memcpy(exe_buf[0..rec.exe.len], rec.exe); + exe_buf[rec.exe.len] = 0; + exe_len = rec.exe.len; + return; + }; + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; + exe_buf[n] = 0; + exe_len = n; +} + +pub fn exe() ?[:0]const u8 { + return if (exe_len == 0) null else exe_buf[0..exe_len :0]; +} + +// ---- the record ---- + +pub const Pty = struct { pane: u8, pid: libc.pid_t, fd: c_int }; +/// A terminal and its view: the primary one's stub is daemon.stub; its +/// termios is the cooked one it is put back to at the end (it is raw now). +pub const Tty = struct { view: u8, fd: c_int, stub: c_int, primary: bool, termios: posix.termios }; + +pub const Record = struct { + /// daemon.link and daemon.stub. + link: c_int = -1, + stub: c_int = -1, + /// The binary being replaced, open (Linux): a new one that cannot load + /// the dump execs it again. + old: c_int = -1, + /// A window was open (the GUI). + window: bool = false, + ptys: [pardes.MAX_PANES]Pty = undefined, + nptys: usize = 0, + ttys: [pardes.views.max]Tty = undefined, + nttys: usize = 0, + /// The path exec'd, the one a later Upgrade execs again. + exe: []const u8 = "", + /// The session's dump. + dump: []const u8 = "", + /// Why the new build could not load it: a start that went back to the + /// old binary says so. + refused: []const u8 = "", + /// This process's own: its dump would not load, so it started afresh + /// and adopts the shells. + fresh: bool = false, + + pub fn addPty(rec: *Record, pane: usize, pid: libc.pid_t, fd: c_int) void { + if (rec.nptys == rec.ptys.len or fd < 0) return; + rec.ptys[rec.nptys] = .{ .pane = @intCast(pane), .pid = pid, .fd = fd }; + rec.nptys += 1; + } + + pub fn addTty(rec: *Record, t: Tty) void { + if (rec.nttys == rec.ttys.len or t.fd < 0) return; + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } + + /// Every fd it names, close-on-exec (`on`) or kept across one. + pub fn closeOnExec(rec: *const Record, on: bool) void { + const set = struct { + fn f(fd: c_int, cloexec: bool) void { + if (fd >= 0) _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, if (cloexec) 1 else 0)); + } + }.f; + set(rec.link, on); + set(rec.stub, on); + set(rec.old, on); + for (rec.ptys[0..rec.nptys]) |p| set(p.fd, on); + for (rec.ttys[0..rec.nttys]) |t| { + set(t.fd, on); + set(t.stub, on); + } + } + + /// One `word value` a line, the dump's path last (its line is all of it). + pub fn encode(rec: *const Record, w: *std.Io.Writer) !void { + try w.print("link {d}\nstub {d}\nold {d}\nwindow {d}\n", .{ rec.link, rec.stub, rec.old, @intFromBool(rec.window) }); + for (rec.ptys[0..rec.nptys]) |p| try w.print("pty {d} {d} {d}\n", .{ p.pane, p.pid, p.fd }); + for (rec.ttys[0..rec.nttys]) |t| try w.print("tty {d} {d} {d} {d} {x}\n", .{ t.view, t.fd, t.stub, @intFromBool(t.primary), std.mem.asBytes(&t.termios) }); + if (rec.refused.len > 0) try w.print("refused {s}\n", .{rec.refused}); + try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); + } + + /// `text`'s record; its strings are slices of it. + pub fn decode(text: []const u8) !Record { + var rec: Record = .{}; + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (line.len == 0) continue; + const sp = std.mem.indexOfScalar(u8, line, ' ') orelse return error.BadRecord; + const word = line[0..sp]; + const rest = line[sp + 1 ..]; + var it = std.mem.tokenizeScalar(u8, rest, ' '); + if (std.mem.eql(u8, word, "link")) { + rec.link = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "stub")) { + rec.stub = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "old")) { + rec.old = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "window")) { + rec.window = std.mem.eql(u8, rest, "1"); + } else if (std.mem.eql(u8, word, "pty")) { + if (rec.nptys == rec.ptys.len) return error.BadRecord; + const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); + if (pane >= pardes.MAX_PANES) return error.BadRecord; + rec.ptys[rec.nptys] = .{ + .pane = pane, + .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + }; + rec.nptys += 1; + } else if (std.mem.eql(u8, word, "tty")) { + if (rec.nttys == rec.ttys.len) return error.BadRecord; + var t: Tty = .{ + .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), + .termios = undefined, + }; + const hex = it.next() orelse return error.BadRecord; + // Another build's termios is the kernel's, of one size. + if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; + _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } else if (std.mem.eql(u8, word, "refused")) { + rec.refused = rest; + } else if (std.mem.eql(u8, word, "exe")) { + rec.exe = rest; + } else if (std.mem.eql(u8, word, "dump")) { + rec.dump = rest; + } + // A word of a later build's record is passed over. + } + if (rec.dump.len == 0) return error.BadRecord; + return rec; + } +}; + +/// The record this process was started with (`--upgraded=`), set by main. +pub var resumed: ?Record = null; + +// ---- before: asking the new binary, and the exec ---- + +pub const Checked = union(enum) { ok: []const u8, no: []const u8 }; + +/// `path --version` run as it is: a binary not there, not a pardes, or one +/// that cannot start, found before anything is let go. Its first line +/// (`pardes `), or why not, in `buf`. +pub fn check(path: [:0]const u8, buf: []u8) Checked { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return .{ .no = "Upgrade: no pipe to ask the new pardes its version" }; + const argv = [_:null]?[*:0]const u8{ path, "--version", null }; + const pid = libc.fork(); + if (pid < 0) { + _ = libc.close(pipe[0]); + _ = libc.close(pipe[1]); + return .{ .no = "Upgrade: no process to ask the new pardes its version" }; + } + if (pid == 0) { + // Only async-signal-safe calls between fork and exec. + _ = libc.dup2(pipe[1], 1); + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + if (null_fd >= 0) { + _ = libc.dup2(null_fd, 0); + _ = libc.dup2(null_fd, 2); + } + _ = libc.execve(path, &argv, @ptrCast(libc.environ)); + libc._exit(127); + } + _ = libc.close(pipe[1]); + var len: usize = 0; + var waited = false; + while (len < buf.len) { + // A pardes answers --version in milliseconds; 10 s is one that hangs. + var fds = [_]libc.pollfd{.{ .fd = pipe[0], .events = libc.POLL.IN, .revents = 0 }}; + const ready = libc.poll(&fds, 1, 10_000); + if (ready < 0 and libc.errno(ready) == .INTR) continue; + if (ready <= 0) { + _ = libc.kill(pid, .KILL); + waited = true; + break; + } + const n = libc.read(pipe[0], buf[len..].ptr, buf.len - len); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) break; + len += @intCast(n); + } + _ = libc.close(pipe[0]); + var status: c_int = 0; + while (libc.waitpid(pid, &status, 0) < 0 and libc.errno(-1) == .INTR) {} + const s: u32 = @bitCast(status); + const said = buf[0..len]; + const first = said[0 .. std.mem.indexOfScalar(u8, said, '\n') orelse said.len]; + if (!waited and libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 0 and std.mem.startsWith(u8, first, "pardes ")) return .{ .ok = first }; + // The words go where the line was: the rest of `buf`. + var tail: [96]u8 = undefined; + const how = if (waited) + "did not answer --version in 10 s" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 127) + "would not start" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) != 0) + std.fmt.bufPrint(&tail, "exited {d} at --version", .{libc.W.EXITSTATUS(s)}) catch "failed at --version" + else if (libc.W.IFSIGNALED(s)) + std.fmt.bufPrint(&tail, "died of signal {d} at --version", .{libc.W.TERMSIG(s)}) catch "died at --version" + else + "does not answer --version as a pardes does"; + return .{ .no = std.fmt.bufPrint(buf, "Upgrade: {s} {s}; this session stays as it is", .{ path[0..@min(path.len, 400)], how }) catch "Upgrade: the new pardes would not start; this session stays as it is" }; +} + +/// The binary this process runs, open, for a new one to fall back to: on +/// Linux the file even once deleted. ponytail: FreeBSD has no /proc by +/// default, so a new build there that cannot load a dump starts afresh. +pub fn openSelf() c_int { + if (comptime builtin.os.tag != .linux) return -1; + return libc.open("/proc/self/exe", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); +} + +/// This process exec'd as `path` (or the binary open on `old`), the record +/// on its command line and the fds it names kept open. Returns only when +/// the exec failed, those fds close-on-exec again: why, in words. +pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const Record) []const u8 { + var text: std.Io.Writer.Allocating = .init(gpa); + defer text.deinit(); + text.writer.writeAll(flag) catch return "no memory for its record"; + rec.encode(&text.writer) catch return "no memory for its record"; + text.writer.writeByte(0) catch return "no memory for its record"; + const record: [*:0]const u8 = @ptrCast(text.written().ptr); + const argv = gpa.allocSentinel(?[*:0]const u8, given.len + 2, null) catch return "no memory for its arguments"; + defer gpa.free(argv); + argv[0] = if (path) |p| p.ptr else if (exe()) |e| e.ptr else "pardes"; + argv[1] = record; + for (given, argv[2..]) |a, *slot| slot.* = a.ptr; + rec.closeOnExec(false); + const envp: [*:null]const ?[*:0]const u8 = @ptrCast(libc.environ); + const r = if (path) |p| + libc.execve(p, argv.ptr, envp) + else if (comptime builtin.os.tag == .linux) + libc.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true }) + else + -1; + const e = libc.errno(r); + rec.closeOnExec(true); + return switch (e) { + .NOENT => "it is not there", + .ACCES, .PERM => "it may not be run", + .NOEXEC => "it is not a program this system runs", + .@"2BIG" => "its record and arguments are too long", + .NOMEM => "no memory for it", + .TXTBSY => "it is still being written", + else => "the exec failed", + }; +} + +/// The dump of a session to `path` (0600, replaced): false when it could +/// not be written whole. +pub fn writeDump(path: [:0]const u8, bytes: []const u8) bool { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return false; + defer _ = libc.close(fd); + var at: usize = 0; + while (at < bytes.len) { + const n = libc.write(fd, bytes[at..].ptr, bytes.len - at); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) { + _ = libc.unlink(path); + return false; + } + at += @intCast(n); + } + return true; +} + +// ---- after: the new process ---- + +/// The core an upgraded process starts with: the session its dump is. A +/// build that cannot load it execs the binary it replaced to load it; with +/// none, a fresh session (`fresh`) the host adopts the shells into, the +/// dump left where a Restore finds it. `PARDES_TEST_UPGRADE_REFUSE` refuses +/// a load as such a build would (test/daemon.py's failing upgrade), in the +/// first start only, or (`all`) in the start it falls back to as well. +pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Options, rec: *Record) !*pardes.Pardes { + var kept: [pardes.MAX_PANES]bool = @splat(false); + for (rec.ptys[0..rec.nptys]) |p| kept[p.pane] = true; + var path_buf: [4096:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{rec.dump}, 0) catch ""; + const loaded = loaded: { + if (libc.getenv("PARDES_TEST_UPGRADE_REFUSE")) |how| if (rec.refused.len == 0 or std.mem.eql(u8, std.mem.span(how), "all")) + break :loaded error.RefusedByTest; + const bytes = pardes.filesystem.readFile(gpa, rec.dump) catch |err| break :loaded err; + defer gpa.free(bytes); + break :loaded pardes.dump.resumeFromDump(core_gpa, opts, bytes, &kept); + }; + var buf: [700]u8 = undefined; + if (loaded) |p| { + _ = libc.unlink(path); + p.setMessage(p.active, if (rec.refused.len > 0) + std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" + else + "Upgrade: now " ++ version_line); + return p; + } else |err| { + var words_buf: [128]u8 = undefined; + const why = if (err == error.RefusedByTest) "refused by PARDES_TEST_UPGRADE_REFUSE" else pardes.Messages.errorWords(err, &words_buf); + // The binary it replaced loads its own dump: tried once, never from + // a start that is that fallback already. + if (rec.old >= 0 and rec.refused.len == 0) { + var again = rec.*; + again.refused = why; + again.old = -1; + _ = exec(gpa, null, rec.old, &again); + } + if (rec.old >= 0) _ = libc.close(rec.old); + rec.old = -1; + rec.fresh = true; + var o = opts; + o.load_path = null; + const p = try pardes.Pardes.init(core_gpa, o); + p.setMessage(p.active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, rec.dump[0..@min(rec.dump.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); + return p; + } +} + +test "an upgrade's record reads back as it was written, and refuses one cut short" { + var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/run/u/pardes-upgrade-1.dump" }; + rec.addPty(3, 1234, 11); + rec.addPty(0, 99, 12); + var tio: posix.termios = std.mem.zeroes(posix.termios); + std.mem.asBytes(&tio)[0] = 0x5a; + rec.addTty(.{ .view = 2, .fd = 13, .stub = 14, .primary = true, .termios = tio }); + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try rec.encode(&w); + const back = try Record.decode(w.buffered()); + try std.testing.expectEqual(@as(c_int, 5), back.link); + try std.testing.expectEqual(@as(c_int, 9), back.old); + try std.testing.expect(back.window); + try std.testing.expectEqual(@as(usize, 2), back.nptys); + try std.testing.expectEqual(Pty{ .pane = 3, .pid = 1234, .fd = 11 }, back.ptys[0]); + try std.testing.expectEqual(@as(usize, 1), back.nttys); + try std.testing.expect(back.ttys[0].primary and back.ttys[0].view == 2 and back.ttys[0].stub == 14); + try std.testing.expectEqual(@as(u8, 0x5a), std.mem.asBytes(&back.ttys[0].termios)[0]); + try std.testing.expectEqualStrings("/a b/pardes", back.exe); + try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", back.dump); + try std.testing.expectError(error.BadRecord, Record.decode("link 5\n")); + try std.testing.expectError(error.BadRecord, Record.decode("tty 0 1 2 1 abc\ndump x\n")); +} diff --git a/test/daemon.py b/test/daemon.py index 2be3f355..7d35e427 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -506,8 +506,321 @@ def gui(binary, cycles): shutil.rmtree(root, ignore_errors=True) +def screen_text(client): + shown = client.screen() + cells = ''.join(cell[0] for cell in shown['cells']) + return '\n'.join(cells[i:i + shown['cols']] for i in range(0, len(cells), shown['cols'])) + + +def reconnect(sock, timeout=20): + """The session's 9P again, once its upgraded process listens.""" + return wait_for('the upgraded session\'s 9P', lambda: dial(sock), timeout=timeout) + + +def dial(sock): + try: + return Client(sock) + except (OSError, EOFError): # mid-exec: refused, or cut after the accept + return None + + +def saying(sock, words, timeout=30): + """A connection to the session once its screen shows `words`: dialled + afresh each look, since the process it talks to may exec meanwhile.""" + last = [''] + + def look(): + c = dial(sock) + if c is None: + return None + try: + last[0] = screen_text(c) + if words in last[0]: + return c + except (OSError, EOFError): + pass + c.socket.close() + return None + try: + return wait_for(f'the session saying {words!r}', look, timeout=timeout) + except TimeoutError: + raise TimeoutError(f'the session saying {words!r}; it shows:\n{last[0]}') + + +def exe_of(pid): + """What /proc says the process runs: (its path, whether that file is the + one at the path now).""" + link = os.readlink(f'/proc/{pid}/exe') if procfs else ps('-o', 'comm=', '-p', str(pid)).decode().strip() + if not procfs: + return link, True + try: + same = os.stat(f'/proc/{pid}/exe').st_ino == os.stat(link).st_ino + except OSError: + same = False + return link, same + + +def ppid(pid): + if not procfs: + return int(ps('-o', 'ppid=', '-p', str(pid))) + return int(open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[1]) + + +def install(binary, at): + """As `zig build` and install(1) put a build in place: a new file + renamed over the old, which a process running it keeps, deleted.""" + tmp = at.with_name(at.name + '.new') + subprocess.run(['cp', '--reflink=auto', os.fspath(binary), os.fspath(tmp)], check=True) + os.chmod(tmp, 0o755) + os.replace(tmp, at) + + +def upgrades(old, new): + """Upgrade (docs/next-daemon.md, phase 6): a tty session with an unsaved + edit, a shell running `sleep 1000; echo done` and two terminals (two + views) moved onto another build installed at the path it started from, + the daemon with it; then a build that refuses the dump, which falls back + to the running one, and one that refuses it everywhere, which starts + afresh with the shells adopted and the dump kept.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rtu-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'bin').mkdir() + binary = root / 'bin' / 'pardes' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + started = [] + try: + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=work'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-work.sock' + wait_for('the session\'s 9P socket', sock.exists) + client = Client(sock) + session = int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) + started.append(session) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + # A second terminal: a view of its own. + term2 = Term(30, 100) + stub2 = term2.spawn([str(binary), '--attach=work'], env) + started.append(stub2.pid) + wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) + old_build = semantics(old) + replica = Replica(root / 'pardes-replica.sock') + assert replica.hello('work', old_build)[0] == 'welcome' + before = len(term.text()), len(term2.text()) + + install(new, binary) + assert exe_of(session)[1] is False, 'the session runs the old build, deleted' + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + wait_for('the session on the new build', lambda: exe_of(session) == (str(binary), True), timeout=30) + client = reconnect(sock) + assert int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) == session + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + assert alive(daemon) and stub.poll() is None and stub2.poll() is None + # A replica of the old build is told why it is let go. + tag, body = replica.read() + assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) + replica.close() + # The edit and its undo. + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + assert any(row.split()[0] == str(text).encode() and row.split()[2] == b'1' for row in client.read('/index').splitlines()), client.read('/index') + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n', client.read(f'/pane/{text}/body') + # The shell, its job and its pty: the same, the session's still. + assert alive(shell_pid) and ppid(shell_pid) == session and alive(sleeper) + # Both terminals drawn again, each its own view. + wait_for('the first terminal redrawn', lambda: b'\x1b[?1049h' in term.text()[before[0]:]) + wait_for('the second terminal redrawn', lambda: b'\x1b[?1049h' in term2.text()[before[1]:]) + wait_for('the word said', lambda: 'Upgrade: now pardes' in screen_text(client)) + # The daemon lists the session under its new build. + new_build = semantics(new) + assert new_build != old_build + said = Replica(root / 'pardes-replica.sock').hello('work', new_build) + assert said == ('welcome', session, 'work'), said + said = Replica(root / 'pardes-replica.sock').hello('work', old_build) + assert said[0] == 'refused' and 'another build' in said[1], said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: session {session}, its daemon {daemon}, shell {shell_pid} and two terminals on the new build; ' + 'the edit, its undo and the job kept') + + # A build that cannot load the dump: back to the one running. + refuser = root / 'refuser' + refuser.write_text('#!/bin/sh\n# A pardes whose load refuses the dump (upgrade.zig).\n' + f'case "$1" in --upgraded=*) PARDES_TEST_UPGRADE_REFUSE=1 exec {new} "$@";; esac\n' + f'exec {new} "$@"\n') + refuser.chmod(0o755) + running = os.stat(f'/proc/{session}/exe').st_ino + install(refuser, binary) + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'it goes on as') + assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' + assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') + + # One that refuses it everywhere: a fresh session, the shells adopted. + refuser.write_text(refuser.read_text().replace('=1 exec', '=all exec')) + install(refuser, binary) + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'brings the rest back') + kept = root / '.local' / 'share' / 'pardes' / f'pardes-upgrade-{session}.dump' + assert kept.exists(), list((root / '.local' / 'share' / 'pardes').iterdir()) + assert alive(shell_pid) and ppid(shell_pid) == session + pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} + adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] + assert adopted, ('the shell is a pane of the fresh session', shell_pid, pids, client.read('/index')) + print(f'upgrade: a build that refused it everywhere started afresh, shell {shell_pid} adopted, the dump kept') + + # The daemon, three times upgraded, still holds the shell's master: + # the session killed outright, another adopts it. + install(new, binary) + os.kill(session, signal.SIGKILL) + stub.wait(timeout=10) + stub2.wait(timeout=10) + client.socket.close() + term.close() + term2.close() + wait_for('the killed session gone', lambda: not alive(session)) + time.sleep(.3) + assert alive(shell_pid) and alive(daemon) + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=again'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-again.sock' + wait_for('the next session', sock.exists) + client = Client(sock) + started.append(int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid'])) + before = set(terminals(client)) + client.write(f'/pane/{min(before)}/exec', b'Adopt\n') + pane = wait_for('the adopted pane', lambda: set(terminals(client)) - before).pop() + assert run(client, pane, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print(f'upgrade: the upgraded daemon kept shell {shell_pid} through kill -9 of its session, and it was adopted') + client.write(f'/pane/{pane}/exec', b'Exit\n') + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the shell hung up', lambda: not alive(shell_pid)) + wait_for('the daemon gone once idle', lambda: not alive(daemon), timeout=5) + left = [p for p in processes(root) if alive(p)] + assert not left, [cmdline(p) for p in left] + print('upgrade: nothing left behind') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def gui_upgrades(old, new): + """Upgrade in a window session (test mode: hidden windows, captured): + two windows, an unsaved edit and a shell's job, onto another build.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + root = Path(tempfile.mkdtemp(prefix='rtgu-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + display = Path(os.environ.get('WAYLAND_DISPLAY', 'wayland-0')) + if not display.is_absolute(): + display = Path(os.environ.get('XDG_RUNTIME_DIR', f'/run/user/{os.getuid()}')) / display + caps = root / 'caps' + caps.mkdir() + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + SHELL='/bin/bash', PARDES_NOTIME='1', PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(caps), + SDL_VIDEODRIVER='wayland', WAYLAND_DISPLAY=str(display), PARDES_DAEMON='on') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'bin').mkdir() + binary = root / 'bin' / 'pardes-gui' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + feed_r, feed_w = os.pipe() + proc = subprocess.Popen([str(binary), '--9p=win'], env=env, cwd=root, stdin=feed_r, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + os.close(feed_r) + started = [proc.pid] + try: + sock = root / 'pardes-9p-win.sock' + wait_for('the window session', sock.exists, timeout=20) + client = Client(sock) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + attach = subprocess.run([str(binary), '--attach=win'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert attach.returncode == 0, attach.stderr + frames = [caps / 'latest.ppm', caps / 'view-1' / 'latest.ppm'] + wait_for('both windows drawn', lambda: all(f.exists() for f in frames)) + install(new, binary) + time.sleep(.05) + mark = time.time() + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + wait_for('the session on the new build', lambda: exe_of(proc.pid) == (str(binary), True), timeout=30) + client = reconnect(sock) + assert proc.poll() is None + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert alive(shell_pid) and ppid(shell_pid) == proc.pid and alive(sleeper) + wait_for('both windows drawn again', lambda: all(f.stat().st_mtime > mark for f in frames)) + said = Replica(root / 'pardes-replica.sock').hello('win', semantics(new)) + assert said == ('welcome', proc.pid, 'win'), said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: window session {proc.pid}, its daemon, shell {shell_pid} and two windows on the new build; ' + 'the edit, its undo and the job kept') + client.write(f'/pane/{shell}/exec', b'Exit\n') + proc.wait(timeout=10) + client.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=5) + print('upgrade: window session ended, nothing left behind') + finally: + os.close(feed_w) + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + if __name__ == '__main__': - if sys.argv[1:2] == ['--ping']: + if sys.argv[1:3] == ['--upgrade', '--gui']: + gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve()) + elif sys.argv[1:2] == ['--upgrade']: + upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve()) + elif sys.argv[1:2] == ['--ping']: ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) elif '--gui' in sys.argv: args = [a for a in sys.argv[1:] if a != '--gui'] -- cgit v1.3 From cb2688602b3f668790a9c35890973af80143697f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 06:25:57 -0300 Subject: gui: the window a replica client (pardes-gui --attach=: and :, as the tty's, far through ssh as amort's: its blobs by hash and its stream in zstd) and a window session serving replicas (a terminal's client or a window's): the client feeds its replica what its window does, read over its own view (views.within) and applied after, draws its view between steps, takes the stream on its own reader and rejoins as the tty's does (replica.Link, the client's half shared with it, the far path's Have with it); the session takes clients at its step boundary, one person's input a wait, a client's view no window of its own, its font and opacity answers taped; a client says its frontend first (Event.frontend, in wire 16 beside the far word) and the tty and window builds step alike (one desktop semantics: each has the other's words, which a view takes by its own frontend, builtins.registry.here); a check asked 300 ms after the last input and check, not per wake; test/daemon.py --window-clients Co-Authored-By: Claude Opus 5.5 --- src/Output.zig | 22 +- src/builtins.zig | 58 ++++- src/builtins_json.zig | 13 +- src/config.zig | 2 +- src/detached/wire.zig | 35 ++- src/draw.zig | 26 +-- src/exec.zig | 8 +- src/gui/gui.zig | 573 +++++++++++++++++++++++++++++++++++++++++++++----- src/main.zig | 27 ++- src/ninep/ctl.zig | 38 ++-- src/ninep/tree.zig | 2 +- src/pardes.zig | 50 ++++- src/replica.zig | 310 +++++++++++++++++++++++++++ src/tape.zig | 15 ++ src/tty/tty.zig | 311 +++++---------------------- src/views.zig | 54 +++-- test/daemon.py | 188 +++++++++++++++++ test/output.zig | 9 +- 18 files changed, 1351 insertions(+), 390 deletions(-) (limited to 'src/config.zig') diff --git a/src/Output.zig b/src/Output.zig index 29e49180..359db1cf 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -905,7 +905,7 @@ fn openStepped(p: *Pardes, id: usize, from: Origin, content: []u8) !void { look.armLookWalk(p, free); } -fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { +fn helpContent(p: *const Pardes, gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { const full_header = "pardes builtins, and how to run each:\nSPC and its keys, a chord, a button, the\ntopbar - or the name, executed anywhere.\n\n"; const group_header = "pardes builtins under SPC"; const calls_help = "\nCallstack: static caller chains,\nwith signatures and clickable call sites.\nCallers / Callees: one direction at a time.\n"; @@ -934,8 +934,10 @@ fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { } for (pardes.builtin_rows) |row| { // a path-less builtin filters as the empty path: in the full listing - // (which starts with nothing) and out of every group + // (which starts with nothing) and out of every group; a window's own + // word is none in a terminal's view if (!std.mem.startsWith(u8, row.path orelse "", prefix)) continue; + if (!builtins.registry.here(p, row.cmd)) continue; try w.print("{s}\n", .{row.line}); } if (show_calls_help) try w.writeAll(calls_help); @@ -966,21 +968,23 @@ fn helpContent(gpa: std.mem.Allocator, prefix: []const u8) ![]u8 { } pub fn openHelp(p: *Pardes, id: usize, prefix: []const u8) !void { - const content = try helpContent(p.gpa, prefix); + const content = try helpContent(p, p.gpa, prefix); // content is handed off unfreed on purpose: openRead adopts it or frees // it, and nothing between the alloc above and this line can fail. return openRead(p, id, .{ .cmd = .Help }, prefix, content); } test "full Help renders builtin rows, a call hierarchy guide, then language keys" { - const content = try helpContent(std.testing.allocator, ""); + const core = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + const content = try helpContent(core, std.testing.allocator, ""); defer std.testing.allocator.free(content); // FIRST blank line: the end of the header (the footer opens with one too) const body = content[(std.mem.indexOf(u8, content, "\n\n") orelse return error.MissingHelpHeader) + 2 ..]; var lines = std.mem.splitScalar(u8, body, '\n'); - for (pardes.builtin_rows) |row| + for (pardes.builtin_rows) |row| if (builtins.registry.here(core, row.cmd)) try std.testing.expectEqualStrings(row.line, lines.next() orelse return error.MissingBuiltinHelpRow); // The short call guide disambiguates the three related commands. @@ -995,7 +999,7 @@ test "full Help renders builtin rows, a call hierarchy guide, then language keys return error.MissingLanguageKeys); try std.testing.expect(std.mem.indexOf(u8, body, "\ngd gD gy gi gr goto: definition,\n") != null); // The language group also explains call commands, without the motion footer. - const group = try helpContent(std.testing.allocator, "l"); + const group = try helpContent(core, std.testing.allocator, "l"); defer std.testing.allocator.free(group); try std.testing.expect(std.mem.indexOf(u8, group, "language keys") == null); try std.testing.expect(std.mem.indexOf(u8, group, "Callstack: static caller chains") != null); @@ -1013,13 +1017,13 @@ pub fn openConfig(p: *Pardes, id: usize) !void { .locations = located.written(), .default_dump_dir = if (host_fs) (pardes.dump.defaultDirectory(&dir_buf) orelse "") else "", .startup_config_path = p.opts.startup_config_path, - .platform = @tagName(pardes.platform), + .platform = builtins.viewPlatform(p), .theme_name = p.theme().name, .theme_file = pardes.colors.loadedThemeFile(p), .compiled_default_shell = config.defaultShell(), .hover_delay_frames = config.look_preview_delay_frames, .native_images = p.native_images, - .capabilities = builtins.capabilities, + .capabilities = builtins.viewCapabilities(p), .state = &p.settings, }); const content = try out.toOwnedSlice(); @@ -1061,7 +1065,7 @@ pub fn openEffectCode(p: *Pardes, id: usize, argument: []const u8) !void { .transition, .scene => {}, else => return error.NotAnEffect, } - if (!setting.enabled(builtins.capabilities)) return error.EffectUnavailable; + if (!setting.enabled(builtins.viewCapabilities(p))) return error.EffectUnavailable; // Without -Dembed-sources the paths would name nothing this build can open. if (!limits.embedded_sources) return error.EffectUnavailable; const paths = effect_sources.forSetting(setting) orelse diff --git a/src/builtins.zig b/src/builtins.zig index 239edeaf..69ac3113 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -16,7 +16,8 @@ const builtin = @import("builtin"); const Header = @import("esp32p4_gpio.zig").Header; const limits = @import("memory.zig").limits; -pub const capabilities: config.Runtime.Capabilities = .{ +/// What this build's own frontend draws and its host does: the render's. +pub const frontend: config.Runtime.Capabilities = .{ .font_picker = pardes.font_picker, .panel_transitions = pardes.hosted, .scene_shaders = pardes.platform == .gui or pardes.platform == .macos, @@ -32,6 +33,30 @@ pub const capabilities: config.Runtime.Capabilities = .{ .lift = pardes.platform == .gui, }; +/// The terminal's build and the window's (docs/next-daemon.md, "Phase 6: +/// GUI replica notes"): each has the other's words, which a view takes as +/// its own frontend does (`viewCapabilities`), so a terminal's replica of a +/// window's session steps as the window's own does, and back. +pub const desktop = pardes.platform == .tty or pardes.platform == .gui; +const window_capabilities: config.Runtime.Capabilities = .{ .font_picker = true, .panel_transitions = true, .scene_shaders = true, .tagline_font_size = true, .window_opacity = true, .window_blur = false, .ligatures = true, .lift = true }; +const terminal_capabilities: config.Runtime.Capabilities = .{ .font_picker = false, .panel_transitions = true, .scene_shaders = false, .tagline_font_size = false, .window_opacity = false, .window_blur = false, .ligatures = false, .lift = false }; + +/// The words this build has: a desktop build's are the window's. +pub const capabilities: config.Runtime.Capabilities = if (desktop) window_capabilities else frontend; + +/// What the live view's frontend takes: a window's, or a terminal's +/// (`Pardes.windowed`, which its client says, Event.frontend). +pub fn viewCapabilities(p: *const Pardes) config.Runtime.Capabilities { + if (comptime !desktop) return frontend; + return if (p.windowed) window_capabilities else terminal_capabilities; +} + +/// The live view's frontend, by name (the Settings report's platform). +pub fn viewPlatform(p: *const Pardes) []const u8 { + if (comptime !desktop) return @tagName(pardes.platform); + return if (p.windowed) "gui" else "tty"; +} + /// Where a builtin acts: at the pane it runs from, or on the editor as a /// whole. acme keeps its window verbs on a window's ctl and its session /// commands elsewhere, as webfs and upas/fs keep session settings on a root @@ -93,6 +118,7 @@ fn isEnabled(comptime T: type) bool { return !@hasDecl(T, "enabled") or T.enabled; } + /// Why `word`, a builtin some build has, is none in this one: `GUI-only` /// for the GUI's own (`gui_only`), else `not in this build`; null for a /// word that is one here, or in no build. @@ -155,6 +181,21 @@ pub const OutputTraits = struct { // Keep enum-dependent signatures out of the outer declaration walk. pub const registry = struct { + /// Whether builtin `b` is one the live view's frontend takes: a window's + /// own words (a builtin that says `in_window`, the settings a terminal + /// cannot show) in a window's view alone. The rest of a build's words are + /// every view's. + pub fn here(p: *const Pardes, b: Builtin()) bool { + return hereFor(b, viewCapabilities(p), !desktop or p.windowed); + } + + /// `here` for a frontend of these capabilities, windowed or not. + pub fn hereFor(b: Builtin(), caps: config.Runtime.Capabilities, windowed: bool) bool { + inline for (comptime manualBuiltinList(), 0..) |T, i| if (@backingInt(b) == i) return !@hasDecl(T, "in_window") or windowed; + inline for (comptime settingList(), comptime manualBuiltinList().len..) |setting, i| if (@backingInt(b) == i) return setting.enabled(caps); + unreachable; + } + pub fn Builtin() type { // Duplicate-name validation compares every pair. @setEvalBranchQuota(200_000); @@ -453,11 +494,14 @@ test "Look does not announce itself on the message row; Del still does" { } test "Ligatures is a setting only in the shell that shapes text" { - const exists = @hasField(registry.Builtin(), "Ligatures"); - try std.testing.expectEqual(pardes.platform == .gui, exists); const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer p.deinit(); - try std.testing.expectEqual(exists, p.executeBuiltinLine(p.active, "Ligatures off")); + // A desktop build has the word, which a window's view takes alone. + try std.testing.expectEqual(desktop, @hasField(registry.Builtin(), "Ligatures")); + const exists = registry.here(p, .Ligatures); + try std.testing.expectEqual(pardes.platform == .gui, exists); + // Taken either way: run, or said to be GUI-only. + try std.testing.expect(p.executeBuiltinLine(p.active, "Ligatures off")); try std.testing.expectEqual(!exists, p.settings.ligatures); var storage: [8192]u8 = undefined; var out: std.Io.Writer = .fixed(&storage); @@ -468,7 +512,7 @@ test "Ligatures is a setting only in the shell that shapes text" { .compiled_default_shell = config.defaultShell(), .hover_delay_frames = null, .native_images = false, - .capabilities = capabilities, + .capabilities = viewCapabilities(p), .state = &p.settings, }); try std.testing.expectEqual(exists, std.mem.indexOf(u8, storage[0..out.end], "Ligatures off\n") != null); @@ -912,6 +956,7 @@ pub const Fonts = struct { pub const output: OutputTraits = .{ .name = config.fonts_buffer, .steps = true, .commands = true }; pub const enabled = capabilities.font_picker; pub const gui_only = true; + pub const in_window = true; pub fn run(c: Ctx) void { if (comptime enabled) apply(c) else unreachable; } @@ -1665,8 +1710,9 @@ pub const Pet = struct { pub const scope: Scope = .session; pub const takes_arg = true; pub const requires_arg = true; - pub const enabled = pardes.platform == .gui; + pub const enabled = desktop; pub const gui_only = true; + pub const in_window = true; pub fn run(c: Ctx) void { const name = std.mem.trim(u8, c.arg orelse return, " \t\r\n"); c.p.settings.pet = std.meta.stringToEnum(@TypeOf(c.p.settings.pet), name) orelse return; diff --git a/src/builtins_json.zig b/src/builtins_json.zig index 3598a3e2..489f1e22 100644 --- a/src/builtins_json.zig +++ b/src/builtins_json.zig @@ -20,9 +20,16 @@ fn lessName(_: void, a: Entry, b: Entry) bool { } pub fn write(w: *std.Io.Writer) !void { - var order: [pardes.builtin_rows.len]Entry = undefined; - for (pardes.builtin_rows, 0..) |row, i| order[i] = .{ .name = @tagName(row.cmd), .row = i }; - std.mem.sort(Entry, &order, {}, lessName); + var all: [pardes.builtin_rows.len]Entry = undefined; + var len: usize = 0; + // This build's own frontend's words (a desktop build has the other's + // too, which a view of its kind takes: builtins.zig `here`). + for (pardes.builtin_rows, 0..) |row, i| if (pardes.builtins.registry.hereFor(row.cmd, pardes.builtins.frontend, pardes.platform != .tty)) { + all[len] = .{ .name = @tagName(row.cmd), .row = i }; + len += 1; + }; + const order = all[0..len]; + std.mem.sort(Entry, order, {}, lessName); try w.writeAll("[\n"); for (order, 0..) |e, n| { const row = pardes.builtin_rows[e.row]; diff --git a/src/config.zig b/src/config.zig index 742e9010..ebca14ea 100644 --- a/src/config.zig +++ b/src/config.zig @@ -164,7 +164,7 @@ pub const leader_path = paths: { table.set(.Font, null); table.set(.TaglineSize, null); } - if (pardes.platform == .gui) table.set(.Pet, null); + if (builtins.Pet.enabled) table.set(.Pet, null); table.set(.MessageAnimation, null); table.set(.MessageLinger, null); table.set(.MessageFall, null); diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 2f2b95c8..119601fc 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -11,28 +11,40 @@ const config = @import("pardes_config"); /// 16: a far client says so, the session sends it its blobs' hashes before /// the rest and only the blobs it asks for, all of it in zstd (phase 6's -/// content cache and zstd notes); 15: the session streams a replica its -/// dump and units, and terminal deltas both ways (phase 5 part B); 14: the hello names a session and +/// content cache and zstd notes), and a client says its frontend (a window +/// or a terminal) as its first input, a window replicating a terminal's +/// session and back (phase 6's GUI replica notes); 15: the session streams +/// a replica its dump and units, and terminal deltas both ways (phase 5 +/// part B); 14: the hello names a session and /// carries the semantics hash, and a ping is answered (phase 5's /// groundwork); 13: the frames went (phase 3). pub const version: u16 = 16; +/// The terminal's build and the window's step alike (`semantics`). +const family = switch (config.platform) { + .tty, .gui => "desktop", + else => @tagName(config.platform), +}; + /// What makes two cores step differently, hashed: a replica's hello carries /// its own, and one of another build is refused. The protocol, the release /// and commit, the platform and build options, every dependency's content /// hash (grammars, ghostty, Unicode) and the walker codec's assumptions /// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD /// session is the main case, and what differs there is the host's, taped. +/// Nor the terminal's build against the window's: their cores step alike, +/// the GUI's own words by the view's frontend (builtins.zig `here`), so a +/// window replicates a terminal's session and a terminal a window's. /// ponytail: an unstamped build (Debug with -Dplatform, every test build) /// has no commit, so two dev trees of one release are told apart only by /// their options and dependencies; -Dstamp-commit stamps one. pub const semantics: u64 = blk: { @setEvalBranchQuota(1_000_000); break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint( - "wire {d}\npardes {s} {s}\n{t} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", + "wire {d}\npardes {s} {s}\n{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}", .{ version, config.version, config.commit orelse "unstamped", - config.platform, config.syntax_highlighting, config.syntax_zig_grammar, + family, config.syntax_highlighting, config.syntax_zig_grammar, config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf, config.theme_animation, config.workspace_tag, config.embed_sources, config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize), @@ -78,6 +90,8 @@ pub const ClientTag = enum(u8) { touch_scroll = 0x1c, pointer_leave = 0x1d, focus = 0x1e, + /// Which frontend the client is: a window, or a terminal. + frontend = 0x20, }; const ButtonTag = enum(u8) { @@ -505,6 +519,11 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { .touch_scroll => |v| try w.writeInt(u32, @bitCast(v), .little), .pointer_leave => {}, .focus => |focused| try w.writeByte(@intFromBool(focused)), + .frontend => |f| { + try w.writeByte(@intFromBool(f.windowed)); + try w.writeByte(@intFromBool(f.native_images)); + try w.writeByte(@intFromBool(f.native_image_patches)); + }, // See `clientTag`: no tag, so nothing to encode. .output, .eof, .exited, .lsp_resp, .pipe_resp, .search, .file_changed, .handed, .tick => unreachable, }, @@ -577,6 +596,7 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { .touch_scroll => .{ .event = .{ .touch_scroll = try getF32(&r) } }, .pointer_leave => .{ .event = .pointer_leave }, .focus => .{ .event = .{ .focus = try getBool(&r) } }, + .frontend => .{ .event = .{ .frontend = .{ .windowed = try getBool(&r), .native_images = try getBool(&r), .native_image_patches = try getBool(&r) } } }, }; try end(&r); return msg; @@ -594,7 +614,7 @@ pub fn clientBound(msg: ClientMsg) usize { .bye, .ping, .check, .far => 0, .screen_ack, .want => |bytes| bytes.len, .event => |ev| switch (ev) { - .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, + .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus, .frontend => 0, .key => |k| k.text.len, .paste => |b| b.len, .command => |line| line.len, @@ -732,9 +752,12 @@ test "detached wire: a session is never told to do a frontend's remembering" { // ...and how far it is, and which of the join's blobs it lacks. "far", "want", // ...and everything a person can do to a window. - "key", "mouse", "resize", + "key", "mouse", "resize", "paste", "command", "pdf_scroll", "pinch", "touch_scroll", "pointer_leave", "focus", + // ...and which of its frontends the person sits at: a window, or a + // terminal (what the view draws, and the words it takes). + "frontend", }; // One: the tag set is exactly that, named rather than counted, so diff --git a/src/draw.zig b/src/draw.zig index 03bc0cf9..8503197c 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -133,7 +133,7 @@ pub fn place(p: *Pardes, s: *Surface) void { // frame's own time, not the stepped animations' 16 ms grid. A grid // snaps (it has no in-between), and so does nothing that shows it. const shown = lift != .off or inactiveDim(p) != 0; - const motion = animation.Motion.of(if (shown and builtins.capabilities.lift) p.settings.motion else .off); + const motion = animation.Motion.of(if (shown and builtins.frontend.lift) p.settings.motion else .off); // A flavour exaggerates what is only a few pixels (Motion.gain). spring.retarget(if (id == p.active) animation.Motion.of(p.settings.motion).gain else 0, p.now_ns, motion); _ = spring.step(p.now_ns); @@ -224,7 +224,7 @@ fn glideCursor(p: *Pardes, s: *Surface) void { s.chrome.cursor_glide = 0; s.chrome.cursor_alpha = 1; s.chrome.cursor_idle_ms = std.math.maxInt(u32); - if (!builtins.capabilities.lift) return; + if (!builtins.frontend.lift) return; const box = focusedCursorBox(p, s) orelse { // No cursor drawn (a PDF or picture took focus, a program hid its // own): a glide under way has nowhere to land, and its springs, @@ -1737,7 +1737,7 @@ test "a lift is sampled at each frame's own time, and asks for frames only while const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); // A grid has no in-between: there a lift snaps. - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; _ = try p.setTestFile("text\n"); p.newScratchBelow(0); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -1912,7 +1912,7 @@ test "with no lift, no dim and no motion a focus switch asks for no frames" { test "each Motion flavour reaches the lift as its own trajectory" { // A grid snaps; the flavours are a pixel shell's. - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); _ = try p.setTestFile("text\n"); @@ -1951,7 +1951,7 @@ test "each Motion flavour reaches the lift as its own trajectory" { } test "the caret is its own body: a page leaves it still under the moving text, a wheel scroll lands its springs on its line's new row" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); var text: std.ArrayList(u8) = .empty; @@ -1995,7 +1995,7 @@ test "the caret is its own body: a page leaves it still under the moving text, a } test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([20][20]u8, @splat("some text on a line\n".*)))); @@ -2078,7 +2078,7 @@ test "the focused cursor glides a jump, eases a step, the thin caret too, and is } test "a tag caret past its row's end glides to the tag's right edge, where the layer's is" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 16 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 60, .rows = 16, .row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 } } }); @@ -2128,7 +2128,7 @@ test "a tag caret past its row's end glides to the tag's right edge, where the l } test "a terminal's cursor lands with scrolling output, never gliding against it" { - if (!builtins.capabilities.lift or !pardes.terminal_panes) return error.SkipZigTest; + if (!builtins.frontend.lift or !pardes.terminal_panes) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 12 }); defer p.deinit(); while (p.nextEffect()) |_| {} @@ -2323,7 +2323,7 @@ fn twoColumns(p: *Pardes, arena: *std.heap.ArenaAllocator) !void { } test "while its pane slides the cursor rides with it, at the same cell of the pane every frame" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2376,7 +2376,7 @@ test "while its pane slides the cursor rides with it, at the same cell of the pa } test "focus moved to another column glides from where the cursor was drawn" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2412,7 +2412,7 @@ test "focus moved to another column glides from where the cursor was drawn" { } test "a click that takes focus glides the cursor once, to where it lands on release" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2446,7 +2446,7 @@ test "a click that takes focus glides the cursor once, to where it lands on rele } test "focus moving to a pane with no cursor while the cursor glides lets the session go idle" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); defer p.deinit(); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -2475,7 +2475,7 @@ test "focus moving to a pane with no cursor while the cursor glides lets the ses } test "a pane focus reaches brightens on the same spring the one it leaves dims on, and Motion off snaps" { - if (!builtins.capabilities.lift) return error.SkipZigTest; + if (!builtins.frontend.lift) return error.SkipZigTest; const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer p.deinit(); _ = try p.setTestFile(std.mem.asBytes(&@as([4][20]u8, @splat("some text on a line\n".*)))); diff --git a/src/exec.zig b/src/exec.zig index df397b37..e1c13daa 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -1033,7 +1033,7 @@ fn applyInitText(p: *Pardes, text: []const u8, settings_only: bool) void { if (settings_only and setting == null and std.meta.stringToEnum(Builtin, word[0 .. std.mem.indexOfAny(u8, word, " \t+") orelse word.len]) != null) continue; // One init file serves the window and the terminal: a setting only // the other has (Font, GripWidth in a terminal) is no failure here. - if (setting) |s| if (!s.enabled(builtins.capabilities)) continue; + if (setting) |s| if (!s.enabled(builtins.viewCapabilities(p))) continue; const failures = p.fs.failures; const known = executeBuiltinLine(p, p.active, line); if (known and p.fs.failures == failures) continue; @@ -1057,6 +1057,12 @@ pub fn runBuiltin(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]co /// `runBuiltin` saying whether a gesture on the pane's body made it. pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ?[]const u8, from_body: bool) void { if (!normal.multiOnce(p)) return; // a builtin is per-keystroke, never per-cursor + // A window's own word in a terminal's view (Fonts, Lift): said, as a + // word a build lacks is, and never run. + if (!builtins.registry.here(p, b)) { + var said: [96]u8 = undefined; + return p.reportFailure(id, std.fmt.bufPrint(&said, "{s} is GUI-only, invalid here", .{@tagName(b)}) catch "GUI-only, invalid here"); + } // No pane at all (the window Delcol emptied): a word runs all the same, // with a stand-in that is no pane (New, Newcol, Exit are what make sense). const pane = p.panes[id] orelse (if (builtins.registry.scope(b) == .session or p.anyPane() == null) p.standIn() orelse return else return); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a88a7335..f62871c4 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -24,6 +24,8 @@ const ninep_io = @import("../9p_io.zig"); const daemon = @import("../daemon.zig"); const upgrade = @import("../upgrade.zig"); +const replica = @import("../replica.zig"); +const wire = @import("../detached/wire.zig"); const tracy = @import("../tracy.zig"); pub const c = @import("gui_c"); // SDL3 and font.h, translated by build.zig @@ -671,24 +673,29 @@ test "closing tombstone overlays but never owns canonical cells" { } fn updateCoreResize(core: *pardes.Pardes, cols: u16, rows: u16, cell_w: u32, cell_h: u32, tag_w: u32, tag_h: u32) bool { + core.update(resizeOf(core, cols, rows, cell_w, cell_h, tag_w, tag_h) orelse return false); + return true; +} + +/// The resize a window of `cols` by `rows` cells of these sizes is to the +/// live view, or null when it is that already. +fn resizeOf(core: anytype, cols: u16, rows: u16, cell_w: u32, cell_h: u32, tag_w: u32, tag_h: u32) ?pardes.Event { const metrics: pardes.RowMetrics = .{ .body_w = @intCast(cell_w), .body_h = @intCast(cell_h), .tagline_w = @intCast(tag_w), .tagline_h = @intCast(tag_h) }; const same_metrics = std.meta.eql(metrics, core.row_metrics); if (comptime pardes.pdf_enabled) { const cell_px_w: u16 = @intCast(@min(@max(1, cell_w), std.math.maxInt(u16))); const cell_px_h: u16 = @intCast(@min(@max(1, cell_h), std.math.maxInt(u16))); if (cols == core.screen_w and rows == core.screen_h and - cell_px_w == core.cell_pixels.w and cell_px_h == core.cell_pixels.h and same_metrics) return false; - core.update(.{ .resize = .{ + cell_px_w == core.cell_pixels.w and cell_px_h == core.cell_pixels.h and same_metrics) return null; + return .{ .resize = .{ .cols = cols, .rows = rows, .cell_pixels = .{ .w = cell_px_w, .h = cell_px_h }, .row_metrics = metrics, - } }); - } else { - if (cols == core.screen_w and rows == core.screen_h and same_metrics) return false; - core.update(.{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = metrics } }); + } }; } - return true; + if (cols == core.screen_w and rows == core.screen_h and same_metrics) return null; + return .{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = metrics } }; } const TouchSample = struct { x: f32 = 0, y: f32 = 0, pressure: f32 = 1 }; @@ -1963,12 +1970,23 @@ fn setTransitionFields( } } -pub const run = runNative; +pub fn run(init: std.process.Init, opts: pardes.Options, headless: bool) !void { + return runNative(init, opts, headless, null); +} + +/// `pardes-gui --attach=:` (and `:`, +/// through ssh, main.zig): this window a replica client of a session +/// elsewhere, as the tty's `remote` is a terminal's (`remoteSession`). +pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far: bool) !void { + return runNative(init, opts, false, .{ .path = path, .session = session, .far = far }); +} -fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool) !void { +const Dial = struct { path: []const u8, session: []const u8, far: bool }; + +fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, dial: ?Dial) !void { const gpa = init.gpa; const env = init.environ_map; - if (env.get("PARDES_TEST_GRID") != null) return runGrid(init, opts_in); + if (dial == null and env.get("PARDES_TEST_GRID") != null) return runGrid(init, opts_in); const test_mode = env.get("PARDES_TEST") != null; const capture_dir = env.get("PARDES_TEST_CAPTURE_DIR"); @@ -2294,6 +2312,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool) !v // compositor moves it, as it does a title bar. if (!test_mode and !c.SDL_SetWindowHitTest(window, hitTest, &g)) log.warn("SDL_SetWindowHitTest: {s}", .{c.SDL_GetError()}); + if (dial) |to| return remoteSession(init, &g, opts_in, test_mode, to); try localSession(init, &g, opts_in, test_mode, headless); } @@ -2458,6 +2477,10 @@ fn localSession( frame_capture_dir = if (std.c.getenv("PARDES_FRAMES_CAPTURE")) |dir| std.mem.span(dir) else null; const host = shell.host(); core.host = host; + shell.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake = sdlWake, .alone_unattended = headless }; + defer shell.peers.deinit(); + // Before the core goes: each client sees its link end. + defer shell.peers.dropAll(shell.core); while (core.nextEffect()) |e| core.perform(e); // An upgraded session's shells, once a fresh session's own has a @@ -2496,20 +2519,25 @@ fn localSession( if (headless) { shell.closeTheWindow(); // Nothing else may wake the first wait. - daemon.watch(&queue, deliverNote); + daemon.watch(&shell, deliverNote); } while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Its unit to each replica client, and theirs taken in. + shell.peers.boundary(core); upgrade.locate(); // Asked in the step (the word), done between steps: the session whole. if (shell.upgrade_asked) |pane| shell.upgradeNow(pane); // A window asked for again, or an adopted shell, from the daemon's // thread through the queue; started after the first frame. - daemon.watch(&queue, deliverNote); + daemon.watch(&shell, deliverNote); if (core.quit) break; // a session that ended does not restore into one if (core.takeRestore()) |rp| blk: { + // ponytail: a Restore replaces the core its clients replicate; + // they are let go, and attach again (as the tty's). + shell.peers.dropAll(core); const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| { core.reportError(core.active, "Restore", err); break :blk; @@ -2525,6 +2553,7 @@ fn localSession( // the others close. shell.closeOtherWindows(); shell.gview = 0; + shell.windowed = 1; shell.stopPtys(); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( inotify_fd, @@ -2561,6 +2590,314 @@ fn localSession( } } +/// A window's link to a session elsewhere (`remoteSession`): what the +/// window's input made, fed to the replica once its own view is put back, +/// and what the link's reader took off the stream. +const Client = struct { + link: replica.Link, + io: std.Io, + gpa: std.mem.Allocator, + /// The window's inputs since the last feed, their texts in `texts`: + /// read over its own view (`Replica.within`), applied after it. + pending: std.ArrayListUnmanaged(pardes.Event) = .empty, + texts: std.heap.ArenaAllocator, + /// The reader's, under `lock`: what it took, and whether the link ended. + lock: std.Io.Mutex = .init, + inbox: std.ArrayListUnmanaged(replica.Said) = .empty, + taken: std.ArrayListUnmanaged(replica.Said) = .empty, + ended: bool = false, + + fn deinit(cl: *Client) void { + cl.pending.deinit(cl.gpa); + cl.texts.deinit(); + cl.inbox.deinit(cl.gpa); + cl.taken.deinit(cl.gpa); + } + + fn post(cl: *Client, ev: pardes.Event) void { + var e = ev; + const a = cl.texts.allocator(); + switch (e) { + .key => |*k| k.text = a.dupe(u8, k.text) catch "", + .paste, .command => |*b| b.* = a.dupe(u8, b.*) catch return, + else => {}, + } + cl.pending.append(cl.gpa, e) catch {}; + } + + /// What the window's input made, applied at once and sent. + fn feed(cl: *Client) void { + for (cl.pending.items) |ev| cl.link.input(ev); + cl.pending.clearRetainingCapacity(); + _ = cl.texts.reset(.retain_capacity); + } + + /// The reader's: a message of the stream, for the loop, which it wakes. + fn said(cl: *Client, msg: replica.Said) bool { + cl.lock.lockUncancelable(cl.io); + defer cl.lock.unlock(cl.io); + cl.inbox.append(cl.gpa, msg) catch return false; + sdlWake(null); + return true; + } + + /// What the reader took, in its order, and the link's end after it. + fn takeSaid(cl: *Client) void { + cl.lock.lockUncancelable(cl.io); + std.mem.swap(std.ArrayListUnmanaged(replica.Said), &cl.inbox, &cl.taken); + const ended = cl.ended; + cl.lock.unlock(cl.io); + for (cl.taken.items) |msg| { + // PARDES_TEST_LATENCY: when a terminal's delta came (an echo's). + if (msg == .screen) latencyNote("S {d}\n", .{monoNs()}); + cl.link.take(msg); + } + cl.taken.clearRetainingCapacity(); + if (ended) { + cl.link.quit = true; + cl.link.gone = true; + } + } + + /// Its own view drawn in the window, outside any step, as the tty's + /// client draws its terminal (tty.zig `Remote.draw`): what presenting + /// writes in the core (a frame skipped, one owed) is put back. + fn draw(cl: *Client, s: *Shell) void { + const r = cl.link.r; + const core = r.core(); + const skipped = core.present_skipped; + const needs = core.needs_frame; + core.frames.mark_start = pardes.FrameStats.nowNs(); + core.frames.mark_update = core.frames.mark_start; + r.draw(.{ .ctx = s, .f = present }); + core.frames.mark_render = core.frames.mark_update; + cl.link.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, cl.link.owed); + core.present_skipped = skipped; + core.needs_frame = needs; + cl.link.drawn(s.presented); + } +}; + +/// The session's stream, onto the client's inbox: its units, and its end. +fn clientReader(cl: *Client, fd: c_int, rest: *replica.Framer) void { + replica.readLink(cl.gpa, fd, rest, cl, Client.said); + cl.lock.lockUncancelable(cl.io); + cl.ended = true; + cl.lock.unlock(cl.io); + sdlWake(null); +} + +/// What the window does over its own view (`Replica.within`), its inputs +/// posted to the client (`Input.remote`): an SDL event dispatched, its size +/// (the tagline's face too), the pointer under a frame that moved, the +/// timedemo's and the test feed's steps. +const OwnView = struct { + g: *Gui, + in: *Input, + s: *Shell, + sev: ?*const c.SDL_Event = null, + pointer: bool = false, + eof: bool = false, + + fn run(v: *OwnView, core: *pardes.Pardes) anyerror!void { + if (v.sev) |sev| return dispatch(v.g, v.in, sev); + const g = v.g; + if (fitTaglineFont(g, core.settings.font.tagline_percent)) v.s.remote.?.link.dirty = true; + const geom = windowCells(g); + if (resizeOf(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) |ev| v.in.post(ev); + if (v.pointer) refreshPresentedPointer(g, v.in); + runDemo(g, v.in, core); + if (v.s.test_mode) { + const r = v.s.feed.pump(v.s.gpa, v.in, g) catch return error.TestFeed; + if (r.eof) v.eof = true; + } + } +}; + +/// `pardes-gui --attach=:`: the window, made as a session's +/// is, a replica client of that session (docs/next-daemon.md, "Phase 6: GUI +/// replica notes"). Its own core, loaded from the session's dump, applies +/// its input at once (src/replica.zig) and draws its own view; the +/// session's units are the truth it rolls back to. The tty's `remote`, with +/// a window for a terminal. +fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_mode: bool, to: Dial) !void { + const io = init.io; + const gpa = init.gpa; + const Said = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [800]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes-gui: --attach: " ++ fmt ++ "\n", args) catch "pardes-gui: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + var why: [600]u8 = @splat(0); + // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash + // and its stream in zstd, as the tty's client (replica.zig `Have`). + const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else to.far; + var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; + defer have_store.deinit(); + const have: ?*replica.Have = if (far) &have_store else null; + var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + var joined = replica.dial(gpa, to.path, to.session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); + + const allocs = pardes.memory.init(gpa); + defer pardes.memory.deinit(); + pardes.image.start(io, allocs.image); + if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + pardes.syntax.start(allocs.tree_sitter); + defer { + pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.pdf.stop(); + pardes.syntax.stop(); + } + var o = opts_in; + o.image_allocator = allocs.image; + o.pdf_allocator = allocs.pdf; + var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + + var client: Client = .{ .link = .{ .r = r, .fd = joined.fd }, .io = io, .gpa = gpa, .texts = .init(gpa) }; + defer client.deinit(); + const link = &client.link; + try link.fromEnv(gpa, init.environ_map); + defer link.deinit(gpa); + // A client spawns, watches and serves nothing: the Shell's tables are + // empty ones, there for what draws. + var ptys: [pardes.MAX_PANES]?Pty = @splat(null); + var gens: [pardes.MAX_PANES]u32 = @splat(0); + var queue: Queue = .{ .io = io, .gpa = gpa, .lsp_allocator = allocs.lsp }; + defer queue.deinit(); + var pipe_tasks: PipeTasks = .{}; + var watches: file_watch.Table = @splat(null); + const prompt_rcs: host_io.Shell.PromptFiles = .{}; + var shell: Shell = .{ + .core = r.core(), + .gui = g, + .io = io, + .gpa = gpa, + .lsp_allocator = allocs.lsp, + .prompt_rcs = &prompt_rcs, + .ptys = &ptys, + .gens = &gens, + .queue = &queue, + .pipe_tasks = &pipe_tasks, + .inotify_fd = -1, + .watches = &watches, + .test_mode = test_mode, + .remote = &client, + }; + defer shell.shaders.deinit(gpa, -1, &watches); + g.no_blink = test_mode; + defer g.hit_core = null; + _ = c.SDL_StartTextInput(g.window); + if (test_mode) setStdinRaw() catch {}; + // A join a pass: the first, and one after the replica diverged or the + // session let it go to upgrade. The window stays as it is. + while (true) { + r.gmtoff = joined.gmtoff; + const core = r.core(); + shell.core = core; + shell.gview = joined.view; + shell.surface = null; + g.hit_core = core; + frame_probe = &core.frames; + link.reset(r, joined.fd); + link.joinedAs(&joined, dial_ms); + client.ended = false; + // ponytail: why it joined again is the window's title (and its + // log) until a key, where a terminal's client draws it over its + // frame: a window's frame is the core's cells, which it cannot write. + var titled = link.notice_len > 0; + if (titled) { + var title: [260]u8 = undefined; + _ = c.SDL_SetWindowTitle(g.window, std.fmt.bufPrintSentinel(&title, "pardes: {s}", .{link.notice()}, 0) catch "pardes"); + log.warn("{s}", .{link.notice()}); + } + const reader = try std.Thread.spawn(.{}, clientReader, .{ &client, joined.fd, &joined.rest }); + var in: Input = .{ .core = core, .shell = &shell, .remote = &client }; + // What it is, then its size, are its view's first inputs. + link.input(.{ .frontend = .{ .windowed = true, .native_images = true, .native_image_patches = true } }); + var drew = false; + while (!link.quit) { + core.frames.period_ns = refreshNs(g); + syncLigatures(g, core); + { + var passes: [Post.max]ShaderBuild.Pass = undefined; + g.post.sync(gpa, g.device, g.swapchain_format, shell.shaders.view(&core.settings.post, &passes), shell.shaders.revision, core); + } + var own: OwnView = .{ .g = g, .in = &in, .s = &shell, .pointer = drew }; + r.within(&own, OwnView.run); + client.feed(); + if (own.eof) break; + // Its predicted echo judged and caught up, once a frame. + if (r.echoFrame()) link.dirty = true; + drew = false; + if (link.dirty) { + client.draw(&shell); + drew = shell.presented; + } + const predicting = r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0; + var ms: c_int = if (test_mode) 16 else -1; + if (demoDue(g)) |due| ms = if (ms < 0) due else @min(ms, due); + if (predicting or link.owed) ms = if (ms < 0) frame_ms else @min(ms, frame_ms); + if (link.quietCheck()) |wait| ms = if (ms < 0) @intCast(wait) else @min(ms, @as(c_int, @intCast(wait))); + // Idle, its frame drawn: the next input needs no copy taken. + if (ms != 0) r.ready(); + var sev = std.mem.zeroes(c.SDL_Event); + g.input_seen = false; + var got = c.SDL_WaitEventTimeout(&sev, ms); + while (got) : (got = c.SDL_PollEvent(&sev)) { + if (sev.type == c.SDL_EVENT_USER) continue; + var one: OwnView = .{ .g = g, .in = &in, .s = &shell, .sev = &sev }; + r.within(&one, OwnView.run); + client.feed(); + if (link.quit) break; + } + client.takeSaid(); + r.settle(); + if (titled and link.notice_len == 0) { + titled = false; + _ = c.SDL_SetWindowTitle(g.window, "pardes"); + } + if (link.owed or r.echo.changed) link.dirty = true; + if (r.diverged != null) link.quit = true; + } + if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); + if (!link.gone) replica.bye(joined.fd); + // The reader ends with the link: what it took meanwhile is let go. + _ = libc.shutdown(joined.fd, 2); + reader.join(); + client.lock.lockUncancelable(io); + for (client.inbox.items) |msg| switch (msg) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }; + client.inbox.clearRetainingCapacity(); + client.lock.unlock(io); + _ = libc.close(joined.fd); + joined.rest.deinit(gpa); + if (!link.rejoins()) { + // What it made since it joined, kept for its next start. + if (have) |h| h.keep(r.core()); + r.deinit(); + if (link.gone) Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}); + return; + } + // The session again, the core it leaves held meanwhile. + if (have) |h| h.leaving(r.core()); + dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + joined = replica.Link.redial(gpa, to.path, to.session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (have) |h| h.left(); + r.deinit(); + r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + } +} + +const frame_ms: c_int = @intCast(pardes.animation.frame_ns / std.time.ns_per_ms); + fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { const io = init.io; const gpa = init.gpa; @@ -3090,14 +3427,33 @@ const Input = struct { /// The shell whose windows these are: a window closed while others /// stay takes its view with it (views.zig). shell: ?*Shell = null, + /// A replica client's (`remote`): its inputs go to its link, never + /// straight to the core, which only reads here. + remote: ?*Client = null, quit: bool = false, fn post(in: *Input, ev: pardes.Event) void { + if (in.remote) |r| return r.post(ev); + // The window's input is its own view's, a replica client's input + // the session's last or not. + if (in.shell) |s| if (s.core.view != s.gview) { + _ = s.windowView(); + }; if (in.core) |core| return core.update(ev); } fn close(in: *Input) void { - if (in.shell) |s| if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); + if (in.remote) |r| { + r.link.quit = true; + in.quit = true; + return; + } + if (in.shell) |s| { + if (s.windowsOpen() > 1) return s.closeView(s.core.view); + // The last window, with replica clients: they keep the session, + // as a Detach would, the daemon holding it. + if (s.peers.count() > 0 and daemon.kept()) return s.closeTheWindow(); + } if (in.core) |core| core.quit = true; in.quit = true; } @@ -3127,7 +3483,9 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { updateDisplayScale(g.window); // Underlines are drawn into the glyphs a logical pixel thick. resetGlyphAtlas(g); - if (in.core) |core| core.needs_frame = true; + if (in.remote) |r| { + r.link.dirty = true; + } else if (in.core) |core| core.needs_frame = true; }, c.SDL_EVENT_WINDOW_FOCUS_GAINED => g.post.focus(true), c.SDL_EVENT_WINDOW_FOCUS_LOST => g.post.focus(false), @@ -3141,7 +3499,7 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), c.SDL_EVENT_WINDOW_FOCUS_GAINED => { in.post(.{ .focus = true }); - if (in.core) |core| upgrade.noticeNewer(core); + if (in.remote == null) if (in.core) |core| upgrade.noticeNewer(core); }, c.SDL_EVENT_WINDOW_FOCUS_LOST => in.post(.{ .focus = false }), c.SDL_EVENT_WINDOW_MOUSE_LEAVE => { @@ -3161,8 +3519,14 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { const want = std.math.clamp(g.px + step, font_px_min, font_px_max); if (want != g.px) { g.px = want; - refitFont(g, in.core); - if (in.core) |core| observeGuiFont(g, core); + // A client's view is sized by its next loop, as its input. + if (in.remote) |r| { + refitFont(g, null); + r.link.dirty = true; + } else { + refitFont(g, in.core); + if (in.core) |core| observeGuiFont(g, core); + } } return; } @@ -3590,6 +3954,18 @@ fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { in.post(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mc.col, .row = mc.row, .tag_hit = mc.tag_hit, .body_hit = mc.body_hit, .ctrl = w.mods.ctrl, .shift = w.mods.shift, .alt = w.mods.alt } }); while (g.program_wheel <= -1) : (g.program_wheel += 1) in.post(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = mc.col, .row = mc.row, .tag_hit = mc.tag_hit, .body_hit = mc.body_hit, .ctrl = w.mods.ctrl, .shift = w.mods.shift, .alt = w.mods.alt } }); + } else if (in.remote != null) { + // ponytail: a replica client's wheel moves the view a + // whole row a turn, with no glide and no touchpad's + // fraction: its core's clock is the session's, which + // moves only as its units come. + if (notchOf(w.y, w.integer_y)) |lines| { + g.program_wheel = @floatFromInt(lines); + } else g.program_wheel = accumulateWheelDelta(g.program_wheel, w.y); + while (g.program_wheel >= 1) : (g.program_wheel -= 1) + in.post(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mc.col, .row = mc.row, .shift = true } }); + while (g.program_wheel <= -1) : (g.program_wheel += 1) + in.post(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = mc.col, .row = mc.row, .shift = true } }); } else { g.scroll_pane = id; g.scroll_col = mc.col; @@ -3680,10 +4056,11 @@ fn heldPointerButton(g: *const Gui) ?pardes.Mouse.Button { return null; } -fn refreshPresentedPointer(g: *Gui, core: *pardes.Pardes) void { +fn refreshPresentedPointer(g: *Gui, in: *Input) void { if (!g.pointer_present) return; + const core = in.core orelse return; const mc = mouseCell(g, core, g.pad_x, g.pad_y) orelse { - if (g.pointer_mapped) core.update(.pointer_leave); + if (g.pointer_mapped) in.post(.pointer_leave); g.pointer_mapped = false; return; }; @@ -3693,7 +4070,7 @@ fn refreshPresentedPointer(g: *Gui, core: *pardes.Pardes) void { g.pointer_cell = mc; if (!moved) return; const held = heldPointerButton(g); - core.update(.{ .mouse = .{ + in.post(.{ .mouse = .{ .button = held orelse .none, .kind = if (held != null) .drag else .motion, .col = mc.col, @@ -3806,6 +4183,22 @@ const Shell = struct { gview: u8 = 0, /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). upgrade_asked: ?u8 = null, + /// This window a replica client of a session elsewhere (`remote`). + remote: ?*Client = null, + /// The session's replica clients (src/replica.zig), on sockets: views + /// of their own with no window here (`windowed`). + peers: replica.Peers = .{}, + /// The views that are this process's windows (the rest are clients'). + windowed: u8 = 1, + /// This wait took only a replica's post (its input, its join): the pump + /// after it reads no new time, so it moves nothing the replicas' + /// speculations stand on (tty.zig's `mail_only`). + mail_only: bool = false, + waited: bool = false, + /// With replica clients, a wait takes one person's input (tty.zig + /// `split`): the next is held for the next wait. + took_input: bool = false, + held: ?c.SDL_Event = null, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3894,9 +4287,11 @@ const Shell = struct { /// window, for `pardes-gui --attach` to open one again. fn detachWindow(ctx: ?*anyopaque) void { const s = shellOf(ctx); + // A replica client's: its link ends. + if (s.peers.detach(s.core.view)) return; if (s.gui == null) return s.core.reportFailure(s.core.active, "Detach: no window is open"); // One of several: its window goes, and the session stays in the rest. - if (pardes.views.count(s.core) > 1) return s.closeView(s.core.view); + if (s.windowsOpen() > 1) return s.closeView(s.core.view); if (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session; closing its window ends it"); s.closeTheWindow(); } @@ -3906,6 +4301,9 @@ const Shell = struct { const g = s.gui orelse return; const k = s.core.view; if (s.gview == k) return; + // A replica client's view has no window here: the Gui keeps the one + // it holds (`windowView` puts that view back when it acts). + if (s.windowed & (@as(u8, 1) << @intCast(k)) == 0) return; if (s.gview < s.windows.len) copyWindow(&s.windows[s.gview], g); copyWindow(g, &s.windows[k]); s.gview = k; @@ -3916,7 +4314,11 @@ const Shell = struct { if (!pardes.views.several(s.core)) return; const g = s.gui orelse return; const w = c.SDL_GetWindowFromEvent(sev) orelse return; - if (w == g.window) return; + // The Gui's window's, while a client's view is live: its own again. + if (w == g.window) { + _ = s.windowView(); + return; + } for (s.windows, 0..) |win, k| if (k != s.gview and pardes.views.isOpen(s.core, @intCast(k)) and win.window == w) { s.core.toView(@intCast(k)); return s.follow(); @@ -3950,10 +4352,20 @@ const Shell = struct { if (g.capture_dir.len > 0) std.Io.Dir.cwd().createDirPath(s.io, g.capture_dir) catch {}; } s.gview = k; + s.windowed |= @as(u8, 1) << @intCast(k); s.core.toView(k); s.core.needs_frame = true; } + /// The Gui's window's view live, for what the window does to its own + /// (its size, its scroll): a replica client's input may have been the + /// last. True when it was not, and is now. + fn windowView(s: *Shell) bool { + if (s.core.view == s.gview or !pardes.views.isOpen(s.core, s.gview)) return false; + s.core.toView(s.gview); + return true; + } + /// View `k`'s window closed while others stay: the view goes with it. fn closeView(s: *Shell, k: u8) void { const g = s.gui orelse return; @@ -3962,11 +4374,40 @@ const Shell = struct { closeWindow(g); releaseWindowParts(g, s.gpa); s.gview = std.math.maxInt(u8); + s.windowed &= ~(@as(u8, 1) << @intCast(k)); s.core.closeView(k); + // Into another window's view, if a client's was the one left live. + for (0..pardes.views.max) |i| if (s.windowed & (@as(u8, 1) << @intCast(i)) != 0 and pardes.views.isOpen(s.core, @intCast(i))) { + if (s.core.view != i) s.core.toView(@intCast(i)); + break; + }; s.follow(); s.core.needs_frame = true; } + /// With replica clients, a wait takes one person's input and its pump + /// makes one unit of it (tty.zig `split`): whether `sev`, a second, is + /// held for the next wait. Any event but a wake is no replica's post. + fn split(s: *Shell, sev: *const c.SDL_Event) bool { + if (sev.type != c.SDL_EVENT_USER) s.mail_only = false; + if (!s.peers.recording) return false; + switch (sev.type) { + c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP, c.SDL_EVENT_MOUSE_MOTION, c.SDL_EVENT_MOUSE_WHEEL, c.SDL_EVENT_WINDOW_FOCUS_GAINED, c.SDL_EVENT_WINDOW_FOCUS_LOST, c.SDL_EVENT_WINDOW_MOUSE_LEAVE, c.SDL_EVENT_FINGER_DOWN, c.SDL_EVENT_FINGER_MOTION, c.SDL_EVENT_FINGER_UP, c.SDL_EVENT_PINCH_BEGIN, c.SDL_EVENT_PINCH_UPDATE => {}, + else => return false, + } + if (s.took_input) { + s.held = sev.*; + return true; + } + s.took_input = true; + return false; + } + + /// How many of the session's views are windows here. + fn windowsOpen(s: *const Shell) usize { + return @popCount(s.windowed & s.core.views_open); + } + /// The session's end: every window but the live one closed. fn closeOtherWindows(s: *Shell) void { const g = s.gui orelse return; @@ -3989,7 +4430,9 @@ const Shell = struct { s.gui = null; s.presented = false; s.surface = null; - s.core.unattended = true; + // Its clients look at it still (Peers keeps it attended). + s.peers.alone_unattended = true; + s.core.unattended = s.peers.count() == 0; daemon.setState(true); } @@ -4001,6 +4444,7 @@ const Shell = struct { } s.gui = g; s.parked = null; + s.peers.alone_unattended = false; s.core.unattended = false; s.core.needs_frame = true; resetScroll(g); @@ -4051,6 +4495,9 @@ const Shell = struct { }; s.drainQueue(); core.settle(); + // Its replica clients told, their views gone before the dump: each + // dials the session again once it is back. + s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); var at_buf: [1024:0]u8 = undefined; @@ -4100,6 +4547,7 @@ const Shell = struct { for (0..pardes.views.max) |i| { const k: u8 = @intCast(i); if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + s.windowed |= @as(u8, 1) << @intCast(live); } s.core.toView(live); s.follow(); @@ -4200,6 +4648,7 @@ const Shell = struct { defer s.takeExits(); s.queue.takeInto(&s.batch); const msgs = &s.batch; + if (msgs.len > 0) s.mail_only = false; var check_files = false; for (msgs.slice()) |m| switch (m) { .output => |o| { @@ -4311,6 +4760,9 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const core = s.core; var in: Input = .{ .core = core, .shell = s }; s.follow(); + s.mail_only = true; + s.took_input = false; + defer s.waited = true; if (s.gui) |g| { var sev = std.mem.zeroes(c.SDL_Event); // Idle is asleep until an event: SDL's own, and the user events the @@ -4372,9 +4824,13 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { ms = 0; } g.input_seen = false; + // An input held from the last wait (`split`) is this one's, at once. + const held = s.held; + s.held = null; + if (held) |h| sev = h; // The wait is the 9P connections' turn with the core. pardes.turn.rest(); - const got = c.SDL_WaitEventTimeout(&sev, ms); + const got = held != null or c.SDL_WaitEventTimeout(&sev, ms); pardes.turn.wake(); // A virtual clock: while a render-time animation moves, each loop // is a presented frame and moves it by one display frame @@ -4402,6 +4858,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // each would repeat hover and selection work the next undoes. var motion: ?c.SDL_Event = null; while (true) { + if (s.split(&sev)) break; if (sev.type == c.SDL_EVENT_MOUSE_MOTION and (motion == null or motion.?.motion.windowID == sev.motion.windowID)) { motion = sev; } else { @@ -4440,17 +4897,20 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // 1.9 ms spinning against 7-8 ms asleep (docs/next-anim.md). // ponytail: a core while something moves (~250 ms a scroll); a // sleep to a millisecond short of the tick if the power matters more. - while (paced and !g.input_seen) { + while (paced and !g.input_seen and s.held == null) { if (tick -| c.SDL_GetTicksNS() < 50 * std.time.ns_per_us) break; pardes.turn.rest(); const more = c.SDL_PollEvent(&sev); pardes.turn.wake(); if (more) { - s.route(&sev); - dispatch(g, &in, &sev); - while (c.SDL_PollEvent(&sev)) { + if (!s.split(&sev)) { s.route(&sev); dispatch(g, &in, &sev); + while (c.SDL_PollEvent(&sev)) { + if (s.split(&sev)) break; + s.route(&sev); + dispatch(g, &in, &sev); + } } } else { // The events asked for every 100 µs, the core busy between. @@ -4510,6 +4970,16 @@ fn pollFrame(ctx: ?*anyopaque) void { } return; }; + // A replica client's view live: the window's own view back before what + // the window does to it (its size, its scroll, its font). + if (core.view != s.gview) { + const geom = windowCells(g); + const own = if (core.parked[s.gview]) |slot| &slot.view else null; + const due = g.scroll_pane != null or g.scroll_notch != 0 or g.glide.moving() or + core.settings.font.tagline_percent != g.tagline_percent or core.settings.font.pending or + (own != null and resizeOf(own.?, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height) != null); + if (due) _ = s.windowView(); + } syncWindowOpacity(g, core); core.frames.period_ns = refreshNs(g); syncTaglineFont(g, core); @@ -4680,16 +5150,8 @@ fn refreshNs(g: *const Gui) u64 { } fn finishWindowOpacity(core: *pardes.Pardes, applied: *u8, failure: ?[]const u8) void { - core.settings.window_opacity_pending = false; - if (failure) |reason| { - // Do not claim a value the compositor refused; an explicit retry of - // the same command will arm a new request. - core.settings.window_opacity = applied.*; - var buf: [256]u8 = undefined; - core.setMessage(core.active, std.fmt.bufPrint(&buf, "WindowOpacity: {s}", .{reason[0..@min(reason.len, 240)]}) catch "WindowOpacity: compositor rejected the request"); - } else { - applied.* = core.settings.window_opacity; - } + core.windowOpacityDone(applied.*, failure); + applied.* = core.settings.window_opacity; } fn syncWindowOpacity(g: *Gui, core: *pardes.Pardes) void { @@ -4758,7 +5220,9 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { g.scene_failures +|= 1; log.err("scene target unavailable (attempt {d}/3)", .{g.scene_failures}); if (g.scene_failures >= 3) { - core.disableSceneEffects(); + // ponytail: a client's window keeps asking (its core is the + // session's to change). + if (s.remote == null) core.disableSceneEffects(); g.scene_failures = 0; } } else if (scene_requested and s.presented) g.scene_failures = 0; @@ -4772,12 +5236,20 @@ fn postPresent(ctx: ?*anyopaque) void { // the pump advances it all the same. if (!s.presented) return; const frame = s.surface orelse return; - refreshPresentedPointer(g, s.core); + // A client's view live: the window's frame is acknowledged when its + // own view is live and drawn. + if (s.core.view != s.gview) return; + var in: Input = .{ .core = s.core }; + refreshPresentedPointer(g, &in); s.core.acknowledgePanelPresentation(frame.panelTracks()); } fn guiNow(ctx: ?*anyopaque) u64 { const s = shellOf(ctx); + if (s.waited) { + s.waited = false; + if (s.mail_only and s.peers.count() > 0 and s.core.clock_started) return s.core.now_ns; + } return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS(); } @@ -4871,7 +5343,8 @@ fn takeShell(s: *Shell, pane: u8, a: Adoption) void { /// The daemon's thread: what it says, onto the queue (drainQueue). fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { - const q: *Queue = @ptrCast(@alignCast(ctx)); + const s: *Shell = @ptrCast(@alignCast(ctx)); + const q = s.queue; switch (note) { .attach => |a| { // A terminal for a window session: the daemon refuses that kind @@ -4884,9 +5357,8 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| q.push(.{ .adopted = .{ .pid = pid, .fd = fd } }); q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); }, - // ponytail: a window session serves no replica yet; a terminal's - // does (tty.zig). - .replica => |fd| daemon.refuseReplica(fd, "a window session takes no replica yet: run the session in a terminal, or --detach"), + // Joined at the next step boundary (Peers, any thread). + .replica => |fd| s.peers.handed(fd), } } @@ -7974,8 +8446,14 @@ fn syncLigatures(g: *Gui, core: *pardes.Pardes) void { } fn syncTaglineFont(g: *Gui, core: *pardes.Pardes) void { - const percent = core.settings.font.tagline_percent; - if (percent == g.tagline_percent) return; + if (!fitTaglineFont(g, core.settings.font.tagline_percent)) return; + const geom = windowCells(g); + _ = updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height); +} + +/// The tagline's face at `percent` of the body's: whether it changed. +fn fitTaglineFont(g: *Gui, percent: u8) bool { + if (percent == g.tagline_percent) return false; g.tagline_percent = percent; const tagline = taglineRaster(g.font, g.px, g.cell_w, g.cell_h, percent); g.tagline_px = tagline.px; @@ -7983,8 +8461,7 @@ fn syncTaglineFont(g: *Gui, core: *pardes.Pardes) void { g.tagline_baseline = tagline.baseline; g.tagline_height = tagline.height; resetGlyphAtlas(g); - const geom = windowCells(g); - _ = updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height); + return true; } /// What text draws, so a frame does not work it out again. `cells` is the diff --git a/src/main.zig b/src/main.zig index d4edc74d..ada5f3b9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -109,7 +109,8 @@ const help_text = \\ --attach= ...the session called (its --9p or \\ --detach name, else its pid) \\ --attach=: a client of session on another machine, - \\ over `ssh -L` to its pardes-replica.sock + \\ a terminal's or a window's (either kind's + \\ session), over `ssh -L` to its pardes-replica.sock \\ ($PARDES_SSH: the ssh command, words, default \\ `ssh`; and \\ $PARDES_REMOTE_RUNTIME: its runtime directory, @@ -802,7 +803,6 @@ fn nativeMain(std_init: std.process.Init) !void { opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { - if (comptime pardes.platform != .tty) badArgs(init.io, "a window is no replica client yet: run `pardes --attach={s}` in a terminal", .{name}); remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); }; if (attach) |name| if (!resuming) attachTo(init.io, name); @@ -981,12 +981,23 @@ fn commOf(pid: []const u8, buf: *[64]u8) []const u8 { return std.mem.trimEnd(u8, buf[0..@intCast(got)], "\n"); } -/// `pardes --attach=:`: a replica client (tty.zig `remote`), -/// through the socket `where` names, or through `ssh -L` of host `where`'s -/// replica socket to one of this process's. +/// The replica client of this build's frontend: a terminal's (tty.zig) or a +/// window's (gui.zig). +fn clientOf(init: std.process.Init, opts: pardes.Options, path: []const u8, name: []const u8, far: bool) !void { + switch (pardes.platform) { + .tty => try @import("tty/tty.zig").remote(init, opts, path, name, far), + .gui => try @import("gui/gui.zig").remote(init, opts, path, name, far), + else => unreachable, + } +} + +/// `pardes --attach=:` (`pardes-gui` too): a replica client +/// (tty.zig and gui.zig `remote`), through the socket `where` names, or +/// through `ssh -L` of host `where`'s replica socket to one of this +/// process's. fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, name: []const u8) noreturn { const io = init.io; - if (comptime pardes.platform != .tty) unreachable; + if (comptime pardes.platform != .tty and pardes.platform != .gui) unreachable; const Say = struct { fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { var buf: [700]u8 = undefined; @@ -995,7 +1006,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, } }; if (std.mem.indexOfScalar(u8, where, '/') != null) { - @import("tty/tty.zig").remote(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); + clientOf(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); std.process.exit(0); } // ssh -L : , until the socket here answers. @@ -1066,7 +1077,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; _ = std.c.nanosleep(&ts, null); } - @import("tty/tty.zig").remote(init, opts, here, name, true) catch |err| { + clientOf(init, opts, here, name, true) catch |err| { Ssh.end(); Say.no(io, "{t}", .{err}); }; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 1b9d73f5..2859304a 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -524,10 +524,12 @@ fn checkBuiltin(p: *Pardes, req: Req, line: []const u8, scope: builtins.Scope) ? break now; } else typed; const arg = if (sp) |s| std.mem.trim(u8, line[s + 1 ..], " \t") else ""; - const b = std.meta.stringToEnum(Builtin, word) orelse { + const here = if (std.meta.stringToEnum(Builtin, word)) |b| builtins.registry.here(p, b) else false; + const b = (if (here) std.meta.stringToEnum(Builtin, word) else null) orelse { // A setting this build's frontend cannot show (Lift, GripWidth on a - // terminal) is known, and says so rather than sound misspelt. - if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null) @as([]const u8, "GUI-only") else null) |absent| { + // terminal, the window's own words in a terminal's view) is known, + // and says so rather than sound misspelt. + if (builtins.absentWhy(word) orelse if (config.Runtime.find(word) != null or std.meta.stringToEnum(Builtin, word) != null) @as([]const u8, "GUI-only") else null) |absent| { var why: [64]u8 = undefined; // EINVAL, a request this build cannot take, in words 9ns reads // as that: `invalid`. @@ -618,18 +620,24 @@ const e_prompt = "control message needs its argument"; /// What is not a setting -- platform, themes, where the config came from -- /// is /status's or the DumpConfig window's. fn writeSettings(p: *Pardes, w: *std.Io.Writer) !void { - try writeSettingsOf(p, &p.settings, w); + try writeSettingsFor(p, &p.settings, w, builtins.viewCapabilities(p)); try w.writeAll("LocationsConfig "); try p.locations_config.write(w); try w.writeByte('\n'); - if (comptime builtins.Pet.enabled) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)}); + if (comptime builtins.Pet.enabled) if (builtins.registry.here(p, .Pet)) try w.print("Pet {s}\n", .{@tagName(p.settings.pet)}); } /// The setting words of `s`, one a line, as the root ctl reads them; a Dump /// keeps those that differ from a fresh session's (dump.zig). pub fn writeSettingsOf(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer) !void { - inline for (config.Runtime.settings) |setting| { + return writeSettingsFor(p, s, w, builtins.capabilities); +} + +/// ...those a frontend of `caps` takes (the root ctl's: the live view's). +fn writeSettingsFor(p: *Pardes, s: *const config.Runtime, w: *std.Io.Writer, caps: config.Runtime.Capabilities) !void { + inline for (config.Runtime.settings) |setting| skip: { if (comptime !setting.enabled(builtins.capabilities)) continue; + if (!setting.enabled(caps)) break :skip; const word = setting.word; switch (setting.action) { .toggle => |field| try w.print("{s} {s}\n", .{ word, if (@field(s, @tagName(field))) "on" else "off" }), @@ -909,8 +917,10 @@ fn stripCitations(text: []u8) []u8 { return text[0..out]; } -fn writeCommands(w: *std.Io.Writer) !void { +fn writeCommands(p: *const Pardes, w: *std.Io.Writer) !void { for (std.enums.values(Builtin)) |b| { + // A window's own word is none in a terminal's view. + if (!builtins.registry.here(p, b)) continue; // Edit is a pane's word the root's ctl takes too, at the active pane. const scope: []const u8 = if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane"; try w.print("{s}{s} {s}", .{ @@ -938,16 +948,16 @@ fn writeCommands(w: *std.Io.Writer) !void { } } -pub fn commandsLen() u64 { +pub fn commandsLen(p: *const Pardes) u64 { var buf: [256]u8 = undefined; var counting: std.Io.Writer.Discarding = .init(&buf); - writeCommands(&counting.writer) catch return 0; + writeCommands(p, &counting.writer) catch return 0; return counting.fullCount(); } pub fn readCommands(p: *Pardes, req: Req) Reply { var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); - writeCommands(&text.writer) catch return Reply.fail(req.tag, E.NOMEM); + writeCommands(p, &text.writer) catch return Reply.fail(req.tag, E.NOMEM); p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch return Reply.fail(req.tag, E.NOMEM); return tree.stagedReply(p, req); } @@ -1362,7 +1372,9 @@ test "commands lists every builtin with its argument and the ctl that takes it" // Then, where the source has one, ` -- ` and what it does. if (words.next()) |dashes| try testing.expectEqualStrings("--", dashes); } - try testing.expectEqual(std.enums.values(Builtin).len, lines); + var here: usize = 0; + for (std.enums.values(Builtin)) |b| here += @intFromBool(builtins.registry.here(p, b)); + try testing.expectEqual(here, lines); for ([_][]const u8{ "\nDel arg pane -- ", "\nNewcol root -- ", "\nSave arg pane", "\nVerbose arg root on,off", "\nPlacement arg root acme,pardes", "\nUndo pane -- ", "\nEdit arg both -- acme's Edit" }) |row| try testing.expect(std.mem.indexOf(u8, listed.bytes, row) != null); } @@ -1738,6 +1750,8 @@ test "status reads the editor, and look and exec validate whole batches" { const gpa = testing.allocator; const p = try withFile(gpa, "Msg root\n"); defer p.deinit(); + // A terminal's view first (a window's build starts windowed). + p.windowed = false; const status = rd(p, root_status, 0, 4096); try testing.expectEqual(Status.ok, status.reply.status); var lines = std.mem.tokenizeScalar(u8, status.bytes, '\n'); @@ -2855,7 +2869,7 @@ test "a setting this frontend cannot show says GUI-only, and DumpDir reads back const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); const root_ctl = @backingInt(tree.TopFile.ctl); - if (!builtins.capabilities.lift) { + if (!builtins.viewCapabilities(p).lift) { try testing.expectEqualStrings("Lift is GUI-only, invalid here \"Lift\"", wr(p, root_ctl, "Lift on\n").reply.ename); try testing.expectEqualStrings("GripWidth is GUI-only, invalid here \"GripWidth\"", wr(p, root_ctl, "GripWidth 120\n").reply.ename); // A builtin the GUI alone has, written to a ctl or an exec, never diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bda2c1d1..bd87f5bc 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -678,7 +678,7 @@ fn topSize(p: *Pardes, f: TopFile) u64 { // one (a 9ns mount's splice), and a follower's reads go past it. .log => 0, .ctl => ctl.rootLen(p), - .commands => ctl.commandsLen(), + .commands => ctl.commandsLen(p), .focus => focus: { if (p.header_focus) break :focus 0; const pn = p.panes[p.active] orelse break :focus 0; diff --git a/src/pardes.zig b/src/pardes.zig index 2b1fae61..d7f35ed9 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -2868,7 +2868,9 @@ test "Pet command is SDL-only optional and validates its argument" { defer p.deinit(); try std.testing.expectEqual(.off, p.settings.pet); if (comptime platform != .gui) { - try std.testing.expect(std.meta.stringToEnum(builtins.registry.Builtin(), "Pet") == null); + // A terminal's view has the word and takes it not. + try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); + try std.testing.expectEqual(.off, p.settings.pet); return; } try std.testing.expect(p.executeBuiltinLine(p.active, "Pet cat")); @@ -3717,9 +3719,16 @@ pub const Event = union(enum) { /// How the host's handover `seq` went (Handover.finish): taken by `to`, /// or `failure` says why not. Borrowed for the step. handed: struct { seq: u32, to: []const u8 = "", failure: ?[]const u8 = null }, + /// The live view's frontend, which its replica client says first + /// (docs/next-daemon.md, "Phase 6: GUI replica notes"): a window, which + /// draws pictures itself and takes the GUI's own words, or a terminal. + frontend: Frontend, tick, }; +/// A view's frontend: what a client of a session elsewhere says it is. +pub const Frontend = struct { windowed: bool, native_images: bool = false, native_image_patches: bool = false }; + pub const PendingWrite = struct { bytes: []u8, off: usize = 0 }; /// `command` is Kill's: SIGTERM to the terminal's foreground job, and never @@ -4550,7 +4559,10 @@ pub const Pardes = struct { /// A pixel shell's window, which hears the pointer leave it: only then /// does /status say whether the pointer is over it (a terminal's mouse /// reports never say it left, so a tty would claim it for ever). - windowed: bool = false, + /// The live view's frontend is a window (Event.frontend): what it draws, + /// and the words it takes (builtins.zig `here`). A window's build + /// starts so, a terminal's not. + windowed: bool = platform == .gui, /// The last plain B1 click, for acme's double-click (text.c:881, /// textselect: a second click at the same point within 500 ms). last_click: ?struct { id: usize, on_tag: bool, row: i32, col: i32, ms: i64 } = null, @@ -5526,6 +5538,8 @@ pub const Pardes = struct { pub fn takeFontRequest(p: *Pardes) ?[]const u8 { if (comptime !font_picker) return null; + const taped = tape.enter(p, .take_font_request); + defer tape.leave(p, taped); if (!p.settings.font.pending or p.font_request_taken) return null; const path = p.settings.font.requested_path.get(); if (path.len == 0) return null; @@ -5540,6 +5554,8 @@ pub const Pardes = struct { unit: config.Runtime.FontSizeUnit, ) bool { if (comptime !font_picker) return false; + const taped = tape.enter(p, .{ .observe_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer tape.leave(p, taped); if (!p.settings.font.effective_name.set(effective_name)) return false; p.settings.font.effective_size_hundredths = effective_size_hundredths; p.settings.font.effective_size_unit = unit; @@ -5555,6 +5571,8 @@ pub const Pardes = struct { unit: config.Runtime.FontSizeUnit, ) bool { if (comptime !font_picker) return false; + const taped = tape.enter(p, .{ .acknowledge_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return false; const changed = !std.mem.eql( u8, @@ -5571,6 +5589,8 @@ pub const Pardes = struct { pub fn rejectFont(p: *Pardes) void { if (comptime !font_picker) return; + const taped = tape.enter(p, .reject_font); + defer tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return; p.settings.font.pending = false; p.font_request_taken = false; @@ -6346,6 +6366,11 @@ pub const Pardes = struct { if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words); }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), + .frontend => |f| { + p.windowed = f.windowed; + p.native_images = f.native_images; + p.native_image_patches = f.native_image_patches; + }, .pointer_leave => { p.pointer_inside = false; p.pointer_body_hit = null; @@ -6818,11 +6843,14 @@ pub const Pardes = struct { p.leader_on = true; }; // a path-less builtin is a row here too (it is the same index Help - // reads), and SPC cannot reach one — so both scans skip it + // reads), and SPC cannot reach one — so both scans skip it; and a + // window's own word is no path in a terminal's view for (builtin_rows) |row| { + if (!builtins.registry.here(p, row.cmd)) continue; if (std.mem.eql(u8, row.path orelse continue, pfx)) return exec.runBuiltin(p, row.cmd, p.active, "", null); } for (builtin_rows) |row| { + if (!builtins.registry.here(p, row.cmd)) continue; if (std.mem.startsWith(u8, row.path orelse continue, pfx)) { p.leader_on = true; return; @@ -7398,9 +7426,25 @@ pub const Pardes = struct { /// A native post pass proved unavailable: the chain is emptied, which /// DumpConfig then reports. pub fn disableSceneEffects(p: *Pardes) void { + const taped = tape.enter(p, .disable_scene_effects); + defer tape.leave(p, taped); p.settings.post.clear(); } + /// The window's answer to a WindowOpacity: done, or `failure` says why + /// not, `applied` the opacity it keeps. + pub fn windowOpacityDone(p: *Pardes, applied: u8, failure: ?[]const u8) void { + const taped = tape.enter(p, .{ .window_opacity_done = .{ .applied = applied, .failure = failure } }); + defer tape.leave(p, taped); + p.settings.window_opacity_pending = false; + const reason = failure orelse return; + // Do not claim a value the compositor refused; an explicit retry of + // the same command will arm a new request. + p.settings.window_opacity = applied; + var buf: [256]u8 = undefined; + p.setMessage(p.active, std.fmt.bufPrint(&buf, "WindowOpacity: {s}", .{reason[0..@min(reason.len, 240)]}) catch "WindowOpacity: compositor rejected the request"); + } + // ---- doc panes ---- pub fn openTutorView(p: *Pardes, id: usize) !*Pane { diff --git a/src/replica.zig b/src/replica.zig index 9d12de70..32fd4fbe 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -910,6 +910,16 @@ pub const Replica = struct { pardes.views.drawOne(r.core(), r.view, present) catch {}; } + /// `f(ctx, core)` with its own view live, outside any step, as `draw` + /// draws it (views.within): what its frontend reads of its own view + /// (its pointer's cells, its size) while another client's input was the + /// session's last. + pub fn within(r: *Replica, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) void { + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + pardes.views.within(r.core(), r.view, ctx, f) catch {}; + } + /// One of the authority's terminal messages (`Screens.flush`, or an /// answer to `screenRequests`), taken between units in stream order: a /// delta written into its pane's emulator, or fetched rows. The @@ -2486,6 +2496,306 @@ pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, compti rest.run(gpa, fd, ctx, take); } +/// What the session streams a client after its dump, as its frontend's loop +/// takes it: a unit's and a terminal message's bytes and a refusal's words +/// on the C heap, the loop's to free. +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8 }; + +/// The session's stream off `fd`, each message handed to `post` until the +/// link ends (a refusal ends it too); `post` says whether it took it (one +/// it did not is freed here). +pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime post: fn (@TypeOf(ctx), Said) bool) void { + const Take = struct { + fn take(x: @TypeOf(ctx), tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false; + const msg: Said = switch (t) { + .unit => .{ .unit = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .screen => .{ .screen = std.heap.c_allocator.dupe(u8, payload) catch return false }, + .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, + .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .pong, .welcome, .dump, .blobs, .zstd => return true, + }; + if (!post(x, msg)) switch (msg) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }; + return msg != .refused; + } + }; + rest.run(gpa, fd, ctx, Take.take); +} + +/// A replica client's link to its session, the terminal's (tty.zig +/// `remote`) and the window's (gui.zig `remote`): its replica, its socket, +/// what the session said, and what it checks. Drawing is the frontend's. +pub const Link = struct { + r: *Replica, + fd: c_int, + quit: bool = false, + /// The session hung up. + gone: bool = false, + /// Something to draw: an input applied, a unit taken. + dirty: bool = true, + /// The last frame was held for the frontend's answer (a terminal's + /// FrameAck, a window's skipped image). + owed: bool = false, + /// PARDES_REPLICA_CHECK=: once quiet a while after a change, the + /// session's digest is asked, and whether this replica's agrees is + /// written there, a line a check. + check: ?[:0]const u8 = null, + changed: bool = false, + asked: bool = false, + /// When its own last input was, and its last check asked (`quietCheck` + /// waits a while after each). + input_ns: u64 = 0, + asked_ns: u64 = 0, + /// How many of its own inputs the session recorded otherwise when it + /// last drew: a frame after one that draws something is a rollback that + /// may have been seen (counted so: an upper bound, the frame shows the + /// inputs after it too). + mispredicted: u32 = 0, + visible: u32 = 0, + /// What the session said as it let this client go (the C heap's). + refused: ?[]u8 = null, + /// A line over its frame, until its next key (`notify`): why it joined + /// again. The client's own, never the session's core. + notice_buf: [240]u8 = undefined, + notice_len: usize = 0, + /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core + /// is put off the session's (a digested counter moved), the next unit's + /// digest finds it, and it joins again (test/daemon.py). + test_diverge: u32 = 0, + /// Put off so in this join: its checks are not written (they differ). + put_off: bool = false, + /// How its last join went (Joined's counts), and how long its dial and + /// load took (PARDES_REPLICA_STATS). + join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, + join_ms: u64 = 0, + + pub const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; + + /// The replica the dial's dump makes, checked against the session as + /// it was dumped; null, and why in `why`. The dump is let go, a far + /// join's to the thread that keeps the blobs it was sent (`Have.store`). + pub fn load(gpa: Allocator, joined: *Joined, o: pardes.Options, why: []u8, have: ?*Have) ?*Replica { + defer { + if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); + joined.dump = &.{}; + } + const r = Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { + _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; + return null; + }; + // PARDES_NO_ECHO: no predicted echo (the gates' baseline). + if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; + if (!r.loaded(joined.digest)) { + r.deinit(); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + return null; + } + return r; + } + + /// The test knobs the environment sets (PARDES_REPLICA_CHECK, + /// PARDES_TEST_DIVERGE). + pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void { + if (env.get("PARDES_REPLICA_CHECK")) |file| c.check = try gpa.dupeSentinel(u8, file, 0); + if (env.get("PARDES_TEST_DIVERGE")) |n| c.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; + } + + pub fn deinit(c: *Link, gpa: Allocator) void { + if (c.check) |file| gpa.free(file); + if (c.refused) |words| std.heap.c_allocator.free(words); + } + + /// How its join went, its dial and load `ms`. + pub fn joinedAs(c: *Link, j: *const Joined, ms: u64) void { + c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes }; + c.join_ms = ms; + } + + /// A join's link, from its start. + pub fn reset(c: *Link, r: *Replica, fd: c_int) void { + c.r = r; + c.fd = fd; + c.quit = false; + c.gone = false; + c.dirty = true; + c.owed = false; + c.changed = false; + c.asked = false; + c.mispredicted = 0; + c.put_off = false; + } + + pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void { + const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; + for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { + b.* = '?'; + }; + c.notice_len = text.len; + } + + pub fn notice(c: *const Link) []const u8 { + return c.notice_buf[0..c.notice_len]; + } + + /// The frontend's input: applied at once, and sent to the session. + pub fn input(c: *Link, ev: pardes.Event) void { + // Its notice read: a key of its own puts it away. + if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; + c.input_ns = nowNs(); + const msg = c.r.input(ev) catch return; + if (!sendInput(c.fd, msg)) c.quit = true; + c.dirty = true; + } + + /// What the session streamed, in its order; the bytes are let go. + pub fn take(c: *Link, msg: Said) void { + switch (msg) { + .unit => |bytes| { + defer std.heap.c_allocator.free(bytes); + c.r.confirm(bytes); + if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { + c.test_diverge = 0; + c.put_off = true; + c.r.core().next_serial +%= 1000; + } + if (c.r.moved) c.dirty = true; + c.r.moved = false; + c.changed = true; + }, + .screen => |bytes| { + defer std.heap.c_allocator.free(bytes); + if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) { + c.quit = true; + }; + c.dirty = true; + }, + .checked => |d| c.checked(d), + .refused => |words| { + if (c.refused) |old| std.heap.c_allocator.free(old); + c.refused = words; + c.quit = true; + }, + } + if (c.r.diverged != null) c.quit = true; + } + + /// Its own view drawn, by the frontend (`drew` is whether anything was + /// written to it): a rollback it may have shown counted, and the + /// history rows it shows and does not hold (a scroll back) asked for, + /// blank until they come. + pub fn drawn(c: *Link, drew: bool) void { + if (c.r.stats.mispredicted > c.mispredicted) { + c.mispredicted = c.r.stats.mispredicted; + if (drew) c.visible += 1; + } + c.dirty = false; + while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) { + c.quit = true; + break; + }; + } + + /// Each pass of the frontend's loop: quiet (nothing of its own + /// pending) 300 ms after its last input and its last check, and + /// changed since, the session's digest is asked once, answered in + /// stream order after the units before it (a session that pumps on, a + /// window's test feed, sends a unit a frame; the check's own wakes it + /// once). The wake it wants, ms. + pub fn quietCheck(c: *Link) ?u32 { + if (c.check == null or !c.changed or c.asked or !c.r.quiet()) return null; + const quiet_ns = 300 * std.time.ns_per_ms; + const since = nowNs() -| @max(c.input_ns, c.asked_ns); + if (since < quiet_ns) return @intCast((quiet_ns - since) / std.time.ns_per_ms + 1); + c.changed = false; + c.asked = askCheck(c.fd); + c.asked_ns = nowNs(); + return null; + } + + /// The session's digest against this replica's, if it is quiet still. + fn checked(c: *Link, theirs: u64) void { + c.asked = false; + const path = c.check orelse return; + if (!c.r.quiet() or c.put_off) return; + const mine = c.r.digest(); + var buf: [96]u8 = undefined; + const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return; + appendTo(path, line); + } + + /// PARDES_REPLICA_STATS=: how its inputs and the session's units + /// went, a line at its end (test/daemon.py, the gates). + pub fn writeStats(c: *Link, path: []const u8) void { + const st = &c.r.stats; + var buf: [600]u8 = undefined; + const e = &c.r.echo.stats; + const line = std.fmt.bufPrint(&buf, "inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}\n", .{ + st.inputs, st.speculated, st.stopped, st.units, st.fast, + c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, + pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, + pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, + c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, + }) catch return; + var z: [4096]u8 = undefined; + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, line); + } + + /// The link ended: whether the client joins again, and why in its + /// notice (it diverged, or the session let it go to upgrade). + pub fn rejoins(c: *Link) bool { + if (c.r.diverged) |reason| { + c.notify("joined again: this client diverged from the session ({s})", .{reason}); + return true; + } + if (c.refused) |words| { + c.notify("joined again: {s}", .{words}); + std.heap.c_allocator.free(words); + c.refused = null; + return true; + } + return false; + } + + /// The session again, after a divergence or an upgrade: an upgrade's + /// takes the time its exec and load take, and its daemon's, so it is + /// dialled every 100 ms for 20 s. A far client's (`have`) holds the + /// core it leaves meanwhile (`Have.leaving`): its blobs are not sent + /// again. Null, and why in `why`. + pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have) ?Joined { + var tries: u32 = 0; + while (true) : (tries += 1) { + why.* = @splat(0); + if (dial(gpa, path, session, why, have)) |j| { + if (j.semantics == wire.semantics) return j; + bye(j.fd); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (tries > 200) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + } +}; + +fn appendTo(path: [:0]const u8, line: []const u8) void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, line.ptr, line.len); + _ = libc.close(fd); +} + pub fn digestOf(a: *Authority) u64 { const was = Stepping.begin(&a.t, null); defer was.end(); diff --git a/src/tape.zig b/src/tape.zig index f0766bd1..70a22ab0 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -61,6 +61,15 @@ pub const Input = union(enum) { open_view, close_view: u8, to_view: u8, + /// What a window says of its font and its opacity (gui.zig). + take_font_request, + observe_font: Font, + acknowledge_font: Font, + reject_font, + disable_scene_effects, + window_opacity_done: struct { applied: u8, failure: ?[]const u8 }, + + pub const Font = struct { name: []const u8, size: u16, unit: pardes.config.Runtime.FontSizeUnit }; }; /// The core's own syscalls (each `turn.yield()`s around itself), numbered @@ -544,6 +553,12 @@ pub const Tape = struct { .open_view => _ = p.openView(), .close_view => |k| p.closeView(k), .to_view => |k| p.toView(k), + .take_font_request => _ = p.takeFontRequest(), + .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), + .acknowledge_font => |f| _ = p.acknowledgeFont(f.name, f.size, f.unit), + .reject_font => p.rejectFont(), + .disable_scene_effects => p.disableSceneEffects(), + .window_opacity_done => |w| p.windowOpacityDone(w.applied, w.failure), } t.finishInput(); } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 2007c6f7..a1a28181 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -156,14 +156,8 @@ pub const Command = struct { replica: posix.fd_t, /// A replica's reader posted: the step boundary takes it in. replica_mail, - /// A replica client's: what the session streamed (replica.Client). - unit: []u8, - /// ...a terminal's delta or rows, before the units after it. - screen: []u8, - /// ...why it let this client go (an Upgrade's exec), the stream's end. - refused: []u8, - /// The session's digest, asked by PARDES_REPLICA_CHECK. - checked: u64, + /// A replica client's: what the session streamed (replica.Link). + said: replica.Said, link_gone, } = .nop; }; @@ -1274,191 +1268,42 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { try localSession(init, opts, &screen, &loop, &paste_buf); } -/// A replica client's link to its session (`remote`). +/// A replica client's link to its session (`remote`), drawn here. const Remote = struct { - r: *replica.Replica, - fd: posix.fd_t, - quit: bool = false, - /// The session hung up. - gone: bool = false, - /// Something to draw: an input applied, a unit taken. - dirty: bool = true, - /// The last frame was held for the terminal's answer (FrameAck). - owed: bool = false, - /// PARDES_REPLICA_CHECK=: once quiet a while after a change, the - /// session's digest is asked, and whether this replica's agrees is - /// written there, a line a check. - check: ?[:0]const u8 = null, - changed: bool = false, - asked: bool = false, - /// How many of its own inputs the session recorded otherwise when it - /// last drew: a frame after one that writes to the terminal is a - /// rollback that may have been seen (counted so: an upper bound, the - /// frame shows the inputs after it too). - mispredicted: u32 = 0, - visible: u32 = 0, - /// What the session said as it let this client go (owned, the C heap's). - refused: ?[]u8 = null, - /// A line over its frame, until its next key (`notify`): why it joined - /// again. The client's own, never the session's core. - notice_buf: [240]u8 = undefined, - notice_len: usize = 0, - /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core - /// is put off the session's (a digested counter moved), the next unit's - /// digest finds it, and it joins again (test/daemon.py). - test_diverge: u32 = 0, - /// Put off so in this join: its checks are not written (they differ). - put_off: bool = false, - /// How its last join went (replica.Joined's counts), and how long its - /// dial and load took (PARDES_REPLICA_STATS). - join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, - join_ms: u64 = 0, - - /// The replica the dial's dump makes, checked against the session as - /// it was dumped; null, and why in `why`. The dump is let go, a far - /// join's to the thread that keeps the blobs it was sent (`Have.store`). - fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8, have: ?*replica.Have) ?*replica.Replica { - defer { - if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); - joined.dump = &.{}; - } - const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { - _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; - return null; - }; - // PARDES_NO_ECHO: no predicted echo (the gates' baseline). - if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false; - if (!r.loaded(joined.digest)) { - r.deinit(); - _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; - return null; - } - return r; - } - - /// A join's link, from its start. - fn reset(c: *Remote, r: *replica.Replica, fd: posix.fd_t) void { - c.r = r; - c.fd = fd; - c.quit = false; - c.gone = false; - c.dirty = true; - c.owed = false; - c.changed = false; - c.asked = false; - c.mispredicted = 0; - c.put_off = false; - } - - fn notify(c: *Remote, comptime fmt: []const u8, args: anytype) void { - const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; - for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { - b.* = '?'; - }; - c.notice_len = text.len; - } - - /// PARDES_REPLICA_STATS=: how its inputs and the session's units - /// went, a line at its end (test/daemon.py, the gates). - fn writeStats(c: *Remote, path: []const u8) void { - const st = &c.r.stats; - var buf: [600]u8 = undefined; - const line = std.fmt.bufPrint(&buf, "inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d}\n", .{ - st.inputs, st.speculated, st.stopped, st.units, st.fast, - c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, - pct(st.spec_ns.items, 50) / 1000, pct(st.spec_ns.items, 99) / 1000, pct(st.copy_ns.items, 50) / 1000, pct(st.copy_ns.items, 99) / 1000, pct(st.rollback_ns.items, 50) / 1000, - pct(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, - c.join.sent_bytes >> 10, - }) catch return; - var z: [4096]u8 = undefined; - const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; - const fd = libc.open(pz, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); - if (fd < 0) return; - _ = libc.write(fd, line.ptr, line.len); - _ = libc.close(fd); - } - - fn pct(samples: []u64, p: usize) u64 { - if (samples.len == 0) return 0; - std.mem.sort(u64, samples, {}, std.sort.asc(u64)); - return samples[@min(samples.len - 1, samples.len * p / 100)]; - } - - /// The session's digest against this replica's, if it is quiet still. - fn checked(c: *Remote, theirs: u64) void { - c.asked = false; - const path = c.check orelse return; - if (!c.r.quiet() or c.put_off) return; - const mine = c.r.digest(); - var buf: [96]u8 = undefined; - const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return; - const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); - if (fd < 0) return; - _ = libc.write(fd, line.ptr, line.len); - _ = libc.close(fd); - } + link: replica.Link, /// The client's own view drawn on its terminal, outside any step: the /// replica's digest stays the session's (views.drawOne). What the host /// writes in the core as it presents (a frame skipped, one owed) is put /// back, as the frame statistics are this process's. fn draw(c: *Remote, s: *Shell) void { - const core = c.r.core(); + const r = c.link.r; + const core = r.core(); const skipped = core.present_skipped; const needs = core.needs_frame; core.frames.mark_start = pardes.FrameStats.nowNs(); core.frames.mark_update = core.frames.mark_start; - c.r.draw(.{ .ctx = s, .f = Shell.present }); + r.draw(.{ .ctx = s, .f = Shell.present }); core.frames.mark_render = core.frames.mark_update; - if (c.r.stats.mispredicted > c.mispredicted) { - c.mispredicted = c.r.stats.mispredicted; - if (core.frames.bytes > 0) c.visible += 1; - } - c.owed = core.present_skipped; - core.frames.record(pardes.FrameStats.nowNs(), false, c.owed); + const drew = core.frames.bytes > 0; + c.link.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, c.link.owed); core.present_skipped = skipped; core.needs_frame = needs; - c.dirty = false; - // The history rows it shows and does not hold (a scroll back), - // asked for: blank until they come. - while (c.r.screenRequest()) |req| if (!replica.sendScreen(c.fd, req)) { - c.quit = true; - break; - }; + c.link.drawn(drew); } }; /// The session's stream, onto the loop: its units, and its end. fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loop: *Loop, done: *std.atomic.Value(bool)) void { defer done.store(true, .release); - const Take = struct { - fn take(l: *Loop, tag: u8, payload: []const u8) bool { - const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false; - switch (t) { - .unit => { - const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; - l.postEvent(.{ .unit = bytes }) catch std.heap.c_allocator.free(bytes); - }, - .check => { - const d = (wire.decodeServer(tag, payload) catch return false).check; - l.postEvent(.{ .checked = d }) catch {}; - }, - .screen => { - const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; - l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); - }, - .refused => { - const words = (wire.decodeServer(tag, payload) catch return false).refused; - const bytes = std.heap.c_allocator.dupe(u8, words) catch return false; - l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes); - return false; - }, - .pong, .welcome, .dump, .blobs, .zstd => {}, - } + const Post = struct { + fn post(l: *Loop, said: replica.Said) bool { + l.postEvent(.{ .said = said }) catch return false; return true; } }; - replica.readStream(gpa, fd, rest, loop, Take.take); + replica.readLink(gpa, fd, rest, loop, Post.post); loop.postEvent(.link_gone) catch {}; } @@ -1489,7 +1334,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se const have: ?*replica.Have = if (far) &have_store else null; var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; var joined = replica.dial(gpa, path, session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); @@ -1504,7 +1349,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var o = opts; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - var r = Remote.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); // Its join's time, the dial and the load (PARDES_REPLICA_STATS). var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); @@ -1528,10 +1373,10 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var loop: Loop = .init(io, &screen.tty, &screen.vx); var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame); defer frame_arena.deinit(); - var link: Remote = .{ .r = r, .fd = joined.fd }; - if (init.environ_map.get("PARDES_REPLICA_CHECK")) |file| link.check = try gpa.dupeSentinel(u8, file, 0); - defer if (link.check) |file| gpa.free(file); - if (init.environ_map.get("PARDES_TEST_DIVERGE")) |n| link.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; + var remote_link: Remote = .{ .link = .{ .r = r, .fd = joined.fd } }; + const link = &remote_link.link; + try link.fromEnv(gpa, init.environ_map); + defer link.deinit(gpa); var sh: Shell = .{ .io = io, .gpa = gpa, @@ -1543,7 +1388,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se .paste_buf = &paste_buf, .inotify_fd = -1, .from = joined.view, - .remote = &link, + .remote = &remote_link, }; try startInput(&loop, &screen); @@ -1561,18 +1406,18 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se sh.terms[joined.view] = &screen; screen.feed.view = joined.view; link.reset(r, joined.fd); - link.join = .{ .blobs = joined.blobs, .from_memory = joined.from_memory, .from_disk = joined.from_disk, .sent_bytes = joined.sent_bytes }; - link.join_ms = dial_ms; + link.joinedAs(&joined, dial_ms); var reader_done: std.atomic.Value(bool) = .init(false); const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); - // Its size is its view's first input. + // What it is, then its size, are its view's first inputs. + link.input(.{ .frontend = .{ .windowed = false } }); screen.size = null; sh.winched(); screen.vx.queueRefresh(); while (!link.quit) { // Its predicted echo judged and caught up, once a frame. if (r.echoFrame()) link.dirty = true; - if (link.dirty) link.draw(&sh); + if (link.dirty) remote_link.draw(&sh); const first = (loop.tryEvent() catch null) orelse idle: { // Idle, its frame drawn: the next key needs no copy taken. r.ready(); @@ -1596,14 +1441,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se if (link.owed or r.echo.changed) link.dirty = true; // Predictions out: judged a frame at a time while they are. if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) requestWake(io, @intCast(pardes.animation.frame_ns / std.time.ns_per_ms)); - if (link.check != null) { - // Quiet a while after a change: asked once (a wake, then the - // next quiet one). - if (first == .wake and link.changed and !link.asked and r.quiet()) { - link.changed = false; - link.asked = replica.askCheck(joined.fd); - } else if (link.changed and r.quiet()) requestWake(io, 300); - } + if (link.quietCheck()) |ms| requestWake(io, ms); if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); @@ -1616,7 +1454,10 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se while (true) { const ended = reader_done.load(.acquire); while (loop.tryEvent() catch null) |ev| switch (ev) { - .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .said => |said| switch (said) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }, .paste => |b| gpa.free(@constCast(b)), else => {}, }; @@ -1627,13 +1468,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se _ = libc.close(joined.fd); joined.rest.deinit(gpa); // Why it joins again, in a line over its frame until a key. - if (r.diverged) |reason| { - link.notify("joined again: this client diverged from the session ({s})", .{reason}); - } else if (link.refused) |words| { - link.notify("joined again: {s}", .{words}); - std.heap.c_allocator.free(words); - link.refused = null; - } else { + if (!link.rejoins()) { // What it made since it joined, kept for its next start. if (have) |h| h.keep(r.core()); r.deinit(); @@ -1644,38 +1479,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se return; } // The session again, the core it leaves held meanwhile: its blobs - // are not sent again. An upgrade's takes the time its exec and load - // take, and its daemon's. + // are not sent again. if (have) |h| h.leaving(r.core()); - var tries: u32 = 0; dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = while (true) : (tries += 1) { - why = @splat(0); - if (replica.dial(gpa, path, session, &why, have)) |j| { - if (j.semantics == wire.semantics) break j; - replica.bye(j.fd); - _ = libc.close(j.fd); - var jj = j; - jj.rest.deinit(gpa); - gpa.free(j.dump); - screen.leave(); - Said.no(io, "{s}", .{upgrade_words}); - } - const words = std.mem.sliceTo(&why, 0); - if (std.mem.indexOf(u8, words, "runs another build") != null) { - screen.leave(); - Said.no(io, "{s}", .{upgrade_words}); - } - if (tries > 200) { - screen.leave(); - Said.no(io, "{s}", .{words}); - } - const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); + joined = replica.Link.redial(gpa, path, session, &why, have) orelse { + screen.leave(); + Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; if (have) |h| h.left(); r.deinit(); - r = Remote.load(gpa, &joined, o, &why, have) orelse { + r = replica.Link.load(gpa, &joined, o, &why, have) orelse { screen.leave(); Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; @@ -1683,7 +1496,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se } } -const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; /// A terminal a session draws on, while it has one: entered (raw, the /// alternate screen, mouse and paste reports) and left (all of it put back, @@ -2191,11 +2003,7 @@ const Shell = struct { /// (applied at once there, and sent to the session). fn feed(s: *Shell, ev: pardes.Event) void { const c = s.remote orelse return s.core.update(ev); - // Its notice read: a key of its own puts it away. - if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; - const msg = c.r.input(ev) catch return; - if (!replica.sendInput(c.fd, msg)) c.quit = true; - c.dirty = true; + c.link.input(ev); } /// Whether a terminal of this process's shows the session, or @@ -2800,7 +2608,7 @@ const Shell = struct { }, .quit => { if (s.remote) |c| { - c.quit = true; + c.link.quit = true; return true; } // One of several: its view goes with it. @@ -2827,39 +2635,16 @@ const Shell = struct { s.input_seen = true; }, .replica_mail => s.input_seen = true, - .unit => |bytes| { - if (s.remote) |c| { - c.r.confirm(bytes); - if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { - c.test_diverge = 0; - c.put_off = true; - c.r.core().next_serial +%= 1000; - } - if (c.r.moved) c.dirty = true; - c.r.moved = false; - c.changed = true; - } - std.heap.c_allocator.free(bytes); - }, - .screen => |bytes| { - if (s.remote) |c| { - if (c.r.screen(bytes)) |reply| if (!replica.sendScreen(c.fd, reply)) { - c.quit = true; - }; - c.dirty = true; - } - std.heap.c_allocator.free(bytes); + .said => |said| if (s.remote) |c| { + c.link.take(said); + if (said == .refused) return true; + } else switch (said) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, }, - .checked => |d| if (s.remote) |c| c.checked(d), - .refused => |words| if (s.remote) |c| { - if (c.refused) |old| std.heap.c_allocator.free(old); - c.refused = words; - c.quit = true; - return true; - } else std.heap.c_allocator.free(words), .link_gone => if (s.remote) |c| { - c.quit = true; - c.gone = true; + c.link.quit = true; + c.link.gone = true; return true; }, .focus_in => s.feed(.{ .focus = true }), @@ -3055,7 +2840,7 @@ const Shell = struct { const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; paintCells(win, trailed.cells, trailed.cols, trailed.rows); - if (s.remote) |c| if (c.notice_len > 0) noticeLine(win, c.notice_buf[0..c.notice_len], trailed.cols, trailed.rows); + if (s.remote) |c| if (c.link.notice_len > 0) noticeLine(win, c.link.notice(), trailed.cols, trailed.rows); tz_cells.end(); for (surface.images[0..surface.nimages]) |maybe| { const place = maybe orelse continue; diff --git a/src/views.zig b/src/views.zig index 361d14a3..b9731089 100644 --- a/src/views.zig +++ b/src/views.zig @@ -523,6 +523,19 @@ pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *cons /// are the slot's again (else a frame drawn would take them from a view, /// and a replica that draws nothing would keep them). fn drawParked(p: *Pardes, k: u8, present: Present) !void { + try unparked(p, k, present, drawLive); + copy(&drawn_fields, &p.parked[k].?.view, p); +} + +fn drawLive(present: Present, p: *Pardes) anyerror!void { + _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); + const surface = try p.render(p.frame_arena.allocator()); + if (present.f) |f| f(present.ctx, surface); +} + +/// `f(ctx, p)` with parked view `k` unparked, its slot's selections its own +/// again after (`drawParked`). +fn unparked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined; for (p.panes, 0..) |s, id| if (s) |pane| { sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel }; @@ -532,10 +545,7 @@ fn drawParked(p: *Pardes, k: u8, present: Present) !void { pane.pv[k].body.sels = sels[id][0]; pane.pv[k].body.nsel = sels[id][1]; }; - _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); - const surface = try p.render(p.frame_arena.allocator()); - if (present.f) |f| f(present.ctx, surface); - copy(&drawn_fields, &p.parked[k].?.view, p); + try f(ctx, p); } /// View `k` drawn and presented outside any step, by a replica client @@ -543,12 +553,32 @@ fn drawParked(p: *Pardes, k: u8, present: Present) !void { /// view put back after exactly as it was, so the replica's digest is the /// authority's still. No post_present, as there. pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { - if (k == p.view or !isOpen(p, k)) { - _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); - const surface = try p.render(p.frame_arena.allocator()); - if (present.f) |f| f(present.ctx, surface); - return; - } + if (k == p.view or !isOpen(p, k)) return drawLive(present, p); + const Draw = struct { + fn draw(pr: Present, q: *Pardes, kk: u8) anyerror!void { + try drawParked(q, kk, pr); + } + }; + try withParked(p, k, present, Draw.draw); +} + +/// `f(ctx, p)` with view `k` live, outside any step, and the live view put +/// back after exactly as it was, as `drawOne` draws one: what a replica +/// client's frontend reads of its own view (its pointer's cells, its size) +/// while another client's input was the session's last. `f` reads only. +pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { + if (k == p.view or !isOpen(p, k)) return f(ctx, p); + const Read = struct { + fn read(x: @TypeOf(ctx), q: *Pardes, kk: u8) anyerror!void { + try unparked(q, kk, x, f); + } + }; + try withParked(p, k, ctx, Read.read); +} + +/// The live view parked, `f(ctx, p, k)`, and the live view put back after +/// exactly as it was. +fn withParked(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes, u8) anyerror!void) !void { const live = p.view; park(p); defer { @@ -562,10 +592,10 @@ pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { pane.cols = pv.cols; pane.rows = pv.rows; } - if (pane.file) |*f| f.scroll = pv.scroll; + if (pane.file) |*file| file.scroll = pv.scroll; }; } - try drawParked(p, k, present); + try f(ctx, p, k); } // ---- tests ---- diff --git a/test/daemon.py b/test/daemon.py index d9ca7153..ebee286f 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -25,6 +25,9 @@ private HOME and runtime directory: the replica clients alone python3 -B test/daemon.py --far the far clients and their blob cache alone + python3 -B test/daemon.py --window-clients + window replica clients (hidden, offscreen) of a terminal's session, + at 0/30/80/207 ms, and a window's session serving replicas """ import fcntl import os @@ -528,6 +531,189 @@ def manage(binary): shutil.rmtree(root, ignore_errors=True) +class Window: + """A pardes-gui in test mode, its window offscreen and hidden, its keys + written to its stdin (the test feed: Escape as `CSI 27 u`, which a + pipe's lone ESC would hold for the next key).""" + + def __init__(self, root, name, argv, env): + (root / name).mkdir() + self.err = root / f'{name}.err' + self.p = subprocess.Popen(argv, env=dict(env, PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(root / name), SDL_VIDEO_DRIVER='offscreen'), + cwd=root, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=open(self.err, 'w'), start_new_session=True) + self.pid = self.p.pid + + def keys(self, text): + self.p.stdin.write(text.replace(b'\x1b', b'\x1b[27u')) + self.p.stdin.flush() + + def said(self): + return self.err.read_text(errors='replace') if self.err.exists() else '' + + def close(self): + if self.p.poll() is None: + self.p.stdin.close() + self.p.wait(timeout=15) + return self.p.returncode + + +def window_clients(binary, gui_binary): + """Window replica clients (`pardes-gui --attach=:`): of a + terminal's detached session, directly and through the delay relay at + 30, 80 and 207 ms, beside a terminal's client, one put off the session's + state that joins again, one that detaches; and a window's session served + to a terminal's client and a window's. Each one's keys in the session's + text, every client's digest the session's once quiet; nothing left.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='wc-', dir=os.environ.get('TMPDIR') or scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + notes = root / 'notes.txt' + notes.write_text(''.join(f'line {i} of the notes\n' for i in range(300))) + started = [] + terms = [] + windows = [] + try: + sess = subprocess.Popen([binary, '--detach=files', str(notes)], env=env, cwd=root, stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + started.append(sess.pid) + sock = root / 'pardes-replica.sock' + wait_for('the replica socket', sock.exists) + wait_for('the session\'s 9P socket', (root / 'pardes-9p-files.sock').exists) + nine = Client(root / 'pardes-9p-files.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + body = lambda: nine.read(f'/pane/{pane}/body') + alone = fds(sess.pid) + # A terminal's client beside the windows, typing as they do. + ta = Term(30, 100) + terms.append(ta) + a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check'))) + started.append(a.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in ta.text(), timeout=30) + agreements = [] + for i, rtt in enumerate((0, 30, 80, 207)): + to = sock + relay = None + if rtt: + relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'), + str(root / f'relay{rtt}.sock'), str(sock), '--delay', str(rtt / 2)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + started.append(relay.pid) + wait_for('the relay', (root / f'relay{rtt}.sock').exists) + to = root / f'relay{rtt}.sock' + check = root / f'w{rtt}.check' + w = Window(root, f'w{rtt}', [gui_binary, f'--attach={to}:files'], dict(env, PARDES_REPLICA_CHECK=str(check))) + windows.append(w) + started.append(w.pid) + seen = len(checks(root / 'a.check')) + w.keys(b'jjjjjjj' * (i + 1) + f'Ofrom-window-{rtt}\x1b'.encode()) + os.write(ta.master, b'jjjjOfrom-terminal-%d\x1b' % rtt) + wait_for(f'the window\'s and the terminal\'s keys ({rtt} ms)', lambda: f'from-window-{rtt}'.encode() in body() + and b'from-terminal-%d' % rtt in body(), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(ta), timeout=20) + wait_for(f'the window client agrees ({rtt} ms)', lambda: agreed(check, 0), timeout=30) + wait_for('the terminal client agrees', lambda: agreed(root / 'a.check', seen), timeout=30) + # Moved and scrolled, its own view only: agreed again. + n = len(checks(check)) + w.keys(b'jjjjjkkk\x06\x06\x02') + wait_for(f'the window client agrees again ({rtt} ms)', lambda: agreed(check, n), timeout=30) + agreements.append(len(checks(check))) + assert w.close() == 0, w.said()[-800:] + if relay: + relay.kill() + relay.wait() + # One put off the session's state (PARDES_TEST_DIVERGE): the next + # unit it replays finds it, it joins again, and its keys after go in. + dcheck = root / 'wd.check' + wd = Window(root, 'wd', [gui_binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(dcheck), PARDES_TEST_DIVERGE='1')) + windows.append(wd) + started.append(wd.pid) + time.sleep(2) + wd.keys(b'jj') + time.sleep(.5) + os.write(ta.master, b'jj') + wait_for('the window client diverged and joined again', lambda: 'joined again' in wd.said(), timeout=30) + assert wd.p.poll() is None + seen = len(checks(dcheck)) + wd.keys(b'Oafter-rejoin-W\x1b') + wait_for('the window\'s keys after its rejoin', lambda: b'after-rejoin-W' in body(), timeout=30) + wait_for('the window client agrees after its rejoin', lambda: agreed(dcheck, seen), timeout=30) + # Detach typed in it: it alone goes, and says why. + wd.keys(b' sD') + wait_for('the window client let go', lambda: wd.p.poll() is not None, timeout=15) + assert 'let this client go' in wd.said(), wd.said()[-500:] + os.write(ta.master, b' sD') + wait_for('the terminal client let go', lambda: a.poll() is not None, timeout=10) + wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) + print(f'daemon: window clients of a terminal\'s session at 0/30/80/207 ms beside a terminal\'s, digests agreed ' + f'{agreements} and {len(checks(root / "a.check"))} times; one put off the session\'s state joined again and ' + f'agreed {len(checks(dcheck))} times; one detached') + try: + nine.write(f'/pane/{pane}/exec', b'Exit\n') + except OSError as e: + # An edited file asks before it goes. + assert 'Exit again' in str(e), e + nine.write(f'/pane/{pane}/exec', b'Exit\n') + sess.wait(timeout=10) + nine.socket.close() + # A window's session (its daemon on, as a desktop's is), served to a + # terminal's client and a window's, the window typing as they do. + win = Window(root, 'win', [gui_binary, '--9p=win', str(notes)], dict(env, PARDES_DAEMON='on')) + windows.append(win) + started.append(win.pid) + wait_for('the window session\'s 9P socket', (root / 'pardes-9p-win.sock').exists, timeout=30) + wait_for('the replica socket', sock.exists) + nine = Client(root / 'pardes-9p-win.sock') + pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text') + tb = Term(30, 100) + terms.append(tb) + b = tb.spawn([binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check'))) + started.append(b.pid) + wait_for('the terminal client\'s first frame', lambda: b'line 3 of the notes' in tb.text(), timeout=30) + wg = Window(root, 'wg', [gui_binary, f'--attach={sock}:win'], dict(env, PARDES_REPLICA_CHECK=str(root / 'g.check'))) + windows.append(wg) + started.append(wg.pid) + time.sleep(2) + os.write(tb.master, b'jjjOfrom-T\x1b') + wg.keys(b'jjjjjjOfrom-G\x1b') + win.keys(b'jjjjjjjjjOfrom-W\x1b') + wait_for('everyone\'s keys in the window\'s session', lambda: all(x in body() for x in (b'from-T', b'from-G', b'from-W')), timeout=30) + wait_for('the terminal client normal again', lambda: normal_again(tb), timeout=20) + for k in range(3): + nb, ng = len(checks(root / 'b.check')), len(checks(root / 'g.check')) + os.write(tb.master, b'jk') + wg.keys(b'kj') + win.keys(b'jj') + wait_for('the terminal client agrees with the window', lambda: agreed(root / 'b.check', nb), timeout=30) + wait_for('the window client agrees with the window', lambda: agreed(root / 'g.check', ng), timeout=30) + assert wg.close() == 0, wg.said()[-800:] + os.write(tb.master, b' sD') + wait_for('the terminal client let go', lambda: b.poll() is not None, timeout=10) + assert win.p.poll() is None + print(f'daemon: a window\'s session served a terminal\'s client and a window\'s, digests agreed ' + f'{len(checks(root / "b.check"))} and {len(checks(root / "g.check"))} times') + win.close() + nine.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=15) + print('daemon: window clients\' sessions ended, nothing left behind') + finally: + for t in terms: + t.close() + for w in windows: + if w.p.poll() is None: + w.p.kill() + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + def joins(path): """A replica client's joins (PARDES_REPLICA_STATS, a line a link): how many blobs each listed, how many it held already from the core it left @@ -1341,6 +1527,8 @@ if __name__ == '__main__': manage(sys.argv[2]) elif sys.argv[1:2] == ['--far']: far(sys.argv[2]) + elif sys.argv[1:2] == ['--window-clients']: + window_clients(sys.argv[2], sys.argv[3]) elif sys.argv[1:2] == ['--ping']: ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) elif '--gui' in sys.argv: diff --git a/test/output.zig b/test/output.zig index 2d872bd1..2b8bf1f5 100644 --- a/test/output.zig +++ b/test/output.zig @@ -684,7 +684,8 @@ test "every enabled setting builtin mutates the State Config reports" { var chosen_font: ?[]const u8 = null; var buf: [512]u8 = undefined; for (config.Runtime.settings) |setting| { - if (!setting.enabled(builtins.capabilities)) continue; + // The view's own (a terminal's takes no window's words). + if (!setting.enabled(builtins.viewCapabilities(p))) continue; const command: []const u8 = switch (setting.action) { .theme => try std.fmt.bufPrint(&buf, "{s} acme", .{setting.word}), .shell => try std.fmt.bufPrint(&buf, "{s} fish", .{setting.word}), @@ -716,7 +717,7 @@ test "every enabled setting builtin mutates the State Config reports" { "Theme acme\n", "Shell fish\n", }) |line| try std.testing.expect(std.mem.indexOf(u8, report, line) != null); - const transition = if (!builtins.capabilities.panel_transitions) + const transition = if (!builtins.viewCapabilities(p).panel_transitions) "PanelSlide unsupported\n" else if (p.settings.panel_transition == .off) "PanelSlide off\n" @@ -724,12 +725,12 @@ test "every enabled setting builtin mutates the State Config reports" { try std.fmt.bufPrint(&buf, "{s} on\n", .{config.Runtime.findAction(.{ .transition = p.settings.panel_transition }).?.word}); try std.testing.expect(std.mem.indexOf(u8, report, transition) != null); // A bare word turns a bundled pass on at its default level. - const scene_status = if (builtins.capabilities.scene_shaders) "2" else "unsupported"; + const scene_status = if (builtins.viewCapabilities(p).scene_shaders) "2" else "unsupported"; for ([_][]const u8{"Crt"}) |name| { const line = try std.fmt.bufPrint(&buf, "{s} {s}\n", .{ name, scene_status }); try std.testing.expect(std.mem.indexOf(u8, report, line) != null); } - const tagline = if (!builtins.capabilities.tagline_font_size) + const tagline = if (!builtins.viewCapabilities(p).tagline_font_size) "TaglineSize unsupported\n" else try std.fmt.bufPrint(&buf, "TaglineSize {d}\n", .{p.settings.font.tagline_percent}); -- cgit v1.3 From c07baa7bf83039600a54b261eaae7fdd4d673768 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:06:51 -0300 Subject: builtins: Tty9p is a word of every hosted build, not a Linux build's alone, so a build's words, the rows of its Help, are the same on FreeBSD: a Linux client of a box session made SPC ?'s +Help with one row more than its session's and joined again (`pane 4.file.content`, and `.splice` over a +Help made again: two of the box soak's three divergences); whether its host is Linux is the authority's answer (tape.authority), so a replica of a FreeBSD session refuses Tty9p as its session does; test/cross_replay.py's session runs Help (a FreeBSD tape replayed here diverged at its pane's content) Co-Authored-By: Claude Opus 5.5 --- src/builtins.zig | 5 ++++- src/builtins_json.zig | 2 +- src/config.zig | 2 +- src/exec.zig | 10 ++++++++-- test/cross_replay.py | 7 ++++--- 5 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src/config.zig') diff --git a/src/builtins.zig b/src/builtins.zig index b101e34d..38a4d835 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -1487,8 +1487,11 @@ pub const Tty = struct { }; /// A Linux terminal with the current session mounted through kernel v9fs. +/// A word of every hosted build, Linux's or not: a build's words are its +/// Help's rows, which a replica of another build's session makes as its +/// session did (a Linux client of a FreeBSD one diverged at SPC ?). pub const Tty9p = struct { - pub const enabled = pardes.hosted and @import("builtin").os.tag == .linux; + pub const enabled = pardes.hosted; pub fn run(c: Ctx) void { exec.spawnV9fsTty(c.p, c.id); } diff --git a/src/builtins_json.zig b/src/builtins_json.zig index 489f1e22..3402b2ee 100644 --- a/src/builtins_json.zig +++ b/src/builtins_json.zig @@ -74,7 +74,7 @@ pub fn write(w: *std.Io.Writer) !void { test "docs/typ/builtins-.json is what the registry says now" { // a terminal build checks its file, a window build its own; both are - // Linux builds' lists (Tty9p is Linux's) + // Linux builds' lists if (comptime @import("builtin").os.tag != .linux) return error.SkipZigTest; const file = switch (pardes.platform) { .tty => "builtins-tty.json", diff --git a/src/config.zig b/src/config.zig index ebca14ea..c1f1981c 100644 --- a/src/config.zig +++ b/src/config.zig @@ -2283,7 +2283,7 @@ const doc_typ = struct { for (std.enums.values(Builtin)) |b| if (leader_path.get(b)) |p| if (std.mem.eql(u8, p, path[0..n])) return true; for (leader_args) |la| if (std.mem.eql(u8, la.path, path[0..n])) return true; - // A word this build leaves out (Tty9p off Linux) keeps its leader in the other's. + // A word this build leaves out keeps its leader in the other's. var buf: [96]u8 = undefined; const needle = std.fmt.bufPrint(&buf, "\"leader\": \"{s}\"", .{k}) catch return false; return std.mem.indexOf(u8, other_build, needle) != null; diff --git a/src/exec.zig b/src/exec.zig index c5218279..af312c45 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -409,14 +409,20 @@ pub fn spawnTty(p: *Pardes, from: usize) ?*Pane { } pub fn spawnV9fsTty(p: *Pardes, from: usize) void { - if (comptime !hosted or @import("builtin").os.tag != .linux) + if (comptime !hosted) return p.reportError(from, "Tty9p", error.LinuxRequired); + // The session's host's and its 9P server's: a replica, of another + // build too (a Linux client of a FreeBSD session), has their answers. + if (!pardes.tape.authority(onLinux, .{})) return p.reportError(from, "Tty9p", error.LinuxRequired); - // The 9P server's: a replica has the authority's answer. if (!pardes.tape.authority(servesSocket, .{p})) return p.reportError(from, "Tty9p", error.SessionSocketRequired); _ = spawnTtyWithMount(p, from, true); } +fn onLinux() bool { + return @import("builtin").os.tag == .linux; +} + fn servesSocket(p: *Pardes) bool { return p.fs.socket_path.len != 0; } diff --git a/test/cross_replay.py b/test/cross_replay.py index 9ce7d152..21367ca1 100644 --- a/test/cross_replay.py +++ b/test/cross_replay.py @@ -20,8 +20,9 @@ Restore of a dump that is not there (errors the host reports, anyerrors). It greps and finds, as a script does, which a 9P write runs on the loop: its reads are a tape site (a replica of a session a script greps in read no file and diverged). And it holds what a digest must name the same in -every build: a +LocationsConfig, whose origin is a Builtin, and a file -pane renamed (H19: no Linux client could join a FreeBSD session so). +every build: a +LocationsConfig, whose origin is a Builtin, a file +pane renamed (H19: no Linux client could join a FreeBSD session so), and +a +Help, which lists the build's words (Tty9p was a Linux build's alone). """ import os import shutil @@ -75,7 +76,7 @@ def record(binary, out): started.append(int(status['pid'])) pane = next(int(row.split()[0]) for row in c.read('/index').splitlines() if row.split()[1] == b'text') for line in (f'Look {root / "fifo"}', f'Look {root / "dir"}', f'Save {root / "a.txt" / "x"}', - f'Restore {root / "nothing.dump"}', 'Grep hello', 'Find txt', 'LocationsConfig'): + f'Restore {root / "nothing.dump"}', 'Grep hello', 'Find txt', 'LocationsConfig', 'Help'): try: c.write(f'/pane/{pane}/exec', line.encode() + b'\n') except Exception: -- cgit v1.3