diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /src | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src')
69 files changed, 20610 insertions, 7602 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig index a089a7cf..377440bd 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -12,7 +12,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {}; const log = std.log.scoped(.ninep); pub const darwin = builtin.os.tag.isDarwin(); -pub const supported = builtin.os.tag == .linux or darwin; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or darwin; pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len; pub fn setCloexec(fd: c_int) void { @@ -29,7 +29,7 @@ pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 { pub const FileFacts = struct { mode: u32, uid: libc.uid_t }; pub fn statNoFollow(path: [:0]const u8) ?FileFacts { - if (comptime darwin) { + if (comptime builtin.os.tag != .linux) { var stat: libc.Stat = undefined; if (libc.fstatat(libc.AT.FDCWD, path, &stat, libc.AT.SYMLINK_NOFOLLOW) != 0) return null; return .{ .mode = stat.mode, .uid = stat.uid }; @@ -195,6 +195,9 @@ pub const Listener = struct { paused_ms: i64 = 0, path_buf: [sun_path_len]u8 = undefined, path_len: usize = 0, + /// The socket's lock (`claim`), held for the listener's life; -1 when none. + lock: c_int = -1, + lock_buf: [lock_path_len:0]u8 = undefined, /// The registry entry this editor posted /// (`$XDG_RUNTIME_DIR/9p/pardes/<name>`), zero when it did not. posted_buf: [sun_path_len]u8 = undefined, @@ -721,6 +724,12 @@ pub const Listener = struct { /// and only between the editor's steps: a step out in a syscall may be /// reading one of these entries. fn collectOs(l: *Listener) void { + // A request out in a host syscall may be using an entry's path, or + // made one that no fid names yet (a walk's next element): swept at + // the next quiet instead. Taken back after a step of its own, the + // editor's frame (tty pollFrame) came here with one out, and a walk + // of a directory just made was answered ENOENT. + if (pardes.turn.out != 0) return; const core = l.core; var i: usize = 0; while (i < core.fs.os_paths.items.len) { @@ -893,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(); @@ -913,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(); } @@ -939,6 +955,8 @@ pub const Listener = struct { z[l.path_len] = 0; _ = libc.unlink(z[0..l.path_len :0]); } + // The lock after the socket: a socket there never lacks a held lock. + if (l.lock >= 0) release(l.lock, std.mem.sliceTo(&l.lock_buf, 0)); freeRunner(l.runner); gpa.destroy(l); } @@ -961,27 +979,32 @@ fn freeRunner(runner: *Runner) void { /// The socket this process listens on, kept where a fatal signal's handler /// can reach it without allocating (unlinkOwnSocket); empty when none. -var own_socket: [sun_path_len:0]u8 = undefined; +var own_socket: [lock_path_len:0]u8 = undefined; var own_socket_len: usize = 0; -/// Removes the socket this process listens on. Async-signal-safe (one -/// unlink): SIGTERM and SIGHUP call it before the signal's own death, so a -/// killed editor leaves no socket behind. +/// Removes the socket this process listens on, then its lock (the death +/// lets go of it). Async-signal-safe (two unlinks): SIGTERM and SIGHUP call +/// it before the signal's own death, so a killed editor leaves no socket +/// behind. pub fn unlinkOwnSocket() void { if (comptime !supported) return; const n = own_socket_len; if (n == 0) return; own_socket_len = 0; _ = libc.unlink(own_socket[0..n :0]); + own_socket[n..][0..lock_suffix.len].* = lock_suffix.*; + own_socket[n + lock_suffix.len] = 0; + _ = libc.unlink(own_socket[0 .. n + lock_suffix.len :0]); } -/// SIGTERM and SIGHUP with no handler of their own (a GUI's): the socket -/// goes, then the signal's own death. A host that handles them itself (the -/// terminal's Killed, the detached server's quit) removes it its own way. +/// SIGTERM, SIGHUP and SIGINT with no handler of their own (a GUI's, a +/// detached session's): the socket goes, then the signal's own death. A +/// host that handles them itself (the terminal's Killed, SDL's quit) +/// removes it its own way. fn armSocketCleanup() void { const sig = std.posix.SIG; const on: std.posix.Sigaction = .{ .handler = .{ .handler = onFatalSignal }, .mask = std.posix.sigemptyset(), .flags = std.posix.SA.RESETHAND }; - for ([_]std.posix.SIG{ sig.TERM, sig.HUP }) |s| { + for ([_]std.posix.SIG{ sig.TERM, sig.HUP, sig.INT }) |s| { var was: std.posix.Sigaction = undefined; std.posix.sigaction(s, null, &was); if (was.handler.handler == sig.DFL) std.posix.sigaction(s, &on, null); @@ -995,6 +1018,8 @@ pub var fatal_cleanup: ?*const fn () void = null; fn onFatalSignal(s: std.posix.SIG) callconv(.c) void { unlinkOwnSocket(); if (fatal_cleanup) |cleanup| cleanup(); + // Its shells go with it, the daemon's dups let go. + @import("daemon.zig").hangUp(); // SA_RESETHAND put the default back: the same signal, now fatal. _ = std.c.raise(s); } @@ -1024,10 +1049,76 @@ pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void { const facts = statNoFollow(path) orelse continue; if (facts.mode & 0o170000 != 0o140000 or facts.uid != libc.getuid()) continue; if (probe(path) == .live) continue; - _ = libc.unlink(path); + removeDead(path); } } +/// A socket's lock, `<socket>.lock` beside it: flocked by the socket's owner +/// from before it binds to after it unlinks (`claim`, `release`), so held +/// is alive and free is dead. A connect cannot say that: FreeBSD refuses one +/// to a listener whose queue is full exactly as to a socket nobody listens +/// on, and a starting owner's socket refuses until it listens. +const lock_suffix = ".lock"; +const lock_path_len = sun_path_len + lock_suffix.len; + +fn lockPath(buf: *[lock_path_len:0]u8, sock: []const u8) ?[:0]const u8 { + return std.fmt.bufPrintSentinel(buf, "{s}" ++ lock_suffix, .{sock}, 0) catch null; +} + +/// `sock`'s lock taken (its path in `buf`), made when it is missing: null +/// while someone else holds it. A probe holds it shared for a moment, so a +/// refusal is asked again for a while before it counts. One taken on a file +/// its owner unlinked as it let go is taken again on the one at the path. +fn claim(sock: []const u8, buf: *[lock_path_len:0]u8) ?c_int { + const path = lockPath(buf, sock) orelse return null; + var tries: u32 = 0; + while (tries < 50) : (tries += 1) { + const fd = libc.open(path, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return null; + if (libc.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB) != 0) { + _ = libc.close(fd); + Client.nap(1); + continue; + } + const io = std.Io.Threaded.global_single_threaded.io(); + const held = (std.Io.File{ .handle = fd, .flags = .{ .nonblocking = false } }).stat(io) catch null; + const named = std.Io.Dir.cwd().statFile(io, path, .{ .follow_symlinks = false }) catch null; + if (held != null and named != null and held.?.inode == named.?.inode) return fd; + _ = libc.close(fd); + } + return null; +} + +/// A claim let go, its lock file removed while still held: nobody else +/// unlinks it, and a claim on the unlinked file is taken again (`claim`). +fn release(fd: c_int, path: [:0]const u8) void { + _ = libc.unlink(path); + _ = libc.close(fd); +} + +/// What `sock`'s lock says of its owner: held is `.live`, free `.stale`, and +/// none at all `.none`, a socket of a pardes from before the locks, which a +/// connect judges. Anything unclear is `.live`. +fn lockState(sock: []const u8) Probe { + var buf: [lock_path_len:0]u8 = undefined; + const path = lockPath(&buf, sock) orelse return .live; + const fd = libc.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (fd < 0) return if (libc.errno(fd) == .NOENT) .none else .live; + defer _ = libc.close(fd); + return if (libc.flock(fd, std.posix.LOCK.SH | std.posix.LOCK.NB) == 0) .stale else .live; +} + +/// A dead owner's socket removed, and its lock, under that lock: no new +/// owner can come between the judging and the unlink. Under the lock, an +/// owner that took it is gone, and a socket of a pardes from before the +/// locks still has its connect to answer. +fn removeDead(sock: [:0]const u8) void { + var buf: [lock_path_len:0]u8 = undefined; + const fd = claim(sock, &buf) orelse return; + if (connectProbe(sock) == .stale) _ = libc.unlink(sock); + release(fd, std.mem.sliceTo(&buf, 0)); +} + /// The names in `dir` that `keep` takes (all with none), into `names` as a length byte and /// the name each, until it is full; the bytes staged. They are staged /// before anything is unlinked, so a sweep never asks a directory to keep @@ -1105,6 +1196,15 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ .greet_timeout_ms = Listener.greet_deadline_ms, }); const p = socketPath(&l.path_buf, dir, entry_name).?; + // The name's lock first, held from here to the end: whoever else holds + // it is alive, however its socket answers a connect. Held, a socket + // there is a dead owner's, or one from a pardes before the locks, which + // the connects below still ask. + l.lock = claim(p, &l.lock_buf) orelse { + log.warn("something is already listening on {s}", .{p}); + l.deinit(gpa); + return null; + }; // Listened on under a name of its own, then renamed into place: a client // that waits for the socket to appear finds it listening already. Bound // at its own name, it was there a moment before listen(2), and a connect @@ -1204,6 +1304,17 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: [ const alive = transport.isListening; +/// Whether a live session listens under `name` already: a second +/// `--detach=<name>` is refused before anything is made. +pub fn taken(name: []const u8) bool { + if (comptime !supported) return false; + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf) orelse return false; + var buf: [sun_path_len]u8 = undefined; + const p = socketPath(&buf, dir, name) orelse return false; + return probe(p) == .live; +} + /// What is at a socket path, in cloud9's three answers (`post.Probe`). /// Asked here with libc rather than through `cloud9.post.probe`, whose /// raw Linux syscalls darwin cannot compile, but the classification is @@ -1221,7 +1332,18 @@ const Probe = enum { none, stale, live }; /// `fcntl` instead. const probe_socket_kind: c_uint = libc.SOCK.STREAM | (if (darwin) 0 else libc.SOCK.NONBLOCK); +/// Whether whoever made the socket at `path` (or the one a registry entry +/// there names) is alive: its lock decides (`lockState`), and a connect +/// only for a socket that has none. fn probe(path: [:0]const u8) Probe { + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(path, &link_buf, link_buf.len); + const sock = if (n > 0 and n < link_buf.len and link_buf[0] == '/') link_buf[0..@intCast(n)] else path; + const locked = lockState(sock); + return if (locked == .none) connectProbe(path) else locked; +} + +fn connectProbe(path: [:0]const u8) Probe { if (path.len + 1 > sun_path_len) return .live; // cannot ask; assume occupied var addr: libc.sockaddr.un = .{ .path = @splat(0) }; @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); @@ -1261,7 +1383,8 @@ fn probe(path: [:0]const u8) Probe { /// the somebody. It is only ever a symlink that is followed or /// unlinked, because that is the only thing `postToRegistry` makes and /// anything else under a name belongs to whoever put it there; and only -/// a definite refusal counts as gone. The socket a stale entry points +/// a lock let go (or, with none, a definite refusal) counts as gone +/// (`probe`). The socket a stale entry points /// at goes too, but not before a stat agrees it is a socket of ours: a /// plain file answers a connect with the same refusal. /// Is this the kind of path this editor is allowed to delete? A registry @@ -1310,12 +1433,10 @@ fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void { if (!ourSocket(target, sockets)) continue; const t = statNoFollow(target) orelse continue; if (t.mode & 0o170000 != 0o140000 or t.uid != libc.getuid()) continue; - // Ask again, immediately before deleting. The first probe was of the - // ENTRY and is by now several syscalls old; a socket that is bound but - // has not reached listen(2) yet answers ECONNREFUSED exactly like a - // dead one, and that window is every server's startup. - if (probe(target) != .stale) continue; - _ = libc.unlink(target); + // Judged again, under its lock: the first probe was of the ENTRY and + // is by now several syscalls old, and a new owner may have claimed + // the name since. + removeDead(target); } if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc }); } @@ -1414,21 +1535,42 @@ test "the registry sweep takes the dead entries and leaves everything else" { const outsider_sock = try std.fmt.bufPrintSentinel(&paths[5], "{s}/sweep-outsider-{d}.sock", .{ base, pid }, 0); var outsider_buf: [sun_path_len:0]u8 = undefined; const outsider = try std.fmt.bufPrintSentinel(&outsider_buf, "{s}/outsider", .{svc}, 0); + // Live, its lock held, but bound and not listening: a connect is + // refused, on every system, as FreeBSD refuses one to a live session + // whose queue is full. Its lock is what says it lives. + var starting_paths: [2][sun_path_len:0]u8 = undefined; + const starting_sock = try std.fmt.bufPrintSentinel(&starting_paths[0], "{s}/pardes-9p-sweepstarting-{d}.sock", .{ base, pid }, 0); + const starting = try std.fmt.bufPrintSentinel(&starting_paths[1], "{s}/starting", .{svc}, 0); + var locks: [3][lock_path_len:0]u8 = undefined; + const dead_lock = lockPath(&locks[2], dead_sock).?; defer { - for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider }) |p| _ = libc.unlink(p); + for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider, starting_sock, starting, dead_lock }) |p| _ = libc.unlink(p); _ = libc.rmdir(svc); } + const live_lock = claim(live_sock, &locks[0]) orelse return error.LockFailed; + defer release(live_lock, std.mem.sliceTo(&locks[0], 0)); const listening = bindSocket(live_sock); try testing.expect(listening >= 0); defer _ = libc.close(listening); try testing.expectEqual(@as(c_int, 0), libc.listen(listening, 1)); - // Bound and then dropped: the file stays, and nobody answers it — - // exactly what an aborted test leaves behind. + const starting_lock = claim(starting_sock, &locks[1]) orelse return error.LockFailed; + defer release(starting_lock, std.mem.sliceTo(&locks[1], 0)); + const bound = bindSocket(starting_sock); + try testing.expect(bound >= 0); + defer _ = libc.close(bound); + try testing.expectEqual(@as(c_int, 0), libc.symlink(starting_sock, starting)); + + // Bound and then dropped, its lock let go: the files stay, and nobody + // answers it -- exactly what a killed session leaves behind. const abandoned = bindSocket(dead_sock); try testing.expect(abandoned >= 0); _ = libc.close(abandoned); + const dropped = libc.open(dead_lock, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + try testing.expect(dropped >= 0); + try testing.expectEqual(@as(c_int, 0), libc.flock(dropped, std.posix.LOCK.EX)); + _ = libc.close(dropped); try testing.expectEqual(@as(c_int, 0), libc.symlink(live_sock, live)); try testing.expectEqual(@as(c_int, 0), libc.symlink(dead_sock, dead)); @@ -1485,6 +1627,9 @@ test "the registry sweep takes the dead entries and leaves everything else" { try testing.expect(statNoFollow(dead) == null); try testing.expect(statNoFollow(dead_sock) == null); + try testing.expect(statNoFollow(dead_lock) == null); + try testing.expect(statNoFollow(starting) != null); + try testing.expect(statNoFollow(starting_sock) != null); try testing.expect(statNoFollow(live) != null); try testing.expect(statNoFollow(live_sock) != null); try testing.expect(statNoFollow(stranger) != null); @@ -1616,8 +1761,9 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" // adopting the registry moves nothing, it only advertises. try testing.expect(statNoFollow(sock) != null); // It appeared already listening, renamed into place from a name of - // its own, which is gone. + // its own, which is gone; its lock held all the while. try testing.expect(alive(sock)); + try testing.expectEqual(Probe.live, lockState(sock)); var tmp_buf: [sun_path_len:0]u8 = undefined; try testing.expect(statNoFollow(try std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ sock, libc.getpid() }, 0)) == null); // And the registry holds a symlink to it one directory down, so @@ -1627,10 +1773,20 @@ test "a listening editor posts itself into the 9P registry and unposts on stop" const n = libc.readlink(entry, &link, link.len); try testing.expect(n > 0); try testing.expectEqualStrings(sock, link[0..@intCast(n)]); + // A host path no fid names is swept, but never while a request is + // out in a syscall: it may be that request's, mid-walk. + try p.fs.os_paths.append(gpa, .{ .node = @import("fs.zig").os_node | 1, .path = try gpa.dupe(u8, "/tmp") }); + pardes.turn.out = 1; + l.collectOs(); + pardes.turn.out = 0; + try testing.expectEqual(@as(usize, 1), p.fs.os_paths.items.len); + l.collectOs(); + try testing.expectEqual(@as(usize, 0), p.fs.os_paths.items.len); } // Stopping takes the name back out, so the next editor of that name - // is not refused by its own corpse. + // is not refused by its own corpse; the lock goes with the socket. try testing.expect(libc.readlink(entry, &link, link.len) < 0); + try testing.expectEqual(Probe.none, lockState(sock)); } test "Unix TCP and QUIC share one listener through reads writes reconnects and reset" { @@ -1821,9 +1977,98 @@ test "a change waits while the editor is out mid-step, a read does not, and the pardes.turn.wake(); try testing.expect(writer.done.load(.acquire)); if (writer.failure) |err| return err; + // Its bytes are held until its open is let go (pane.zig `batchFor`), and + // the client lets it go without waiting: rested again until that lands, + // the body read only while the editor has the turn. + while (!std.mem.eql(u8, pane.file.?.content, "after\n") and Client.nowMs() < deadline) { + pardes.turn.rest(); + Client.nap(1); + pardes.turn.wake(); + } try testing.expectEqualStrings("after\n", pane.file.?.content); } +test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var directory: [64:0]u8 = undefined; + _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-kept-XXXXXX", .{}, 0); + if (mkdtemp(&directory) == null) return error.TempDirectoryFailed; + defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); + const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; + defer { + if (old_runtime) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + gpa.free(v); + } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + } + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1)); + + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("before\n"); + while (p.nextEffect()) |_| {} + const l = listen(testing.io, gpa, p, "kept", "", null, null) orelse return error.ListenFailed; + defer { + l.reset(p); + l.deinit(gpa); + } + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); + const long = try gpa.alloc(u8, 2 * msize + 5); + defer gpa.free(long); + @memset(long, 'k'); + long[long.len - 1] = '\n'; + + const Worker = struct { + l: *Listener, + body: []const u8, + long: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; + }; + } + fn read(w: *@This(), want: []const u8) !void { + const got = try Client.readMount(testing.allocator, w.l.path(), w.body, w.body, limits.max_file_bytes); + defer testing.allocator.free(got); + try testing.expectEqualStrings(want, got); + } + fn check(w: *@This()) !void { + defer Client.forget(w.l.path()); + try w.read("before\n"); + try w.read("before\n"); + // A write longer than a message, and a miss, on the same one. + try Client.writeMount(testing.allocator, w.l.path(), w.body, w.long); + try w.read(w.long); + try testing.expectError(error.NotFound, Client.readMount(testing.allocator, w.l.path(), "/pane/no-such", "/pane/no-such", 64)); + try testing.expectEqual(@as(u64, 1), w.l.accepts.load(.acquire)); + // The peer hangs up (a Restore cuts its clients): the next + // operation dials again and is answered. + for (&w.l.runner.conns) |*conn| if (conn.live()) conn.close(); + const by = Client.nowMs() + Client.budget_ms; + while (w.l.runner.count() != 0 and Client.nowMs() < by) Client.nap(1); + try w.read(w.long); + try testing.expectEqual(@as(u64, 2), w.l.accepts.load(.acquire)); + } + }; + var worker: Worker = .{ .l = l, .body = body, .long = long }; + pardes.turn.rest(); + const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); + const deadline = Client.nowMs() + 3 * Client.budget_ms; + while (!worker.done.load(.acquire) and Client.nowMs() < deadline) Client.nap(1); + thread.join(); + // Forgotten: the kept connection is closed, and its task sees it. + const settle_by = Client.nowMs() + Client.budget_ms; + while (l.runner.count() != 0 and Client.nowMs() < settle_by) Client.nap(1); + pardes.turn.wake(); + if (worker.failure) |err| return err; + try testing.expectEqual(@as(usize, 0), l.runner.count()); +} + test "a connection holds up to 128 reads, and the next is refused in words" { try testing.expectEqual(@as(usize, 128), ninep.max_held); if (comptime supported) try testing.expectEqualStrings("too many reads waiting: 128", Runner.Engine.e_waiting); @@ -2162,11 +2407,20 @@ pub const Client = struct { } pub fn readLimit(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(false, gpa, dial, path, display_path, max_bytes); + } + + /// readLimit through a mount: its connection kept for the next (`kept`). + pub fn readMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return readWith(true, gpa, dial, path, display_path, max_bytes); + } + + fn readWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) catch |err| { + return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes), keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2182,7 +2436,16 @@ pub const Client = struct { /// Whether a peer answers at `dial`: a version and attach, and its root /// read. Any answer from the peer, an error of its own included, is one. pub fn probe(gpa: std.mem.Allocator, dial: []const u8) !void { - const got = readLimit(gpa, dial, "", "", 0) catch |err| switch (err) { + return probeWith(false, gpa, dial); + } + + /// probe for a Mount: the connection kept as the mount's. + pub fn probeMount(gpa: std.mem.Allocator, dial: []const u8) !void { + return probeWith(true, gpa, dial); + } + + fn probeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8) !void { + const got = readWith(keep, gpa, dial, "", "", 0) catch |err| switch (err) { error.IsDirectory, error.NotFound, error.Remote, error.FileTooLarge => return, else => return err, }; @@ -2190,11 +2453,20 @@ pub const Client = struct { } pub fn write(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(false, gpa, dial, path, bytes); + } + + /// write through a mount: its connection kept for the next (`kept`). + pub fn writeMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + return writeWith(true, gpa, dial, path, bytes); + } + + fn writeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { if (comptime !supported) return error.Unsupported; var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| { + const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes, keep) catch |err| { if (err == error.Remote) said = remote; return err; }; @@ -2206,9 +2478,13 @@ pub const Client = struct { len: usize = 0, fn set(r: *RemoteError, msg: []const u8) error{Remote} { + r.keep(msg); + return error.Remote; + } + + fn keep(r: *RemoteError, msg: []const u8) void { r.len = @min(msg.len, r.buf.len); @memcpy(r.buf[0..r.len], msg[0..r.len]); - return error.Remote; } }; @@ -2262,6 +2538,18 @@ pub const Client = struct { in: [msize]u8 = undefined, out: [msize]u8 = undefined, stage: [msize]u8 = undefined, + /// Version and attach answered: fid 0 is the peer's root, and each + /// operation walks a fid of its own from it, so a kept connection + /// (`kept`) pays for neither again. + attached: bool = false, + root_qid: ninep.Qid = undefined, + next_fid: u32 = 1, + /// Tags whose answers nobody waits for (a clunk's): dropped by + /// whoever settles next. + unwaited: u32 = 0, + /// The dial a kept connection answers for. + dial_buf: [sun_path_len + 32]u8 = undefined, + dial_len: usize = 0, fn wait(s: *Session, events: i16) Error!void { while (true) { @@ -2314,7 +2602,13 @@ pub const Client = struct { while (true) { if (nowMs() >= s.deadline) return Error.Timeout; try s.flush(); - if (s.cl.take()) |done| return done; + if (s.cl.take()) |done| { + if (done.tag < 32 and s.unwaited & bit(done.tag) != 0) { + s.unwaited &= ~bit(done.tag); + continue; + } + return done; + } if (s.cl.dead) return Error.Botch; const room = s.cl.in.len - s.cl.in_len; if (room == 0) return Error.Botch; @@ -2352,39 +2646,290 @@ pub const Client = struct { } fn dropNoWait(s: *Session, fid: u32) void { - _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + s.unwaited |= bit(tag); s.flush() catch {}; } + + fn send(s: *Session, req: ninep.Client.Request) Error!u16 { + return s.cl.submit(req) catch Error.Botch; + } + + fn newFid(s: *Session) u32 { + const fid = s.next_fid; + s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; + return fid; + } + + /// A new connection's Tversion and Tattach, sent with the first + /// operation's requests behind them in one flight (`land`). + fn greet(s: *Session, f: *Flight) Error!void { + if (s.attached) return; + _ = f.add(.version, try s.send(.{ .version = .{} })); + // ponytail: cloud9's client submits nothing while a Tversion is + // out, so its msize is taken as asked until the Rversion lands + // (`land` puts it back); only a pardes is dialled here, and every + // one offers ninep.msize. A Twrite in this flight stays under the + // least a listener offers (`flight_write`). The fix is cloud9's + // client taking requests while it versions. + s.cl.versioning = false; + s.cl.msize = s.cl.asked; + _ = f.add(.attach, try s.send(.{ .attach = .{ .fid = 0, .uname = uname } })); + } + + /// The walk to a new fid at `names` (two Twalks past 16 names, the + /// second from the fid to itself), from the root. + fn walkNew(s: *Session, f: *Flight, names: []const []const u8) Error!u32 { + const fid = s.newFid(); + const first = names[0..@min(names.len, ninep.max_welem)]; + f.add(.walk, try s.send(.{ .walk = .{ .fid = 0, .newfid = fid, .names = first } })).asked = @intCast(first.len); + if (names.len > first.len) + f.add(.walk, try s.send(.{ .walk = .{ .fid = fid, .newfid = fid, .names = names[first.len..] } })).asked = @intCast(names.len - first.len); + return fid; + } + + /// Sends the flight and takes every answer of it, a read's bytes + /// into `sink` as they come (each valid only until the next), so + /// that the connection is left with nothing out but its clunks. A + /// refused Tversion is Remote at once: nothing after it is answered. + fn land(s: *Session, f: *Flight, sink: anytype) Error!void { + if (f.get(.version) != null) s.cl.versioning = true; + while (f.open()) { + const done = try s.settle(); + const item = f.find(done.tag) orelse return Error.Botch; + item.done = true; + switch (done.result) { + .fail => |words| { + item.failed = true; + item.said.keep(words); + if (item.step == .version) return Error.Remote; + }, + .version => { + if (item.step != .version or s.cl.msize == 0) return Error.Botch; + }, + .attach => |qid| { + if (item.step != .attach) return Error.Botch; + s.root_qid = qid; + s.attached = true; + }, + .walk => |w| { + if (item.step != .walk) return Error.Botch; + item.n = w.nwqid; + if (w.nwqid > 0) item.qid = w.wqid[w.nwqid - 1]; + }, + .stat => |st| { + if (item.step != .stat) return Error.Botch; + item.n = st.length; + }, + .open => |o| { + if (item.step != .open) return Error.Botch; + item.qid = o.qid; + }, + .read => |data| { + if (item.step != .read) return Error.Botch; + item.n = data.len; + sink.take(item, data); + // A read is timed by its progress, as a long write + // is: each answer gives the rest the budget again, so + // a large file over a slow link is not cut off. + s.deadline = @max(s.deadline, nowMs() +| budget_ms); + }, + .write => |count| { + if (item.step != .write) return Error.Botch; + item.n = count; + }, + else => return Error.Botch, + } + } + } }; - /// Version, attach and a walk to `names`: the fid there and its qid. - fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { - _ = try s.ask(.{ .version = .{} }, remote); - if (s.cl.msize == 0) return Error.Botch; + fn bit(tag: u16) u32 { + return @as(u32, 1) << @intCast(tag); + } - const root: u32 = 0; - var here = (try s.ask(.{ .attach = .{ .fid = root, .uname = uname } }, remote)).attach; - var cur: u32 = root; - var next: u32 = 1; + /// The requests of one flight, each its own tag, sent back to back: a + /// pardes takes one connection's requests in order (cloud9's engine + /// starts a request once the one before it is answered or parked), so + /// a walk, the open of its fid and the first read or write need no + /// answer between them, and an operation costs one round trip. + const Flight = struct { + items: [ninep.Client.window]Item = undefined, + len: usize = 0, - var i: usize = 0; - while (i < names.len) { - const n = @min(ninep.max_welem, names.len - i); - const w = (try s.ask(.{ .walk = .{ - .fid = cur, - .newfid = next, - .names = names[i..][0..n], - } }, remote)).walk; - if (w.nwqid != n) return Error.NotFound; - here = w.wqid[n - 1]; - if (cur != root) s.drop(cur); - cur = next; - next = if (next == 1) 2 else 1; - i += n; + const Step = enum { version, attach, walk, stat, open, read, write }; + const Item = struct { + step: Step, + tag: u16, + done: bool = false, + failed: bool = false, + said: RemoteError = .{}, + /// walk: names asked; read: its offset and count. + asked: u16 = 0, + off: u64 = 0, + count: u32 = 0, + /// walk: qids answered; stat: the length; read, write: bytes. + n: u64 = 0, + qid: ninep.Qid = undefined, + }; + + fn add(f: *Flight, step: Step, tag: u16) *Item { + f.items[f.len] = .{ .step = step, .tag = tag }; + f.len += 1; + return &f.items[f.len - 1]; + } + + fn find(f: *Flight, tag: u16) ?*Item { + for (f.items[0..f.len]) |*item| if (item.tag == tag and !item.done) return item; + return null; + } + + fn get(f: *Flight, step: Step) ?*Item { + for (f.items[0..f.len]) |*item| if (item.step == step) return item; + return null; + } + + fn open(f: *const Flight) bool { + for (f.items[0..f.len]) |item| if (!item.done) return true; + return false; + } + + /// The first refusal in the order asked: a version's or an attach's + /// words, a walk short of its names, an open's refusal. + fn check(f: *Flight, remote: *RemoteError) Error!void { + for (f.items[0..f.len]) |*item| switch (item.step) { + .version, .attach, .open => if (item.failed) return remote.set(item.said.buf[0..item.said.len]), + .walk => { + if (item.failed) return remote.set(item.said.buf[0..item.said.len]); + if (item.n != item.asked) return Error.NotFound; + }, + .stat, .read, .write => {}, + }; + } + + /// The walked file's qid: the last walk's, or the root's for none. + fn qid(f: *Flight, root: ninep.Qid) ninep.Qid { + var at = root; + for (f.items[0..f.len]) |item| if (item.step == .walk and item.n > 0) { + at = item.qid; + }; + return at; + } + + /// Whether the walk made the fid, so that it is owed a clunk. + fn made(f: *Flight) bool { + const first = f.get(.walk) orelse return false; + return !first.failed and first.n == first.asked; } - return .{ .fid = cur, .qid = here }; + }; + + /// A write in a new connection's first flight is sent before the msize + /// is known: no larger than the least a pardes listener offers. + const flight_write = ninep.min_msize - 23; + + /// Reads ahead this many Treads at once (the window, less a clunk + /// still out and one spare). + const read_ahead = ninep.Client.window - 2; + + /// Version and attach on a new connection, and a walk to `names`, in + /// one flight: the fid there and its qid. + fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } { + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + try landChecked(s, &f, NoSink{}, remote); + f.check(remote) catch |err| { + if (f.made()) s.dropNoWait(fid); + return err; + }; + return .{ .fid = fid, .qid = f.qid(s.root_qid) }; } + /// `land`, saying a refused Tversion in its own words: nothing else of + /// that flight is answered. + fn landChecked(s: *Session, f: *Flight, sink: anytype, remote: *RemoteError) Error!void { + s.land(f, sink) catch |err| { + if (err == Error.Remote) try f.check(remote); + return err; + }; + } + + const NoSink = struct { + fn take(_: NoSink, _: *Flight.Item, _: []const u8) void {} + }; + + /// A read's bytes taken in order: a file's as they are, a directory's + /// entries as rows of names. A read ahead that came back out of order, + /// or past one that came back short, is not taken: what it held is + /// read again from where the taken bytes end. + const Taker = struct { + out: *std.Io.Writer.Allocating, + display_path: []const u8, + limit: usize, + /// The flight with the walk, whose answer says what was reached: + /// it lands before any read (a walk never parks). + flight: *Flight, + s: *const Session, + /// Bytes taken from the start; a directory's next offset. + end: u64 = 0, + /// The read that ended at `end` came back short, or empty (the end). + short: bool = false, + empty: bool = false, + /// What taking said (too large, a bad entry): the operation's + /// answer once every read out has landed. + err: ?Error = null, + + fn take(t: *Taker, item: *Flight.Item, data: []const u8) void { + if (t.err != null or item.off != t.end or t.short) return; + t.put(data) catch |err| { + t.err = err; + return; + }; + t.end += data.len; + t.short = data.len < item.count; + t.empty = data.len == 0; + } + + fn directory(t: *const Taker) bool { + return t.flight.qid(t.s.root_qid).type & ninep.qtdir != 0; + } + + fn maxBytes(t: *const Taker) u64 { + return @min(t.limit, @as(usize, if (t.directory()) limits.max_stream_bytes else limits.max_file_bytes)); + } + + fn wireLimit(t: *const Taker) u64 { + return if (t.directory()) limits.max_stream_bytes else t.maxBytes(); + } + + fn put(t: *Taker, data: []const u8) Error!void { + if (t.end + data.len > t.wireLimit()) return Error.FileTooLarge; + if (t.directory()) { + var pos: usize = 0; + while (pos < data.len) { + if (data.len - pos < 2) return Error.Botch; + const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); + if (len > data.len - pos) return Error.Botch; + const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; + const display_dir = std.mem.trimEnd(u8, t.display_path, "/"); + const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); + if (row_len > t.maxBytes() - t.out.written().len) return Error.FileTooLarge; + t.out.writer.print("{s}/{s}", .{ display_dir, entry.name }) catch return Error.Botch; + if (entry.qid.type & ninep.qtdir != 0) t.out.writer.writeByte('/') catch return Error.Botch; + t.out.writer.writeByte('\n') catch return Error.Botch; + pos += len; + } + } else t.out.writer.writeAll(data) catch return Error.Botch; + } + }; + + /// One operation in as few round trips as its answers allow: the walk, + /// a read's stat, the open and the first read or write go in one flight + /// (behind a new connection's version and attach). A file of the + /// length its stat said is whole when a read comes back short at that + /// length; a longer one is read ahead, the Treads for the rest sent at + /// once. Anything else (a directory, a file of no stated length) reads + /// on until a read comes back empty, the only end 9P has. fn transact( s: *Session, names: []const []const u8, @@ -2393,23 +2938,52 @@ pub const Client = struct { write_bytes: ?[]const u8, read_limit: usize, ) !void { - const at = try walkTo(s, names, remote); - const cur = at.fid; - const here = at.qid; - defer s.dropNoWait(cur); - - const directory = here.type & ninep.qtdir != 0; - - const mode: u8 = if (write_bytes != null) ninep.owrite else ninep.oread; - const truncate = write_bytes != null and names.len > 0 and + const writing = write_bytes != null; + const mode: u8 = if (writing) ninep.owrite else ninep.oread; + const truncate = writing and names.len > 0 and (std.mem.eql(u8, names[0], "os") or std.mem.eql(u8, names[names.len - 1], "body")); - _ = try s.ask(.{ .open = .{ .fid = cur, .mode = mode | if (truncate) ninep.otrunc else 0 } }, remote); + const fresh = !s.attached; + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + if (!writing) _ = f.add(.stat, try s.send(.{ .stat = .{ .fid = fid } })); + _ = f.add(.open, try s.send(.{ .open = .{ .fid = fid, .mode = mode | if (truncate) ninep.otrunc else 0 } })); + const chunk_max: usize = if (fresh) flight_write else s.cl.maxWrite(); + const first: []const u8 = if (write_bytes) |bytes| bytes[0..@min(bytes.len, chunk_max)] else ""; + if (writing and first.len != 0) { + // Out of the client's buffer first (it holds one msize): still + // sent without waiting. + try s.flush(); + _ = f.add(.write, try s.send(.{ .write = .{ .fid = fid, .offset = 0, .data = first } })); + } + // A read's count before the msize is known is the asked msize's. + if (!writing) { + const item = f.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } })); + item.count = s.cl.maxRead(); + } + var taker: Taker = .{ .out = out, .display_path = s.display_path, .limit = read_limit, .flight = &f, .s = s }; + try landChecked(s, &f, &taker, remote); + defer if (f.made()) s.dropNoWait(fid); + try f.check(remote); + if (taker.err) |err| return err; + const directory = taker.directory(); + if (write_bytes) |bytes| { if (directory) return Error.IsDirectory; var written: usize = 0; + if (f.get(.write)) |w| { + // Refused unopened, the open parked (a truncating one while + // the editor is out): sent again below, after it. + if (w.failed and !std.mem.eql(u8, w.said.buf[0..w.said.len], ninep.fs.e_bad_use)) return remote.set(w.said.buf[0..w.said.len]); + if (!w.failed) { + if (w.n == 0 or w.n > first.len) return Error.Botch; + written = w.n; + } + } while (written < bytes.len) { + s.deadline = nowMs() +| write_budget_ms; const chunk = bytes[written..][0..@min(bytes.len - written, s.cl.maxWrite())]; - const count = (try s.ask(.{ .write = .{ .fid = cur, .offset = written, .data = chunk } }, remote)).write; + const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, remote)).write; if (count == 0 or count > chunk.len) return Error.Botch; written += count; // A long write is timed by its progress: each answer gives @@ -2417,36 +2991,36 @@ pub const Client = struct { // in through one open, whole, however long it takes. The // budget is a write's: a server putting in tens of MiB it // held (pane.zig batchFor) answers that one write late. - s.deadline = nowMs() +| write_budget_ms; } return; } - const max_bytes: u64 = @min(read_limit, @as(usize, if (directory) limits.max_stream_bytes else limits.max_file_bytes)); - const wire_limit: u64 = if (directory) limits.max_stream_bytes else max_bytes; - var off: u64 = 0; + if (f.get(.read)) |r| if (r.failed and !std.mem.eql(u8, r.said.buf[0..r.said.len], ninep.fs.e_bad_use)) + return remote.set(r.said.buf[0..r.said.len]); + const length: u64 = if (f.get(.stat)) |st| (if (st.failed or directory) 0 else st.n) else 0; + const step: u64 = s.cl.maxRead(); while (true) { - const want: u32 = @intCast(@min(@as(u64, s.cl.maxRead()), wire_limit + 1 - off)); - const data = (try s.ask(.{ .read = .{ .fid = cur, .offset = off, .count = want } }, remote)).read; - if (data.len == 0) return; - if (off + data.len > wire_limit) return Error.FileTooLarge; - if (directory) { - var pos: usize = 0; - while (pos < data.len) { - if (data.len - pos < 2) return Error.Botch; - const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); - if (len > data.len - pos) return Error.Botch; - const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; - const display_dir = std.mem.trimEnd(u8, s.display_path, "/"); - const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); - if (row_len > max_bytes - out.written().len) return Error.FileTooLarge; - try out.writer.print("{s}/{s}", .{ display_dir, entry.name }); - if (entry.qid.type & ninep.qtdir != 0) try out.writer.writeByte('/'); - try out.writer.writeByte('\n'); - pos += len; - } - } else try out.writer.writeAll(data); - off += data.len; + if (taker.empty) return; + if (taker.short and length != 0 and taker.end == length) return; + // The rest of a file of known length at once; a directory, a + // file of none, or the end to be seen, a read at a time. + // ponytail: a file of no stated length is not read ahead. + const want: u64 = if (directory or taker.short or length <= taker.end) 1 else @min(read_ahead, (length - taker.end) / step + 1); + var batch: Flight = .{}; + var off = taker.end; + for (0..want) |_| { + if (off > taker.wireLimit()) break; + const count: u32 = @intCast(@min(step, taker.wireLimit() + 1 - off)); + const item = batch.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = off, .count = count } })); + item.off = off; + item.count = count; + off += count; + } + taker.short = false; + try s.land(&batch, &taker); + if (taker.err) |err| return err; + for (batch.items[0..batch.len]) |item| if (item.failed and item.off == taker.end and !taker.short) + return remote.set(item.said.buf[0..item.said.len]); } } @@ -2458,24 +3032,58 @@ pub const Client = struct { write_bytes: ?[]const u8, display_path: []const u8, read_limit: usize, + keep: bool, ) ![]u8 { if (comptime !supported) return Error.Dial; - const s = try startSession(gpa, dial, display_path); - defer endSession(gpa, s); + if (keep) if (kept.take(dial)) |s| { + if (fetchOn(s, gpa, names, remote, write_bytes, read_limit, display_path, true)) |bytes| return bytes else |err| switch (err) { + // Let go of by the peer since (its hangup still on the way): + // a read is asked again on a new connection. A write is not, + // as it may have been done. + error.Hangup, error.Botch => if (write_bytes != null) return err, + else => return err, + } + }; + return fetchOn(try startSession(dial, display_path), gpa, names, remote, write_bytes, read_limit, display_path, keep); + } + + fn fetchOn( + s: *Session, + gpa: std.mem.Allocator, + names: []const []const u8, + remote: *RemoteError, + write_bytes: ?[]const u8, + read_limit: usize, + display_path: []const u8, + keep: bool, + ) ![]u8 { + s.deadline = nowMs() +| budget_ms; + s.display_path = display_path; + var clean = false; + defer endSession(s, keep and clean); var out: std.Io.Writer.Allocating = .init(gpa); errdefer out.deinit(); - try transact(s, names, &out, remote, write_bytes, read_limit); + transact(s, names, &out, remote, write_bytes, read_limit) catch |err| { + // Refused with every answer in: the connection is as good as before. + clean = switch (err) { + error.Remote, error.NotFound, error.IsDirectory, error.FileTooLarge => true, + else => false, + }; + return err; + }; + clean = true; return out.toOwnedSlice(); } - /// A connected session at `dial` whose requests have the usual deadline. - fn startSession(gpa: std.mem.Allocator, dial: []const u8, display_path: []const u8) !*Session { + /// A new connection at `dial` whose requests have the usual deadline. + fn startSession(dial: []const u8, display_path: []const u8) !*Session { var sock_buf: [sun_path_len]u8 = undefined; const sock = try resolve(&sock_buf, dial); const deadline = nowMs() +| budget_ms; - const s = try gpa.create(Session); + // Not the caller's allocator: a kept one outlives the call (an arena's). + const s = try std.heap.page_allocator.create(Session); s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path }; - errdefer endSession(gpa, s); + errdefer endSession(s, false); if (sock == .quic) { if (comptime quic_enabled) { s.quic = quic.Connection.dial(sock.quic) catch return Error.Dial; @@ -2483,14 +3091,91 @@ pub const Client = struct { } else return Error.QuicUnavailable; } else s.fd = try connect(sock, deadline); s.cl = .init(.{ .in = &s.in, .out = &s.out }); + if (dial.len <= s.dial_buf.len) { + @memcpy(s.dial_buf[0..dial.len], dial); + s.dial_len = dial.len; + } return s; } - fn endSession(gpa: std.mem.Allocator, s: *Session) void { + fn endSession(s: *Session, keep: bool) void { + if (keep and s.attached and s.dial_len != 0 and !(quic_enabled and s.quic != null) and kept.put(s)) return; if (quic_enabled and s.quic != null) { s.quic.?.deinit(); } else if (s.fd >= 0) _ = libc.close(s.fd); - gpa.destroy(s); + std.heap.page_allocator.destroy(s); + } + + /// Connections kept open between operations, a few dials' (a mount's, + /// as Plan 9's mount keeps its channel): each costs the peer one of its + /// `max_conns` while kept, so only mounts and the Mount probe keep one. + const kept = struct { + const max = 8; + var lock: std.atomic.Mutex = .unlocked; + var slots: [max]?*Session = @splat(null); + + fn hold() void { + while (!lock.tryLock()) std.atomic.spinLoopHint(); + } + + /// The session kept for `dial`, out of the table, if it is still + /// there: what came while it waited (a clunk's answer) taken, and + /// a hangup or anything else seen as its end. + fn take(dial: []const u8) ?*Session { + hold(); + const s = for (&slots) |*slot| { + const got = slot.* orelse continue; + if (std.mem.eql(u8, got.dial_buf[0..got.dial_len], dial)) { + slot.* = null; + break got; + } + } else null; + lock.unlock(); + const found = s orelse return null; + if (usable(found)) return found; + endSession(found, false); + return null; + } + + fn usable(s: *Session) bool { + while (true) { + var fds = [1]libc.pollfd{.{ .fd = s.fd, .events = poll_in, .revents = 0 }}; + if (libc.poll(&fds, 1, 0) < 0) return false; + if (fds[0].revents == 0) return true; + if (fds[0].revents & poll_in == 0) return false; + const room = s.cl.in.len - s.cl.in_len; + if (room == 0) return false; + const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return false) orelse return true; + if (got == 0) return false; + _ = s.cl.push(s.stage[0..got]); + while (s.cl.take()) |done| { + if (done.tag >= 32 or s.unwaited & bit(done.tag) == 0) return false; + s.unwaited &= ~bit(done.tag); + } + if (s.cl.dead) return false; + } + } + + fn put(s: *Session) bool { + hold(); + defer lock.unlock(); + for (&slots) |*slot| if (slot.* == null) { + slot.* = s; + return true; + }; + return false; + } + + /// Closes what is kept for `dial` (an Unmount). + fn forget(dial: []const u8) void { + while (take(dial)) |s| endSession(s, false); + } + }; + + /// Lets go of a connection kept for `dial`: its mount is gone. + pub fn forget(dial: []const u8) void { + if (comptime !supported) return; + kept.forget(dial); } /// Writes `request`, then `more`, to `path` and reads the answer on the @@ -2504,8 +3189,8 @@ pub const Client = struct { const n = try elements(path, &names); var remote: RemoteError = .{}; errdefer said = remote; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); var written: usize = 0; @@ -2545,8 +3230,9 @@ pub const Client = struct { var names: [max_depth][]const u8 = undefined; const n = try elements(path, &names); var remote: RemoteError = .{}; - const s = try startSession(gpa, dial, path); - defer endSession(gpa, s); + _ = gpa; + const s = try startSession(dial, path); + defer endSession(s, false); const fid = (try walkTo(s, names[0..n], &remote)).fid; _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote); @@ -2622,7 +3308,7 @@ pub const Client = struct { const before = nowMs(); try testing.expectError( Error.Dial, - fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes), + fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes, false), ); try testing.expect(nowMs() - before < budget_ms); } @@ -2631,7 +3317,7 @@ pub const Client = struct { const transport_bytes = if (quic_enabled) @sizeOf(?quic.Connection) else 0; try testing.expectEqual(@as(usize, msize), @as(usize, (Session{ .fd = -1, .deadline = 0, .display_path = "" }).in.len)); try testing.expect(transport_bytes <= 64); - try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + transport_bytes); + try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + @sizeOf(@FieldType(Session, "dial_buf")) + transport_bytes); try testing.expect(@sizeOf(ninep.Client) <= 512); } diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 4466ad26..86cc8733 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,41 @@ ## Unreleased +- A mount keeps its connection, and sends each walk, open and first + read or write together: an operation through it is one round trip + (two for a directory or a file of no stated length) where it was six + or seven, and a file of known length is read ahead. A large remote + file opens however slow the link (reads are timed by their progress; + 1.3 MB at 80 ms used to time out). docs/next-daemon.md, phase 6. +- The daemon also listens on `pardes-replica.sock`, a stream socket `ssh + -L` can forward, for the replicated clients to come (docs/next-daemon.md, + phase 5): a hello names a session and the client's build, another + build is refused in words, and an accepted connection is handed to its + session, which so far only welcomes it and answers pings. + `pardes --version` prints that build's semantics hash on a second line. +- One `pardes --daemon` per user keeps sessions, started by the first + session and gone once it keeps nothing. `pardes` in a terminal forks + its session off and stays as its stub: `Detach` (`SPC s D`), or a + terminal that closes, puts the terminal back and leaves the session + running in place (no fork of the core, no dump), and the stub ends. + `pardes --attach[=name]` in any terminal takes it back there. A + window's Detach closes the window and keeps the process; + `pardes-gui --attach[=name]` opens it again. A terminal session takes a + terminal and a window session a window, and the other is refused in + words. The daemon holds a dup of every shell's pty: a session killed + outright leaves its shells running, and `Adopt` in another session + takes them in as terminal panes (what they showed is gone; a resize + makes them redraw). A hangup never ends a session the daemon keeps (it + is the terminal's end); TERM and INT do, its shells with it. + `PARDES_DAEMON=off` runs a session as before. +- The frame wire goes: `src/detached/server.zig`, `client.zig` and the + cell frames of `wire.zig`, the frontends that drew a session's screen + from them, the smallest-common-grid rule, and the `Attach` word that + switched a session's own terminal to another. A `--detach` session is + now a terminal (or window) session with none attached yet. Attaching + from another machine is gone until the replicated clients + (docs/next-daemon.md, phase 5); `wire.zig` keeps the framing, the hello + and the input events for them. - One PNG and one JPEG decoder in a binary: images decode through wuffs, the decoders ghostty uses (ghostty-vt already links them), and stb_image keeps only the other formats. stb's own inflate and image writer go, as 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 6df49c98..7acf8dd6 100644 --- a/src/File.zig +++ b/src/File.zig @@ -13,7 +13,6 @@ const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const diff = @import("diff.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const locations = @import("locations.zig"); const Colors = @import("colors.zig"); @@ -31,6 +30,7 @@ const long_paint: usize = 16 << 10; /// Content and primary selection at one file edit boundary. Keeping only the /// primary avoids putting Text.max_selections ranges in every history entry. pub const Snapshot = struct { + pub const replica = .{ .blob = .{"content"} }; content: []u8, cur_row: i32, cur_col: i32, @@ -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,14 @@ 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" }, + // Replaced whole at every edit (setContentSpan), never written. + .blob = .{ "content", "line_starts", "location_rows" }, + }; + path: []u8, content: []u8, revision: u32 = 0, @@ -216,231 +225,6 @@ pub const State = struct { pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; -pub fn dumpPane( - p: *Pardes, - arena: std.mem.Allocator, - pane: *const Pane, - file: *const State, - tag: []const u8, - body: []const u8, - scroll: usize, - origin: []const u8, - origin_arg: []const u8, -) !dump.Pane { - const dirty = file.revision != file.saved_revision; - const on_disk = onDisk(file.path); - // A clean file's text is its file's: read from disk when restored, so - // a change made there since is what comes back. Any other is kept, once. - const from_disk = file.output == null and !dirty and on_disk; - // What an unsaved pane's text was read against: the disk as it is, or, - // when a change there was seen already, something it is not. - const disk_hash: ?u64 = if (file.output != null or !dirty or !on_disk) null else if (file.disk_newer) |newer| newer +% 1 else blk: { - const bytes = filesystem.read(p, file.path) catch break :blk null; - defer p.gpa.free(bytes); - break :blk std.hash.Wyhash.hash(0, bytes); - }; - return .{ - .kind = .file, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .file = .{ - .tree_context = file.tree_context, - // A query pane's rows are its own (line 1 the query, gutter - // numbers), not location rows Restore reads: it comes back as - // plain text. - .location_rows = if (pane.query != null) &.{} else try dumpLocationRows(arena, file.location_rows), - .path = file.path, - .content_b64 = if (from_disk) "" else try dump.encodeBytes(arena, file.content), - .dirty = dirty, - .disk_hash = disk_hash, - .on_disk = on_disk, - .origin = origin, - .origin_arg = origin_arg, - .mini_source = if (file.mini) |mini| mini.source else "", - .mini_colors_b64 = if (file.mini) |mini| try dump.encodeBytes(arena, mini.colors) else "", - }, - }; -} - -fn dumpLocationRows(arena: std.mem.Allocator, rows: []const locations.Row) ![]const dump.LocationRow { - const saved = try arena.alloc(dump.LocationRow, rows.len); - for (rows, saved) |row, *out| out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = row.path, - .line = row.at.line, - .col = row.at.col, - .end_line = row.at.end_line, - .end_col = row.at.end_col, - .location_end = row.location_end, - .code_start = row.code_start, - .colors_b64 = try dump.encodeBytes(arena, row.colors), - }; - return saved; -} - -fn sameSavedLocation(a: dump.LocationRow, b: dump.LocationRow) bool { - return std.mem.eql(u8, a.path, b.path) and a.line == b.line and a.col == b.col and - a.end_line == b.end_line and a.end_col == b.end_col; -} - -fn restoreLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow) ![]locations.Row { - return restoreOutputLocationRows(gpa, content, saved, false); -} - -fn restoreOutputLocationRows(gpa: std.mem.Allocator, content: []const u8, saved: []const dump.LocationRow, callstack: bool) ![]locations.Row { - if (saved.len == 0) return &.{}; - if (saved.len > std.mem.count(u8, content, "\n") + 1) return error.InvalidLocationRows; - const rows = try gpa.alloc(locations.Row, saved.len); - var initialized: usize = 0; - errdefer { - for (rows[0..initialized]) |row| { - gpa.free(row.path); - gpa.free(row.colors); - } - gpa.free(rows); - } - var lines = std.mem.splitScalar(u8, content, '\n'); - var previous_line: []const u8 = ""; - for (saved, rows, 0..) |row, *out, index| { - const line = lines.next() orelse return error.InvalidLocationRows; - if ((!callstack and row.path.len == 0) or row.path.len >= 4096 or - row.location_end > row.code_start or row.code_start > line.len or - (row.kind == .match and row.location_end == 0) or - (row.declaration and row.kind != .context) or - row.line > std.math.maxInt(i32) or row.col > std.math.maxInt(i32) or - row.end_line > std.math.maxInt(i32) or row.end_col > std.math.maxInt(i32)) - return error.InvalidLocationRows; - if (callstack) { - if (row.location_end > 0) { - const prefix = locations.treePrefix(line); - const parsed = locations.parseRaw(line[prefix.end..]); - if (row.kind != .match or parsed.at.line == 0 or - !std.mem.eql(u8, parsed.path, row.path) or - parsed.at.line != row.line or parsed.at.col != row.col or - parsed.at.end_line != row.end_line or parsed.at.end_col != row.end_col or - prefix.end + parsed.end != row.location_end or - prefix.end + parsed.code_start != row.code_start) return error.InvalidLocationRows; - } else if (row.kind == .preview) { - if (index == 0 or saved[index - 1].kind != .match or !sameSavedLocation(saved[index - 1], row)) return error.InvalidLocationRows; - } else if (row.kind != .context or (row.path.len == 0 and (row.line != 0 or row.code_start != line.len))) return error.InvalidLocationRows; - } else { - const inline_row = row.code_start > row.location_end and row.code_start > 0 and line[row.code_start - 1] == '\t'; - const address_row = !inline_row and row.location_end > 0 and row.code_start == line.len; - const source_row = row.location_end == 0 and row.code_start == 0; - if (!inline_row and !address_row and !source_row) return error.InvalidLocationRows; - if (row.kind == .preview) { - if (!source_row or index == 0) return error.InvalidLocationRows; - const previous = saved[index - 1]; - if (previous.kind != .match or previous.location_end == 0 or previous.code_start != previous_line.len or previous_line[previous_line.len - 1] == '\t' or - !sameSavedLocation(previous, row)) return error.InvalidLocationRows; - } - if (address_row) { - if (index + 1 >= saved.len) return error.InvalidLocationRows; - const following = saved[index + 1]; - const expected: @TypeOf(row.kind) = if (row.kind == .match) .preview else .context; - if (following.kind != expected or following.code_start != 0 or following.location_end != 0 or - following.declaration != row.declaration or !sameSavedLocation(row, following)) return error.InvalidLocationRows; - } - } - const code_len = line.len - row.code_start; - if (row.colors_b64.len > std.base64.standard.Encoder.calcSize(code_len)) return error.InvalidLocationRows; - const colors = try dump.decodeBytes(gpa, row.colors_b64); - errdefer gpa.free(colors); - if (colors.len != 0 and colors.len != code_len) return error.InvalidLocationRows; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidLocationRows; - const path = try gpa.dupe(u8, row.path); - out.* = .{ - .kind = @fromBackingInt(@intCast(@backingInt(row.kind))), - .declaration = row.declaration, - .mark_occurrence = row.mark_occurrence, - .source_column = row.source_column, - .path = path, - .at = .{ .line = row.line, .col = row.col, .end_line = row.end_line, .end_col = row.end_col }, - .location_end = row.location_end, - .code_start = row.code_start, - .colors = colors, - }; - initialized += 1; - previous_line = line; - } - if (lines.next()) |remaining| if (remaining.len != 0 or lines.next() != null) return error.InvalidLocationRows; - return rows; -} - -test "a clean pane whose file was never on disk comes back clean from a Restore; one whose file has gone is said deleted" { - if (comptime !pardes.hosted) return; - for ([_]bool{ false, true }) |was_on_disk| { - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const id = p.freeSlot().?; - const pane = try restore(p, id, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = "/tmp/pardes-never-written/init", .content = "", .on_disk = was_on_disk }, - }); - try std.testing.expectEqual(was_on_disk, pane.file.?.disk_gone); - try std.testing.expectEqual(pane.file.?.saved_revision, pane.file.?.revision); - } -} - -test "location metadata restore rejects offsets and invalid syntax bytes" { - const gpa = std.testing.allocator; - var row: dump.LocationRow = .{ - .kind = .context, - .path = "x.zig", - .line = 1, - .location_end = 0, - .code_start = 3, - }; - const rows = try restoreLocationRows(gpa, "| \tx\n", &.{row}); - defer locations.freeRows(gpa, rows); - try std.testing.expectEqualStrings("x.zig", rows[0].path); - row.code_start = 8; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.code_start = 3; - row.colors_b64 = "/w=="; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); - row.colors_b64 = ""; - row.kind = .match; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "| \tx\n", &.{row})); -} - -test "stacked location metadata requires matching adjacent preview ownership" { - const gpa = std.testing.allocator; - const header = "x.c:2:1-3"; - const content = header ++ "\nfoo\n"; - const match: dump.LocationRow = .{ .kind = .match, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = header.len, .code_start = header.len }; - const preview: dump.LocationRow = .{ .kind = .preview, .path = "x.c", .line = 2, .col = 1, .end_line = 2, .end_col = 3, .location_end = 0, .code_start = 0 }; - const restored = try restoreLocationRows(gpa, content, &.{ match, preview }); - defer locations.freeRows(gpa, restored); - try std.testing.expectEqual(.preview, restored[1].kind); - try std.testing.expectEqual(restored[0].at, restored[1].at); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, "foo\n", &.{preview})); - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\n", &.{match})); - var inline_match = match; - inline_match.code_start += 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, header ++ "\t\nfoo\n", &.{ inline_match, preview })); - var wrong = preview; - wrong.path = "other.c"; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.line = 3; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); - wrong = preview; - wrong.code_start = 1; - try std.testing.expectError(error.InvalidLocationRows, restoreLocationRows(gpa, content, &.{ match, wrong })); -} - pub fn graphemeDisplayWidth(grapheme: []const u8) usize { if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now; if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1; @@ -509,10 +293,13 @@ pub fn rawDisplayCol(line_text: []const u8, raw_col: usize) usize { return displayWidth(line_text[0..bounded]) +| (raw_col -| line_text.len); } +/// Walked only as far as the column: a row's last byte, a million bytes +/// into a line, is found without measuring the rest of it. Only a line +/// that ends before the column is measured whole (it is short). pub fn rawAtDisplay(line_text: []const u8, display_col: usize) usize { - const width = displayWidth(line_text); - if (display_col > width) return line_text.len +| (display_col - width); - return byteAtDisplay(line_text, display_col); + const at = byteAtDisplay(line_text, display_col); + if (at < line_text.len) return at; + return line_text.len +| (display_col -| displayWidth(line_text)); } pub fn byteAtDisplayFrom(line_text: []const u8, from_raw: usize, display_col: usize) usize { @@ -1018,128 +805,6 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { return pane; } -pub fn restore(p: *Pardes, id: usize, src: dump.Pane) !*Pane { - const saved = src.file.?; - if (saved.mini_source.len > 0) { - const encoded_limit = std.base64.standard.Encoder.calcSize(Mini.max_output_bytes); - if (saved.mini_source.len >= 4096 or saved.content.len > Mini.max_output_bytes or - saved.content_b64.len > encoded_limit or saved.mini_colors_b64.len > encoded_limit) - return error.InvalidMini; - } - // A clean file's text is read from its file: one changed on disk since - // the dump comes back changed, never as the old text marked clean (and - // a dump from before, which kept it, is read so too). Gone from disk, the - // dump's text if it kept one. - const from_disk: ?[]u8 = if (Output.fromWord(saved.origin) == null and !saved.dirty and saved.on_disk and - saved.mini_source.len == 0 and filesystem.localPath(saved.path) != null) - filesystem.read(p, saved.path) catch null - else - null; - const content: []u8 = if (from_disk) |bytes| bytes else if (saved.content_b64.len > 0) - try dump.decodeBytes(p.gpa, saved.content_b64) - else - try p.gpa.dupe(u8, saved.content); - errdefer p.gpa.free(content); - const path = try p.gpa.dupe(u8, saved.path); - errdefer p.gpa.free(path); - - const output: ?Output.State = if (Output.fromWord(saved.origin)) |origin| blk: { - var value: Output.State = .{ .from = origin }; - try Output.setArg(&value, saved.origin_arg); - break :blk value; - } else null; - - if (saved.location_rows.len != 0 and output == null) return error.InvalidLocationRows; - const is_callstack = if (output) |state| std.meta.eql(state.from, Output.Origin{ .query = .callstack }) else false; - const location_rows = try restoreOutputLocationRows(p.gpa, content, saved.location_rows, is_callstack); - errdefer locations.freeRows(p.gpa, location_rows); - - var mini: ?Mini.State = null; - errdefer if (mini) |*state| state.deinit(p.gpa); - if (saved.mini_source.len > 0) { - if (output == null or !std.meta.eql(output.?.from, Output.Origin{ .cmd = .Mini })) return error.InvalidMini; - const source = try p.gpa.dupe(u8, saved.mini_source); - errdefer p.gpa.free(source); - const colors = try dump.decodeBytes(p.gpa, saved.mini_colors_b64); - errdefer p.gpa.free(colors); - if (colors.len != content.len or colors.len > Mini.max_output_bytes) return error.InvalidMini; - for (colors) |color| if (color > @backingInt(syntax.Syn.comment)) return error.InvalidMini; - mini = .{ .source = source, .colors = colors }; - } else if (saved.mini_colors_b64.len > 0) return error.InvalidMini; - - const history = try History.create(p.gpa); - errdefer p.gpa.destroy(history); - const pane = try p.newDocPane(id); - pane.file = .{ - .path = path, - .content = content, - .output = output, - .mini = mini, - .location_rows = location_rows, - .scroll = src.scroll, - .revision = @intFromBool(saved.dirty), - // Restored with unsaved text, it has text to lose as an edited pane - // has: a hash its text does not match, so Exit, Restore, Del and - // get ask first (builtins.warnModifiedIn reads only edited panes). - .saved_hash = if (saved.dirty) std.hash.Wyhash.hash(0, content) +% 1 else null, - .history = history, - }; - pane.file.?.tree_context = saved.tree_context and supportsContext(pane); - pane.body.cur_pinned = true; - pane.body.cur_row = @intCast(src.scroll); - // Its dot as it was, where the view the dump kept shows it. - if (saved.dot) |dot| if (dot[0] <= dot[1] and dot[1] <= content.len) { - const a = modal.positionAt(content, dot[0]); - const h = modal.positionAt(content, if (dot[1] > dot[0]) modal.prevGrapheme(content, dot[1]) else dot[0]); - pane.body.vsel = .{ .active = dot[1] > dot[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; - pane.cols = @max(1, src.cols); - pane.rows = @max(1, src.rows); - // A directory pane is its directory as it is now, laid out at the next - // sync; one that cannot be read keeps the text the dump kept. - if (output) |o| if (o.from == .dir) { - const f = &pane.file.?; - if (panes.Dir.read(p.gpa, panes.Dir.ofName(path))) |names| { - f.listing = .{ .names = names, .revision = f.revision }; - } else |_| f.listing = .{ .names = &.{}, .revision = f.revision -% 1 }; - }; - if (output == null and filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true, .mode = .baseline_disk } }); - // Its file gone from disk since the dump: its text is nowhere else now, - // so it comes back as one deleted on disk, dirty, and Del, Exit and - // Restore ask before throwing it away. - // One never on disk (Config's new file, a name not yet saved) is - // restored as it was: clean, and nothing deleted. - if (output == null and saved.on_disk and !onDisk(path)) deleted(p, @intCast(id)); - // Unsaved, and its file changed on disk since the text was read: said, - // and its first Save asks before overwriting the newer file. - if (saved.disk_hash) |read_against| if (output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - if (std.hash.Wyhash.hash(0, bytes) != read_against) changed(p, @intCast(id), bytes); - } else |_| {} - }; - // An older dump says nothing of what the text was read against: its - // first Save asks, as for a file changed on disk, rather than guess. - if (saved.dirty and saved.disk_hash == null and output == null and onDisk(path)) { - if (filesystem.read(p, path)) |bytes| { - defer p.gpa.free(bytes); - const f = &pane.file.?; - f.disk_newer = std.hash.Wyhash.hash(0, bytes); - f.disk_newer_warned = false; - } else |_| {} - } - return pane; -} - -/// Whether `path` names a file on this host's disk now. -fn onDisk(path: []const u8) bool { - if (comptime !pardes.hosted) return false; - const local = filesystem.localPath(path) orelse return false; - return @import("exec.zig").kindOf(local) != null; -} - pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.output == null) for (p.panes, 0..) |slot, id| { if (slot == pane) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); @@ -1151,10 +816,12 @@ pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (p.panes[id].?.file.?.line_starts.ptr == file.line_starts.ptr) break true; } else false; p.gpa.free(file.path); - if (ntwins == 0) p.gpa.free(file.content); + // Raw, as every text is freed (a replica's rollback copy pins them, + // src/rollback.zig, and a safe build's free writes over what it frees). + if (ntwins == 0) pardes.memory.freeUnfilled(p.gpa, file.content); if (file.listing) |listing| p.gpa.free(listing.names); if (file.mini) |*mini| mini.deinit(p.gpa); - if (file.line_starts.len > 0 and !index_shared) p.gpa.free(file.line_starts); + if (file.line_starts.len > 0 and !index_shared) pardes.memory.freeRaw(p.gpa, file.line_starts); if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); p.gpa.free(file.pager_spans); locations.freeRows(p.gpa, file.location_rows); @@ -1178,6 +845,7 @@ fn reportEdit(p: *Pardes, f: *State, new: []const u8) void { } pub const DroppedMarks = struct { + pub const replica = .{ .blob = .{"rows"} }; len: usize, hash: u64, rows: []locations.Row, @@ -1200,7 +868,12 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { // the caller did not say, is what the line index wants anyway. var twins: [pardes.MAX_PANES]usize = undefined; const ntwins = twinsOf(p, f, &twins); - const span: ?Span = span_in orelse if (ntwins > 0) spanOf(f.content, new) else null; + // The views not live keep their place in it as twins do (views.zig). + const viewed = pardes.views.several(p); + const span: ?Span = span_in orelse if (ntwins > 0 or viewed) spanOf(f.content, new) else null; + var parked: ParkedPlaces = .{}; + if (viewed) parked.before(p, f, span.?, new.len); + defer if (viewed) parked.after(p); var moved: [pardes.MAX_PANES]struct { cursor: usize, scroll: usize } = undefined; for (twins[0..ntwins], 0..) |id, i| { const t = p.panes[id].?; @@ -1241,16 +914,17 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { f.saved_len = f.content.len; } // Raw: `free` overwrites a safe build's freed memory, a whole file's - // worth per keystroke. - if (f.content.len > 0) p.gpa.rawFree(f.content, .of(u8), @returnAddress()); + // worth per keystroke; and its digest word goes (src/walk.zig). + pardes.memory.freeUnfilled(p.gpa, f.content); f.content = new; f.revision +%= 1; f.splice = .{ .revision = f.revision, .head = known.head, .tail = known.tail, .old_len = old_len }; 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(); - if (f.line_starts.len > 0) p.gpa.free(f.line_starts); + // 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) pardes.memory.freeRaw(p.gpa, f.line_starts); f.line_starts = starts; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); f.highlights = &.{}; @@ -1262,7 +936,7 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { const tf = &t.file.?; // Its own index, built while the shared one was empty, goes; the // shared one, freed above, is replaced. - if (tf.line_starts.len > 0 and tf.line_starts.ptr != old_starts) p.gpa.free(tf.line_starts); + if (tf.line_starts.len > 0 and tf.line_starts.ptr != old_starts) pardes.memory.freeRaw(p.gpa, tf.line_starts); tf.content = f.content; tf.line_starts = f.line_starts; tf.revision = f.revision; @@ -1292,6 +966,50 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { } } +/// Each other view's cursor and scroll on a text being changed, as offsets +/// moved by the splice (`spliced`), put back as places in the new text once +/// it is in: a view not live keeps its place, as a twin does. Its +/// selections go, as a twin's do. +const ParkedPlaces = struct { + const max = pardes.MAX_PANES * pardes.views.max; + at: [max]struct { pane: *Pane, view: u8, cursor: usize, scroll: usize } = undefined, + n: usize = 0, + + fn before(pp: *ParkedPlaces, p: *Pardes, f: *State, span: Span, new_len: usize) void { + for (p.panes) |slot| { + const pane = slot orelse continue; + const tf = if (pane.file) |*file| file else continue; + if (tf != f and (f.twin == 0 or tf.twin != f.twin)) continue; + for (0..pardes.views.max) |k| { + if (k == p.view or !pardes.views.isOpen(p, @intCast(k)) or pp.n == max) continue; + const v = &pane.pv[k]; + const at = lineStart(p.gpa, tf, @intCast(@max(0, v.body.cur_row))) + @as(usize, @intCast(@max(0, v.body.cur_col))); + pp.at[pp.n] = .{ + .pane = pane, + .view = @intCast(k), + .cursor = spliced(@min(at, f.content.len), f.content.len, new_len, span), + .scroll = spliced(@min(lineStart(p.gpa, tf, v.scroll), f.content.len), f.content.len, new_len, span), + }; + pp.n += 1; + } + } + } + + fn after(pp: *ParkedPlaces, p: *Pardes) void { + for (pp.at[0..pp.n]) |a| { + const tf = &(a.pane.file orelse continue); + const v = &a.pane.pv[a.view]; + const cursor = positionIn(p.gpa, tf, @min(a.cursor, tf.content.len)); + v.body.cur_row = @intCast(cursor.row); + v.body.cur_col = @intCast(cursor.col); + v.body.vsel.active = false; + v.body.msel.active = false; + v.body.nsel = 0; + v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row; + } + } +}; + /// Where an offset into the old text is in the new, after the splice that /// kept `span` at either end: past it, moved by what it added; inside it, /// at its start, as acme moves a clone's q0 (textinsert, textdelete). @@ -1425,45 +1143,6 @@ pub fn leaveTwins(p: *Pardes, f: *State) void { if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; } -/// Restore's: pane `id`, restored on its own, back on pane `of`'s buffer -/// as the Zerox twin it was dumped as (dump.File.twin_of). Not a file pane -/// there (a dump edited by hand), it stays on its own. -pub fn relink(p: *Pardes, of: usize, id: usize, dot: ?[2]u32) void { - const src = p.panes[of] orelse return; - const pane = p.panes[id] orelse return; - const sf = if (src.file) |*file| file else return; - const f = if (pane.file) |*file| file else return; - if (sf.output != null or f.output != null or sf.mini != null) return; - if (sf.twin == 0) { - const group = p.gpa.dupe(u8, sf.path) catch return; - p.twin_groups.append(p.gpa, .{ .id = p.next_twin, .saved_revision = sf.saved_revision, .saved_hash = sf.saved_hash, .saved_len = sf.saved_len, .disk_newer = sf.disk_newer, .disk_gone = sf.disk_gone, .path = group }) catch return p.gpa.free(group); - sf.twin = p.next_twin; - p.next_twin +%= 1; - } - p.gpa.free(f.content); - if (f.line_starts.len > 0) p.gpa.free(f.line_starts); - f.history.destroy(p.gpa); - f.content = sf.content; - f.line_starts = sf.line_starts; - f.history = sf.history; - f.revision = sf.revision; - f.saved_revision = sf.saved_revision; - f.saved_hash = sf.saved_hash; - f.saved_len = sf.saved_len; - f.twin = sf.twin; - if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); - f.highlights = &.{}; - f.syntax_dirty = true; - // Its dot as dumped, read now that its text is back. - if (dot) |d| if (d[0] <= d[1] and d[1] <= f.content.len) { - const a = modal.positionAt(f.content, d[0]); - const h = modal.positionAt(f.content, if (d[1] > d[0]) modal.prevGrapheme(f.content, d[1]) else d[0]); - pane.body.vsel = .{ .active = d[1] > d[0], .row = @intCast(a.row), .col = @intCast(a.col), .explicit = true }; - pane.body.cur_row = @intCast(h.row); - pane.body.cur_col = @intCast(h.col); - }; -} - pub const TwinGroup = struct { id: u32, saved_revision: u32, @@ -1644,6 +1323,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void { /// `<file> deleted on disk (Save writes it again)`, logged `changed /// <serial> 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; @@ -1700,6 +1381,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(); @@ -1707,12 +1399,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; @@ -1904,18 +1590,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; + return fillBody(null, pane, f, width, true); +} - 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; +/// 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/FrameStats.zig b/src/FrameStats.zig index cabb003b..c27e74af 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -173,7 +173,7 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void if (stats.armed and @as(u64, gap) * std.time.ns_per_us * 2 > stats.period_ns * 3) stats.jank += 1; if (@as(u64, frame.update_us + frame.render_us + frame.present_us) * std.time.ns_per_us > stats.period_ns) stats.over_budget += 1; if (skipped) stats.skipped += 1; - stats.ring[stats.count % capacity] = frame; + stats.ring[@intCast(stats.count % capacity)] = frame; stats.count += 1; stats.last_end_ns = end_ns; stats.armed = moving; @@ -269,6 +269,9 @@ pub fn dump(stats: *const FrameStats, path: [:0]const u8) void { } write.line(fd, &buf, "# start_ms\tupdate_us\trender_us\tpresent_us\tgap_us\tmoving\tskipped\tscroll\n", .{}); const t0 = if (all[0].len != 0) all[0][0].start_ns else 0; + // The monotonic clock is the machine's: a driver lines its own times + // up with the frames' by this. + write.line(fd, &buf, "# t0_ns {d}\n", .{t0}); // The timedemo's phases, on the frames' clock. if (stats.demo_ns != 0) if (named(stats.demo_name)) |steps| for (steps) |step| switch (step.act) { .phase => |name| write.line(fd, &buf, "# phase {s} {d:.1}\n", .{ name, @as(f64, @floatFromInt((stats.demo_ns + @as(u64, step.at_ms) * std.time.ns_per_ms) -| t0)) / std.time.ns_per_ms }), @@ -328,6 +331,7 @@ pub fn named(name: []const u8) ?[]const Step { if (std.mem.eql(u8, name, "pdf-scroll")) return &pdf_scroll; if (std.mem.eql(u8, name, "pdf-sdm")) return &pdf_sdm; if (std.mem.eql(u8, name, "one")) return &one; + if (std.mem.eql(u8, name, "keys")) return &keystrokes; if (std.mem.eql(u8, name, "jumps")) return &jumps; if (std.mem.eql(u8, name, "longlines")) return &longlines; if (std.mem.eql(u8, name, "clip-flick")) return &clip_flick; @@ -592,6 +596,56 @@ pub const pdf_sdm = blk: { break :blk out; }; +/// Keystroke-to-present, a phase a gesture, on the file the session opened: +/// test/perf.zig's --live sends these same steps to the tty through a pty +/// and --live-gui runs them here, so the two frontends are timed alike. +pub const keystrokes = blk: { + @setEvalBranchQuota(50_000); + var steps: [1024]Step = undefined; + var n: usize = 0; + var t: u32 = 1500; + const at = struct { + fn add(s: []Step, k: *usize, ms: u32, act: @FieldType(Step, "act")) void { + s[k.*] = .{ .at_ms = ms, .act = act }; + k.* += 1; + } + }; + at.add(&steps, &n, t, .{ .phase = "key-down" }); + for (0..150) |_| { + at.add(&steps, &n, t, .{ .key = .{ .cp = 'j', .text = "j" } }); + t += 30; + } + at.add(&steps, &n, t, .{ .phase = "key-right" }); + for (0..150) |i| { + at.add(&steps, &n, t, .{ .key = if (i % 2 == 0) .{ .cp = 'l', .text = "l" } else .{ .cp = 'h', .text = "h" } }); + t += 30; + } + at.add(&steps, &n, t, .{ .phase = "edit-char" }); + at.add(&steps, &n, t, .{ .key = .{ .cp = 'i', .text = "i" } }); + t += 30; + for (0..150) |_| { + at.add(&steps, &n, t, .{ .key = .{ .cp = 'x', .text = "x" } }); + t += 30; + } + at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.escape } }); + t += 300; + at.add(&steps, &n, t, .{ .phase = "page-down" }); + for (0..60) |_| { + at.add(&steps, &n, t, .{ .key = .{ .cp = pardes.Key.page_down } }); + t += 50; + } + at.add(&steps, &n, t, .{ .phase = "wheel" }); + for (0..150) |_| { + at.add(&steps, &n, t, .{ .wheel = 1 }); + t += 30; + } + t += 500; + at.add(&steps, &n, t, .{ .phase = "done" }); + at.add(&steps, &n, t, .end); + const out = steps[0..n].*; + break :blk out; +}; + /// `scrollSet` alone, on the file the session opened: one kind, for a /// profiler's run. pub const one = blk: { diff --git a/src/Handover.zig b/src/Handover.zig index 6124e49f..549e4d58 100644 --- a/src/Handover.zig +++ b/src/Handover.zig @@ -1,7 +1,8 @@ //! Panes between windows, as a browser's tabs go between its windows. A //! pane's grip, or a column's, released past the window's edge carries it -//! out: dumped alone (dump.dumpBytes, `carried`), it opens in a new window -//! of its own, `pardes-gui -l` of that dump. The pane closes here only once +//! out: dumped alone (`Carried`, each pane as dump.savePane makes it), it +//! opens in a new window of its own, `pardes-gui -l` of that dump. The pane +//! closes here only once //! the new window answers on its 9P socket, so its text is never in no //! window at all; a failed handover leaves it where it was, and a pane //! edited while it was on its way stays here too (both then have it). @@ -25,15 +26,33 @@ const dump = @import("dump.zig"); const ninep_io = @import("9p_io.zig"); const panes = @import("panes.zig"); const tagline = @import("tagline.zig"); +const walk = @import("walk.zig"); const Pardes = pardes.Pardes; const MAX_PANES = pardes.MAX_PANES; const libc = std.c; +/// What a grip carries out of its window: one pane with the panes attached +/// to it, or a whole column. +pub const Kind = enum { pane, column }; + +/// A handover's dump: `carried_magic`, then this. +const Carried = struct { + kind: Kind, + /// A carried column's tag. + tag: ?[]const u8 = null, + /// Each pane as dump.savePane makes it, top to bottom. + panes: []const []const u8, + weights: []const f32, + /// Which of them each one is attached to (layout.zig, "Attached panes"). + owners: []const ?u32, +}; +pub const carried_magic = "pardes-carried\n"; + /// The handover in flight, one at a time: its number, what it carries, /// and each carried pane's serial and revision as it was dumped. seq: u32 = 0, flying: bool = false, -carried: dump.Carried = .pane, +carried: Kind = .pane, column_serial: u32 = 0, count: usize = 0, serials: [MAX_PANES]u32 = @splat(0), @@ -44,6 +63,10 @@ bytes: ?[]u8 = null, /// its answer comes in (`finish`), cancelled at the session's end. task: ?struct { future: std.Io.Future(void), io: std.Io } = null, +/// The dump in flight and its carrier are the host's errand, not the +/// session's (src/walk.zig). +pub const replica = .{ .authority = .{ "bytes", "task" } }; + pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { // The session ends with the carrier still out: it stops at its next // wait (a 9P call it is in finishes first, within its own budget). @@ -62,8 +85,7 @@ pub fn deinit(h: *Handover, gpa: std.mem.Allocator) void { /// an attachment), or column `at`, released past the window's edge: dumped, /// and the host asked to carry it. Only text panes travel: a terminal's /// shell is this process's child and would die with the pane here. -pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { - var keep: [MAX_PANES]bool = @splat(false); +pub fn begin(h: *Handover, p: *Pardes, carried: Kind, at: usize) void { var ids: [MAX_PANES]usize = undefined; var n: usize = 0; switch (carried) { @@ -92,8 +114,7 @@ pub fn begin(h: *Handover, p: *Pardes, carried: dump.Carried, at: usize) void { else "only text panes go to another window, for now"); } - for (ids[0..n]) |id| keep[id] = true; - const bytes = dump.dumpBytes(p, &keep, carried) catch |err| return p.reportError(said, "handover", err); + const bytes = dumpCarried(p, carried, at, ids[0..n]) catch |err| return p.reportError(said, "handover", err); h.deinit(p.gpa); h.bytes = bytes; h.seq +%= 1; @@ -158,6 +179,31 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co std.fmt.bufPrint(&buf, "copied to {s}; edited on the way, kept here too", .{to}) catch "copied"); } +/// The dump of panes `ids` (gpa's), carried as `kind` from column `at`'s +/// (a pane's, `at` is its slot). +fn dumpCarried(p: *Pardes, kind: Kind, at: usize, ids: []const usize) ![]u8 { + const arena = p.scratch.allocator(); + const bytes = try arena.alloc([]const u8, ids.len); + const weights = try arena.alloc(f32, ids.len); + const owners = try arena.alloc(?u32, ids.len); + for (ids, 0..) |id, i| { + const pane = p.panes[id].?; + bytes[i] = try dump.savePane(arena, pane, false); + weights[i] = pane.vweight; + const owner = layout.ownerOf(p, id); + owners[i] = if (owner) |o| if (std.mem.indexOfScalar(usize, ids, o)) |k| @intCast(k) else null else null; + } + const value: Carried = .{ + .kind = kind, + .tag = if (kind == .column) p.col_tags[at].own else null, + .panes = bytes, + .weights = weights, + .owners = owners, + }; + const body = try walk.save(Carried, arena, &value); + return std.mem.concat(p.gpa, u8, &.{ carried_magic, body }); +} + // ---- the receiving window ---- /// A carried dump (`begin`'s) taken into this window where the pointer is: @@ -166,13 +212,12 @@ pub fn finish(h: *Handover, p: *Pardes, seq: u32, to: []const u8, failure: ?[]co /// alone. Every other pane stays where it was. Answers the slot of the /// first pane taken; on an error nothing of it is left here. pub fn adopt(p: *Pardes, bytes: []const u8) !usize { - var parsed = try dump.readZon(p.gpa, bytes, "adopt"); - defer parsed.deinit(); - const st = parsed.value; - const carried = st.carried orelse return error.NotCarried; - if (st.columns.len != 1 or st.columns[0].panes.len == 0) return error.NotCarried; - const order = st.columns[0].panes; - for (order) |i| if (i >= st.panes.len or st.panes[i].kind != .file) return error.NotText; + if (!std.mem.startsWith(u8, bytes, carried_magic)) return error.NotCarried; + var arena: std.heap.ArenaAllocator = .init(p.gpa); + defer arena.deinit(); + const st = try walk.load(Carried, arena.allocator(), bytes[carried_magic.len..], null); + const carried = st.kind; + if (st.panes.len == 0 or st.weights.len != st.panes.len or st.owners.len != st.panes.len) return error.NotCarried; // Where the pointer is, else the middle of the pane with the keyboard. layout.compute(p); const here = p.rects[p.active]; @@ -181,19 +226,17 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { var slots: [MAX_PANES]usize = undefined; var n: usize = 0; errdefer for (slots[0..n]) |slot| p.removePane(slot, null) catch {}; - for (order) |i| { + for (st.panes) |one| { const slot = p.freeSlot() orelse return error.TooManyPanes; - const src = st.panes[i]; - const pane = try panes.File.restore(p, slot, src); + const pane = try dump.loadPane(p, slot, one); slots[n] = slot; n += 1; - tagline.restoreDumpTail(p, pane, src); - pane.collapsed = false; + if (pane.file == null) return error.NotText; } // An attachment names its owner by its place in the dump. - for (order, 0..) |i, k| { - const owner = st.panes[i].attached_to orelse continue; - const at = std.mem.indexOfScalar(usize, order, owner) orelse continue; + for (st.owners, 0..) |owner, k| { + const at = owner orelse continue; + if (at >= n) continue; p.panes[slots[k]].?.attached_to = p.panes[slots[at]].?.serial; } const tc = layout.targetColumn(p, x); @@ -201,12 +244,12 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { if (carried == .column or p.ncol == 0) { const c = if (p.ncol == 0) layout.insertColumn(p, 0, false) else layout.insertColumn(p, tc, x < p.col_x[tc] + p.col_w[tc] / 2); const col = c orelse return error.NoRoom; - if (carried == .column) if (st.columns[0].tag) |tag| { + if (carried == .column) if (st.tag) |tag| { p.col_tags[col].own = try p.gpa.dupe(u8, tag); }; for (slots[0..n], 0..) |slot, k| { layout.insert(p, col, k, slot); - p.panes[slot].?.vweight = st.panes[order[k]].vweight; + p.panes[slot].?.vweight = st.weights[k]; } p.active = slots[0]; p.setActiveColumn(col); @@ -240,10 +283,10 @@ pub fn adopt(p: *Pardes, bytes: []const u8) !usize { heights[owner] = keeps; // The carried share what was given as they shared their old rows. var total: f32 = 0; - for (order) |i| total += st.panes[i].vweight; + for (st.weights) |w| total += w; var left = give; for (slots[0..n], 0..) |slot, j| { - const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.panes[order[j]].vweight / @max(total, 1)))); + const share: u16 = if (j + 1 == n) left else @intFromFloat(@max(1, @floor(@as(f32, @floatFromInt(give)) * st.weights[j] / @max(total, 1)))); heights[slot] = @min(share, left); left -|= share; } @@ -284,7 +327,7 @@ pub const Job = struct { pub fn dumpPath(gpa: std.mem.Allocator, seq: u32) ![:0]u8 { var dir_buf: [1024]u8 = undefined; const dir: []const u8 = if (libc.getenv("XDG_RUNTIME_DIR")) |x| std.mem.span(x) else dump.defaultDirectory(&dir_buf) orelse return error.NoDirectory; - return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.zon", .{ dir, libc.getpid(), seq }, 0); + return std.fmt.allocPrintSentinel(gpa, "{s}/pardes-handover-{d}-{d}.dump", .{ dir, libc.getpid(), seq }, 0); } /// Runs the handover's carrier on `io` (io.concurrent), kept in `h.task` @@ -451,12 +494,13 @@ test "a grip let go past the window's edge dumps its pane alone, and closes it o try std.testing.expect(p.drag == .none); try std.testing.expect(p.panes[0] != null); try std.testing.expectEqual(before, p.rects[other]); - var parsed = try dump.readZon(std.testing.allocator, p.handover.bytes.?, "test"); - defer parsed.deinit(); - try std.testing.expectEqual(@as(?dump.Carried, .pane), parsed.value.carried); - try std.testing.expectEqual(@as(usize, 1), parsed.value.panes.len); - try std.testing.expectEqual(@as(usize, 1), parsed.value.columns.len); - try std.testing.expectEqualStrings("/test.txt", parsed.value.panes[0].file.?.path); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const carried = try walk.load(Carried, arena.allocator(), p.handover.bytes.?[carried_magic.len..], null); + try std.testing.expectEqual(Kind.pane, carried.kind); + try std.testing.expectEqual(@as(usize, 1), carried.panes.len); + const pane = try walk.load(pardes.Pane, arena.allocator(), carried.panes[0], null); + try std.testing.expectEqualStrings("/test.txt", pane.file.?.path); // A second one waits for the first. releaseOutside(p, other); try std.testing.expect(p.panes[other] != null); @@ -543,7 +587,7 @@ test "a column let go over another window goes beside the column the pointer is from.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = from.screen_w - 1, .row = r.y } }); from.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = from.screen_w - 1, .row = r.y, .outside = true } }); try std.testing.expect(from.handover.flying); - try std.testing.expectEqual(dump.Carried.column, from.handover.carried); + try std.testing.expectEqual(Kind.column, from.handover.carried); const to = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 30 }); defer to.deinit(); _ = try to.setTestFile("x\n"); diff --git a/src/Layer.zig b/src/Layer.zig index ba45bc88..609ddbc4 100644 --- a/src/Layer.zig +++ b/src/Layer.zig @@ -28,6 +28,18 @@ pub const Metrics = struct { tagline_h: u16 = 1, }; +/// What a hit is resolved against: the layer it landed on, as the frame +/// the frontend hit-tested had it. A hit carries it, so the step resolves +/// the hit (again, or clamped back onto it) from the input, never from a +/// frame: the frame is the process's own (Pardes.replica's `surface`), and +/// a session never draws a replica's view. +pub const Place = struct { + viewport: pardes.Rect = .{}, + cols: u16 = 0, + rows: u16 = 0, + context_rows: u16 = 0, +}; + /// A point on a tag, notice or header layer: which row of it and which /// column at the tagline's pitch. pub const TagHit = struct { @@ -40,6 +52,7 @@ pub const TagHit = struct { pixel_x: f32, pixel_y: f32, metrics: Metrics, + place: Place = .{}, }; kind: Kind = .body, @@ -103,14 +116,22 @@ const Point = struct { index: u16, col: u16, metrics: Metrics }; /// The row and column a window pixel lands on, each row at its own pitch: /// a tag's at the tagline's width, a body's compact rows too. +pub fn place(layer: *const Layer) Place { + return .{ .viewport = layer.viewport, .cols = layer.cols, .rows = layer.rows, .context_rows = layer.context_rows }; +} + fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point { + return pointIn(layer.kind, layer.place(), x, y, body_w, body_h, tagline_w, tagline_h); +} + +fn pointIn(kind: Kind, layer: Place, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?Point { if (layer.rows == 0 or !std.math.isFinite(x) or !std.math.isFinite(y)) return null; // A band's hit is refused outright on metrics no host could have; a // body's has always been floored to a pixel instead, and both refuse // what is no number or past a u16 (a host's floats: web's exports). for ([_]f32{ body_w, body_h, tagline_w, tagline_h }) |metric| { if (!std.math.isFinite(metric) or metric > std.math.maxInt(u16)) return null; - if (layer.kind != .body and metric < 1) return null; + if (kind != .body and metric < 1) return null; } const bw = @max(1, body_w); const bh = @max(1, body_h); @@ -122,7 +143,7 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin const headers = @as(f32, @floatFromInt(layer.context_rows)) * th; const index: u16 = @intFromFloat(if (dy < headers) @floor(dy / th) else @as(f32, @floatFromInt(layer.context_rows)) + @floor((dy - headers) / bh)); if (index >= layer.rows) return null; - const pitch = if (layer.kind != .body or index < layer.context_rows) tw else bw; + const pitch = if (kind != .body or index < layer.context_rows) tw else bw; return .{ .index = index, .col = @intFromFloat(@min(@as(f32, @floatFromInt(layer.cols -| 1)), @floor(dx / pitch))), @@ -134,15 +155,39 @@ fn pointAt(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, taglin /// pane's text edge). pub fn bodyHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?pardes.Mouse.BodyHit { if (layer.kind != .body) return null; - const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null; - return .{ .serial = layer.serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics }; + return bodyHitIn(layer.place(), layer.serial, x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); } /// A band's pixel as the tag cell under it. pub fn tagHit(layer: *const Layer, x: f32, y: f32, body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32) ?TagHit { if (layer.kind == .body) return null; - const point = layer.pointAt(x, y, body_w, body_h, tagline_w, tagline_h) orelse return null; - return .{ .kind = layer.kind, .id = layer.id, .serial = layer.serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics }; + return tagHitIn(layer.kind, layer.id, layer.serial, layer.place(), x, y, .{ .body_w = body_w, .body_h = body_h, .tagline_w = tagline_w, .tagline_h = tagline_h }); +} + +const Pixels = struct { body_w: f32, body_h: f32, tagline_w: f32, tagline_h: f32 }; + +fn pixelsOf(m: Metrics) Pixels { + return .{ .body_w = @floatFromInt(m.body_w), .body_h = @floatFromInt(m.body_h), .tagline_w = @floatFromInt(m.tagline_w), .tagline_h = @floatFromInt(m.tagline_h) }; +} + +fn bodyHitIn(at: Place, serial: u32, x: f32, y: f32, m: Pixels) ?pardes.Mouse.BodyHit { + const point = pointIn(.body, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; + return .{ .serial = serial, .row = point.index, .col = point.col + config.GUTTER, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; +} + +fn tagHitIn(kind: Kind, id: u16, serial: u32, at: Place, x: f32, y: f32, m: Pixels) ?TagHit { + const point = pointIn(kind, at, x, y, m.body_w, m.body_h, m.tagline_w, m.tagline_h) orelse return null; + return .{ .kind = kind, .id = id, .serial = serial, .line = point.index, .col = point.col, .pixel_x = x, .pixel_y = y, .metrics = point.metrics, .place = at }; +} + +/// A body hit at its own pixel, on the place it carries. +pub fn bodyAgain(hit: pardes.Mouse.BodyHit) ?pardes.Mouse.BodyHit { + return bodyHitIn(hit.place, hit.serial, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); +} + +/// A tag hit at its own pixel, on the place it carries. +pub fn tagAgain(hit: TagHit) ?TagHit { + return tagHitIn(hit.kind, hit.id, hit.serial, hit.place, hit.pixel_x, hit.pixel_y, pixelsOf(hit.metrics)); } test "compact body layer rows fill reclaimed height and invert exact boundaries" { @@ -215,15 +260,14 @@ pub fn sameCell(a: ?TagHit, b: ?TagHit) bool { pub fn columnAt(p: *const pardes.Pardes, kind: Kind, id: usize, supplied: ?TagHit, clamp: bool, line: u16) ?u16 { var point = supplied orelse return null; if (point.kind != kind or point.id != id) return null; - if (clamp) for (p.surface.tagLayers()) |*layer| { - if (layer.rows == 0 or layer.kind != kind or layer.id != id or layer.serial != point.serial or line >= layer.rows) continue; + if (clamp and line < point.place.rows) { + const v = point.place.viewport; const bw: f32 = @floatFromInt(point.metrics.body_w); const bh: f32 = @floatFromInt(point.metrics.body_h); - const left = @as(f32, @floatFromInt(layer.viewport.x)) * bw; - const right = @as(f32, @floatFromInt(layer.viewport.x + layer.viewport.w)) * bw; + const left = @as(f32, @floatFromInt(v.x)) * bw; + const right = @as(f32, @floatFromInt(v.x + v.w)) * bw; point.pixel_x = std.math.clamp(point.pixel_x, left, @max(left, right - 0.001)); - point.pixel_y = (@as(f32, @floatFromInt(layer.viewport.y + line)) + 0.5) * bh; - break; - }; + point.pixel_y = (@as(f32, @floatFromInt(v.y + line)) + 0.5) * bh; + } return if (p.reprojectTagHit(point)) |mapped| mapped.col else null; } diff --git a/src/Messages.zig b/src/Messages.zig index cb52907a..371ed168 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,9 @@ 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(); + // The clock when it was said (the session clock is the last pump's, + // and a 9P write after an idle hour would say it an hour ago). + pane.msg_said_ms = pardes.tape.value(.clock, nowMs, .{}); p.needs_frame = true; if (arrives) { pane.msg_life = .{ .phase = if (p.settings.message_animation and n > 0) .entering else .shown }; @@ -200,6 +213,15 @@ fn showMessage(p: *Pardes, id: usize, text: []const u8, stack: bool) void { } } +/// 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 +/// clock, checked as the core is stepped (each event, and each pump after +/// its wait, which a message wakes) -- not as a 9P request is served, since +/// a read is answered while a step is out and must change nothing, nor by +/// the host, whose call is no input a replay makes. +/// ponytail: a pane's older lines go with its newest, not each on its own +/// clock; the render pipeline's clock will give every line its own. fn nowMs() i64 { if (comptime !pardes.hosted) return 0; var ts: std.c.timespec = undefined; @@ -207,17 +229,10 @@ fn nowMs() i64 { 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 -/// clock, checked as the core is stepped and by the detached session's loop -/// between steps -- not as a 9P request is served, since a read is answered -/// while a step is out and must change nothing. -/// ponytail: a pane's older lines go with its newest, not each on its own -/// 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 clock as the session that recorded read it (src/tape.zig). + const now = pardes.tape.value(.clock, nowMs, .{}); for (p.panes) |slot| { const pane = slot orelse continue; if (pane.msg_len == 0 and pane.msg_older_len == 0) continue; @@ -453,6 +468,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 +592,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 @@ -713,10 +743,11 @@ test "with no frontend attached a message goes once it has lingered, on the cloc // Detached with nobody attached: past its linger it is gone, and the // line above it with it. p.unattended = true; - // A read served meanwhile changes nothing; the session's loop expires. + // A read served meanwhile changes nothing; the session's step after its + // wait expires, not the host (whose call no replay makes). _ = p.serveFs(.{ .tag = 1, .op = .read, .node = @backingInt(ctlfs.TopFile.index), .size = 64 }); try std.testing.expect(pane.msg_len > 0); - expireUnattended(p); + try p.pump(.{}); try std.testing.expectEqual(@as(u16, 0), pane.msg_len); try std.testing.expectEqual(@as(u8, 0), pane.msg_older_len); // A fresh one stays until its linger is up. diff --git a/src/Names.zig b/src/Names.zig index 0c8ba075..63eec14f 100644 --- a/src/Names.zig +++ b/src/Names.zig @@ -33,6 +33,10 @@ query_len: u8 = 0, /// Bumped whenever `hits` change: the rows are made again only then. gen: u32 = 0, +/// The query is `query_buf[0..query_len]`: what lies past it is undefined +/// (0xAA in Debug, not in a release build), so no digest reads it. +pub const replica = .{ .len = .{.{ "query_buf", "query_len" }} }; + pub const Cand = struct { /// The row: `bytes[start..][0..len]`. start: u32, @@ -146,8 +150,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 +167,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..7c8b7f59 100644 --- a/src/Output.zig +++ b/src/Output.zig @@ -7,7 +7,6 @@ const exec = @import("exec.zig"); const config = @import("config.zig"); const Pardes = pardes.Pardes; const modal = @import("modal.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const builtins = @import("builtins.zig"); const effect_sources = @import("effect_sources.zig"); @@ -40,9 +39,11 @@ pub const Origin = union(enum) { preview, }; -pub const max_arg = dump.max_origin_arg; +/// An output's argument is typed in the bounded one-line tag storage. +pub const max_arg = limits.max_tag_tail; pub const State = struct { + pub const replica = .{ .len = .{.{ "arg_buf", "arg_len" }} }; from: Origin, arg_buf: [max_arg]u8 = undefined, arg_len: u16 = 0, @@ -319,36 +320,6 @@ pub fn grain(out: ?State) Grain { return if (tr.steps) .line else .word; } -pub fn word(o: Origin) []const u8 { - return switch (o) { - .cmd => |b| @tagName(b), - .query => |k| @tagName(k), - .search => "/", - .errors => config.errors_buffer, - .pdf_links => config.pdf_links_buffer, - .unsaved => config.unsaved_buffer, - .dir => dir_word, - .preview => config.preview_buffer, - }; -} - -/// A directory pane's origin as a dump names it. -const dir_word = "dir"; - -pub fn fromWord(w: []const u8) ?Origin { - if (w.len == 0) return null; - if (std.mem.eql(u8, w, "/")) return .search; - if (std.mem.eql(u8, w, config.errors_buffer)) return .errors; - if (std.mem.eql(u8, w, config.pdf_links_buffer)) return .pdf_links; - if (std.mem.eql(u8, w, config.unsaved_buffer)) return .unsaved; - if (std.mem.eql(u8, w, dir_word)) return .dir; - if (std.mem.eql(u8, w, config.preview_buffer)) return .preview; - if (std.meta.stringToEnum(Builtin, w)) |b| - if (builtins.registry.outputTraits(b) != null) return .{ .cmd = b }; - if (std.meta.stringToEnum(lsp.Kind, w)) |k| return .{ .query = k }; - return null; -} - pub fn resultsFrom(p: *Pardes, pane: *Pane, from: Origin) bool { const rp = p.panes[pane.search_pane orelse return false] orelse return false; const f = rp.file orelse return false; @@ -934,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"; @@ -963,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); @@ -995,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. @@ -1024,12 +999,18 @@ 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); } +/// Where a dump goes by default, as the session's environment says (the +/// authority's, `tape.authority`): in `buf`. +fn dumpDirectory(buf: []u8) []const u8 { + return pardes.dump.defaultDirectory(buf) orelse ""; +} + pub fn openConfig(p: *Pardes, id: usize) !void { var out: std.Io.Writer.Allocating = .init(p.gpa); errdefer out.deinit(); @@ -1040,15 +1021,15 @@ pub fn openConfig(p: *Pardes, id: usize) !void { try p.locations_config.write(&located.writer); try config.Runtime.writeReport(&out.writer, .{ .locations = located.written(), - .default_dump_dir = if (host_fs) (pardes.dump.defaultDirectory(&dir_buf) orelse "") else "", - .startup_config_path = p.opts.startup_config_path, - .platform = @tagName(pardes.platform), + .default_dump_dir = if (host_fs) pardes.tape.authority(dumpDirectory, .{@as([]u8, &dir_buf)}) else "", + .startup_config_path = p.startupPath(), + .platform = builtins.viewPlatform(p), .theme_name = p.theme().name, .theme_file = pardes.colors.loadedThemeFile(p), .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(); @@ -1090,7 +1071,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 @@ -1115,7 +1096,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/Pipe.zig b/src/Pipe.zig index 850f9c64..903c1dba 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -15,6 +15,10 @@ const Key = pardes.Key; const Pardes = pardes.Pardes; const Pipe = @This(); +/// A pipe's run, an Edit's and its writes, out with the host: errands of +/// the session that performs them, not a dump's (src/walk.zig). +pub const replica = .{ .authority = .{ "wait", "edit_run", "writes" } }; + /// Bumped for every request, so a late answer to an older one is ignored. seq: u32 = 0, /// The request in flight, and what it needs to apply its answer. @@ -52,6 +56,8 @@ pub fn pipeMarker(how: modal.Normal.PipeBehavior) []const u8 { } pub const PendingPipe = struct { + /// The digest's walk (src/walk.zig) of the ranges, as far as they go. + pub const replica = .{ .len = .{.{ "ranges", "nranges" }} }; id: u32, pane: usize, serial: u32, diff --git a/src/Places.zig b/src/Places.zig index cff7c22e..a60a0d65 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. @@ -178,6 +182,7 @@ pub fn firstFrom(hits: []const Hit, at: usize) usize { pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, from: u32, pattern: []const u8, literal: bool, seek: ?usize, moves: bool) void { const f = if (pane.file) |*file| file else return; const gpa = p.gpa; + set.derive(gpa); // The same question again (a key that left the pattern as it was): // the places stand, the current one too. if (set.started and set.re != null and set.literal == literal and std.mem.eql(u8, set.pattern, pattern)) { @@ -194,14 +199,12 @@ pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, fro if (std.ascii.isUpper(c)) break false; } else true; set.literal = literal; - const arena = p.scratch.allocator(); - const folded = if (set.fold) std.ascii.allocLowerString(arena, pattern) catch return else pattern; - set.re = if (literal) null else normal.SearchRegex.compile(gpa, folded) catch null; + set.derive(gpa); if (set.re == null) { + // It does not compile: the characters it is. set.literal = true; - var escaped: std.ArrayList(u8) = .empty; - normal.appendSearchLiteral(&escaped, arena, folded) catch return; - set.re = normal.SearchRegex.compile(gpa, escaped.items) catch return; + set.derive(gpa); + if (set.re == null) return; } set.started = true; set.revision = f.revision +% 1; // `fill` starts it over for the text as it is @@ -211,6 +214,25 @@ pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, fro set.fill(p, pane, 0, frame_steps); } +/// `re` made from the pattern: derived (Places.replica), so neither a dump +/// nor a rollback copy holds it, and a set loaded with a pattern and none +/// was a list's to `fill` (a client that joined found no places an edit +/// made, 2026-10-30). A list's places have no pattern. +fn derive(set: *Places, gpa: std.mem.Allocator) void { + if (set.re != null or set.pattern.len == 0) return; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const folded = if (set.fold) std.ascii.allocLowerString(a, set.pattern) catch return else set.pattern; + if (!set.literal) { + set.re = normal.SearchRegex.compile(gpa, folded) catch null; + return; + } + var escaped: std.ArrayList(u8) = .empty; + normal.appendSearchLiteral(&escaped, a, folded) catch return; + set.re = normal.SearchRegex.compile(gpa, escaped.items) catch null; +} + /// A list's places in this text, as it gives them (sorted here), in place /// of whatever fed them before: a language server's references, say. /// `current`: the one at or after this offset is current. @@ -260,6 +282,14 @@ fn freshHay(set: *Places, gpa: std.mem.Allocator, f: *File.State) bool { if (set.hay.len > 0) gpa.rawFree(set.hay, .of(u8), @returnAddress()); set.hay = &.{}; set.hay = memory.unfilled(gpa, f.content.len) catch return false; + // What is not lowered yet is in the digest and a dump as well: zeros, + // the same in every process, not whatever its heap held (a replica's + // diverged). A pass over the text, as the edit that made it was. + // ponytail: 0.4 ms for 10 MB, 10 ms for 100 MB in one frame (ReleaseFast, + // page faults and all; lowering it is four times that, over frames). A + // digest and a dump of the lowered pieces alone if a huge text's first + // search ever shows. + @memset(set.hay, 0); set.low.resize(gpa, (f.content.len + chunk_bytes - 1) / chunk_bytes, false) catch return false; set.low.unsetAll(); set.hay_revision = f.revision; @@ -385,6 +415,7 @@ fn restart(set: *Places, p: *Pardes, f: *File.State) void { pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) void { const started_ns = nowNs(); if (!set.started) return; + set.derive(p.gpa); const f = if (pane.file) |*file| file else return set.clear(p.gpa); const gpa = p.gpa; if (f.revision != set.revision or f.content.len != set.len) { @@ -477,7 +508,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; @@ -874,3 +907,52 @@ test "zz stress analysis cost" { std.testing.allocator.free(a.colors); std.debug.print("decls {d} us ({d}), analysis with colours {d} us\n", .{ (t1 - t0) / 1000, d.len, (t2 - t1) / 1000 }); } + +test "a lowered copy's pieces not searched yet are the same in every process: a digest and a dump take them" { + // Two sessions searching one text, each heap handing out other bytes in + // what is not written yet (rollback.Epoch.fill): their places agree. + const rollback = @import("rollback.zig"); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(std.testing.allocator); + for (0..2000) |i| try text.print(std.testing.allocator, "line {d} of a text in four pieces\n", .{i}); + var digests: [2]u64 = undefined; + for (&digests, [_]u8{ 0x11, 0x5a }) |*d, b| { + var e: rollback.Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + rollback.Epoch.fill = b; + defer rollback.Epoch.fill = null; + const p = try Pardes.init(e.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile(text.items); + // The view's piece found now, the rest a frame at a time after. + pane.places.search(p, pane, .typed, 0, "piece", false, null, false); + try std.testing.expect(!pane.places.complete() and pane.places.fold); + d.* = pardes.walk.digest(Places, &pane.places); + } + try std.testing.expectEqual(digests[0], digests[1]); +} + +test "a session's places loaded from its dump find what an edit makes as the session's do" { + // A client's core is the session's dump; `re` is derived, in no dump: + // made again from the pattern, else the loaded set took itself for a + // list's and found nothing an edit made (a soak's rejoin loop). + const gpa = std.testing.allocator; + const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer a.deinit(); + const pane = try a.setTestFile("alpha beta\ngamma alpha\n"); + typeKeys(a, "e*"); + try std.testing.expectEqual(@as(usize, 2), pane.places.hits.items.len); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + defer b.deinit(); + try std.testing.expectEqual(pardes.walk.digest(Pardes, a), pardes.walk.digest(Pardes, b)); + for ([_]*Pardes{ a, b }) |p| { + const pn = p.panes[p.active].?; + const new = try gpa.dupe(u8, "alpha beta\ngamma alpha alpha\n"); + File.setContentSpan(p, &pn.file.?, new, null); + pn.places.fill(p, pn, 0, frame_steps); + try std.testing.expectEqual(@as(usize, 3), pn.places.hits.items.len); + } + try std.testing.expectEqual(pardes.walk.digest(Pardes, a), pardes.walk.digest(Pardes, b)); +} diff --git a/src/Query.zig b/src/Query.zig index 55d7b0b5..747022b0 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -48,6 +48,7 @@ const memory = @import("memory.zig"); const Names = @import("Names.zig"); const Places = @import("Places.zig"); const Stream = @import("Stream.zig"); +const Store = @import("Store.zig"); const Surface = @import("surface.zig").Surface; const Pardes = pardes.Pardes; const Pane = panes.Pane; @@ -173,6 +174,7 @@ capped: bool = false, saved: ?Saved = null, pub const Saved = struct { + pub const replica = .{ .blob = .{"rows"} }; content: []u8, rows: []locations.Row, asked: []u8, @@ -370,24 +372,8 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved { const f = &rp.file.?; const content = try p.gpa.dupe(u8, f.content); errdefer p.gpa.free(content); - const rows = try p.gpa.alloc(locations.Row, f.location_rows.len); - var made: usize = 0; - errdefer { - for (rows[0..made]) |r| { - p.gpa.free(r.path); - p.gpa.free(r.colors); - } - p.gpa.free(rows); - } - for (f.location_rows, 0..) |r, i| { - rows[i] = r; - rows[i].path = try p.gpa.dupe(u8, r.path); - rows[i].colors = p.gpa.dupe(u8, r.colors) catch |err| { - p.gpa.free(rows[i].path); - return err; - }; - made += 1; - } + const rows = try locations.cloneRows(p.gpa, f.location_rows); + errdefer locations.freeRows(p.gpa, rows); const asked = try p.gpa.dupe(u8, q.asked); errdefer p.gpa.free(asked); return .{ .content = content, .rows = rows, .asked = asked, .source = q.source, .items = try p.gpa.dupe(Item, q.items.items), .current = q.current, .total = q.total, .k = q.k }; @@ -590,12 +576,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz if (p.fs.serving) Stream.step(p); }, .recent => { - // Every window's files: what the others wrote since, read. - p.store.refresh(p); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - Output.recentText(p, &out.writer, true) catch {}; - var lines = std.mem.splitScalar(u8, out.written(), '\n'); + var lines = std.mem.splitScalar(u8, pardes.tape.authority(recentRows, .{ p, &out }), '\n'); while (lines.next()) |ln| { if (ln.len == 0) continue; const rel = Names.relative(ln, dir); @@ -616,10 +599,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz } // Then the panes this session closed, the latest first, each // `(closed)` and dimmer (makeNameRows): Enter brings one back - // where it was. Its name alone is matched, not the word. - var i = p.store.closed.items.len; - while (i > 0) : (i -= 1) { - const c = p.store.closed.items[i - 1]; + // where it was. Its name alone is matched, not the word. The + // store's, which a replica has from the authority. + for (pardes.tape.authority(Store.closedNames, .{p})) |c| { var buf: [4096 + 48]u8 = undefined; const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, Names.relative(c.name, dir) }) catch continue; const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; @@ -651,6 +633,14 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz /// What a Panes row of a closed pane ends with. const closed_word = " (closed)"; +/// Every window's files, as a Recent query lists them: what the others +/// wrote since, read first. The store's (`tape.authority`): in `out`. +fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { + p.store.refresh(p); + Output.recentText(p, &out.writer, true) catch {}; + return out.written(); +} + /// The owner's declarations, from a parse that already exists: its sticky /// headers' (TreeContext) at this revision, or the locations cache's. None /// is made here, on the loop, unless `small` and the text is small: a @@ -859,18 +849,29 @@ pub fn recolor(p: *Pardes) void { const f = &rp.file.?; var sh: Shades = .{}; var lines = std.mem.splitScalar(u8, f.content, '\n'); - var changed = false; - for (f.location_rows) |row| { + // The rows are a blob (File.State): a digest keeps its word for + // them and a rollback copy pins them, so they are made again, never + // written. Written in place, a session's digest kept the word of + // their colours before; a client joining hashed them after, and + // loaded "as another session" (and so did a replica that hashed them + // at another time). + var fresh: ?[]locations.Row = null; + for (f.location_rows, 0..) |row, i| { const text = lines.next() orelse break; if (row.kind != .match or row.location_end == 0 or row.at.line == 0 or row.colors.len == 0) continue; if (std.mem.indexOfNone(u8, row.colors, &.{0}) != null) continue; const code = text[@min(row.code_start, text.len)..]; const got = sh.of(p, row.path, row.at.line, code[0..@min(code.len, row.colors.len)], row.source_column); if (got.len == 0) continue; - @memcpy(row.colors[0..got.len], got); - changed = true; + const rows = fresh orelse locations.cloneRows(p.gpa, f.location_rows) catch break; + fresh = rows; + @memcpy(rows[i].colors[0..got.len], got); + } + if (fresh) |rows| { + locations.freeRows(p.gpa, f.location_rows); + f.location_rows = rows; + dropHighlights(p, f); } - if (changed) dropHighlights(p, f); } } @@ -1743,11 +1744,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; @@ -1869,11 +1866,15 @@ pub fn land(p: *Pardes, id: usize, row: ?usize) void { p.active = oid; return; } - const c = q.current orelse { + const f = &rp.file.?; + // A current row whose place went with its text (a frozen pane's rows + // edited: setContentSpan drops them, and an Esc's `saved` keeps that) + // is none: there is nowhere to land, as with no current one. + const current = if (q.current) |i| (if (i < q.items.items.len and i + 1 < f.location_rows.len) i else null) else null; + const c = current orelse { if (owner_id) |oid| p.active = oid; return; }; - const f = &rp.file.?; const it = q.items.items[c]; const meta = f.location_rows[c + 1]; p.jump_force = true; @@ -2288,7 +2289,7 @@ test "a / query: keystroke by keystroke, the owner shows the set, rows from its try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); } -test "a dump holding a query pane, live or frozen, restores, the pane as plain text" { +test "a dump holding a query pane, live or frozen, restores it as it was" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); defer p.deinit(); _ = try p.setTestFile("alpha one\nbeta\nalphabet\n"); @@ -2299,9 +2300,10 @@ test "a dump holding a query pane, live or frozen, restores, the pane as plain t const restored = try pardes.dump.initFromDump(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }, p.dump_out.?); defer restored.deinit(); const back = restored.panes[qid].?; - try std.testing.expect(back.query == null); - try std.testing.expectEqual(@as(usize, 0), back.file.?.location_rows.len); - try std.testing.expect(std.mem.startsWith(u8, back.file.?.content, "/alpha\n")); + const was = p.panes[qid].?; + try std.testing.expectEqual(was.query.?.frozen, back.query.?.frozen); + try std.testing.expectEqual(was.file.?.location_rows.len, back.file.?.location_rows.len); + try std.testing.expectEqualStrings(was.file.?.content, back.file.?.content); p.update(.{ .key = .{ .cp = Key.enter } }); } } @@ -2711,6 +2713,27 @@ test "B1 on a query pane's rows places the caret and selects, nothing else, the try std.testing.expectEqual(@as(i32, 3), pane.body.cur_row); } +test "Enter on a frozen query pane's rows after its text was edited: no row to land on, the keyboard to its owner" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + _ = try p.setTestFile("alpha\n"); + p.newScratchBelow(0); + p.active = 0; + // SPC b b and Enter: the current row lands, the rows frozen. + leader(p, "bb"); + const qid = queryPane(p).?; + p.update(.{ .key = .{ .cp = Key.enter } }); + const qp = p.panes[qid].?; + try std.testing.expect(qp.query.?.frozen and qp.query.?.current != null); + // Its text edited: the rows' places go with the text they were of. + p.active = qid; + typeKeys(p, "ggjxd"); + try std.testing.expectEqual(@as(usize, 0), qp.file.?.location_rows.len); + try std.testing.expect(qp.body.cur_row > 0); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(usize, 0), p.active); +} + test "a query row's place is its text: the match tinted after it, B3 on it opens the place, the rows saved and read again still Look" { if (!filesystem.platform_has_fs) return; var tmp = std.testing.tmpDir(.{}); diff --git a/src/Registers.zig b/src/Registers.zig index 5e23da5b..6df7401d 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -5,13 +5,19 @@ //! computed from the editor rather than stored (`_`, `#`, `.`, `%`) and the //! clipboard's paste side are read in edit.zig, which has the pane at hand. const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const Registers = @This(); list: std.ArrayList(Reg) = .empty, last_search: u21 = '/', +/// The values (their array too) and the joined text are replaced whole, +/// never written in place (`put` makes a new array), freed raw: blobs +/// (src/walk.zig), which a digest hashes once and a replica's rollback copy +/// pins as it does a file's text. pub const Reg = struct { + pub const replica = .{ .blob = .{ "values", "joined" } }; name: u21, /// gpa-owned, one per range, in document order values: [][]u8, @@ -30,9 +36,9 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void { } fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void { - for (reg.values) |v| gpa.free(v); - gpa.free(reg.values); - if (reg.joined) |j| gpa.free(j); + for (reg.values) |v| freeRaw(gpa, v); + freeRaw(gpa, reg.values); + if (reg.joined) |j| freeRaw(gpa, j); } pub fn get(r: *Registers, name: u21) ?*Reg { @@ -60,19 +66,20 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8, }; break :blk &r.list.items[r.list.items.len - 1]; }; - if (fresh or reg.values.len != n) { - const values = gpa.alloc([]u8, n) catch { - gpa.free(copy); - return; - }; - for (values) |*v| v.* = &.{}; - freeReg(gpa, reg); - reg.* = .{ .name = name, .values = values }; - } - gpa.free(reg.values[i]); - reg.values[i] = copy; - if (reg.joined) |j| gpa.free(j); - reg.joined = null; + // A new array, the old one's values moved into it but value `i`. + const values = gpa.alloc([]u8, n) catch { + gpa.free(copy); + return; + }; + const keep = !fresh and reg.values.len == n; + for (values, 0..) |*v, j| v.* = if (keep and j != i) reg.values[j] else &.{}; + values[i] = copy; + if (keep) { + freeRaw(gpa, reg.values[i]); + freeRaw(gpa, reg.values); + if (reg.joined) |j| freeRaw(gpa, j); + } else freeReg(gpa, reg); + reg.* = .{ .name = name, .values = values }; } /// The whole register as one text: its values joined by newlines, no diff --git a/src/Store.zig b/src/Store.zig index 5e47d695..9c726705 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -78,7 +78,7 @@ cap: u64 = max_bytes, session: u64 = 0, pid: i32 = 0, /// The file as read so far: its inode, generation and how far. -inode: std.Io.File.INode = 0, +inode: std.Io.File.INode = std.mem.zeroes(std.Io.File.INode), gen: u64 = 0, end: u64 = 0, /// Its length when first read whole: a file past twice that (and 1 MiB) @@ -468,6 +468,21 @@ fn unlist(s: *Store, gpa: std.mem.Allocator, serial: u32) void { }; } +/// A closed pane as Panes lists it: its serial and what its row shows. +pub const Named = struct { serial: u32, name: []const u8 }; + +/// This session's closed panes, the latest first (scratch; the names the +/// store's): what a step lists of them (`tape.authority`). +pub fn closedNames(p: *Pardes) []const Named { + const items = p.store.closed.items; + const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{}; + for (out, 0..) |*o, i| { + const c = items[items.len - 1 - i]; + o.* = .{ .serial = c.serial, .name = c.name }; + } + return out; +} + /// The closed pane with this serial, if this session has it. pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed { for (s.closed.items) |*c| if (c.serial == serial) return c; @@ -498,27 +513,30 @@ pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept { const pane = p.panes[id] orelse return null; if (pane.fs.unannounced or pane.query != null) return null; if (pane.file) |f| if (f.output) |o| if (o.from == .preview) return null; + // An output pane's record holds its text whole: one past the bound is + // known before a dump of it is made, which for a 256 MiB +Pager held + // the session up for seconds only to be thrown away. + if (pane.file) |f| if (f.output != null and f.content.len > max_record) return null; const gpa = p.recentGpa(); - const arena = p.scratch.allocator(); - var dp = dump.dumpPane(p, arena, pane) catch return null; - var zon = oneDump(p, arena, dp) catch return null; - // A terminal's history is what makes it big: kept without it, it comes - // back a fresh shell in its directory. - if (zon.len > max_record) if (dp.terminal) |*t| { - t.stream = ""; - t.stream_b64 = ""; - dp.body = ""; - zon = oneDump(p, arena, dp) catch return null; - }; - if (zon.len > max_record) return null; + var owned = dump.savePane(gpa, pane, false) catch return null; + // A terminal's history or a file's undo is what makes it big: kept + // without them it comes back a fresh shell in its directory, or its + // file as it is on disk. + if (owned.len > max_record) { + gpa.free(owned); + owned = dump.savePane(gpa, pane, true) catch return null; + } + if (owned.len > max_record) { + gpa.free(owned); + return null; + } const what: []const u8 = if (pane.file) |f| f.path else if (pane.pdfPath()) |pdf| pdf else if (pane.image) |iv| iv.path else if (pane.command) |c| c else pane.cwdSlice(); - var name = gpa.alloc(u8, @min(what.len, 4096)) catch return null; - // One line, a tab its end: a newline or a tab in it is a space. - for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; - const owned = gpa.dupe(u8, zon) catch { - gpa.free(name); + var name = gpa.alloc(u8, @min(what.len, 4096)) catch { + gpa.free(owned); return null; }; + // One line, a tab its end: a newline or a tab in it is a space. + for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; const at = layout.findPane(p, id); const owner = layout.ownerOf(p, id); return .{ @@ -548,18 +566,6 @@ pub const Kept = struct { } }; -/// A Dump of the one pane `dp`, as a Restore would read it. -fn oneDump(p: *Pardes, arena: std.mem.Allocator, dp: dump.Pane) ![]const u8 { - const ids = [_]usize{0}; - const columns = [_]dump.Column{.{ .panes = &ids }}; - const one = [_]dump.Pane{dp}; - const state: dump.State = .{ .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, .columns = &columns, .panes = &one }; - try dump.validate(state); - var out: std.Io.Writer.Allocating = .init(arena); - try std.zon.stringify.serialize(state, .{}, &out.writer); - return out.written(); -} - /// The pane `k` kept is gone, its rows given to `gainer` (a slot, after /// they moved; null none): listed, and its record framed for the store. pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void { @@ -794,25 +800,19 @@ fn compact(s: *Store, p: *Pardes, file: File, st: File.Stat, path: []const u8) v /// from `from`. False when it is not this session's, or its record is gone /// or bad; the keyboard on it otherwise. pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { - s.refresh(p); const gpa = p.recentGpa(); - const place = (s.closedBySerial(serial) orelse return false).*; - const body = s.bodyOf(p, place) orelse return false; - // Read from the file: this call's to free (`unlist` frees one held). - defer if (place.body.len == 0) gpa.free(body); + // The store's: a replica has the authority's (`tape.authority`). + const place = pardes.tape.authority(record, .{ s, p, serial }) orelse return false; + const body = place.body; const m = parseMeta(body) orelse return false; - var parsed = dump.readZon(p.gpa, body[m.dump_at..], "closed pane") catch return false; - defer parsed.deinit(); const free = p.freeSlot() orelse { p.reportError(from, "Panes", error.NoPaneSlots); return false; }; - const src = parsed.value.panes[0]; - const pane = dump.restorePane(p, free, src) catch |err| { + _ = dump.loadPane(p, free, body[m.dump_at..]) catch |err| { p.reportError(from, "Panes", err); return false; }; - pane.collapsed = false; const owner = if (place.owner != 0) p.paneBySerial(place.owner) else null; const gainer = if (place.gainer != 0) p.paneBySerial(place.gainer) else null; const placed = if (owner) |o| @@ -839,6 +839,21 @@ pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { return true; } +/// Where a closed pane goes back, and its record's body (scratch). +pub const Record = struct { gainer: u32, below: bool, owner: u32, rows: u16, body: []const u8 }; + +/// Closed pane `serial`'s record, the store read on first; null when it is +/// not this session's, or its record is gone or bad. +fn record(s: *Store, p: *Pardes, serial: u32) ?Record { + s.refresh(p); + const c = (s.closedBySerial(serial) orelse return null).*; + const body = s.bodyOf(p, c) orelse return null; + // Read from the file: this call's to free (`unlist` frees one held). + defer if (c.body.len == 0) p.recentGpa().free(body); + const copy = p.scratch.allocator().dupe(u8, body) catch return null; + return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = copy }; +} + /// A closed pane's record body: held here, or read from the file and /// checked against its CRC (gpa; the caller frees it when it is not /// `c.body`). @@ -868,8 +883,9 @@ fn bodyOf(s: *Store, p: *Pardes, c: Closed) ?[]u8 { return body; } -/// `@p<serial>` naming a pane this session closed: its serial. -pub fn closedAddress(s: *const Store, word: []const u8) ?u32 { +/// `@p<serial>` alone, the pane and no place in it: its serial, for a pane +/// this session closed to come back by (`reopen` says whether it did). +pub fn closedAddress(word: []const u8) ?u32 { const addr = @import("config.zig").pane_addr; if (!std.mem.startsWith(u8, word, addr)) return null; const digits = word[addr.len..]; @@ -878,8 +894,7 @@ pub fn closedAddress(s: *const Store, word: []const u8) ?u32 { } else digits.len; // `@p7` alone, or `@p7:` with nothing after it: the pane, no place in it. if (end == 0 or (end < digits.len and !(digits[end] == ':' and end + 1 == digits.len))) return null; - const serial = std.fmt.parseInt(u32, digits[0..end], 10) catch return null; - return if (s.closedBySerial(serial) != null) serial else null; + return std.fmt.parseInt(u32, digits[0..end], 10) catch null; } // ---- tests ---- diff --git a/src/Stream.zig b/src/Stream.zig index da7d12eb..69dc311f 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 } }); @@ -470,7 +512,7 @@ pub fn parseFor(p: *Pardes, id: usize) void { // Here and now: a small text, a host with no workers. var got: Collect = .{ .gpa = p.gpa }; defer got.batches.deinit(p.gpa); - parseWork(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + parseWork(if (comptime pardes.hosted) std.Io.Threaded.global_single_threaded.io() else std.Io.failing, job, &got, Collect.take); for (got.batches.items) |*b| { receive(p, b); b.deinit(p.gpa); @@ -666,19 +708,28 @@ pub fn runHere(p: *Pardes, gen: u32) void { }; defer job.deinit(); job.waits = false; - var got: Collect = .{ .gpa = p.gpa }; - defer got.batches.deinit(p.gpa); - { - pardes.turn.yield(); - defer pardes.turn.back(); - work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); - } - for (got.batches.items) |*b| { + // Its reads a site of the tape's (a 9P write's Grep or Find runs here): + // a replica takes the batches the session got, as it takes a worker's + // (Event.search), and reads no file. + const batches = pardes.tape.site(.search, p.gpa, searchHere, .{ p.gpa, job }); + defer p.gpa.free(batches); + for (batches) |*b| { receive(p, b); b.deinit(p.gpa); } } +/// The whole search, its batches gpa's. +fn searchHere(gpa: std.mem.Allocator, job: *Job) []Batch { + var got: Collect = .{ .gpa = gpa }; + work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + return got.batches.toOwnedSlice(gpa) catch { + for (got.batches.items) |b| b.deinit(gpa); + got.batches.deinit(gpa); + return &.{}; + }; +} + const Collect = struct { gpa: std.mem.Allocator, batches: std.ArrayList(Batch) = .empty, @@ -1145,22 +1196,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 +1285,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 +1387,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 +1426,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 +1469,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" { 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 +1523,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 +1567,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(); @@ -1588,6 +1647,79 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work Stats.print("other frames", idle.items); } +test "Stream: a Grep's rows recoloured are made again, never written, and a dump of them dropped by an edit loads to its digest" { + if (comptime !filesystem.platform_has_fs) return; + if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + // Rows enough that a digest keeps a word for them (walk's blob_min). + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..80) |i| try text.print(testing.allocator, "pub const value_{d}: u32 = {d};\n", .{ i, i }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text.items }); + var root_buf: [4096]u8 = undefined; + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + var shell: TestShell = .init(); + shell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + panes.Query.open(p, 0, .grep, "value", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const f = &p.panes[qid].?.file.?; + const coloured = struct { + fn count(rows: []const @import("locations.zig").Row) usize { + var n: usize = 0; + for (rows) |row| n += @intFromBool(row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + return n; + } + }.count; + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + // The rows, plain, digested (their word kept) before their colours land. + while (now() < until and f.location_rows.len < 80) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expect(f.location_rows.len >= 80 and coloured(f.location_rows) == 0); + _ = pardes.walk.digest(Pardes, p); + const plain = f.location_rows.ptr; + while (now() < until and (coloured(f.location_rows) < 80 or p.stream.rows_paint != null)) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 80), coloured(f.location_rows)); + // Made again: a Debug build's digest finds no blob written in place + // (the plain rows' kept word is theirs), and the rows are new ones. + _ = pardes.walk.digest(Pardes, p); + try testing.expect(f.location_rows.ptr != plain); + // An edit drops them with the text they were for (dropped_marks): a + // join's load of the session is it, digest for digest. + panes.File.setContent(p, f, try p.gpa.dupe(u8, "value\n")); + try testing.expect(f.dropped_marks != null); + const bytes = try pardes.dump.dumpBytes(p); + defer p.gpa.free(bytes); + const q = try pardes.dump.load(testing.allocator, p.opts, bytes, null); + defer q.deinit(); + try testing.expectEqual(pardes.walk.digest(panes.File.State, f), pardes.walk.digest(panes.File.State, &q.panes[qid].?.file.?)); +} + test "Stream: a Grep's rows are coloured as their file's language colours them, the file painted on a worker, the match tint on top" { if (comptime !filesystem.platform_has_fs) return; if (!syntax.enabled or !syntax.readyApart("c.zig")) return; @@ -1604,7 +1736,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..da4be590 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 and the ranges: +/// as far as they go (the ranges' room is made whole and used `nsel` deep). +pub const replica = .{ .len = .{ .{ "ed_undo", "ed_undo_len" }, .{ "ed_redo", "ed_redo_len" }, .{ "sels", "nsel" } } }; + what: What = .body, /// The characters of a text that holds its own (gpa-owned): everything but /// 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..7a4103c3 100644 --- a/src/body_layer.zig +++ b/src/body_layer.zig @@ -169,22 +169,16 @@ 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); + // The pane's geometry as it is (File.geometry): made by the step that + // moved it if one read it, else here, and the same either way. + 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; @@ -487,6 +481,9 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane } } + // A replica's predicted echo, over what its authority's screen shows. + if (p.echo) |e| if (pane.isTerminal()) e.paint(p, s, pane, id, tx, tw, body_y, body_h, active); + if (rail) |bar| if (bar.rect.h > 0) { // The rail covers the notice bands too: nothing else paints those // two gutter columns, so a gap there keeps the frame-wide border @@ -512,6 +509,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 1f1e674e..38a4d835 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); @@ -725,7 +769,7 @@ pub const Restore = struct { var why_buf: [256]u8 = undefined; pardes.dump.check(c.p.gpa, bytes, &why_buf) catch |err| { var buf: [limits.host_path_cap + 320]u8 = undefined; - const why = if (err == error.ParseZon) why_buf[0..std.mem.indexOfScalar(u8, &why_buf, 0).?] else pardes.Messages.errorWords(err, &why_buf); + const why = if (err == error.OutOfMemory) pardes.Messages.errorWords(err, &why_buf) else why_buf[0..std.mem.indexOfScalar(u8, &why_buf, 0).?]; return c.p.reportFailure(c.id, std.fmt.bufPrint(&buf, "Restore: {s}: not a pardes dump: {s}", .{ path, why }) catch "Restore: not a pardes dump"); }; } @@ -737,51 +781,41 @@ pub const Restore = struct { } }; -/// Attach this terminal to a detached pardes session by name. -pub const Attach = struct { +/// Detach this pardes from its terminal or window, leaving the session +/// running: `pardes --attach` (or `pardes-gui --attach`) takes it back. +pub const Detach = struct { pub const scope: Scope = .session; - pub const takes_arg = true; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - if (comptime enabled) ask(c) else unreachable; + if (comptime enabled) + c.p.emit(.{ .detach = .{ .pane = @intCast(c.id) } }) + else + unreachable; } - fn ask(c: Ctx) void { - const name = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); - if (name.len > pardes.attach_name_max) - return c.p.reportError(c.id, comptime word(@This()), error.NameTooLong); - // A detached session has no terminal to move: bare, there is no - // "the one session there" but itself; a name is said for what it is. - if (c.p.opts.detached_session) { - if (name.len == 0) return c.p.reportFailure(c.id, "wrong #args in control message \"Attach\": a detached session names the session"); - // A session is its socket, `pardes-detached-<name>.sock` beside - // the 9P ones (detached/server.zig socketPath); not imported, as - // the core links no frontend's code. - const ninep_io = @import("9p_io.zig"); - var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; - var path_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; - const exists = if (ninep_io.socketDir(&dir_buf)) |dir| - (if (std.fmt.bufPrintSentinel(&path_buf, "{s}/pardes-detached-{s}.sock", .{ dir, name }, 0)) |path| ninep_io.statNoFollow(path) != null else |_| false) - else - false; - var said: [pardes.attach_name_max + 96]u8 = undefined; - return c.p.reportFailure(c.id, if (exists) - std.fmt.bufPrint(&said, "Attach: {s}: a detached session has no terminal to attach; Attach from a frontend", .{name}) catch "Attach: a detached session has no terminal to attach" - else - std.fmt.bufPrint(&said, "Attach: {s}: no such session", .{name}) catch "Attach: no such session"); - } - c.p.emit(.{ .attach = .{ .pane = @intCast(c.id), .name = .from(name) } }); +}; + +/// Take in the shells of a session that died holding them, each as a +/// terminal pane here: the pardes daemon kept their terminals open. +pub const Adopt = struct { + pub const scope: Scope = .session; + pub const enabled = pardes.can_attach; + pub fn run(c: Ctx) void { + if (comptime !enabled) unreachable; + const adopt = c.p.host.vtable.adopt orelse return c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); + adopt(c.p.host.ctx, @intCast(c.id)); } }; -/// Detach this pardes from its terminal, leaving the session running. -pub const Detach = 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) - c.p.emit(.{ .detach = .{ .pane = @intCast(c.id) } }) - else - unreachable; + 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)); } }; @@ -816,11 +850,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.probeMount, .{ c.p.gpa, dial }); reached catch |err| { if (c.p.fs.mounts.pop()) |gone| { c.p.gpa.free(gone.name); @@ -911,7 +941,7 @@ pub const DumpThemes = struct { pub const enabled = pardes.hosted; pub fn run(c: Ctx) void { if (comptime enabled) { - if (c.p.opts.config_dir == null) { + if (c.p.configDir() == null) { c.p.reportError(c.id, "dump themes", error.NoConfigDirectory); return; } @@ -926,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; } @@ -950,11 +981,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; } }; @@ -1448,7 +1475,8 @@ pub const Tty = struct { // start its fallback in its place. if (comptime pardes.hosted) if (arg.len > 0) { var why: [320]u8 = undefined; - if (@import("host_io.zig").Shell.refusal(arg[0..@min(arg.len, 200)], &why)) |refused| { + // The authority's lookup, taped, as Shell's (exec.zig). + if (pardes.tape.value(.shell, @import("host_io.zig").Shell.refusal, .{ arg[0..@min(arg.len, 200)], &why })) |refused| { var said: [340]u8 = undefined; return c.p.reportFailure(c.id, std.fmt.bufPrint(&said, "Tty: {s}", .{refused}) catch "Tty: no such shell"); } @@ -1459,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); } @@ -1580,7 +1611,7 @@ pub const Help = struct { pub const Config = struct { pub const scope: Scope = .session; pub fn run(c: Ctx) void { - const path = c.p.opts.startup_config_path orelse + const path = c.p.startupPath() orelse return c.p.reportFailure(c.id, "Config: no startup file here: no configuration directory"); var real_buf: [4096]u8 = undefined; const real = if (pardes.filesystem.resolveOs(path, &real_buf)) |r| r.path else null; @@ -1683,8 +1714,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 4440130d..3402b2ee 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]; @@ -66,7 +73,9 @@ pub fn write(w: *std.Io.Writer) !void { } test "docs/typ/builtins-<platform>.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 + 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/c_heap.zig b/src/c_heap.zig index 2a3e865f..efd2064d 100644 --- a/src/c_heap.zig +++ b/src/c_heap.zig @@ -39,21 +39,28 @@ pub fn Heap(comptime current: *const std.mem.Allocator) type { var last: std.atomic.Value(usize) = .init(0); pub fn malloc(size: usize) callconv(.c) ?*anyopaque { + // `current` as its bytes are: std's stateless allocators (c_, + // page_, smp_allocator) leave `ptr` undefined, and a heap's + // allocator never repointed is a constant to a release build, + // which then takes `ptr` for whatever it likes at each + // comparison below: never found, an entry a malloc, the 65th a + // panic (a box session's zstd after 15 far joins, 2026-10-30). + const now = @as(*const volatile std.mem.Allocator, current).*; const total = std.math.add(usize, header, size) catch return null; - const bytes = current.alignedAlloc(u8, .@"16", total) catch return null; + const bytes = now.alignedAlloc(u8, .@"16", total) catch return null; var index = last.load(.monotonic); if (index >= allocators_len.load(.acquire) or - allocators[index].ptr != current.ptr or allocators[index].vtable != current.vtable) + allocators[index].ptr != now.ptr or allocators[index].vtable != now.vtable) { while (!allocators_lock.tryLock()) std.atomic.spinLoopHint(); defer allocators_lock.unlock(); const len = allocators_len.load(.monotonic); index = for (allocators[0..len], 0..) |known, at| { - if (known.ptr == current.ptr and known.vtable == current.vtable) break at; + if (known.ptr == now.ptr and known.vtable == now.vtable) break at; } else len; if (index == len) { if (len == allocators.len) @panic("c_heap: more distinct allocators than it can name"); - allocators[len] = current.*; + allocators[len] = now; allocators_len.store(len + 1, .release); } last.store(index, .monotonic); @@ -133,3 +140,15 @@ test "a C heap block keeps its bytes, frees exactly, and goes back where it came live = null; heap.free(null); } + +/// Never written, as a release session's zstd heap's allocator was: a +/// constant to the compiler, `ptr` (undefined in std's stateless +/// allocators) whatever it likes at each comparison. +var stateless: std.mem.Allocator = std.heap.page_allocator; + +test "a heap over a stateless allocator that is never repointed names it once" { + const heap = Heap(&stateless); + const before = allocators_len.load(.monotonic); + for (0..2 * allocators.len) |_| heap.free(heap.malloc(8) orelse return error.OutOfMemory); + try std.testing.expect(allocators_len.load(.monotonic) <= before + 1); +} diff --git a/src/colors.zig b/src/colors.zig index c016434b..cc417ccb 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -677,14 +677,15 @@ pub fn requestThemeFile(p: *Pardes, id: usize, argument: []const u8) void { } const absolute = std.fs.path.isAbsolute(input); - if (!absolute and p.opts.config_dir == null) { + const config_dir = if (absolute) null else p.configDir(); + if (!absolute and config_dir == null) { p.reportError(id, "theme file", error.NoConfigDirectory); return; } var joined: [4095]u8 = undefined; const resolved = if (absolute) input else path: { - const dir = p.opts.config_dir.?; + const dir = config_dir.?; const separator = if (dir.len > 0 and dir[dir.len - 1] != std.Io.Dir.path.sep) std.Io.Dir.path.sep_str else ""; break :path std.fmt.bufPrint(&joined, "{s}{s}{s}", .{ dir, separator, input }) catch { p.reportError(id, "theme file", error.PathTooLong); diff --git a/src/config.zig b/src/config.zig index 7ed3972c..5e7ddb24 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); @@ -217,8 +217,9 @@ 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); + table.set(.Upgrade, "sU"); } if (builtins.Peek.enabled) { table.set(.Peek, null); @@ -455,6 +456,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/daemon.zig b/src/daemon.zig new file mode 100644 index 00000000..b93f5d43 --- /dev/null +++ b/src/daemon.zig @@ -0,0 +1,1221 @@ +//! The per-user daemon (docs/next-daemon.md, "Phase 3 notes"): one +//! `pardes --daemon` per user per machine, on `pardes-daemon.sock` in the +//! runtime directory (ninep_io.socketDir), its `.lock` flocked for its life +//! so two never run. The first session that finds none starts it (a double +//! fork and an exec), and it exits once it holds nothing: no session +//! connected, no pty master kept. +//! +//! It spawns nothing and is nobody's parent. A session registers on one +//! connection and tells it, over that connection: +//! - a dup of every pty master it forks (`pty`), so a session killed +//! outright never hangs its shells up, and which ones it closed; +//! - whether a terminal or a window shows it (`state`). +//! It hands a terminal (`pardes --attach`) or a window request +//! (`pardes-gui --attach`) to a detached session, and a crashed session's +//! masters to the one that asks for them (Adopt). +//! +//! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it +//! carries (SCM_RIGHTS). +//! +//! A replica (docs/next-daemon.md, phase 5) comes in on a stream socket of +//! its own, `pardes-replica.sock`, which `ssh -L` can forward: its hello +//! (detached/wire.zig) names a session, and once its build is the session's +//! the connection is handed to that session, which it talks to from then on. +const std = @import("std"); +const builtin = @import("builtin"); +const libc = std.c; +const posix = std.posix; +const ninep_io = @import("9p_io.zig"); +const wire = @import("detached/wire.zig"); +const upgrade = @import("upgrade.zig"); +const log = std.log.scoped(.replica); + +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; +/// 2: a session's hello carries its semantics hash (wire.semantics). +const version = 2; +pub const max_fds = 64; +const msg_max = 4096; +/// Idle this long, it exits: long enough for the session that started it to +/// say hello, short enough that nothing lingers. +const linger_ms = 1000; +const max_conns = 64; +const max_held = 256; +/// Replicas whose hello is still on its way. ponytail: one that never sends +/// it holds its slot until it hangs up; past 16 a new one is closed at once. +const max_replicas = 16; +const sun_path_len = ninep_io.sun_path_len; +const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; + +pub const Kind = enum { tty, gui }; + +/// Off for a harness that kills what it starts by its pid and closes its +/// terminal (`PARDES_DAEMON=off`, the snapshot harness's): a session there +/// is the process it started, and a hung-up terminal ends it, as before +/// the daemon. A GUI test window (PARDES_TEST) has none unless asked +/// (`PARDES_DAEMON=on`). +pub fn wanted() bool { + if (comptime !supported) return false; + const v = std.mem.span(libc.getenv("PARDES_DAEMON") orelse return libc.getenv("PARDES_TEST") == null); + return !std.mem.eql(u8, v, "off"); +} + +fn place(buf: *[sun_path_len:0]u8, comptime leaf: []const u8) ?[:0]const u8 { + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = ninep_io.socketDir(&dir_buf) orelse return null; + _ = libc.mkdir(dir, 0o700); + return std.fmt.bufPrintSentinel(buf, "{s}/" ++ leaf, .{dir}, 0) catch null; +} + +// ---- one message: words and fds ---- + +const data_off = std.mem.alignForward(usize, @sizeOf(libc.cmsghdr), @sizeOf(usize)); +fn cmsgSpace(n: usize) usize { + return data_off + std.mem.alignForward(usize, n * @sizeOf(c_int), @sizeOf(usize)); +} + +pub fn send(fd: c_int, words: []const u8, fds: []const c_int) bool { + std.debug.assert(words.len > 0 and fds.len <= max_fds); + var iov = [_]posix.iovec_const{.{ .base = words.ptr, .len = words.len }}; + var cbuf: [cmsgSpace(max_fds)]u8 align(@alignOf(libc.cmsghdr)) = undefined; + var msg: libc.msghdr_const = .{ .name = null, .namelen = 0, .iov = &iov, .iovlen = 1, .control = null, .controllen = 0, .flags = 0 }; + if (fds.len > 0) { + const hdr: *libc.cmsghdr = @ptrCast(&cbuf); + hdr.* = .{ .len = @intCast(data_off + fds.len * @sizeOf(c_int)), .level = libc.SOL.SOCKET, .type = libc.SCM.RIGHTS }; + @memcpy(cbuf[data_off..][0 .. fds.len * @sizeOf(c_int)], std.mem.sliceAsBytes(fds)); + msg.control = &cbuf; + msg.controllen = @intCast(cmsgSpace(fds.len)); + } + while (true) { + // Never blocks: a peer that does not read loses its link instead. + const n = libc.sendmsg(fd, &msg, nosignal | libc.MSG.DONTWAIT); + if (n >= 0) return true; + if (libc.errno(n) != .INTR) return false; + } +} + +pub const Got = struct { words: []const u8, fds: [max_fds]c_int = undefined, nfds: usize = 0 }; + +/// The next message, its fds close-on-exec; null at the end of the link. +pub fn recv(fd: c_int, buf: []u8) ?Got { + var iov = [_]posix.iovec{.{ .base = buf.ptr, .len = buf.len }}; + var cbuf: [cmsgSpace(max_fds)]u8 align(@alignOf(libc.cmsghdr)) = undefined; + var msg: libc.msghdr = .{ .name = null, .namelen = 0, .iov = &iov, .iovlen = 1, .control = &cbuf, .controllen = cbuf.len, .flags = 0 }; + const n = while (true) { + const r = libc.recvmsg(fd, &msg, libc.MSG.CMSG_CLOEXEC); + if (r >= 0) break r; + if (libc.errno(r) != .INTR) return null; + }; + var got: Got = .{ .words = buf[0..@intCast(n)] }; + if (msg.controllen >= data_off) { + const hdr: *const libc.cmsghdr = @ptrCast(&cbuf); + if (hdr.level == libc.SOL.SOCKET and hdr.type == libc.SCM.RIGHTS and hdr.len >= data_off) { + got.nfds = @min((hdr.len - data_off) / @sizeOf(c_int), max_fds); + @memcpy(std.mem.sliceAsBytes(got.fds[0..got.nfds]), cbuf[data_off..][0 .. got.nfds * @sizeOf(c_int)]); + } + } + // A zero-length datagram is never sent: it is the peer's end. + if (n == 0) { + for (got.fds[0..got.nfds]) |f| _ = libc.close(f); + return null; + } + return got; +} + +fn socket() c_int { + return libc.socket(libc.AF.UNIX, libc.SOCK.SEQPACKET | libc.SOCK.CLOEXEC, 0); +} + +fn address(path: [:0]const u8) libc.sockaddr.un { + var addr: libc.sockaddr.un = .{ .path = @splat(0) }; + @memcpy(addr.path[0..path.len], path); + return addr; +} + +/// Connected without ever waiting on a daemon that does not accept (a full +/// backlog parks a blocking connect for good), then blocking again. +fn dial(path: [:0]const u8) ?c_int { + const fd = socket(); + if (fd < 0) return null; + const addr = address(path); + const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); + const nonblock: c_int = @bitCast(@as(u32, @bitCast(libc.O{ .NONBLOCK = true }))); + _ = libc.fcntl(fd, libc.F.SETFL, flags | nonblock); + if (libc.connect(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) { + _ = libc.close(fd); + return null; + } + _ = libc.fcntl(fd, libc.F.SETFL, flags); + return fd; +} + +/// Waits `ms` for `fd` to be readable. +fn readable(fd: c_int, ms: c_int) bool { + var fds = [_]libc.pollfd{.{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }}; + while (true) { + const n = libc.poll(&fds, 1, ms); + if (n >= 0) return n > 0; + if (libc.errno(n) != .INTR) return false; + } +} + +fn sleepMs(ms: u32) void { + const ts: libc.timespec = .{ .sec = 0, .nsec = @as(c_long, ms) * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); +} + +// ---- a client's half ---- + +/// A connection to the daemon. When none answers and `start`, the +/// connection is queued on a socket this process binds, and the daemon is +/// still to be started on it (`startPending`): a terminal's stub does that +/// after its fork, off the session's way; every other caller at once. +pub fn connect(start: bool) ?c_int { + if (comptime !supported) return null; + var sock_buf: [sun_path_len:0]u8 = undefined; + const sock = place(&sock_buf, "pardes-daemon.sock") orelse return null; + if (dial(sock)) |fd| return fd; + if (!start) return null; + var lock_buf: [sun_path_len:0]u8 = undefined; + const lock_path = place(&lock_buf, "pardes-daemon.lock") orelse return null; + // Another starter, or a daemon on its way out, holds the lock: it is + // asked again until the lock is free or the socket answers. + var waited: u32 = 0; + while (waited < 500) : (waited += 2) { + if (begin(sock, lock_path)) |fd| return fd; + sleepMs(2); + if (dial(sock)) |fd| return fd; + } + return null; +} + +/// The listener and lock a daemon is still to be started on (`begin`). +var pending: ?struct { listener: c_int, lock: c_int } = null; + +/// With the lock taken: the socket bound and listening here and this +/// process's connection queued on it, so it is answered as soon as the +/// daemon runs; the daemon itself is `startPending`'s. +fn begin(sock: [:0]const u8, lock_path: [:0]const u8) ?c_int { + const lock = libc.open(lock_path, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (lock < 0) return null; + if (libc.flock(lock, posix.LOCK.EX | posix.LOCK.NB) != 0) { + _ = libc.close(lock); + return null; + } + // Ours to replace: whatever is at the path was a daemon that died. + _ = libc.unlink(sock); + const listener = socket(); + const addr = address(sock); + const conn = if (listener >= 0 and libc.bind(listener, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) == 0 and + libc.chmod(sock, 0o600) == 0 and libc.listen(listener, max_conns) == 0) dial(sock) else null; + if (conn == null) { + if (listener >= 0) _ = libc.close(listener); + _ = libc.close(lock); + return null; + } + pending = .{ .listener = listener, .lock = lock }; + return conn; +} + +/// The daemon `connect` left to start, started (or, `start` false, let go +/// of: a forked session's copies). +pub fn startPending(start: bool) void { + const p = pending orelse return; + pending = null; + if (start and !spawn(p.listener, p.lock)) { + var sock_buf: [sun_path_len:0]u8 = undefined; + if (place(&sock_buf, "pardes-daemon.sock")) |sock| _ = libc.unlink(sock); + } + _ = libc.close(p.listener); + _ = libc.close(p.lock); +} + +extern "c" fn close_range(first: c_uint, last: c_uint, flags: c_int) c_int; + +/// `pardes --daemon`, in a session of its own and nobody's child (forked +/// twice), on the listener and the lock as fds 3 and 4. +fn spawn(listener: c_int, lock: c_int) bool { + var exe_buf: [4096:0]u8 = undefined; + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch return false; + exe_buf[n] = 0; + const argv = [_:null]?[*:0]const u8{ &exe_buf, "--daemon", null }; + const first = libc.fork(); + if (first < 0) return false; + if (first == 0) { + // Only async-signal-safe calls between fork and exec. + _ = libc.setsid(); + if (libc.fork() != 0) libc._exit(0); + const l = libc.fcntl(listener, libc.F.DUPFD, @as(c_int, 10)); + const k = libc.fcntl(lock, libc.F.DUPFD, @as(c_int, 10)); + if (l < 0 or k < 0 or libc.dup2(l, 3) < 0 or libc.dup2(k, 4) < 0) libc._exit(127); + if (close_range(5, std.math.maxInt(c_uint), 0) != 0) { + var fd: c_int = 5; + while (fd < 1024) : (fd += 1) _ = libc.close(fd); + } + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + if (null_fd >= 0) for (0..3) |i| { + _ = libc.dup2(null_fd, @intCast(i)); + }; + if (null_fd > 4) _ = libc.close(null_fd); + _ = libc.chdir("/"); + _ = libc.execve(&exe_buf, &argv, @ptrCast(libc.environ)); + libc._exit(127); + } + var status: c_int = 0; + while (libc.waitpid(first, &status, 0) < 0 and libc.errno(-1) == .INTR) {} + return true; +} + +/// Says who this session is. Its answer is not waited for: a daemon just +/// started is still being exec'd, and no startup waits on it. The reader +/// (`watch`) takes the `ok`, and only then is the session kept (`kept`). +fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) bool { + var buf: [256]u8 = undefined; + const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {x} {s}", .{ version, k, pid, if (detached) "detached" else "attached", wire.semantics, named }) catch return false; + return send(fd, words, &.{}); +} + +// ---- the session's link, process-wide ---- + +/// This session's registration (`register`), which forkShell and the hosts +/// tell of their ptys; -1 with none (no daemon, or PARDES_DAEMON=off). +pub var link: c_int = -1; +/// The daemon said `ok` to the hello, and the link has not ended since; +/// `gone` once it has ended. +var up = std.atomic.Value(bool).init(false); +var gone = std.atomic.Value(bool).init(false); +/// What it is called (its 9P name). +var name_buf: [128]u8 = undefined; +var name_len: usize = 0; +var reader: ?std.Thread = null; + +pub fn name() []const u8 { + return name_buf[0..name_len]; +} + +/// Whether a daemon keeps this session: what Detach, and a terminal's end, +/// need, or the session would be left where nothing can attach to it. A +/// daemon just started answers the hello once it runs, so this waits for +/// that answer (up to 2 s): never on a startup's path, only on those two. +pub fn kept() bool { + if (link < 0) return false; + var waited: u32 = 0; + while (!up.load(.acquire) and !gone.load(.acquire) and waited < 2000) : (waited += 1) sleepMs(1); + return up.load(.acquire); +} + +/// The name a session goes by: its 9P socket's (`--9p=`, `--detach=`), else +/// its pid, as ninep_io.start names it. +pub fn setName(named: []const u8) void { + const n = if (named.len != 0) + std.fmt.bufPrint(&name_buf, "{s}", .{named[0..@min(named.len, name_buf.len)]}) catch unreachable + else + std.fmt.bufPrint(&name_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable; + name_len = n.len; +} + +/// Registers this session on `fd` (connected already, as a terminal's +/// stub does before it forks), or on a new connection when `fd` is null. +pub fn register(fd: ?c_int, k: Kind, named: []const u8, detached: bool) bool { + if (comptime !supported) return false; + setName(named); + const c = fd orelse connect(true) orelse return false; + startPending(true); + if (!hello(c, k, libc.getpid(), name(), detached)) { + _ = libc.close(c); + return false; + } + link = c; + return true; +} + +fn tell(words: []const u8, fds: []const c_int) void { + if (link < 0) return; + _ = send(link, words, fds); +} + +/// A shell forked on `master` (host_io.forkShell): the daemon keeps a dup. +pub fn pty(pid: libc.pid_t, master: c_int) void { + var buf: [32]u8 = undefined; + tell(std.fmt.bufPrint(&buf, "pty {d}", .{pid}) catch return, &.{master}); +} + +/// The pty of `pid` closed here: the daemon lets its dup go too. +pub fn closed(pid: libc.pid_t) void { + if (pid <= 0) return; + var buf: [32]u8 = undefined; + tell(std.fmt.bufPrint(&buf, "close {d}", .{pid}) catch return, &.{}); +} + +pub fn setState(detached: bool) void { + tell(if (detached) "state detached" else "state attached", &.{}); +} + +/// Asks for the masters of sessions that died holding them; they come back +/// as an `adopted` note. +pub fn adopt() void { + tell("adopt", &.{}); +} + +/// This session is about to exec another build (upgrade.zig): the daemon +/// is asked to move too. Its replica clients were told why they are let go +/// (replica.zig `Peers.refuseAll`). +pub fn upgraded() void { + tell("upgrade", &.{}); +} + +/// 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. + attach: struct { tty: c_int, stub: c_int }, + adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize }, + /// A replica's connection, its hello taken (src/replica.zig `Peers`), + /// and the view it joins again for, if it does. + replica: Joining, +}; + +pub const Joining = struct { fd: c_int, view: ?u8 = null }; + +/// Hands every note the daemon sends on to `deliver`, on a thread of its +/// own, until the link ends (`quit`). Called each loop: started once. +pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { + if (link < 0 or reader != null) return; + reader = std.Thread.spawn(.{}, readNotes, .{ link, ctx, deliver }) catch null; +} + +fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { + var buf: [msg_max]u8 = undefined; + // The end of the link (a daemon gone, a hello refused): not kept. + defer { + up.store(false, .release); + gone.store(true, .release); + } + while (recv(fd, &buf)) |got| { + var words = std.mem.tokenizeScalar(u8, got.words, ' '); + const verb = words.next() orelse ""; + if (std.mem.eql(u8, verb, "ok")) { + up.store(true, .release); + } else if (std.mem.eql(u8, verb, "attach")) { + deliver(ctx, .{ .attach = .{ + .tty = if (got.nfds >= 2) got.fds[0] else -1, + .stub = if (got.nfds >= 2) got.fds[1] else -1, + } }); + for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); + } else if (std.mem.eql(u8, verb, "replica")) { + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); + for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); + } else if (std.mem.eql(u8, verb, "adopted")) { + var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; + const a = ¬e.adopted; + for (got.fds[0..got.nfds]) |f| { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { + _ = libc.close(f); + continue; + }; + a.pids[a.n] = pid; + a.fds[a.n] = f; + a.n += 1; + } + deliver(ctx, note); + } else for (got.fds[0..got.nfds]) |f| { + _ = libc.close(f); + } + } +} + +fn writeAll(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) { + const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) return false; + at += @intCast(n); + } + return true; +} + +/// A replica refused by a session that takes none (a window's, so far). +pub fn refuseReplica(fd: c_int, why: []const u8) void { + var out: [600]u8 = undefined; + if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} + _ = libc.close(fd); +} + +/// A session ended by a signal (TERM, HUP, INT): its shells go with it, as +/// at a quit. Async-signal-safe: one send. One killed outright (KILL) or +/// crashed says nothing, and its shells live on for an Adopt. +pub fn hangUp() void { + if (link >= 0) _ = send(link, "quit", &.{}); +} + +/// The session's end: its masters are let go (its shells hang up), the +/// link closed, the reader joined. +pub fn quit() void { + if (link < 0) return; + tell("quit", &.{}); + _ = libc.shutdown(link, 2); + if (reader) |r| r.join(); + reader = null; + _ = libc.close(link); + link = -1; +} + +// ---- `pardes --attach`: a terminal, or a window, for a detached session ---- + +pub const Answer = union(enum) { ok: libc.pid_t, no: []const u8 }; + +/// Asks the daemon to hand `fds` (a terminal and a stub's pipe, or none for +/// a window) to the detached session `named` (any one, when empty), and +/// answers how it went; the words are `buf`'s. +pub fn attach(k: Kind, named: []const u8, fds: []const c_int, buf: []u8) Answer { + const fd = connect(false) orelse return .{ .no = "no detached session is running" }; + defer _ = libc.close(fd); + var line: [256]u8 = undefined; + const words = std.fmt.bufPrint(&line, "attach {t} {s}", .{ k, named }) catch return .{ .no = "that name is too long" }; + if (!send(fd, words, fds) or !readable(fd, 2000)) return .{ .no = "the daemon does not answer" }; + const got = recv(fd, buf) orelse return .{ .no = "the daemon hung up" }; + for (got.fds[0..got.nfds]) |f| _ = libc.close(f); + if (std.mem.startsWith(u8, got.words, "ok ")) { + const pid = std.fmt.parseInt(libc.pid_t, got.words[3..], 10) catch return .{ .no = "the daemon answered nonsense" }; + return .{ .ok = pid }; + } + return .{ .no = if (std.mem.startsWith(u8, got.words, "no ")) got.words[3..] else got.words }; +} + +/// The daemon's answer to `words` (`list`, `kill <name>`, `killall`), in +/// `buf`; null with no daemon running, or none answering. +pub fn ask(words: []const u8, buf: []u8) ?[]const u8 { + const fd = connect(false) orelse return null; + defer _ = libc.close(fd); + if (!send(fd, words, &.{}) or !readable(fd, 2000)) return null; + const got = recv(fd, buf) orelse return null; + for (got.fds[0..got.nfds]) |f| _ = libc.close(f); + return got.words; +} + +// ---- a terminal's stub ---- + +var stub_child: libc.pid_t = 0; + +fn forward(sig: posix.SIG) callconv(.c) void { + if (stub_child > 0) _ = libc.kill(stub_child, sig); + // The terminal's hangup: the session, told, carries on detached, and + // this job ends as a hung-up one does. + if (sig == .HUP) { + const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 }; + posix.sigaction(.HUP, &default, null); + _ = libc.raise(.HUP); + } +} + +/// The job the shell waits on, for a session that is not its child any +/// more (`pardes --attach`) or that it forked (`child`, reaped here): what +/// a terminal sends the job goes on to the session (its size, an end +/// asked for), and it ends when the session is gone from the terminal -- +/// detached (`d<name>`), exited (`x<code>`, or the pipe's end and the +/// child's status), or refused (`r<why>`). +pub fn stubWait(session: libc.pid_t, child: bool, pipe: c_int) noreturn { + stub_child = session; + const on: posix.Sigaction = .{ .handler = .{ .handler = forward }, .mask = posix.sigemptyset(), .flags = posix.SA.RESTART }; + for ([_]posix.SIG{ .WINCH, .TERM, .INT, .QUIT, .HUP }) |sig| posix.sigaction(sig, &on, null); + // Ctrl-Z is a key in the raw terminal (the pane's), as before; a TSTP + // sent here would stop the job under a session still drawing, so it is + // not taken. + const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; + posix.sigaction(.TSTP, &ignore, null); + var buf: [512]u8 = undefined; + var len: usize = 0; + while (len < buf.len) { + const n = libc.read(pipe, buf[len..].ptr, buf.len - len); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) break; + len += @intCast(n); + } + const said = buf[0..len]; + const out = std.Io.File.stderr(); + const io = std.Io.Threaded.global_single_threaded.io(); + if (said.len > 0 and said[0] == 'd') { + var text: [700]u8 = undefined; + const who = said[1..]; + out.writeStreamingAll(io, std.fmt.bufPrint(&text, "pardes: detached from '{s}', which is still running; come back with `pardes --attach={s}`\n", .{ who, who }) catch "pardes: detached\n") catch {}; + libc._exit(0); + } + if (said.len > 0 and said[0] == 'r') { + var text: [700]u8 = undefined; + out.writeStreamingAll(io, std.fmt.bufPrint(&text, "pardes: --attach: {s}\n", .{said[1..]}) catch "pardes: --attach refused\n") catch {}; + libc._exit(1); + } + if (child) { + var status: c_int = 0; + while (libc.waitpid(session, &status, 0) < 0) { + if (libc.errno(-1) != .INTR) libc._exit(1); + } + const s: u32 = @bitCast(status); + if (libc.W.IFEXITED(s)) libc._exit(@intCast(libc.W.EXITSTATUS(s))); + if (libc.W.IFSIGNALED(s)) { + const sig = libc.W.TERMSIG(s); + const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 }; + posix.sigaction(sig, &default, null); + _ = libc.raise(sig); + } + libc._exit(1); + } + if (said.len > 0 and said[0] == 'x') libc._exit(std.fmt.parseInt(u8, said[1..], 10) catch 1); + out.writeStreamingAll(io, "pardes: the session ended without a word\n") catch {}; + libc._exit(1); +} + +/// The session's half of its stub: what `stubWait` reads. +pub var stub: c_int = -1; + +extern "c" fn procctl(idtype: c_int, id: i64, cmd: c_int, data: ?*anyopaque) c_int; + +/// A HUP when this process's parent dies (a forked session's stub, an +/// --attach client's ssh), or no longer. Linux keeps it per thread: set and +/// cleared on the main one. +pub fn hupOnStubDeath(on: bool) void { + var sig: c_int = if (on) @backingInt(posix.SIG.HUP) else 0; + switch (builtin.os.tag) { + .linux => _ = std.os.linux.prctl(@backingInt(std.os.linux.PR.SET_PDEATHSIG), @intCast(sig), 0, 0, 0), + // procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig) + .freebsd => _ = procctl(0, 0, 11, &sig), + else => {}, + } +} + +/// Detached: the stub is told (and goes), and the pipe let go. Its end is +/// no hangup now (hupOnStubDeath). +pub fn tellStubDetached() void { + if (stub < 0) return; + hupOnStubDeath(false); + tellDetached(stub); + stub = -1; +} + +/// The stub on `fd` told its terminal is detached (one of a session's +/// several, views.zig), and the pipe let go. +pub fn tellDetached(fd: c_int) void { + if (fd < 0) return; + var buf: [160]u8 = undefined; + const said = std.fmt.bufPrint(&buf, "d{s}", .{name()}) catch "d"; + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); +} + +/// The session's end, `code` its exit status: the stub is told (and goes). +pub fn tellStubExit(code: u8) void { + tellExit(stub, code); + stub = -1; +} + +pub fn tellExit(fd: c_int, code: u8) void { + if (fd < 0) return; + var buf: [8]u8 = undefined; + const said = std.fmt.bufPrint(&buf, "x{d}", .{code}) catch "x1"; + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); +} + +/// A terminal refused (an attach that came too late): its stub is told why. +pub fn refuseStub(fd: c_int, why: []const u8) void { + var buf: [256]u8 = undefined; + const said = std.fmt.bufPrint(&buf, "r{s}", .{why}) catch "r"; + _ = libc.write(fd, said.ptr, said.len); + _ = libc.close(fd); +} + +/// `pardes` in a terminal: the session forked off as a process of its own, +/// in a session of its own (setsid), which is what lets it outlive the +/// terminal's job, and this process left as its stub (`stubWait`). Returns +/// in the session; null (nothing forked) when it cannot be done, and the +/// session then runs here, as before the daemon. +pub fn forkSession(conn: c_int) ?void { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return null; + ninep_io.setCloexec(pipe[0]); + ninep_io.setCloexec(pipe[1]); + const parent = libc.getpid(); + const pid = libc.fork(); + if (pid < 0) { + _ = libc.close(pipe[0]); + _ = libc.close(pipe[1]); + return null; + } + if (pid == 0) { + _ = libc.close(pipe[0]); + _ = libc.setsid(); + // The stub's death, however it comes (a SIGKILL too), is a HUP: + // the terminal's end, which the session's signal thread takes and + // detaches on. One already dead hangs up at once. + hupOnStubDeath(true); + if (libc.getppid() != parent) _ = libc.raise(.HUP); + stub = pipe[1]; + // The listener and lock are the daemon's, which the stub starts. + startPending(false); + return {}; + } + _ = libc.close(pipe[1]); + _ = libc.close(conn); + // Off the session's way: it is drawing already, its hello queued. + startPending(true); + stubWait(pid, true, pipe[0]); +} + +// ---- the daemon ---- + +const Conn = struct { + fd: c_int = -1, + kind: ?Kind = null, + pid: libc.pid_t = 0, + detached: bool = false, + /// Its build's wire.semantics: a replica's must be the same. + semantics: u64 = 0, + /// `pardes --kill` asked its end: its shells go with it, not kept. + killed: bool = false, + name: [128]u8 = undefined, + name_len: usize = 0, + + fn named(c: *const Conn) []const u8 { + return c.name[0..c.name_len]; + } +}; + +const Held = struct { + fd: c_int = -1, + pid: libc.pid_t = 0, + /// The conn whose session holds it; null: that session died with it. + owner: ?u8 = null, +}; + +/// A replica's connection until its hello is whole: read no further than +/// the hello, so what follows it is the session's to read. +const Replica = struct { + fd: c_int = -1, + buf: [wire.hello_max]u8 = undefined, + len: usize = 0, +}; + +const Daemon = struct { + conns: [max_conns]Conn = @splat(.{}), + held: [max_held]Held = @splat(.{}), + replicas: [max_replicas]Replica = @splat(.{}), + /// 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; + for (d.held) |h| if (h.fd >= 0) return false; + return true; + } + + fn drop(d: *Daemon, h: *Held) void { + _ = d; + _ = libc.close(h.fd); + h.* = .{}; + } + + fn hangUp(d: *Daemon, i: u8) void { + const c = &d.conns[i]; + const killed = c.killed; + _ = libc.close(c.fd); + c.* = .{}; + // Whatever it still held, it died holding: kept for an Adopt, unless + // it was killed on purpose, when its shells hang up with it. + for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) { + if (killed) d.drop(h) else h.owner = null; + }; + } + + /// `pardes --kill`: each session named (every one, for null) sent a + /// TERM and marked, its pids answered; the shells of none kept, for + /// all of them. + fn kill(d: *Daemon, i: u8, named: ?[]const u8) void { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll("ok") catch {}; + var n: usize = 0; + for (&d.conns) |*s| if (s.fd >= 0 and s.kind != null) { + if (named) |want| if (!std.mem.eql(u8, s.named(), want)) continue; + s.killed = true; + _ = libc.kill(s.pid, .TERM); + w.print(" {d}", .{s.pid}) catch {}; + n += 1; + }; + if (named == null) { + for (&d.held) |*h| if (h.fd >= 0 and h.owner == null) d.drop(h); + } else if (n == 0) { + var why: [256]u8 = undefined; + return d.reply(i, std.fmt.bufPrint(&why, "no no session called '{s}'", .{named.?}) catch "no no such session", &.{}); + } + d.reply(i, w.buffered(), &.{}); + } + + /// `pardes --ps`: a line a session (pid, kind, state, name), a line + /// for each shell it holds after it, then the shells nobody holds. + /// ponytail: one message, so a registry past msg_max is cut short. + fn list(d: *Daemon, i: u8) void { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.print("daemon {d}\n", .{libc.getpid()}) catch {}; + for (&d.conns, 0..) |*s, k| if (s.fd >= 0 and s.kind != null) { + w.print("session {d} {t} {s} {s}\n", .{ s.pid, s.kind.?, if (s.detached) "detached" else "attached", s.named() }) catch break; + for (d.held) |h| if (h.fd >= 0 and h.owner == @as(u8, @intCast(k))) w.print("shell {d}\n", .{h.pid}) catch break; + }; + for (d.held) |h| if (h.fd >= 0 and h.owner == null) w.print("orphan {d}\n", .{h.pid}) catch break; + d.reply(i, w.buffered(), &.{}); + } + + fn reply(d: *Daemon, i: u8, words: []const u8, fds: []const c_int) void { + if (!send(d.conns[i].fd, words, fds)) d.hangUp(i); + } + + fn take(d: *Daemon, i: u8, got: Got) void { + var fds_used: usize = 0; + defer for (got.fds[fds_used..got.nfds]) |f| { + _ = libc.close(f); + }; + const c = &d.conns[i]; + var words = std.mem.tokenizeScalar(u8, got.words, ' '); + const verb = words.next() orelse return; + if (std.mem.eql(u8, verb, "hello")) { + const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const state = words.next() orelse ""; + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + if (v != version or k == null or pid <= 0) { + // Another build's: it runs without a daemon. + d.reply(i, "version " ++ std.fmt.comptimePrint("{d}", .{version}), &.{}); + return if (d.conns[i].fd >= 0) d.hangUp(i); + } + c.kind = k; + c.pid = pid; + c.detached = std.mem.eql(u8, state, "detached"); + c.semantics = semantics; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + return d.reply(i, "ok", &.{}); + } + if (std.mem.eql(u8, verb, "state")) { + c.detached = std.mem.eql(u8, words.next() orelse "", "detached"); + return; + } + if (std.mem.eql(u8, verb, "pty")) { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; + if (got.nfds < 1) return; + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = got.fds[0], .pid = pid, .owner = i }; + fds_used = 1; + return; + }; + return; + } + if (std.mem.eql(u8, verb, "close")) { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch return; + for (&d.held) |*h| if (h.fd >= 0 and h.pid == pid and h.owner == i) d.drop(h); + return; + } + if (std.mem.eql(u8, verb, "quit")) { + 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, "list")) return d.list(i); + if (std.mem.eql(u8, verb, "kill")) return d.kill(i, std.mem.trim(u8, words.rest(), " ")); + if (std.mem.eql(u8, verb, "killall")) return d.kill(i, null); + if (std.mem.eql(u8, verb, "adopt")) { + var line: [msg_max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll("adopted") catch {}; + var fds: [max_fds]c_int = undefined; + var given: [max_fds]*Held = undefined; + var n: usize = 0; + for (&d.held) |*h| if (h.fd >= 0 and h.owner == null and n < max_fds) { + w.print(" {d}", .{h.pid}) catch break; + fds[n] = h.fd; + given[n] = h; + n += 1; + }; + if (!send(c.fd, w.buffered(), fds[0..n])) return d.hangUp(i); + for (given[0..n]) |h| h.owner = i; + return; + } + if (std.mem.eql(u8, verb, "attach")) { + const k = std.meta.stringToEnum(Kind, words.next() orelse "") orelse return d.reply(i, "no bad attach", &.{}); + const wanted_name = std.mem.trim(u8, words.rest(), " "); + var why: [512]u8 = undefined; + const target = d.find(k, wanted_name, &why) catch |err| return d.reply(i, switch (err) { + error.Said => std.mem.sliceTo(&why, 0), + }, &.{}); + const t = &d.conns[target]; + if (!send(t.fd, "attach", got.fds[0..got.nfds])) { + d.hangUp(target); + return d.reply(i, "no that session does not answer", &.{}); + } + t.detached = false; + var line: [32]u8 = undefined; + return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch "ok 0", &.{}); + } + } + + /// The detached session of kind `k` called `wanted_name`, or the one + /// there is when it is empty; else why not, in `why` (0-terminated). + fn find(d: *Daemon, k: Kind, wanted_name: []const u8, why: *[512]u8) error{Said}!u8 { + @memset(why, 0); + const other: []const u8 = switch (k) { + .tty => "a window: attach it with `pardes-gui --attach", + .gui => "a terminal session: attach it with `pardes --attach", + }; + if (wanted_name.len != 0) { + for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind != null and std.mem.eql(u8, s.named(), wanted_name)) { + if (s.kind.? != k) { + _ = std.fmt.bufPrint(why, "no '{s}' is {s}={s}`", .{ wanted_name, other, wanted_name }) catch {}; + return error.Said; + } + // Attached already: the session takes another view. + return @intCast(i); + }; + _ = std.fmt.bufPrint(why, "no no session called '{s}'", .{wanted_name}) catch {}; + return error.Said; + } + var found: ?u8 = null; + var count: usize = 0; + var others: usize = 0; + var w: std.Io.Writer = .fixed(why[0 .. why.len - 1]); + for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.detached and s.kind != null) { + if (s.kind.? != k) { + others += 1; + continue; + } + count += 1; + found = @intCast(i); + }; + if (count == 1) return found.?; + // None detached: the one session there is, as another view of it. + if (count == 0) { + for (&d.conns, 0..) |*s, i| if (s.fd >= 0 and s.kind == k) { + count += 1; + found = @intCast(i); + }; + if (count == 1) return found.?; + count = 0; + } + if (count == 0 and others == 0) { + w.writeAll("no no detached session is running") catch {}; + return error.Said; + } + if (count == 0) { + w.print("no the detached session{s} running {s} {s}=<name>`", .{ if (others == 1) " is" else "s are", if (others == 1) "is" else "are each", other }) catch {}; + return error.Said; + } + w.print("no {d} detached sessions are running; say which with --attach=<name>:", .{count}) catch {}; + for (&d.conns) |*s| if (s.fd >= 0 and s.detached and s.kind == k) { + w.print(" {s}", .{s.named()}) catch {}; + }; + return error.Said; + } + + /// More of a replica's hello; once it is whole, the connection refused + /// in words or handed to its session. + fn hear(d: *Daemon, r: *Replica) void { + const want = if (r.len < wire.header_len) wire.header_len else wire.header_len + @as(usize, std.mem.readInt(u32, r.buf[1..5], .little)); + if (want > r.buf.len) return refuse(r, "the first message is not a hello"); + const n = libc.read(r.fd, r.buf[r.len..want].ptr, want - r.len); + if (n < 0 and libc.errno(n) == .INTR) return; + if (n <= 0) { + _ = libc.close(r.fd); + r.* = .{}; + return; + } + r.len += @intCast(n); + if (r.len < wire.header_len or r.len < wire.header_len + std.mem.readInt(u32, r.buf[1..5], .little)) return; + const f = (wire.framed(r.buf[0..r.len]) catch null) orelse return refuse(r, "the first message is not a hello"); + if (f.tag != @backingInt(wire.ClientTag.hello)) return refuse(r, "the first message is not a hello"); + var why: [512]u8 = undefined; + const v = wire.helloVersion(f.payload) catch 0; + if (v != wire.version) return refuse(r, std.fmt.bufPrint(&why, "this client speaks replica protocol {d}, and this daemon {d}: run the same pardes at both ends", .{ v, wire.version }) catch "another protocol"); + const h = (wire.decodeClient(f.tag, f.payload) catch return refuse(r, "a malformed hello")).hello; + const target = for (&d.conns, 0..) |*c, i| { + if (c.fd >= 0 and c.kind != null and std.mem.eql(u8, c.named(), h.session)) break i; + } else return refuse(r, std.fmt.bufPrint(&why, "no session called '{s}'", .{h.session}) catch "no such session"); + const t = &d.conns[target]; + if (t.semantics != h.semantics) return refuse(r, std.fmt.bufPrint(&why, "session '{s}' runs another build of pardes (semantics {x:0>16}, this client's {x:0>16}): a replica needs the same build at both ends, as `pardes --version` shows", .{ h.session, t.semantics, h.semantics }) catch "another build"); + var words: [16]u8 = undefined; + const note = if (h.view) |k| std.fmt.bufPrint(&words, "replica {d}", .{k}) catch unreachable else "replica"; + if (!send(t.fd, note, &.{r.fd})) { + d.hangUp(@intCast(target)); + return refuse(r, "that session does not answer"); + } + log.info("a replica handed to '{s}'", .{h.session}); + _ = libc.close(r.fd); + r.* = .{}; + } + + fn refuse(r: *Replica, why: []const u8) void { + var out: [600]u8 = undefined; + // A fresh connection's buffer is empty: this never blocks. + if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {} + _ = libc.close(r.fd); + r.* = .{}; + } + + fn acceptReplica(d: *Daemon, listener: c_int) void { + const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); + if (fd < 0) return; + for (&d.replicas) |*r| if (r.fd < 0) { + r.* = .{ .fd = fd }; + return; + }; + _ = libc.close(fd); + } + + /// 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; + for (&d.conns) |*c| if (c.fd < 0) { + c.* = .{ .fd = fd }; + return; + }; + _ = libc.close(fd); + } +}; + +/// The replicas' stream socket, bound and listening; -1 when it cannot be +/// (a runtime directory too long for a socket's path: 104 bytes on FreeBSD). +/// Ours to replace: the daemon's lock is held. +fn listenReplicas() c_int { + var sock_buf: [sun_path_len:0]u8 = undefined; + const sock = place(&sock_buf, "pardes-replica.sock") orelse return -1; + _ = libc.unlink(sock); + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (fd < 0) return -1; + const addr = address(sock); + if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) == 0 and libc.chmod(sock, 0o600) == 0 and libc.listen(fd, max_replicas) == 0) return fd; + _ = libc.close(fd); + return -1; +} + +/// 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), 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); + upgrade.locate(); + 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; + var held_of: [slots]u16 = undefined; + var replica_of: [slots]u8 = undefined; + var buf: [msg_max]u8 = undefined; + while (true) { + var n: usize = 0; + fds[n] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 }; + n += 1; + // -1 when there is none: poll skips a negative fd. + fds[n] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 }; + n += 1; + const conns_at = n; + for (&d.conns, 0..) |*c, i| if (c.fd >= 0) { + fds[n] = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 }; + conn_of[n] = @intCast(i); + n += 1; + }; + const replicas_at = n; + for (&d.replicas, 0..) |*r, i| if (r.fd >= 0) { + fds[n] = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 }; + replica_of[n] = @intCast(i); + n += 1; + }; + const held_at = n; + // No events asked of a master: its hangup (every slave closed, the + // shell and all it ran gone) is reported anyway, and its output is + // the session's to read, never this one's. + for (&d.held, 0..) |*h, i| if (h.fd >= 0) { + fds[n] = .{ .fd = h.fd, .events = 0, .revents = 0 }; + held_of[n] = @intCast(i); + n += 1; + }; + const idle = d.idle(); + const ready = libc.poll(&fds, @intCast(n), if (idle) linger_ms else -1); + if (ready < 0) { + if (libc.errno(ready) == .INTR) continue; + break; + } + if (ready == 0) { + if (!idle) continue; + // Idle for the whole linger: one last look for a connection + // just made, then the socket goes, then this process (and its + // lock with it). + if (readable(listener, 0)) { + d.accept(listener); + continue; + } + break; + } + if (fds[0].revents & libc.POLL.IN != 0) d.accept(listener); + if (fds[1].revents & libc.POLL.IN != 0) d.acceptReplica(replicas); + for (fds[replicas_at..held_at], replica_of[replicas_at..held_at]) |pfd, i| { + if (pfd.revents == 0 or d.replicas[i].fd != pfd.fd) continue; + d.hear(&d.replicas[i]); + } + for (fds[conns_at..replicas_at], conn_of[conns_at..replicas_at]) |pfd, i| { + if (pfd.revents == 0 or d.conns[i].fd != pfd.fd) continue; + const got = recv(pfd.fd, &buf) orelse { + d.hangUp(i); + continue; + }; + d.take(i, got); + } + for (fds[held_at..n], held_of[held_at..n]) |pfd, i| { + 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); + if (place(&sock_buf, "pardes-daemon.sock")) |sock| _ = libc.unlink(sock); + if (replicas >= 0) { + if (place(&sock_buf, "pardes-replica.sock")) |sock| _ = libc.unlink(sock); + } + libc._exit(0); +} + +test "a message carries its words and its fds, and the peer's end is null" { + if (comptime !supported) return error.SkipZigTest; + var pair: [2]c_int = undefined; + try std.testing.expectEqual(@as(c_int, 0), libc.socketpair(libc.AF.UNIX, libc.SOCK.SEQPACKET | libc.SOCK.CLOEXEC, 0, &pair)); + defer _ = libc.close(pair[0]); + var pipe: [2]c_int = undefined; + try std.testing.expectEqual(@as(c_int, 0), libc.pipe(&pipe)); + defer _ = libc.close(pipe[0]); + try std.testing.expect(send(pair[1], "pty 42", &.{pipe[1]})); + _ = libc.close(pipe[1]); + var buf: [64]u8 = undefined; + const got = recv(pair[0], &buf).?; + try std.testing.expectEqualStrings("pty 42", got.words); + try std.testing.expectEqual(@as(usize, 1), got.nfds); + // The fd that came is the pipe's write end: what goes in comes out. + try std.testing.expectEqual(@as(isize, 1), libc.write(got.fds[0], "z", 1)); + _ = libc.close(got.fds[0]); + var one: [1]u8 = undefined; + try std.testing.expectEqual(@as(isize, 1), libc.read(pipe[0], &one, 1)); + _ = libc.close(pair[1]); + try std.testing.expect(recv(pair[0], &buf) == null); +} diff --git a/src/detached/client.zig b/src/detached/client.zig deleted file mode 100644 index 617ae342..00000000 --- a/src/detached/client.zig +++ /dev/null @@ -1,1406 +0,0 @@ -//! THE FRONTEND SIDE of a detached session: a socket, a grid, and no core. -//! -//! A FRONTEND IS INPUT AND SCREEN, and that is the whole of it. Keystrokes, -//! mouse, window size go out; frames come back and get painted. This is the -//! shape the ESP32-P4 serial console has always had — a panel and a keypad on -//! the far end of a wire, performing no effects of its own — and after the -//! machine-local IO moved into the daemon it is the shape EVERY frontend has, -//! terminal and window alike. -//! -//! THIS SIDE DOES NOT OWN A `Pardes`. There is no `update`, no `postEvent` and -//! no `Host` in this file. The core lives in the detached process (server.zig), -//! which is also where every `Host.VTable` call now lands: forking a pane's -//! shell, writing a file, watching a path and reading the theme directory are -//! all done BY THE DAEMON, against the same machine's kernel it shares with -//! this frontend over an AF_UNIX socket. NO FORK HAPPENS IN A FRONTEND ANY -//! MORE. That is not a simplification of this file, it is the fix: a pane's -//! shell used to be a child of whichever frontend forked it, so leaving killed -//! the shells of a session whose entire promise is outliving frontends. -//! -//! `Client` is NOT a renderer either. It owns `grid` and `cursor`: the cells the -//! session is showing, kept current by applying frames as they arrive. Whoever -//! owns a terminal (or a window, or an ESP32-P4 panel) reads those and paints. -//! The split is deliberate — pardes already has terminal frontends, and a second -//! one living in here would be a second convention for a job that has one. -//! -//! THE ATTACH IS NOT A BLOCKING HANDSHAKE. `open` connects and writes the -//! `hello`; the `welcome` (or the `refuse`) arrives through the ordinary -//! `wait`/`next` loop like everything else. A frontend therefore has one loop -//! and one place it sleeps, instead of a startup path that can hang for two -//! seconds before its terminal is even in raw mode. `attached()` says whether -//! the session has greeted us; `refusal` says why it did not. -//! -//! WHAT ARRIVES. `next` hands back one decoded `wire.ServerMsg` at a time, and -//! there are exactly seven of them: -//! * `welcome` and `frame` have already been applied to `slot`/`cols`/`rows`/ -//! `grid`/`cursor` by the time you see them. They are returned so a frontend -//! knows the screen moved. -//! * `set_clipboard`, `read_clipboard` and `open_link` are the only effects -//! still on the wire, and they are here because each needs THIS HUMAN'S -//! DISPLAY: a daemon nobody is looking at has no clipboard and no browser. -//! The answer to `read_clipboard` is not a reply message: it is an ordinary -//! `Event.paste` sent back through `send`, which is the same asynchronous -//! shape `read_clipboard` already has in-process. -//! * `refuse` is followed by the session closing the connection, and `quit` -//! means the session itself has ended. -//! The switch in `next` is exhaustive over that set on purpose: putting a -//! machine-local effect back on the wire is a compile error here, and the test -//! at the bottom of this file says so in the other direction too. -//! -//! GEOMETRY. `cols`/`rows` are the SESSION's grid, which with several frontends -//! attached is the smallest common one and can be smaller than this frontend's -//! window. Tell the session about the window with `resize`; do not assume the -//! next frame will agree with it. -//! -//! BORROWED BYTES. Every slice in a returned `ServerMsg` points into this -//! client's receive buffer and is valid until the next call to `next` or -//! `wait`. A frontend that needs a payload for longer copies it — the same rule -//! the core's own `Event.output` bytes have. -const std = @import("std"); -const libc = std.c; -const pardes = @import("../pardes.zig"); -const dump = @import("../dump.zig"); -const server = @import("server.zig"); -const wire = @import("wire.zig"); - -const read_chunk = 16 * 1024; - -pub const Error = error{ - /// No `$XDG_RUNTIME_DIR` and no `$HOME`, or a name that is not one path - /// component — there is no socket path to try. - NoSessionPath, - /// Nothing is listening there: the name is wrong, or that session ended. - /// Its socket file, if it is still lying about, is unlinked by the sweep the - /// next detached session runs. - NoSession, - /// The socket is there and this frontend will not talk to it: the directory - /// or the socket is not a private one of ours (server.zig `vetted`). A - /// planted socket at a derivable path collects every keystroke typed into - /// the frontend that trusts it, so this is refused rather than reported as - /// "no session" — the two need different answers from a human. - NotPrivate, - /// The session hung up, or the connection failed under us. Every read and - /// write path funnels here: a frontend's answer to all of them is the same - /// (report and exit), so distinguishing them would be a distinction nobody - /// acts on. - Closed, - /// The session said something before it said `welcome`. - Ungreeted, -}; - -pub const Client = struct { - gpa: std.mem.Allocator, - fd: c_int = -1, - /// Which client slot the session gave this connection. Diagnostics only, - /// and it exists so both sides print the same number. - slot: u8 = 0, - /// Set by a `refuse`, and the reason a frontend prints before exiting. - refusal: ?wire.Refusal = null, - /// The SESSION's grid, not this frontend's window. Zero until the welcome. - cols: u16 = 0, - rows: u16 = 0, - /// `cols * rows` cells: what the session is showing right now. - grid: std.ArrayListUnmanaged(pardes.Cell) = .empty, - cursor: ?wire.Cursor = null, - pointer_shape: pardes.Surface.PointerShape = .arrow, - body_layers: [pardes.MAX_PANES]pardes.Layer = @splat(.{}), - tag_layers: [pardes.MAX_TAG_LAYERS]pardes.Layer = @splat(.{}), - /// The session's placed regions, as its last frame gave them. - regions: std.ArrayListUnmanaged(pardes.Region) = .empty, - /// The palette its chrome is drawn in, as its last frame gave it. - chrome: pardes.Chrome = .{}, - /// The post chain its frames are drawn through, as the session last - /// sent it; its paths and SPIR-V owned here. `post_key` moves with it. - post: wire.Post = .{}, - post_key: u32 = 0, - in: std.ArrayListUnmanaged(u8) = .empty, - out: std.ArrayListUnmanaged(u8) = .empty, - /// Bytes of `in` belonging to the message `next` returned last. Compacted at - /// the top of the next call, which is exactly what makes that message's - /// borrowed slices valid until then and no longer. - held: usize = 0, - - /// Connect to the session called `name` and say hello, telling it this - /// frontend's window. Does not wait: the greeting arrives through `next`. - pub fn open(gpa: std.mem.Allocator, name: []const u8, cols: u16, rows: u16) (Error || wire.Error)!Client { - var path_buf: [server.path_max]u8 = undefined; - const path = server.sessionPath(&path_buf, name) orelse return error.NoSessionPath; - // `vetted` is one predicate over two different failures, and a human is - // owed different words for them: `NotPrivate` promises, in its own doc - // above, that the socket IS there. So ask the cheap question first, - // because the commonest failure of all is a mistyped session name — - // until this, `Attach nosuchsession` put "attach: NotPrivate" on the - // message row, which reads as an accusation rather than a typo. Spelled - // with the `access` this file already uses in `resolve`. - const F_OK: c_int = 0; - if (libc.access(path, F_OK) != 0) return error.NoSession; - // Both ends vet, and this is this end's half: the session refuses a - // directory or a socket anyone else can reach before it binds, and until - // this a frontend connected to whatever it found at the path it derived. - // See server.zig `vetted` for what is asked and why it is asked there. - if (!server.vetted(path)) return error.NotPrivate; - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); - if (fd < 0) return error.NoSession; - // CLOEXEC anyway, even though a frontend no longer forks pane shells: - // `open_link` runs this display's browser, and a `xdg-open` inheriting - // this socket would keep the session believing a frontend is attached - // long after this process left. - server.setCloexec(fd); - if (comptime server.darwin) { - // linux says MSG_NOSIGNAL per write and darwin says it once per - // socket, which is what `nosignal` being 0 on darwin MEANS — so - // this call is the whole of that platform's protection and the - // comment on `nosignal` used to say `open` made it without `open` - // making it. A session that ends mid-write must not take the - // frontend down with SIGPIPE. server.zig `accept` is the mirror. - const on: c_int = 1; - _ = libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)); - } - // Still blocking for the connect itself, which on AF_UNIX either lands - // in the listener's backlog immediately or is refused; there is no - // in-progress state to poll for. - if (libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) { - _ = libc.close(fd); - return error.NoSession; - } - _ = server.setNonblock(fd); - var c: Client = .{ .gpa = gpa, .fd = fd }; - errdefer c.deinit(); - try c.send(.{ .hello = .{ - .cols = @min(cols, wire.max_cols), - .rows = @min(rows, wire.max_rows), - } }); - return c; - } - - /// Has the session greeted us? Until it has, `grid` is empty and nothing - /// has been drawn. - pub fn attached(c: *const Client) bool { - return c.cols != 0; - } - - pub fn deinit(c: *Client) void { - if (c.fd >= 0) _ = libc.close(c.fd); - c.fd = -1; - c.grid.deinit(c.gpa); - for (&c.body_layers) |*layer| { - c.gpa.free(layer.cells); - layer.* = .{}; - } - for (&c.tag_layers) |*layer| c.gpa.free(layer.cells); - c.regions.deinit(c.gpa); - c.freePost(); - c.in.deinit(c.gpa); - c.out.deinit(c.gpa); - } - - /// Leave without ending the session. The `bye` is a courtesy — the session - /// handles a frontend that simply dies, and proving it does is what that - /// test is for — but it turns "the peer vanished" into "the peer left" in - /// the session's log, which is worth seven bytes. - pub fn detach(c: *Client) void { - c.send(.bye) catch {}; - c.deinit(); - } - - /// One message on its way to the core. Everything a frontend collects goes - /// through here, and after the IO moved into the daemon that is a short - /// list: keys, the mouse, a window resize, and the paste that answers a - /// `read_clipboard`. `ClientMsg.output`/`.eof` still exist on the wire but - /// no frontend sends them any more — pty bytes are read by the process that - /// forked the shell, which is the daemon. - pub fn send(c: *Client, msg: wire.ClientMsg) (Error || wire.Error)!void { - const want = wire.clientBound(msg); - c.out.ensureUnusedCapacity(c.gpa, want) catch return error.Closed; - const at = c.out.items.len; - // Encoded straight into the queue's tail rather than through a scratch - // buffer: a paste is four megabytes and copying it twice is two copies. - c.out.items.len += want; - const bytes = wire.encodeClient(c.out.items[at..], msg) catch |err| { - // AND THE QUEUE GOES BACK. `want` bytes of it are uninitialised - // right now, and leaving them there — which is what a bare `try` - // did — puts that much stack-shaped garbage on the socket at the - // next flush: the session decodes it as a message, refuses it, and - // drops a frontend whose only mistake was a message this protocol - // cannot carry (an `Event.command` past 64 KiB is `Overlong`, and a - // caller that mis-sized the queue is `WriteFailed`). - c.out.items.len = at; - return err; - }; - c.out.items.len = at + bytes.len; - return c.flush(); - } - - /// Tell the session this frontend's window changed. Not a promise about the - /// next frame: with other frontends attached the session grid is the - /// smallest common one. - /// - /// Clamped, like the hello in `open`: a window past `max_cols`/`max_rows` - /// is a geometry the protocol cannot carry, and the decoder on the far end - /// answers one with `BadValue` — which `apply` turns into `close(.protocol)` - /// with no `refuse` behind it, so the frontend was told only that the - /// session "hung up on the connect". A 4K display at a small font is - /// already past `max_rows`, which made an ordinary big screen unable to - /// attach at all. Asking for the largest grid the wire carries is what this - /// file's GEOMETRY note already promises a frontend gets: the session is - /// drawn at ITS size wherever the window is bigger, exactly as it is when - /// another frontend is the smaller one. - /// - /// A ZERO is dropped rather than clamped, and that asymmetry is the whole - /// point of `getCols` refusing zero in the first place: the session grid is - /// the smallest common one, so a frontend that reported 1 would collapse - /// every other frontend to a single cell. `TIOCGWINSZ` answers 0x0 while a - /// terminal is being torn down and tty.zig forwards a `winsize` verbatim - /// (its ATTACH path already refuses a zero one, its resize path did not), - /// so this is reachable without a hostile peer — and it reached the session - /// as the same mute `close(.protocol)` the oversize geometry did. Keeping - /// the last real window is what a momentary zero means. - pub fn resize(c: *Client, cols: u16, rows: u16) (Error || wire.Error)!void { - if (cols == 0 or rows == 0) return; - return c.send(.{ .event = .{ .resize = .{ - .cols = @min(cols, wire.max_cols), - .rows = @min(rows, wire.max_rows), - } } }); - } - - /// Wait up to `timeout_ms` for the session to say something, and push - /// whatever we still owe it. Zero blocks. This is the frontend's one - /// sleeping place FOR THE SOCKET; the terminal it draws on is polled by - /// whoever owns that, which is why this takes a timeout rather than a second - /// descriptor. - pub fn wait(c: *Client, timeout_ms: u32) (Error || wire.Error)!void { - if (c.fd < 0) return error.Closed; - try c.flush(); - var fds: [1]libc.pollfd = .{.{ - .fd = c.fd, - .events = if (c.out.items.len != 0) poll_in | poll_out else poll_in, - .revents = 0, - }}; - const timeout: c_int = if (timeout_ms == 0) -1 else @intCast(@min(timeout_ms, std.math.maxInt(c_int))); - if (libc.poll(&fds, 1, timeout) <= 0) return; // a timeout, or EINTR - if (fds[0].revents & poll_out != 0) try c.flush(); - // POLLIN wins over POLLHUP: a session that wrote a `quit` and then - // closed has bytes worth reading. - if (fds[0].revents & poll_in != 0) return c.fill(); - if (fds[0].revents & (poll_hup | poll_err | poll_nval) != 0) return error.Closed; - } - - /// The next complete message, or null when the buffer holds only part of - /// one. `welcome` and `frame` have already been applied to this client's - /// own state; every slice in the result borrows the receive buffer until the - /// next call here or to `wait`. - pub fn next(c: *Client) (Error || wire.Error)!?wire.ServerMsg { - // Retire the message returned last, now that its borrow window is over. - if (c.held != 0) { - if (c.held == c.in.items.len) { - c.in.clearRetainingCapacity(); - } else { - std.mem.copyForwards(u8, c.in.items, c.in.items[c.held..]); - c.in.items.len -= c.held; - } - c.held = 0; - } - const found = (try wire.framed(c.in.items)) orelse return null; - const msg = try wire.decodeServer(found.tag, found.payload); - c.held = found.total; - switch (msg) { - .welcome => |v| { - // Both sides check the version. This side checks it too because - // a session speaking something else may not have recognised our - // hello as one either, and a frontend must not paint a frame it - // decoded by a layout the other end does not use. - if (v.version != wire.version) return error.Ungreeted; - c.slot = v.slot; - try c.reshape(v.cols, v.rows); - }, - .refuse => |why| c.refusal = why, - .frame => |f| { - // A frame before the greeting would be the session drawing for a - // connection it never accepted. - if (!c.attached()) return error.Ungreeted; - // A geometry change and a late attach are the same case on this - // side too: reshape, and require the FULL frame the session - // promises for it. Applying a diff to a grid we just cleared - // would leave every untouched cell blank. - if (f.cols != c.cols or f.rows != c.rows) { - if (f.kind != .full) return error.BadValue; - try c.reshape(f.cols, f.rows); - } - try f.apply(c.grid.items); - f.applyLayers(c.gpa, &c.body_layers, &c.tag_layers, &c.regions, &c.chrome) catch |err| return switch (err) { - error.OutOfMemory => error.Closed, - else => @as(wire.Error, @errorCast(err)), - }; - c.cursor = f.cursor; - c.pointer_shape = f.pointer_shape; - }, - // The session has ended. Left for the caller to act on, and the - // descriptor stays open so `deinit` is the only place that closes. - .quit => {}, - // THIS frontend is done and the session is not. Same shape as - // `quit` and the same non-answer here — the caller leaves its loop - // — but the opposite meaning about what survives, so a frontend - // must not collapse the two: `quit` is the session ending and - // `detach` is success. Guarded, because a peer that has not greeted - // us has no standing to dismiss us either. - .detach => if (!c.attached()) return error.Ungreeted, - // The three display effects need this process's clipboard and this - // process's browser, so they are the caller's to perform and there - // is nothing for a `Client` to update. Listed rather than swept up - // by an `else`: an `else` here would silently accept a - // machine-local effect returning to the wire, and the point of the - // rewrite is that it cannot. - // - // Guarded like `.frame`, and for a stronger reason than drawing: - // these reach the human's clipboard and the human's browser. The - // protocol version is checked in the `.welcome` arm above and - // nowhere else, so a peer that simply never greets us has had its - // version checked by nobody — and until it does, it does not get to - // open a URI on this display or read this display's selection back - // over the socket. `Ungreeted` is the same refusal a frame gets. - .set_clipboard, .read_clipboard, .open_link => if (!c.attached()) return error.Ungreeted, - // Kept, like the chrome: the frames that follow are drawn - // through it. - .post => |chain| { - if (!c.attached()) return error.Ungreeted; - c.freePost(); - c.post = chain; - for (c.post.passes[0..c.post.len]) |*pass| { - pass.path = c.gpa.dupe(u8, pass.path) catch return error.Closed; - pass.spirv = c.gpa.dupe(u8, pass.spirv) catch return error.Closed; - } - c.post_key +%= 1; - }, - } - return msg; - } - - fn freePost(c: *Client) void { - for (c.post.passes[0..c.post.len]) |pass| { - c.gpa.free(pass.path); - c.gpa.free(pass.spirv); - } - c.post = .{}; - } - - // ---- internals -------------------------------------------------------- - - fn reshape(c: *Client, cols: u16, rows: u16) (Error || wire.Error)!void { - c.grid.resize(c.gpa, @as(usize, cols) * @as(usize, rows)) catch return error.Closed; - // Unpainted, which a frontend draws as the terminal's own default cell. - // The full frame that follows paints over it. - @memset(c.grid.items, .{}); - c.cols = cols; - c.rows = rows; - c.cursor = null; - c.pointer_shape = .arrow; - } - - /// Take everything the kernel is holding, not one chunk of it. The server - /// deliberately reads its clients ONE chunk per round, because it is - /// dividing a loop between thirty-two of them; a frontend has exactly one - /// peer, and reading one 16 KiB slice per poll would leave it a full frame - /// behind on every large one — and the session drops a frontend whose queue - /// it cannot drain (server.zig `out_backlog`). - fn fill(c: *Client) (Error || wire.Error)!void { - var buf: [read_chunk]u8 = undefined; - while (true) { - const got = libc.read(c.fd, &buf, buf.len); - if (got == 0) return error.Closed; - if (got < 0) return switch (libc.errno(got)) { - .INTR => continue, - // Nothing more is ready; what we have is what there was. - .AGAIN => {}, - else => error.Closed, - }; - c.in.appendSlice(c.gpa, buf[0..@intCast(got)]) catch return error.Closed; - } - } - - fn flush(c: *Client) Error!void { - if (c.fd < 0) return error.Closed; - var off: usize = 0; - while (off < c.out.items.len) { - const n = libc.send(c.fd, c.out.items.ptr + off, c.out.items.len - off, nosignal); - if (n < 0) switch (libc.errno(n)) { - .INTR => continue, - // The session is not draining us. The rest waits for POLLOUT; - // the queue is bounded in practice because a frontend's output - // is keystrokes and pty chunks, never frames. - .AGAIN => break, - else => return error.Closed, - }; - if (n == 0) break; - off += @intCast(n); - } - if (off == 0) return; - if (off == c.out.items.len) return c.out.clearRetainingCapacity(); - std.mem.copyForwards(u8, c.out.items, c.out.items[off..]); - c.out.items.len -= off; - } -}; - -// The socket primitives are server.zig's, which is the file that owns this -// transport's conventions and both ends of it — see its `setNonblock`, -// `nosignal` and `poll_*`. There was a third copy of all of them here. -const nosignal = server.nosignal; -const poll_in = server.poll_in; -const poll_out = server.poll_out; -const poll_hup = server.poll_hup; -const poll_err = server.poll_err; -const poll_nval = server.poll_nval; - -/// Which session an attach meant, answered before a frontend opens its window. -/// -/// `--attach=<name>` is taken at its word beyond one `access` on the socket -/// file, because the connect is the real authority on whether anything is -/// listening — and a typo is the one failure worth catching earlier, since the -/// alternative is a full-screen flash on the way to a one-line message. Bare -/// `--attach`, and the bare `Attach` word, is THE session: bare `--detach` -/// names itself by its own pid, and nobody can be expected to read a pid out -/// of `$XDG_RUNTIME_DIR`. With exactly one listening, that is the one meant; -/// with none or several this says WHICH case it is instead of picking one. -/// -/// Both the directory and the filename convention come off ONE probe through -/// `server.sessionPath` rather than being re-derived here, for the reason that -/// function exists at all: the side that binds and the side that looks must -/// never be able to disagree about where a session lives. -/// -/// It lives in this file, rather than in either shell, because BOTH frontends -/// now ask the same question — a terminal for `--attach` and the SDL window -/// for the `Attach` word — and a second copy of a directory scan is exactly -/// how the two of them would start disagreeing. -pub const Resolved = union(enum) { - /// The session to open. Borrows `requested` when it was named, and `buf` - /// when it had to be scanned for. - name: []const u8, - /// No socket of that name, or no session at all. The caller knows which it - /// asked for, so it owns the wording. - none, - /// Several are listening, and choosing between them is not ours to do. - ambiguous: usize, -}; - -pub fn resolve(buf: *[server.path_max]u8, requested: []const u8) Resolved { - if (requested.len != 0) { - var one_buf: [server.path_max]u8 = undefined; - const one = server.sessionPath(&one_buf, requested) orelse return .none; - const F_OK: c_int = 0; - if (libc.access(one, F_OK) != 0) return .none; - return .{ .name = requested }; - } - const probe_name = "0"; - var probe_buf: [server.path_max]u8 = undefined; - const probe = server.sessionPath(&probe_buf, probe_name) orelse return .none; - const base = std.fs.path.basename(probe); - const cut = std.mem.lastIndexOf(u8, base, probe_name).?; - const prefix = base[0..cut]; - const suffix = base[cut + probe_name.len ..]; - var dir_buf: [server.path_max:0]u8 = undefined; - const dir = std.fs.path.dirname(probe) orelse ""; - if (dir.len == 0 or dir.len >= dir_buf.len) return .none; - @memcpy(dir_buf[0..dir.len], dir); - dir_buf[dir.len] = 0; - const d = libc.opendir(dir_buf[0..dir.len :0]) orelse return .none; - defer _ = libc.closedir(d); - var found: usize = 0; - var len: usize = 0; - while (libc.readdir(d)) |ent| { - const entry = std.mem.sliceTo(&ent.name, 0); - if (entry.len <= prefix.len + suffix.len) continue; - if (!std.mem.startsWith(u8, entry, prefix) or !std.mem.endsWith(u8, entry, suffix)) continue; - const name = entry[prefix.len .. entry.len - suffix.len]; - if (name.len > buf.len) continue; - found += 1; - @memcpy(buf[0..name.len], name); - len = name.len; - } - // A socket file whose session is gone still counts here: the sweep that - // unlinks corpses runs when the NEXT session binds (server.zig `sweep`), - // and probing every candidate with a connect would put a phantom frontend - // into a live session's slot table just to count it. One stale file - // therefore fails at `open` with "no such session", which is the truth. - if (found != 1) return if (found == 0) .none else .{ .ambiguous = found }; - return .{ .name = buf[0..len] }; -} - -// Input arrives through frontend queues, so attached clients bound socket waits to half a frame. -pub const poll_ms: u32 = 8; - -/// One round of waiting for the greeting, and how many of them. The COUNT is -/// derived from `server.greet_deadline_default_ms` rather than written down -/// again, so the two ends of the handshake give each other the same grace out -/// of one number instead of two that can drift apart. -/// -/// Rounds rather than a clock, and that is sound rather than lazy: the only -/// message a session may legally send before its `welcome` is a `refuse`. -/// Everything else is refused by `next` as `Ungreeted` — a frame because it -/// would be drawing for a connection that was never accepted, and the three -/// display effects because a peer whose version nobody has checked does not get -/// to open a URI or read a clipboard. So a peer that is silent costs one whole -/// timeout per round, which makes the round count a real wall-clock bound; and -/// a peer that is NOISY but ungreeted fails on its first message rather than -/// spending the budget. -const greet_round_ms: u32 = 250; -const greet_rounds: u32 = server.greet_deadline_default_ms / greet_round_ms; - -/// What came of trying to attach. A VALUE and not an error union, because four -/// of the six outcomes are ordinary answers a human needs different words for, -/// and because the two frontends report them by different mechanisms — a -/// terminal prints a sentence and exits, a window logs and unwinds. `resolve` -/// above returns a union for the same reason. -pub const Attempt = union(enum) { - /// Resolved, connected, AND greeted. The caller owns it. - greeted: Client, - /// No socket of that name, or nothing detached at all. The caller knows - /// which it asked for, so the wording is the caller's. - no_session, - /// Several are listening and choosing is not ours to do. - ambiguous: usize, - /// The session said no, and said why. - refused: wire.Refusal, - /// It accepted and then never greeted us inside the deadline. - silent, - /// Everything else, already reported by the errno it came from. - lost: anyerror, -}; - -/// Wait for welcome before the caller replaces its session; connect alone can -/// still lead to a version refusal. Every non-greeted result owns no resources. -pub fn attempt(gpa: std.mem.Allocator, requested: []const u8, cols: u16, rows: u16) Attempt { - var buf: [server.path_max]u8 = undefined; - const name = switch (resolve(&buf, requested)) { - .name => |n| n, - .none => return .no_session, - .ambiguous => |n| return .{ .ambiguous = n }, - }; - var c = Client.open(gpa, name, cols, rows) catch |err| return switch (err) { - // `open`'s own two ways of saying "there is nothing there" collapse - // into the one the caller has wording for. - error.NoSession, error.NoSessionPath => .no_session, - else => .{ .lost = err }, - }; - var rounds: u32 = 0; - while (rounds < greet_rounds) : (rounds += 1) { - c.wait(greet_round_ms) catch |err| return give(&c, err); - // Drain whatever landed. `next` is what applies the welcome, so the - // loop below is not discarding anything it needs — the state it wants - // is in `c` afterwards. - while (true) { - const msg = c.next() catch |err| return give(&c, err); - if (msg == null) break; - } - if (c.attached()) return .{ .greeted = c }; - if (c.refusal) |why| { - c.deinit(); - return .{ .refused = why }; - } - } - c.deinit(); - return .silent; -} - -/// Close, and say what the failure actually was. A session that refuses us -/// writes its reason and closes in the same pass (server.zig `refuseFd`), so a -/// read error here is usually the far side hanging up on a refusal we have -/// already decoded — reporting that as a lost connection would throw away the -/// one sentence worth telling the human. -fn give(c: *Client, err: anyerror) Attempt { - const why = c.refusal; - c.deinit(); - if (why) |w| return .{ .refused = w }; - return .{ .lost = err }; -} - -// --------------------------------------------------------------------------- -// tests -// --------------------------------------------------------------------------- -// -// One real core, one real unix socket, real frontends. The harness runs the -// server's vtable in the order `Pardes.pump` runs it, so what these exercise is -// the transport as the core actually drives it rather than a mock of it. - -const testing = std.testing; - -extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; -extern "c" fn unsetenv(name: [*:0]const u8) c_int; - -/// A session on a socket of its own under `.zig-cache/tmp`, so a test never -/// collides with a real session in `$XDG_RUNTIME_DIR` and never depends on that -/// variable being set at all. It is process-wide, so it is saved and restored. -const Harness = struct { - tmp: std.testing.TmpDir, - saved: ?[:0]const u8, - saved_buf: [4096:0]u8 = undefined, - core: *pardes.Pardes, - session: server.Session, - arena: std.heap.ArenaAllocator, - name_buf: [32]u8 = undefined, - name: []const u8 = &.{}, - - fn init(h: *Harness, cols: u16, rows: u16) !void { - h.tmp = std.testing.tmpDir(.{}); - errdefer h.tmp.cleanup(); - h.saved = if (libc.getenv("XDG_RUNTIME_DIR")) |v| - try std.fmt.bufPrintSentinel(&h.saved_buf, "{s}", .{std.mem.span(v)}, 0) - else - null; - errdefer h.restoreEnv(); - var dir_buf: [4096:0]u8 = undefined; - const dir = try std.fmt.bufPrintSentinel(&dir_buf, ".zig-cache/tmp/{s}", .{h.tmp.sub_path}, 0); - // `ensureSocketDir` refuses anything with a bit granted to group or - // other, which is the whole point of it; a tmpDir arrives 0755. - try testing.expectEqual(@as(c_int, 0), libc.chmod(dir, 0o700)); - _ = setenv("XDG_RUNTIME_DIR", dir.ptr, 1); - h.core = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true, .cols = cols, .rows = rows }); - errdefer h.core.deinit(); - h.arena = .init(testing.allocator); - errdefer h.arena.deinit(); - // `io` is not optional on `Session`: the daemon does the file watching - // and the theme scan itself now, and both of those take a `std.Io`. - h.session = .{ - .gpa = testing.allocator, - .worker_gpa = testing.allocator, - .io = std.testing.io, - .core = h.core, - .cols = cols, - .rows = rows, - }; - try h.session.initAsync(); - errdefer h.session.deinit(); - h.name = "s"; - try testing.expect(h.session.listen(h.name)); - // The pre-loop drain tty.zig has, for its reason: the startup spawns are - // already queued and a session must not open its socket with panes that - // have not been created. These now fork IN THIS PROCESS, because the - // daemon is the thing that owns pane shells — which is exactly the - // property under test, and the reason no frontend below is ever asked - // to fork anything. - h.core.host = h.session.host(); - while (h.core.nextEffect()) |e| h.core.perform(e); - } - - fn restoreEnv(h: *Harness) void { - if (h.saved) |v| { - _ = setenv("XDG_RUNTIME_DIR", v.ptr, 1); - } else _ = unsetenv("XDG_RUNTIME_DIR"); - } - - fn deinit(h: *Harness) void { - h.session.deinit(); - h.core.deinit(); - h.arena.deinit(); - h.restoreEnv(); - h.tmp.cleanup(); - } - - /// `Pardes.pump`, with the one substitution a single-threaded test needs: a - /// bounded wait, so a frontend that says nothing cannot hang the suite - /// where the real session would sleep until it spoke. The queued-input - /// drain `pump` does between the two is absent because this host has no - /// queue: it calls `update` directly (borrowed bytes), and `postEvent` is - /// for hosts whose worker threads post from off the loop. - fn pump(h: *Harness) !void { - const host = h.session.host(); - host.vtable.wait_input.?(host.ctx, 20); - while (h.core.nextEffect()) |e| h.core.perform(e); - if (h.core.quit) return; - _ = h.arena.reset(.retain_capacity); - const surface = try h.core.render(h.arena.allocator()); - host.vtable.present.?(host.ctx, surface); - } - - /// The round budget every `pumpUntil*` below shares. A round moves at most - /// one socket buffer, because nothing here is concurrent: the session - /// flushes until `EAGAIN`, and only then does the client read. That buffer - /// is 8 KiB on darwin (`net.local.stream.sendspace`) against linux's 208 - /// KiB, which is the same asymmetry the slow-frontend test at the bottom of - /// this file already had to say out loud — and a full frame of the largest - /// grid the protocol carries is near a megabyte, so 64 rounds is a linux- - /// only number. High enough for that frame on the smaller buffer, and still - /// a bound: a broken transport fails a test rather than hanging the suite. - const rounds = 256; - - /// Pump until this client has the message we are waiting for. - fn pumpUntil(h: *Harness, c: *Client, comptime want: std.meta.Tag(wire.ServerMsg)) !wire.ServerMsg { - for (0..rounds) |_| { - try h.pump(); - try c.wait(5); - while (try c.next()) |msg| if (std.meta.activeTag(msg) == want) return msg; - } - return error.NeverArrived; - } - - /// Pump until this client is sent a frame that CHANGES something. The first - /// frame after an input is not always the one carrying it — an effect the - /// input queued (a `Look` on a directory emits a spawn) lands a frame - /// later, and the frame in between legitimately says nothing. - fn pumpUntilChange(h: *Harness, c: *Client) !wire.Frame { - for (0..rounds) |_| { - const msg = try h.pumpUntil(c, .frame); - if (msg.frame.nruns > 0) return msg.frame; - } - return error.NothingChanged; - } - - /// Pump until this client's grid is this shape, draining everything that - /// arrives. A test waits for the STATE rather than for the n-th message - /// because a resize is announced when the session settles it, which may be - /// one empty frame after the pump that caused it. - fn pumpUntilGrid(h: *Harness, c: *Client, cols: u16, rows: u16) !void { - for (0..rounds) |_| { - try h.pump(); - try c.wait(5); - while (try c.next()) |_| {} - if (c.cols == cols and c.rows == rows) return; - } - return error.NeverResized; - } - - /// Pump until two frontends are showing the same screen, draining both on - /// every pass. One pump sends every attached frontend a frame, so a test - /// that drains only one of them is comparing two different instants — and - /// what a shared session promises is that they CONVERGE, which is what this - /// waits for. - fn pumpUntilSameScreen(h: *Harness, a: *Client, b: *Client) !void { - for (0..rounds) |_| { - try h.pump(); - try a.wait(5); - try b.wait(5); - while (try a.next()) |_| {} - while (try b.next()) |_| {} - if (sameScreen(a.grid.items, b.grid.items)) return; - } - return error.NeverConverged; - } - - /// Pump until THIS frontend is showing the core's own screen, draining - /// everything that arrives on every pass. `pumpUntilSameScreen`'s reason, - /// with one frontend instead of two, and a second reason of its own: - /// `pumpUntil` returns the instant it decodes the message it was waiting - /// for and leaves the rest of that burst in the socket, so a client can sit - /// one whole frame behind for the rest of a test. Comparing once against - /// that grid compares the core's NOW with the frontend's THEN — which is - /// nothing at all until the pane shell's prompt lands in the frame nobody - /// read, and then it is a cell that differs. - /// - /// Checked BEFORE the first pump, so a test already in sync spends nothing - /// and no extra frame is manufactured to make one appear. Out of rounds it - /// hands the last comparison to `expectSameScreen`, which names the cell: - /// a transport that really does drop one must fail as a wrong screen, not - /// as a timeout. - fn pumpUntilShowsCore(h: *Harness, c: *Client) !void { - for (0..rounds) |_| { - _ = h.arena.reset(.retain_capacity); - if (sameScreen((try h.core.render(h.arena.allocator())).cells, c.grid.items)) return; - try h.pump(); - try c.wait(5); - while (try c.next()) |_| {} - } - _ = h.arena.reset(.retain_capacity); - return expectSameScreen((try h.core.render(h.arena.allocator())).cells, c.grid.items); - } - - /// Attach a frontend and get it greeted: `open` writes the hello into the - /// listener's backlog, one pump accepts and answers it. Nothing blocks, - /// which is the whole reason the handshake is not a blocking call. - fn attach(h: *Harness, cols: u16, rows: u16) !Client { - var c = try Client.open(testing.allocator, h.name, cols, rows); - errdefer c.deinit(); - _ = try h.pumpUntil(&c, .welcome); - return c; - } -}; - -test "detached session: a frontend attaches, is greeted, and is sent the screen" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - - var c = try h.attach(60, 16); - defer c.deinit(); - try testing.expectEqual(@as(u8, 0), c.slot); - try testing.expectEqual(@as(u16, 60), c.cols); - try testing.expectEqual(@as(u16, 16), c.rows); - - const frame = (try h.pumpUntil(&c, .frame)).frame; - // The first frame a frontend gets must be full: it has nothing to diff - // against. - try testing.expectEqual(wire.FrameKind.full, frame.kind); - try testing.expectEqual(@as(usize, 60 * 16), c.grid.items.len); - // ...and it must be the core's own frame, cell for cell. This is the whole - // claim of the transport — asserted once the frontend has read everything - // the session sent, which `pumpUntil` above deliberately did not do. - try h.pumpUntilShowsCore(&c); -} - -test "detached session: input from a frontend reaches the core and comes back as a diff" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - var c = try h.attach(60, 16); - defer c.deinit(); - _ = try h.pumpUntil(&c, .frame); - - // A builtin line is the cheapest input with a guaranteed visible effect, - // and it travels the same `Event` path a keystroke does. - try c.send(.{ .event = .{ .command = "Look /" } }); - const frame = try h.pumpUntilChange(&c); - // The change arrived as a DIFF: this frontend was already in sync, so - // nothing it already had was re-sent. - try testing.expectEqual(wire.FrameKind.diff, frame.kind); - try h.pumpUntilShowsCore(&c); -} - -test "detached Restore keeps attached frontends and follows queued frames with a full replacement" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - _ = try h.core.setTestFile("saved body\n"); - var c = try h.attach(60, 16); - defer c.deinit(); - _ = try h.pumpUntil(&c, .frame); - try h.pumpUntilShowsCore(&c); - - const before = h.core; - const fd = h.session.clients[c.slot].fd; - try testing.expectError(error.BadDumpMagic, h.session.restore( - ".{ .magic = \"not-a-pardes-dump\", .theme = \"dark\", .screen = .{ .cols = 60, .rows = 16 } }", - "test", - )); - try testing.expectEqual(before, h.session.core); - try testing.expectEqual(fd, h.session.clients[c.slot].fd); - try testing.expect(h.session.clients[c.slot].attached); - - try dump.dumpState(h.core); - const saved = try testing.allocator.dupe(u8, h.core.dump_out.?); - defer testing.allocator.free(saved); - while (h.core.nextEffect()) |_| {} - _ = try h.core.setTestFile("changed after dump\n"); - try h.session.restore(saved, "test"); - h.core = h.session.core; - try testing.expect(h.core != before); - try testing.expectEqual(fd, h.session.clients[c.slot].fd); - try testing.expect(h.session.clients[c.slot].attached); - try testing.expectEqualStrings("saved body\n", h.core.panes[0].?.file.?.content); - try testing.expect(h.session.clients[c.slot].need_full); - var wake = [_]libc.pollfd{.{ .fd = h.session.wake[0], .events = poll_in, .revents = 0 }}; - try testing.expectEqual(@as(c_int, 1), libc.poll(&wake, wake.len, 0)); - for (0..Harness.rounds) |_| { - if ((try h.pumpUntil(&c, .frame)).frame.kind == .full) break; - } else return error.NoFullReplacement; - try h.pumpUntilShowsCore(&c); -} - -test "detached selection pipe runs off the loop and returns through the attached frontend" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - const pane = try h.core.setTestFile("one\ntwo\n"); - pane.body.cur_row = 0; - pane.body.cur_col = 2; - pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - var client = try h.attach(60, 16); - defer client.deinit(); - _ = try h.pumpUntil(&client, .frame); - - try client.send(.{ .event = .{ .key = .{ .cp = '|' } } }); - try client.send(.{ .event = .{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } } }); - try client.send(.{ .event = .{ .key = .{ .cp = pardes.Key.enter } } }); - for (0..Harness.rounds) |_| { - try h.pump(); - try client.wait(5); - while (try client.next()) |_| {} - if (std.mem.eql(u8, pane.file.?.content, "ONE\ntwo\n")) break; - } else return error.PipeDidNotComplete; - try testing.expect(h.core.pipe.wait == null); - try testing.expectEqual(@as(usize, 0), h.session.pipe_tasks.len); - try h.pumpUntilShowsCore(&client); -} - -test "detached session: two frontends share one screen at the smallest common grid" { - var h: Harness = undefined; - try h.init(80, 24); - defer h.deinit(); - var a = try h.attach(80, 24); - defer a.deinit(); - _ = try h.pumpUntil(&a, .frame); - - // A second, smaller frontend. tmux's rule: the session shrinks to what both - // can show, because two people looking at different screens is the point of - // a shared session lost. - var b = try h.attach(50, 12); - defer b.deinit(); - try testing.expectEqual(@as(u8, 1), b.slot); - try testing.expectEqual(@as(u16, 50), b.cols); - try testing.expectEqual(@as(u16, 12), b.rows); - - // Both are now on the 50x12 grid — and getting there IS the proof that the - // reshape was sent as a FULL frame: `next` refuses a diff whose geometry - // does not match the grid it holds, so a client whose shape moved can only - // have been reshaped by a full one. - try h.pumpUntilGrid(&a, 50, 12); - try h.pumpUntilGrid(&b, 50, 12); - try testing.expectEqual(@as(usize, 50 * 12), a.grid.items.len); - // CONVERGENCE, not a snapshot. `pumpUntilGrid(&b, ...)` pumped the session - // while draining only `b`, and the daemon owns the pane shells now: a - // prompt arriving on a pty moves the screen between pumps, so `a` can be - // holding an undrained frame and comparing the two grids here would compare - // two instants. What a shared session promises is that they agree, which is - // what this waits for. - try h.pumpUntilSameScreen(&a, &b); - - // ...and input from EITHER moves that one screen. Both are drained on every - // pump before they are compared: one pump sends every attached frontend a - // frame, so a test that drains only one is comparing two instants. - try b.send(.{ .event = .{ .command = "Look /" } }); - _ = try h.pumpUntilChange(&a); - try h.pumpUntilSameScreen(&a, &b); -} - -test "detached session: a window past the protocol attaches at the largest grid it carries" { - var h: Harness = undefined; - try h.init(80, 24); - defer h.deinit(); - - // A 4K display at a small font is already past `max_rows`, and until the - // clamp in `open` that hello was a geometry the session's decoder refused: - // `apply` answered `BadValue` with `close(.protocol)` and no `refuse` - // behind it, so a frontend on a big screen was told the session "hung up on - // the connect" — at a session with every slot free. It attaches now, at the - // biggest grid the wire has. - var c = try h.attach(wire.max_cols + 400, wire.max_rows + 70); - defer c.deinit(); - try testing.expectEqual(wire.max_cols, c.cols); - try testing.expectEqual(wire.max_rows, c.rows); - - // ...and the full frame that follows is the 65536-cell one whose single run - // does not fit a u16 count (wire.zig `run_max`), which is the half of this - // the clamp alone would have made universal rather than fixed. - const frame = (try h.pumpUntil(&c, .frame)).frame; - try testing.expectEqual(wire.FrameKind.full, frame.kind); - try testing.expectEqual(@as(usize, @as(usize, wire.max_cols) * wire.max_rows), c.grid.items.len); - // TWO runs and not one, which is the assertion that fails first if the - // encoder's bound goes: this grid's full frame is 65536 painted cells and - // a run counts to 65535. - try testing.expectEqual(@as(u32, 2), frame.nruns); - try h.pumpUntilShowsCore(&c); -} - -test "detached session: a frontend that dies takes nothing with it" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - var a = try h.attach(60, 16); - defer a.deinit(); - var b = try h.attach(60, 16); - _ = try h.pumpUntil(&a, .frame); - _ = try h.pumpUntil(&b, .frame); - try testing.expect(h.session.clients[0].attached); - try testing.expect(h.session.clients[1].attached); - - // Not a `bye`: the socket goes away under the session's feet, which is what - // a frontend crashing or being killed looks like from here. - b.deinit(); - _ = try h.pumpUntil(&a, .frame); - try testing.expect(!h.session.clients[1].attached); - try testing.expectEqual(@as(c_int, -1), h.session.clients[1].fd); - // The survivor is still served and the core is still running. - try testing.expect(h.session.clients[0].attached); - try testing.expect(!h.core.quit); - try a.send(.{ .event = .{ .command = "Look /" } }); - try testing.expect((try h.pumpUntilChange(&a)).nruns > 0); - - // The freed slot takes the next frontend, and the screen comes with it. - var d = try h.attach(60, 16); - defer d.deinit(); - try testing.expectEqual(@as(u8, 1), d.slot); - try testing.expectEqual(wire.FrameKind.full, (try h.pumpUntil(&d, .frame)).frame.kind); - // ...and it is the SAME screen the survivor is looking at. Waited for - // rather than snapshotted, for the reason above: `pumpUntil(&d, ...)` - // drained only `d`, and pane output means the screen does not stand still - // between pumps. - try h.pumpUntilSameScreen(&a, &d); -} - -test "detached session: a frontend speaking another protocol is refused, loudly" { - // The protocol before this one (v7 carried one-row tags and no regions) - // and the one after. - for ([_]u16{ wire.version - 1, wire.version + 1 }) |claimed| try refusedHello(claimed); -} - -fn refusedHello(claimed: u16) !void { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - - // A raw socket rather than a `Client`, because the whole point is a peer - // that does not agree with `wire.version` — and `open` would already have - // sent a perfectly good hello. - const fd = try rawConnect(&h); - defer _ = libc.close(fd); - var buf: [64]u8 = undefined; - const hello = try wire.encodeClient(&buf, .{ - .hello = .{ .version = claimed, .cols = 60, .rows = 16 }, - }); - try testing.expectEqual(@as(isize, @intCast(hello.len)), libc.send(fd, hello.ptr, hello.len, nosignal)); - - // Two pumps: one to accept the connection, one to read the hello and - // answer it. - try h.pump(); - try h.pump(); - var got: [64]u8 = undefined; - const n = libc.read(fd, &got, got.len); - try testing.expect(n > 0); - const f = (try wire.framed(got[0..@intCast(n)])).?; - try testing.expectEqual(wire.Refusal.version, (try wire.decodeServer(f.tag, f.payload)).refuse); - // Refused means refused: the slot went back and no frame was ever sent. - for (&h.session.clients) |*slot| try testing.expect(!slot.attached); - try testing.expect(!h.core.quit); -} - -test "detached frontend: a session speaking another protocol is not attached to" { - for ([_]u16{ wire.version - 1, wire.version + 1 }) |claimed| { - var c: Client = .{ .gpa = testing.allocator }; - defer c.deinit(); - var buf: [64]u8 = undefined; - try c.in.appendSlice(testing.allocator, try wire.encodeServer(&buf, .{ .welcome = .{ .version = claimed, .slot = 0, .cols = 60, .rows = 16 } })); - try testing.expectError(error.Ungreeted, c.next()); - try testing.expect(!c.attached()); - } -} - -test "detached session: a peer that sends garbage is dropped, not obeyed" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - var c = try h.attach(60, 16); - defer c.deinit(); - _ = try h.pumpUntil(&c, .frame); - - // A well-formed frame around a tag this protocol has never defined. The - // session must close the connection rather than guess at it. - const junk = [_]u8{ 0xfe, 0x00, 0x00, 0x00, 0x00 }; - try testing.expectEqual(@as(isize, junk.len), libc.send(c.fd, &junk, junk.len, nosignal)); - for (0..8) |_| { - try h.pump(); - if (!h.session.clients[0].attached) break; - } - try testing.expect(!h.session.clients[0].attached); - // The session is untouched: a hostile frontend costs a slot, not a session. - try testing.expect(!h.core.quit); -} - -test "detached session: the session outlives every frontend and keeps its grid" { - var h: Harness = undefined; - try h.init(72, 20); - defer h.deinit(); - { - var c = try h.attach(40, 10); - defer c.detach(); - _ = try h.pumpUntil(&c, .frame); - try testing.expectEqual(@as(u16, 40), h.session.cols); - } - // Nobody attached. The grid stays where the last frontend left it rather - // than collapsing: a detached session is one nobody is looking at, not one - // of no size. - try h.pump(); - try testing.expectEqual(@as(u16, 40), h.session.cols); - try testing.expectEqual(@as(u16, 10), h.session.rows); - try testing.expect(!h.core.quit); - // ...and the next frontend takes the grid over: with one attachment the - // smallest common grid IS that frontend's, so the session follows it up to - // 90x30 rather than pinning the departed one's 40x10 forever. - var again = try h.attach(90, 30); - defer again.deinit(); - try testing.expectEqual(@as(u16, 90), again.cols); - try testing.expectEqual(@as(u16, 30), again.rows); - try h.pumpUntilGrid(&again, 90, 30); - try testing.expectEqual(@as(usize, 90 * 30), again.grid.items.len); -} - -test "detached session: the seam's own routing rules, per surviving effect" { - var h: Harness = undefined; - try h.init(60, 16); - defer h.deinit(); - var a = try h.attach(60, 16); - defer a.deinit(); - var b = try h.attach(60, 16); - defer b.deinit(); - _ = try h.pumpUntil(&a, .frame); - _ = try h.pumpUntil(&b, .frame); - - const host = h.session.host(); - // BROADCAST: the yank register is a fact about the session, so every - // display it is being watched on gets it. - host.vtable.set_clipboard.?(host.ctx, "yank"); - try expectBoth(&h, &a, &b, .set_clipboard); - - // Ask one frontend: two clipboard answers would paste twice. - h.session.origin = 1; - host.vtable.read_clipboard.?(host.ctx); - try expectOnly(&h, &b, &a, .read_clipboard); - // `open_link` follows the same origin: the browser that opens is the one on - // the display of the human who clicked, not the oldest attachment's. - host.vtable.open_link.?(host.ctx, "https://x"); - try expectOnly(&h, &b, &a, .open_link); - // ...and with no origin it falls back to the primary, which is what a link - // opened by something other than a keystroke gets. - h.session.origin = 0; - host.vtable.open_link.?(host.ctx, "https://y"); - try expectOnly(&h, &a, &b, .open_link); -} - -test "detached session: a frontend is never asked to fork, write, or watch" { - // THE INVARIANT OF THE WHOLE DETACHED DESIGN, pinned as a property of the - // protocol rather than of one code path: a frontend is input and screen, so - // the set of messages that can reach it is exactly the nine below. If a - // machine-local effect is ever put back on the wire, a frontend becomes the - // process that owns a pane's shell again — and a pane whose shell belongs to - // a frontend dies when that frontend leaves, which is the bug this replaced. - // - // Adding a name here is meant to be an ARGUMENT, not a formality. The bar is - // the one `quit` and `detach` clear and `spawn` cannot: it needs this - // human's screen, keyboard, clipboard or browser, or it is the session - // telling this frontend about its own membership. Anything that touches a - // disk or a process table fails that bar by construction. - const allowed = [_][]const u8{ - // Session control: who this connection is, and whether it is still one. - // `detach` is this frontend leaving and `quit` is the session ending — - // opposite meanings, same shape, and neither is an effect. - "welcome", "refuse", "frame", "quit", "detach", - // The three that need THIS human's display and cannot be done by a - // daemon nobody is looking at. - "set_clipboard", "read_clipboard", "open_link", - // Display state, like a frame's chrome: the passes this display - // draws its frames through. A file's SPIR-V arrives compiled, so the - // frontend still reads no disk and runs no program. - "post", - }; - - // One: the union a frontend decodes into has no other variant. Named - // rather than counted, so re-adding `spawn` fails with the name in it. - const fields = @typeInfo(wire.ServerMsg).@"union".field_names; - inline for (fields) |name| { - for (allowed) |ok| { - if (std.mem.eql(u8, name, ok)) break; - } else { - std.debug.print("ServerMsg.{s} is not an effect a frontend may perform\n", .{name}); - return error.MachineLocalEffectOnTheWire; - } - } - try testing.expectEqual(allowed.len, fields.len); - - // Two: and no TAG BYTE outside them decodes either — the check above is - // about this build's union, this one is about the bytes on the socket. Every - // other byte must be `BadTag`, including the eight that used to be defined: - // a session built before this change cannot talk a frontend into forking. - var accepted: usize = 0; - for (0..256) |i| { - const tag: u8 = @intCast(i); - // Payloads are deliberately empty: what is asked is whether the TAG is - // known, and every known tag fails later (`EndOfStream`) or succeeds, but - // never with `BadTag`. - if (wire.decodeServer(tag, &.{})) |_| accepted += 1 else |err| switch (err) { - error.BadTag => continue, - else => accepted += 1, - } - const named = for (allowed) |ok| { - const want = std.meta.stringToEnum(wire.ServerTag, ok).?; - if (@backingInt(want) == tag) break true; - } else false; - if (!named) { - std.debug.print("tag 0x{x:0>2} is decodable by a frontend and is not one of the nine\n", .{tag}); - return error.MachineLocalEffectOnTheWire; - } - } - try testing.expectEqual(allowed.len, accepted); -} - -test "detached session: the client table is a refusal, not a queue" { - // A small grid on purpose: these thirty-two peers never read, and a full - // frame of 80x24 each would push them into the backlog rule that the next - // test is about. 20x5 keeps every queue in the kernel's own buffer. - var h: Harness = undefined; - try h.init(20, 5); - defer h.deinit(); - - var fds: [server.max_clients]c_int = @splat(-1); - defer for (fds) |fd| if (fd >= 0) { - _ = libc.close(fd); - }; - var buf: [64]u8 = undefined; - const hello = try wire.encodeClient(&buf, .{ .hello = .{ .cols = 20, .rows = 5 } }); - for (&fds) |*fd| { - fd.* = try rawConnect(&h); - try testing.expectEqual(@as(isize, @intCast(hello.len)), libc.send(fd.*, hello.ptr, hello.len, nosignal)); - } - // The listener's backlog is `max_clients` deep, so this takes a few rounds: - // `accept` drains what is there each time it is woken. - for (0..16) |_| { - try h.pump(); - var attached: usize = 0; - for (&h.session.clients) |*slot| if (slot.attached) { - attached += 1; - }; - if (attached == server.max_clients) break; - } - for (&h.session.clients) |*slot| try testing.expect(slot.attached); - - // The thirty-third is TOLD it does not fit, and told promptly: the - // alternative — leaving it in the backlog — makes a level-triggered poll - // report the listener ready forever and spins the core. - const extra = try rawConnect(&h); - defer _ = libc.close(extra); - try testing.expectEqual(@as(isize, @intCast(hello.len)), libc.send(extra, hello.ptr, hello.len, nosignal)); - var got: [64]u8 = undefined; - const refusal = for (0..8) |_| { - try h.pump(); - const n = libc.read(extra, &got, got.len); - if (n > 0) break got[0..@intCast(n)]; - } else return error.NeverRefused; - const f = (try wire.framed(refusal)).?; - try testing.expectEqual(wire.Refusal.full, (try wire.decodeServer(f.tag, f.payload)).refuse); - // ...and the thirty-two it does serve are untouched. - for (&h.session.clients) |*slot| try testing.expect(slot.attached); - try testing.expect(!h.core.quit); -} - -test "detached session: a frontend that stops reading is dropped, not waited for" { - var h: Harness = undefined; - try h.init(40, 10); - defer h.deinit(); - var good = try h.attach(40, 10); - defer good.deinit(); - - // A peer that says hello and then never reads a byte again — a frontend - // stopped in a debugger, or one whose terminal is blocked. - const mute = try rawConnect(&h); - defer _ = libc.close(mute); - var buf: [64]u8 = undefined; - const hello = try wire.encodeClient(&buf, .{ .hello = .{ .cols = 40, .rows = 10 } }); - try testing.expectEqual(@as(isize, @intCast(hello.len)), libc.send(mute, hello.ptr, hello.len, nosignal)); - for (0..8) |_| { - try h.pump(); - if (h.session.clients[1].attached) break; - } - try testing.expect(h.session.clients[1].attached); - - // Clipboard updates broadcast to every frontend. - const text = try testing.allocator.alloc(u8, 256 * 1024); - defer testing.allocator.free(text); - @memset(text, 'y'); - const host = h.session.host(); - for (0..24) |_| { - if (!h.session.clients[1].attached) break; - host.vtable.set_clipboard.?(host.ctx, text); - // Read `good` back to EMPTY before the next mirror, rather than - // pumping once and taking whatever one write fitted. One pump moves at - // most one socket buffer, and that buffer is 8 KiB here - // (`net.local.stream.sendspace` on Darwin) against Linux's 208 KiB — - // so a single pump per 256 KiB mirror leaves the READING frontend - // falling behind by a quarter megabyte a round and closes it for a - // backlog it never caused. The claim under test is that a frontend - // that reads survives, so it has to actually finish reading. - for (0..256) |_| { - try h.pump(); - try good.wait(5); - while (try good.next()) |_| {} - if (h.session.clients[0].out.items.len == 0) break; - } - } - // Dropped rather than queued without bound, and rather than the core - // blocking on it. - try testing.expect(!h.session.clients[1].attached); - try testing.expectEqual(@as(c_int, -1), h.session.clients[1].fd); - // The frontend that WAS reading is still attached and still being drawn - // for, which is the whole claim: one slow peer costs its own slot. - try testing.expect(h.session.clients[0].attached); - try testing.expect(!h.core.quit); - // A message is a tagline band now, not a grid run, so "still being drawn - // for" is checked on the layers the frontend decoded rather than on nruns. - try good.send(.{ .event = .{ .command = "Msg still here" } }); - try testing.expect(try pumpUntilNotice(&h, &good, "still here")); -} - -/// Pump until this client has a notice band carrying `want`. -fn pumpUntilNotice(h: *Harness, c: *Client, want: []const u8) !bool { - var line: [256]u8 = undefined; - for (0..Harness.rounds) |_| { - try h.pump(); - try c.wait(5); - while (try c.next()) |_| {} - for (&c.tag_layers) |*layer| { - if (layer.rows == 0 or layer.kind != .notice) continue; - var n: usize = 0; - for (layer.cells[0..layer.cols]) |cell| { - const g = cell.grapheme(); - if (n + g.len > line.len) break; - @memcpy(line[n..][0..g.len], g); - n += g.len; - } - if (std.mem.indexOf(u8, line[0..n], want) != null) return true; - } - } - return false; -} - -/// A connected socket with nothing said on it yet, for the tests whose peer is -/// deliberately not a `Client`: one that speaks another protocol, thirty-two -/// that fill the table, one that never reads. -fn rawConnect(h: *Harness) !c_int { - var path_buf: [server.path_max]u8 = undefined; - const path = server.sessionPath(&path_buf, h.name).?; - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); - if (fd < 0) return error.NoSocket; - errdefer _ = libc.close(fd); - if (libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) return error.NoSession; - return fd; -} - -/// `want` reached `to` and nothing reached `other`. -fn expectOnly( - h: *Harness, - to: *Client, - other: *Client, - comptime want: std.meta.Tag(wire.ServerMsg), -) !void { - _ = try h.pumpUntil(to, want); - try other.wait(5); - while (try other.next()) |msg| if (std.meta.activeTag(msg) == want) { - std.debug.print("{t} reached a frontend it was not routed to\n", .{want}); - return error.Misrouted; - }; -} - -fn expectBoth( - h: *Harness, - a: *Client, - b: *Client, - comptime want: std.meta.Tag(wire.ServerMsg), -) !void { - _ = try h.pumpUntil(a, want); - _ = try h.pumpUntil(b, want); -} - -/// Are these two grids showing the same thing? `visuallyEqual` and not -/// `std.meta.eql`, because `Cell.text` past `len` is scratch the decoder does -/// not invent — pardes.zig says in as many words that it must never -/// manufacture a difference. -fn sameScreen(want: []const pardes.Cell, have: []const pardes.Cell) bool { - if (want.len != have.len) return false; - for (want, have) |*x, *y| if (!x.visuallyEqual(y)) return false; - return true; -} - -fn expectSameScreen(want: []const pardes.Cell, have: []const pardes.Cell) !void { - try testing.expectEqual(want.len, have.len); - for (want, have, 0..) |*x, *y, i| if (!x.visuallyEqual(y)) { - std.debug.print("cell {d} differs\n", .{i}); - return error.CellMismatch; - }; -} diff --git a/src/detached/server.zig b/src/detached/server.zig deleted file mode 100644 index d7dda6ba..00000000 --- a/src/detached/server.zig +++ /dev/null @@ -1,1478 +0,0 @@ -const filesystem = @import("../fs.zig"); -const std = @import("std"); -const builtin = @import("builtin"); -const libc = std.c; -const posix = std.posix; -const pardes = @import("../pardes.zig"); -const dump = @import("../dump.zig"); -const wire = @import("wire.zig"); - -const host_io = @import("../host_io.zig"); -const selection_pipe = @import("../selection_pipe.zig"); - -const file_watch = @import("../file_watch.zig"); -const ShaderBuild = @import("../ShaderBuild.zig"); - -const ninep_io = @import("../9p_io.zig"); - -const look = @import("../look.zig"); - -const message = pardes.Messages.Message; - -const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467)); - -const log = std.log.scoped(.detached); - -pub const darwin = ninep_io.darwin; - -const supported = ninep_io.supported; - -const sun_path_len = ninep_io.sun_path_len; - -pub const max_clients = 32; - -const out_backlog = 1 << 20; - -const read_chunk = 16 * 1024; - -const pty_chunk = 64 * 1024; - -const poll_slots = 2 + max_clients + pardes.MAX_PANES + 2 + @as(usize, @intFromBool(ninep_io.quic_enabled)); - -const reload_retries = 4; - -const session_backlog = 2 * @as(usize, wire.max_payload); - -const pty_backlog = 1 << 20; - -pub const greet_deadline_default_ms: u32 = 5_000; - -const idle_retain = read_chunk; - -const accept_pause_ms = 100; - -const Closed = enum { bye, peer, protocol, backlog, silent, write, read, oom, refused, quitting }; - -const Client = struct { - fd: c_int = -1, - attached: bool = false, - greet: bool = false, - cols: u16 = 0, - rows: u16 = 0, - in: std.ArrayListUnmanaged(u8) = .empty, - out: std.ArrayListUnmanaged(u8) = .empty, - mirror: std.ArrayListUnmanaged(pardes.Cell) = .empty, - need_full: bool = true, - accepted_ms: i64 = 0, - /// The post chain last sent (Session.sendPost); null: none yet. - post_sent: ?u64 = null, -}; - -const Pty = struct { - fd: c_int = -1, - pid: posix.pid_t = 0, - kill_at: i64 = 0, - out: std.ArrayListUnmanaged(u8) = .empty, - /// A command pane's child, watched to its exit (host_io.watchExit); - /// the pty stays open, unpolled after its end of file, until both its - /// end of file and its exit. - cmd: host_io.CommandWatch = .{}, -}; - -/// A watched child exited, or a worker finished: wake the loop. -fn wakeForExit(ctx: ?*anyopaque) void { - Session.signal(@ptrCast(@alignCast(ctx.?))); -} - -const RetiredShell = struct { pid: posix.pid_t = 0, kill_at: i64 = 0 }; - -const Source = union(enum) { - listener, - completion, - client: u8, - pty: u8, - inotify, - ninep_quic, -}; - -pub const Session = struct { - gpa: std.mem.Allocator, - worker_gpa: std.mem.Allocator, - io: std.Io, - core: *pardes.Pardes, - /// PARDES_TEST_CLOCK's virtual time (host_io.testClock). - test_clock: ?u64 = null, - listener: c_int = -1, - path_buf: [sun_path_len]u8 = undefined, - path_len: usize = 0, - clients: [max_clients]Client = @splat(.{}), - cols: u16, - rows: u16, - origin: ?u8 = null, - scratch: std.ArrayListUnmanaged(u8) = .empty, - ptys: [pardes.MAX_PANES]Pty = @splat(.{}), - retired_shells: [pardes.MAX_PANES]RetiredShell = @splat(.{}), - /// The workers' answers (initAsync sets it up); `wake` is what wakes - /// the loop's poll for them, and for a watched child's exit. - inbox: host_io.Inbox = undefined, - wake: [2]c_int = .{ -1, -1 }, - pipe_tasks: selection_pipe.Tasks = .{}, - prompt_rcs: host_io.Shell.PromptFiles = .{}, - inotify_fd: c_int = -1, - ninep: ?*ninep_io.Listener = null, - ninep_pending: bool = false, - /// A 9P request changed the core since the wake byte was last drained: - /// the next poll returns at once, to draw it. - ninep_wake: std.atomic.Value(bool) = .init(false), - watches: file_watch.Table = @splat(null), - /// The post chain's files, compiled here for the GUIs attached. - shaders: ShaderBuild = .{}, - check_files: bool = false, - in_loop: bool = false, - greet_deadline_ms: u32 = greet_deadline_default_ms, - accept_paused_ms: i64 = 0, - - pub fn initAsync(s: *Session) !void { - var pair: [2]c_int = undefined; - if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &pair) != 0) return error.SocketFailed; - errdefer for (pair) |fd| { - _ = libc.close(fd); - }; - for (pair) |fd| { - if (libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)) < 0) return error.SocketOptionFailed; - if (!setNonblock(fd)) return error.SocketOptionFailed; - if (comptime darwin) { - const on: c_int = 1; - if (libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)) != 0) - return error.SocketOptionFailed; - } - } - s.wake = pair; - s.inbox = .{ .gpa = s.worker_gpa, .lsp_allocator = s.worker_gpa, .io = s.io, .wake_fn = wakeForExit, .wake_ctx = s }; - pardes.lsp.setStatusSink(&s.inbox, host_io.Inbox.lspStatus); - host_io.setExitWake(s, wakeForExit); - } - - fn signal(s: *Session) void { - while (libc.send(s.wake[1], "w", 1, nosignal) < 0) { - if (libc.errno(-1) != .INTR) break; - } - } - - fn cancelWorkers(s: *Session) void { - if (s.wake[0] < 0) return; // no initAsync: no workers - if (s.inbox.lsp_task) |*task| { - task.future.cancel(s.io); - s.inbox.lsp_task = null; - } - s.pipe_tasks.cancelAll(s.io); - // Its worker may be waiting on the inbox for room. - s.core.stream.cancelTask(); - _ = s.drainCompletions(false); - } - - fn drainCompletions(s: *Session, apply_results: bool) bool { - var bytes: [128]u8 = undefined; - while (libc.recv(s.wake[0], &bytes, bytes.len, 0) > 0) {} - var batch = s.inbox.take(); - for (batch.slice()) |msg| { - defer msg.deinit(s.worker_gpa, s.worker_gpa); - if (!apply_results) continue; - switch (msg) { - .lsp => |result| { - s.core.update(.{ .lsp_resp = .{ .id = result.id, .rows = result.rows } }); - if (s.inbox.lsp_task) |*task| if (task.id == result.id) { - task.future.await(s.io); - s.inbox.lsp_task = null; - }; - }, - .pipe => |result| { - s.core.update(.{ .pipe_resp = .{ - .id = result.id, - .success = result.success, - .outputs = result.outputs, - .failure = result.failure, - } }); - s.pipe_tasks.finish(s.io, result.id); - }, - .lsp_status => |status| { - var buf: [256]u8 = undefined; - s.core.setStatus(s.core.active, message.stamp(&buf, "lsp", status)); - }, - .search => |b| s.core.update(.{ .search = b }), - // Ptys are read in the loop's own poll, not through the inbox. - // No hand_over host entry here (a session with no window): none comes. - .output, .eof, .files_changed, .fs_ready, .handed => {}, - } - } - return batch.len != 0; - } - - pub fn restore(s: *Session, bytes: []const u8, from: []const u8) !void { - const replacement = try dump.restore(s.core, bytes, from); - s.cancelWorkers(); - for (0..s.ptys.len) |pane| s.closePty(@intCast(pane)); - s.harvest(); - for (0..pardes.MAX_PANES) |pane| - file_watch.watchPane(s.inotify_fd, &s.watches, @intCast(pane), null, 0, .{ .text = 0 }); - _ = file_watch.applyThemeEffect(s.core, s.gpa, s.inotify_fd, &s.watches, 0, false, false); - // The new core's chain generations are its own. - s.shaders.generation = null; - s.check_files = false; - if (s.ninep) |listener| listener.reset(replacement); - s.ninep_pending = false; - replacement.host = s.host(); - s.core.deinit(); - s.core = replacement; - for (&s.clients) |*client| if (client.attached) { - client.need_full = true; - }; - s.signal(); - } - - pub fn deinit(s: *Session) void { - // First: its compile thread wakes the loop, whose socket closes below. - s.shaders.deinit(s.gpa, s.inotify_fd, &s.watches); - if (s.wake[0] >= 0) pardes.lsp.setStatusSink(null, null); - if (s.wake[0] >= 0) host_io.setExitWake(null, null); - s.cancelWorkers(); - if (s.wake[0] >= 0) s.inbox.close(); - for (s.wake) |fd| if (fd >= 0) { - _ = libc.close(fd); - }; - s.wake = .{ -1, -1 }; - if (s.ninep) |l| { - l.deinit(s.gpa); - s.ninep = null; - } - for (&s.clients) |*c| if (c.attached) s.send(c, .quit); - for (&s.clients) |*c| if (c.fd >= 0) s.close(c, .quitting); - s.unlisten(); - s.scratch.deinit(s.gpa); - for (0..s.ptys.len) |pane| s.closePty(@intCast(pane)); - for (s.retired_shells) |shell| if (shell.pid > 0) { - _ = libc.kill(shell.pid, libc.SIG.KILL); - }; - for (s.ptys) |pty| if (pty.pid > 0) { - _ = libc.kill(pty.pid, libc.SIG.KILL); - }; - for (&s.retired_shells) |*shell| if (shell.pid > 0) { - while (libc.waitpid(shell.pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} - shell.* = .{}; - }; - for (&s.ptys) |*pty| if (pty.pid > 0) { - while (libc.waitpid(pty.pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} - pty.pid = 0; - }; - if (s.inotify_fd >= 0) { - _ = libc.close(s.inotify_fd); - s.inotify_fd = -1; - } - s.prompt_rcs.deinit(); - } - - pub fn listen(s: *Session, name: []const u8) bool { - if (comptime !supported) return false; - var dir_buf: [sun_path_len:0]u8 = undefined; - const dir = ninep_io.socketDir(&dir_buf) orelse return false; - if (!ninep_io.ensureSocketDir(dir)) return false; - sweep(dir); - const path = socketPath(&s.path_buf, dir, name) orelse return false; - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); - if (fd < 0) return false; - ninep_io.setCloexec(fd); - if (libc.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) { - if (alive(path)) { - log.debug("a detached session is already listening on {s}", .{path}); - _ = libc.close(fd); - return false; - } - _ = libc.unlink(path); - if (libc.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) { - _ = libc.close(fd); - return false; - } - } - _ = libc.chmod(path, 0o600); - if (libc.listen(fd, max_clients) != 0) { - _ = libc.close(fd); - return false; - } - _ = setNonblock(fd); - s.listener = fd; - s.path_len = path.len; - return true; - } - - fn unlisten(s: *Session) void { - if (s.listener < 0) return; - _ = libc.close(s.listener); - s.listener = -1; - var z: [sun_path_len:0]u8 = undefined; - @memcpy(z[0..s.path_len], s.path_buf[0..s.path_len]); - z[s.path_len] = 0; - _ = libc.unlink(z[0..s.path_len :0]); - } - - pub fn host(s: *Session) host_io.Host { - return .{ .ctx = s, .vtable = &vtable }; - } - - fn of(ctx: ?*anyopaque) *Session { - return @ptrCast(@alignCast(ctx.?)); - } - - fn clockNow(ctx: ?*anyopaque) u64 { - const s = of(ctx); - return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - } - - const vtable: host_io.Host.VTable = .{ - .wait_input = waitInput, - .now = clockNow, - .present = present, - .poll_frame = pollFrame, - .watched = watched, - .spawn = spawn, - .pty_write = ptyWrite, - .pty_resize = ptyResize, - .pty_signal = ptySignal, - .close_pty = closePaneShell, - .tty_taken = ttyTaken, - .fg_name = fgName, - .kill_job = killJob, - .write_file = writeFile, - .write_dump = writeDump, - .watch_file = watchFile, - .watch_theme = watchTheme, - .dump_themes = dumpThemes, - .set_clipboard = setClipboard, - .read_clipboard = readClipboard, - .open_link = openLink, - .detach = detach, - .lsp = lspRequest, - .pipe = pipeRequest, - .search = searchRequest, - }; - - fn searchRequest(ctx: ?*anyopaque, gen: u32) void { - const s = of(ctx); - if (s.wake[0] < 0) return pardes.Stream.runHere(s.core, gen); - pardes.Stream.start(s.io, s.worker_gpa, s.core, gen, &s.inbox, host_io.Inbox.pushSearch); - } - - fn lspRequest(ctx: ?*anyopaque, request: host_io.Lsp.Request) void { - const s = of(ctx); - _ = s.drainCompletions(true); - s.inbox.startLsp(s.core, request); - } - - fn pipeRequest(ctx: ?*anyopaque, id: u32) void { - const s = of(ctx); - _ = s.drainCompletions(true); - selection_pipe.start(s.io, s.worker_gpa, s.core, &s.pipe_tasks, id, &s.inbox, host_io.Inbox.pushPipe); - } - - fn primary(s: *Session) ?*Client { - for (&s.clients) |*c| if (c.attached) return c; - return null; - } - - fn origins(s: *Session) ?*Client { - if (s.origin) |i| { - const c = &s.clients[i]; - if (c.attached) return c; - } - return s.primary(); - } - - fn slotOf(s: *Session, c: *const Client) u8 { - return @intCast(@divExact(@intFromPtr(c) - @intFromPtr(&s.clients[0]), @sizeOf(Client))); - } - - fn broadcast(s: *Session, msg: wire.ServerMsg) void { - for (&s.clients) |*c| if (c.attached) s.send(c, msg); - } - - fn spawn(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { - const s = of(ctx); - if (pane >= s.ptys.len) return; // the core indexes its own panes - s.harvest(); - // The new shell starts before a running one goes: one that cannot - // start leaves the running one be (pty/ctl's exec). - const running = s.ptys[pane].fd >= 0; - if (!running and s.ptys[pane].pid != 0) return s.core.reportError(pane, "shell", error.ShellClosing); - for (s.retired_shells) |shell| { - if (shell.pid == 0) break; - } else return s.core.reportError(pane, "shell", error.ShellClosing); - const child = host_io.forkShell( - s.core, - pane, - &s.prompt_rcs, - s.core.shellBin(), - cwd, - s.core.screen_h, - s.core.screen_w, - s.ninep, - ) catch |err| { - if (running) return s.core.restartFailed(pane, err); - return s.core.shellFailed(pane, err); - }; - s.closePty(pane); - s.harvest(); - if (s.ptys[pane].pid != 0) { - _ = libc.close(child.file.handle); - host_io.retireShell(child.pid); - return s.core.reportError(pane, "shell", error.ShellClosing); - } - const command = if (s.core.panes[pane]) |pn| pn.command != null else false; - s.ptys[pane] = .{ .fd = child.file.handle, .pid = child.pid, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; - _ = setNonblock(child.file.handle); - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - // Only a shell given no directory is asked where it is: one given a - // directory is named by it already (newShell), and asked now, before - // its chdir, it would answer this process's, a rename to undo. - if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| s.core.setCwd(pane, wd); - } - - fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { - const s = of(ctx); - if (pane >= s.ptys.len) return; - const pt = &s.ptys[pane]; - if (pt.fd < 0) return; - // The bound holds with this write in: queued and new together. - if (pt.out.items.len + bytes.len > pty_backlog) { - var mbuf: [256]u8 = undefined; - const text = std.fmt.bufPrint( - &mbuf, - "input refused: pane not reading ({d} bytes queued)", - .{pt.out.items.len}, - ) catch "input refused: pane not reading"; - return s.core.setMessage(pane, text); - } - pt.out.appendSlice(s.gpa, bytes) catch { - return s.core.setMessage(pane, "input refused: out of memory"); - }; - s.flushPty(pane); - } - - fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { - const s = of(ctx); - if (pane >= s.ptys.len) return; - const fd = s.ptys[pane].fd; - if (fd < 0) return; - // In pixels too, at the last frontend's cell size (8x16 before - // one attached): a program sizes its kitty graphics by them. - const ws = host_io.ptyWinsize(s.core, cols, rows); - _ = posix.system.ioctl(fd, TIOCSWINSZ, @intFromPtr(&ws)); - } - - fn ptySignal(ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void { - const s = of(ctx); - if (pane >= s.ptys.len) return; - const pt = s.ptys[pane]; - // A command whose exit is recorded is reaped: its group may be another's. - if (pt.fd < 0 or pt.cmd.exited) return; - host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); - } - - fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool { - const s = of(ctx); - if (pane >= s.ptys.len) return false; - const pt = s.ptys[pane]; - if (pt.fd < 0) return false; - return host_io.ttyTaken(pt.pid, pt.fd); - } - - fn fgName(ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 { - const s = of(ctx); - if (pane >= s.ptys.len) return null; - const pt = s.ptys[pane]; - if (pt.fd < 0) return null; - return host_io.foregroundName(pt.pid, pt.fd, buf); - } - - fn killJob(ctx: ?*anyopaque, pane: u8) bool { - const s = of(ctx); - if (pane >= s.ptys.len) return false; - const pt = s.ptys[pane]; - if (pt.fd < 0) return false; - return host_io.killJob(pt.pid, pt.fd); - } - - fn writeFile(ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void { - const s = of(ctx); - host_io.writeFile(s.core, &s.watches, pane, path, bytes); - } - - fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void { - host_io.writeDump(of(ctx).core, bytes); - } - - fn watchFile(ctx: ?*anyopaque, pane: u8, _: []const u8, on: bool, mode: pardes.WatchMode) void { - const s = of(ctx); - if (file_watch.applyEffect(s.core, s.io, s.inotify(), &s.watches, pane, on, mode)) - s.check_files = true; - } - - fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void { - const s = of(ctx); - if (file_watch.applyThemeEffect(s.core, s.gpa, s.inotify(), &s.watches, generation, on, s.in_loop)) - s.check_files = true; - } - - fn dumpThemes(ctx: ?*anyopaque, pane: u8) void { - const s = of(ctx); - host_io.dumpThemes(s.core, s.io, s.gpa, pane); - } - - fn setClipboard(ctx: ?*anyopaque, text: []const u8) void { - const s = of(ctx); - s.core.fallback.setClipboard(text); - s.broadcast(.{ .set_clipboard = text }); - } - - fn readClipboard(ctx: ?*anyopaque) void { - const s = of(ctx); - if (s.origins()) |c| return s.send(c, .read_clipboard); - s.core.update(.{ .paste = s.core.fallback.clipboard.items }); - } - - fn openLink(ctx: ?*anyopaque, url: []const u8) void { - const s = of(ctx); - if (s.origins()) |c| return s.send(c, .{ .open_link = url }); - // No frontend: the session's own desktop is nobody's to open a - // browser on. Kept, and said, so a script sees the look land. - s.core.fallback.setLink(url); - var said: [320]u8 = undefined; - s.core.setMessage(s.core.active, std.fmt.bufPrint(&said, "look: {s}: no frontend attached to open it in a browser", .{url}) catch "look: no frontend attached to open a link"); - } - - fn detach(ctx: ?*anyopaque) void { - const s = of(ctx); - if (s.origins()) |c| s.send(c, .detach); - } - - /// A 9P request changed the core: wake the poll loop to draw it. - fn wakeNinep(ctx: ?*anyopaque) void { - const s = of(ctx); - s.ninep_wake.store(true, .release); - s.signal(); - } - - fn watched(ctx: ?*anyopaque) bool { - const s = of(ctx); - for (&s.clients) |*c| if (c.attached) return true; - return false; - } - - fn pollFrame(ctx: ?*anyopaque) void { - const s = of(ctx); - if (s.ninep) |l| s.ninep_pending = l.tick().pending; - for (&s.ptys, 0..) |*pt, pane| { - if (pt.fd < 0) continue; - var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.ttyCwd(pt.pid, pt.fd, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(pane, cwd); - } - } - - fn closePaneShell(ctx: ?*anyopaque, pane: u8) void { - const s = of(ctx); - if (pane < s.ptys.len) s.closePty(pane); - } - - fn closePty(s: *Session, pane: u8) void { - const pt = &s.ptys[pane]; - if (pt.fd < 0) return; - _ = libc.close(pt.fd); - pt.fd = -1; - pt.out.deinit(s.gpa); - pt.out = .empty; - if (pt.pid == 0) return; - _ = libc.kill(pt.pid, libc.SIG.HUP); - pt.kill_at = std.Io.Clock.awake.now(s.io).toMilliseconds() + 100; - s.harvest(); - if (pt.pid == 0) return; - for (&s.retired_shells) |*shell| if (shell.pid == 0) { - shell.* = .{ .pid = pt.pid, .kill_at = pt.kill_at }; - pt.pid = 0; - pt.kill_at = 0; - return; - }; - } - - fn flushPty(s: *Session, pane: u8) void { - const pt = &s.ptys[pane]; - var off: usize = 0; - while (off < pt.out.items.len) { - const n = libc.write(pt.fd, pt.out.items.ptr + off, pt.out.items.len - off); - if (n < 0) switch (libc.errno(n)) { - .INTR => continue, - .AGAIN => break, - else => { - s.readPty(pane); - if (s.ptys[pane].fd >= 0) s.paneEof(pane); - return; - }, - }; - if (n == 0) break; - off += @intCast(n); - } - if (off == 0) return; - if (off == pt.out.items.len) { - pt.out.clearRetainingCapacity(); - return retire(s.gpa, &pt.out); - } - std.mem.copyForwards(u8, pt.out.items, pt.out.items[off..]); - pt.out.items.len -= off; - } - - fn readPty(s: *Session, pane: u8) void { - var buf: [pty_chunk]u8 = undefined; - const got = libc.read(s.ptys[pane].fd, &buf, buf.len); - if (got == 0) return s.paneEof(pane); - if (got < 0) return switch (libc.errno(got)) { - .INTR, .AGAIN => {}, - else => s.paneEof(pane), - }; - s.core.update(.{ .output = .{ .pane = pane, .bytes = buf[0..@intCast(got)] } }); - } - - fn paneEof(s: *Session, pane: u8) void { - const pt = &s.ptys[pane]; - // A command's pty stays open until its child has exited: closing - // it would hang up one that runs on without it. - // Its exit, if it came first, is told now its output is in. - if (pt.cmd.watched) return host_io.commandEof(s.core, &s.ptys, pane, s, closeWatched); - // Unwatched, a command's exit is read here, as its end, and a - // shell's, for a run waiting on it and the log. - const unwatched = if (s.core.panes[pane]) |pn| pn.command != null else false; - const status = host_io.exitStatus(pt.pid, 100); - if (status != null) pt.pid = 0; // reaped: no shell to retire - s.closePty(pane); - s.harvest(); - if (unwatched or status != null) s.core.update(.{ .exited = .{ .pane = pane, .status = status } }); - s.core.update(.{ .eof = .{ .pane = pane } }); - } - - /// The command panes' exits, told once their output is in (host_io). - fn takeExits(s: *Session) void { - host_io.takeExits(s.core, &s.ptys, s, closeWatched); - } - - fn closeWatched(s: *Session, id: usize) void { - s.closePty(@intCast(id)); - } - - fn harvest(s: *Session) void { - const now = std.Io.Clock.awake.now(s.io).toMilliseconds(); - for (&s.retired_shells) |*shell| host_io.reapRetired(&shell.pid, &shell.kill_at, now); - for (&s.ptys) |*pty| if (pty.fd < 0) host_io.reapRetired(&pty.pid, &pty.kill_at, now); - } - - fn inotify(s: *Session) c_int { - if (s.inotify_fd >= 0) return s.inotify_fd; - s.inotify_fd = file_watch.init(true); - return s.inotify_fd; - } - - fn drainInotify(s: *Session) void { - if (file_watch.drain(s.inotify_fd)) s.check_files = true; - } - - fn reloadWatched(s: *Session) void { - if (!s.check_files) return; - s.check_files = false; - s.shaders.recheck(); - for (0..reload_retries) |_| { - if (!file_watch.reloadChanged(s.core, s.io, s.gpa, &s.watches)) return; - } - } - - fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { - const s = of(ctx); - for (&s.clients) |*c| { - if (!c.attached) continue; - if (c.out.items.len != 0) continue; - s.sendFrame(c, surface); - } - } - - fn sendFrame(s: *Session, c: *Client, surface: *const pardes.Surface) void { - const cells = surface.cells; - const want = wire.frameBound(surface.cols, surface.rows) + wire.layersBound(&surface.body_layers, &surface.tag_layers, surface.regionList()); - s.scratch.ensureTotalCapacity(s.gpa, want) catch return s.close(c, .oom); - const prev: []const pardes.Cell = if (c.need_full or c.mirror.items.len != cells.len) - &.{} - else - c.mirror.items; - const cursor: ?wire.Cursor = if (surface.cursor) |cur| - .{ .x = cur.x, .y = cur.y, .bar = cur.bar } - else - null; - const bytes = wire.encodeFrameLayers( - s.scratch.allocatedSlice()[0..want], - surface.cols, - surface.rows, - cursor, - surface.pointer_shape, - cells, - prev, - &surface.body_layers, - &surface.tag_layers, - surface.regionList(), - surface.chrome, - ) catch |err| { - log.debug("frame {d}x{d} not encodable: {t}", .{ surface.cols, surface.rows, err }); - return; - }; - s.queue(c, bytes); - if (c.fd < 0) return; // the queue closed it; the mirror went with it - c.mirror.resize(s.gpa, cells.len) catch return s.close(c, .oom); - @memcpy(c.mirror.items, cells); - c.need_full = false; - } - - fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { - const s = of(ctx); - // Both before the poll: a wake that landed since the last frame has - // had its byte drained here and must not be waited for again. - const drained = s.drainCompletions(true); - const completed = s.ninep_wake.swap(false, .acq_rel) or drained; - for (&s.clients) |*c| if (c.fd >= 0) s.flush(c); - const regridded = s.reconcile(); - // After the greeting reconcile sends: a chain before it is refused. - const compiled = s.sendPost(); - // Nobody attached, nobody to dismiss a message: the loop expires - // them itself, between the editor's steps (expireUnattended). - s.core.unattended = s.primary() == null; - pardes.Messages.expireUnattended(s.core); - - const now = std.Io.Clock.awake.now(s.io).toMilliseconds(); - var fds: [poll_slots]libc.pollfd = undefined; - var src: [poll_slots]Source = undefined; - var n: usize = 0; - if (s.wake[0] >= 0) { - fds[n] = .{ .fd = s.wake[0], .events = poll_in, .revents = 0 }; - src[n] = .completion; - n += 1; - } - const watching_listener = s.listener >= 0 and now >= s.accept_paused_ms; - if (watching_listener) { - fds[n] = .{ .fd = s.listener, .events = poll_in, .revents = 0 }; - src[n] = .listener; - n += 1; - } - for (&s.clients, 0..) |*c, i| { - if (c.fd < 0) continue; - fds[n] = .{ - .fd = c.fd, - .events = if (c.out.items.len != 0) poll_in | poll_out else poll_in, - .revents = 0, - }; - src[n] = .{ .client = @intCast(i) }; - n += 1; - } - for (&s.ptys, 0..) |*pt, pane| { - if (pt.fd < 0 or pt.cmd.eof) continue; - fds[n] = .{ - .fd = pt.fd, - .events = if (pt.out.items.len != 0) poll_in | poll_out else poll_in, - .revents = 0, - }; - src[n] = .{ .pty = @intCast(pane) }; - n += 1; - } - if (s.inotify_fd >= 0) { - fds[n] = .{ .fd = s.inotify_fd, .events = poll_in, .revents = 0 }; - src[n] = .inotify; - n += 1; - } - if (s.ninep) |l| { - // Unix and TCP connections run on cloud9's runner, which wakes - // this loop through `wake`; only QUIC is polled here. - if (comptime ninep_io.quic_enabled) { - if (l.quic) |*listener| { - fds[n] = listener.poll(); - src[n] = .ninep_quic; - n += 1; - } - } - } - // The wait is the 9P connections' turn with the core. - if (n == 0) { - pardes.turn.rest(); - s.io.sleep(.fromMilliseconds(if (timeout_ms == 0) 16 else timeout_ms), .awake) catch {}; - pardes.turn.wake(); - if (timeout_ms != 0) if (s.test_clock) |*virtual| { - virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*); - }; - return; - } - var timeout: c_int = if (timeout_ms == 0) -1 else @intCast(@min(timeout_ms, std.math.maxInt(c_int))); - if (s.nextWake(now)) |due| timeout = if (timeout < 0) due else @min(timeout, due); - if (completed or compiled or s.check_files or regridded or s.ninep_pending) timeout = 0; - pardes.turn.rest(); - const ready = libc.poll(&fds, @intCast(n), timeout); - pardes.turn.wake(); - if (ready > 0) s.dispatch(fds[0..n], src[0..n]); - // The core's wake ran out; the pump advances it from `now`. - if (timeout_ms != 0 and std.Io.Clock.awake.now(s.io).toMilliseconds() -| now >= timeout_ms) if (s.test_clock) |*virtual| { - virtual.* = @max(virtual.*, s.core.nextWake() orelse virtual.*); - }; - _ = s.drainCompletions(true); - s.takeExits(); - s.expire(std.Io.Clock.awake.now(s.io).toMilliseconds()); - s.harvest(); - s.reloadWatched(); - _ = s.reconcile(); - } - - fn dispatch(s: *Session, fds: []const libc.pollfd, src: []const Source) void { - for (fds, src) |pfd, source| switch (source) { - .listener => if (pfd.revents != 0) s.accept(), - .completion => {}, - .client => |i| { - const c = &s.clients[i]; - if (c.fd < 0) continue; - if (pfd.revents & poll_out != 0) s.flush(c); - if (c.fd < 0) continue; - if (pfd.revents & poll_in != 0) { - s.receive(c, i); - } else if (pfd.revents & (poll_hup | poll_err | poll_nval) != 0) { - s.close(c, .peer); - } - }, - .pty => |pane| { - if (s.ptys[pane].fd < 0) continue; - if (pfd.revents & poll_out != 0) s.flushPty(pane); - if (s.ptys[pane].fd < 0) continue; - if (pfd.revents & poll_in != 0) { - s.readPty(pane); - } else if (pfd.revents & (poll_hup | poll_err | poll_nval) != 0) { - s.paneEof(pane); - } - }, - .inotify => if (pfd.revents & poll_in != 0) s.drainInotify(), - .ninep_quic => {}, - }; - } - - fn nextWake(s: *const Session, now: i64) ?c_int { - var due: ?i64 = null; - for (s.retired_shells) |shell| if (shell.pid != 0) { - due = now + 10; - break; - }; - for (s.ptys) |pty| if (pty.fd < 0 and pty.pid != 0) { - due = now + 10; - break; - }; - for (&s.clients) |*c| { - if (c.fd < 0 or c.attached) continue; - const at = c.accepted_ms + @as(i64, s.greet_deadline_ms); - due = if (due) |d| @min(d, at) else at; - } - if (s.listener >= 0 and s.accept_paused_ms > now) - due = if (due) |d| @min(d, s.accept_paused_ms) else s.accept_paused_ms; - if (s.ninep) |l| { - if (l.nextDue()) |ms| { - const at9 = now + ms; - due = if (due) |d| @min(d, at9) else at9; - } - } - // Unattended messages go on the clock: look again in a while. - if (s.core.unattended) for (s.core.panes) |slot| if (slot) |pane| if (pane.msg_len > 0) { - due = if (due) |d| @min(d, now + 100) else now + 100; - break; - }; - const at = due orelse return null; - return @intCast(@max(0, @min(at - now, std.math.maxInt(c_int)))); - } - - fn expire(s: *Session, now: i64) void { - if (now == 0) return; // no clock: enforce nothing rather than everything - for (&s.clients) |*c| { - if (c.fd < 0 or c.attached) continue; - if (now - c.accepted_ms >= s.greet_deadline_ms) s.close(c, .silent); - } - } - - fn accept(s: *Session) void { - for (0..max_clients + 1) |_| { - const fd = libc.accept(s.listener, null, null); - if (fd < 0) { - switch (libc.errno(fd)) { - .AGAIN, .INTR, .CONNABORTED => return, - else => { - s.accept_paused_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() + accept_pause_ms; - return; - }, - } - } - ninep_io.setCloexec(fd); - _ = setNonblock(fd); - if (comptime darwin) { - const on: c_int = 1; - _ = libc.setsockopt(fd, libc.SOL.SOCKET, libc.SO.NOSIGPIPE, &on, @sizeOf(c_int)); - } - const slot = for (&s.clients, 0..) |*c, i| { - if (c.fd < 0) break i; - } else { - s.refuseFd(fd, .full); - _ = libc.close(fd); - continue; - }; - s.clients[slot] = .{ .fd = fd, .accepted_ms = std.Io.Clock.awake.now(s.io).toMilliseconds() }; - } - } - - fn receive(s: *Session, c: *Client, slot: u8) void { - var buf: [read_chunk]u8 = undefined; - const got = libc.read(c.fd, &buf, buf.len); - if (got == 0) return s.close(c, .peer); // clean EOF: the frontend left - if (got < 0) return switch (libc.errno(got)) { - .INTR, .AGAIN => {}, - else => s.close(c, .read), - }; - c.in.appendSlice(s.gpa, buf[0..@intCast(got)]) catch return s.close(c, .oom); - s.account(); - if (c.fd < 0) return; // it was this one - s.consume(c, slot); - } - - fn consume(s: *Session, c: *Client, slot: u8) void { - var off: usize = 0; - while (true) { - const found = wire.framed(c.in.items[off..]) catch return s.close(c, .protocol); - const msg = found orelse break; - s.apply(c, slot, msg.tag, msg.payload) catch return s.close(c, .protocol); - if (c.fd < 0) return; // apply closed it, buffers and all - off += msg.total; - } - if (off == 0) return; - if (off == c.in.items.len) { - c.in.clearRetainingCapacity(); - return retire(s.gpa, &c.in); - } - std.mem.copyForwards(u8, c.in.items, c.in.items[off..]); - c.in.items.len -= off; - } - - fn apply(s: *Session, c: *Client, slot: u8, tag: u8, payload: []const u8) wire.Error!void { - if (tag == @backingInt(wire.ClientTag.hello)) { - if (c.attached) return error.BadValue; - const claimed = try wire.helloVersion(payload); - if (claimed != wire.version) { - log.debug("frontend speaks protocol {d}, this session speaks {d}", .{ claimed, wire.version }); - return s.refuse(c, .version); - } - } - switch (try wire.decodeClient(tag, payload)) { - .hello => |h| { - if (s.core.quit) return s.refuse(c, .quitting); - c.cols = h.cols; - c.rows = h.rows; - c.attached = true; - c.need_full = true; - c.greet = true; - // Unwatched until now, it may have drawn nothing: a frame - // for the one attaching (watched). - s.core.needs_frame = true; - }, - .bye => s.close(c, .bye), - .event => |ev| { - if (!c.attached) return error.BadValue; - switch (ev) { - .resize => |r| { - c.cols = r.cols; - c.rows = r.rows; - if (r.row_metrics) |metrics| s.core.row_metrics = metrics; - }, - else => { - s.origin = slot; - s.core.update(ev); - }, - } - }, - } - } - - fn reconcile(s: *Session) bool { - var cols: u16 = 0; - var rows: u16 = 0; - for (&s.clients) |*c| { - if (!c.attached) continue; - cols = if (cols == 0) c.cols else @min(cols, c.cols); - rows = if (rows == 0) c.rows else @min(rows, c.rows); - } - var regridded = false; - // Against the core's size too: a root ctl `size` may have set it - // while nobody was attached. - if (cols != 0 and (cols != s.cols or rows != s.rows or cols != s.core.screen_w or rows != s.core.screen_h)) { - s.cols = cols; - s.rows = rows; - for (&s.clients) |*c| c.need_full = true; - s.core.update(.{ .resize = .{ .cols = cols, .rows = rows } }); - regridded = true; - } - for (&s.clients, 0..) |*c, i| { - if (!c.greet) continue; - c.greet = false; - s.send(c, .{ .welcome = .{ .slot = @intCast(i), .cols = s.cols, .rows = s.rows } }); - } - return regridded; - } - - /// Compiles the post chain's files (a finished compile wakes the loop - /// through `wake`) and sends each attached frontend the chain it - /// has not seen: on attach, and when it or a file's SPIR-V changes. True - /// when a compile was taken in, so the loop draws what it said. - /// ponytail: a terminal frontend is sent it too and drops it; a hello - /// saying which frontend it is would spare those bytes. - fn sendPost(s: *Session) bool { - const files = for (s.core.settings.post.list()) |entry| { - if (entry.scene == null) break true; - } else false; - const compiled = s.shaders.sync(s.gpa, s.io, s.core, if (files) s.inotify() else s.inotify_fd, &s.watches, .{ .ctx = s, .call = wakeForExit }); - const key = @as(u64, s.shaders.revision) << 8 | @backingInt(s.core.settings.shader_animation); - var chain: wire.Post = .{ .animation = s.core.settings.shader_animation, .len = s.core.settings.post.len }; - _ = s.shaders.view(&s.core.settings.post, &chain.passes); - for (&s.clients) |*c| { - if (!c.attached or c.greet or c.post_sent == key) continue; - c.post_sent = key; - s.send(c, .{ .post = chain }); - } - return compiled; - } - - fn send(s: *Session, c: *Client, msg: wire.ServerMsg) void { - const want = wire.serverBound(msg); - s.scratch.ensureTotalCapacity(s.gpa, want) catch return s.close(c, .oom); - const bytes = wire.encodeServer(s.scratch.allocatedSlice()[0..want], msg) catch |err| { - log.debug("message {t} not encodable: {t}", .{ msg, err }); - return; - }; - s.queue(c, bytes); - } - - /// A client's queued output is at most `out_backlog + wire.max_payload`: - /// past the backlog it is closed, and one frame lands on top of it first - /// (a frame bigger than the backlog must still go out whole). - fn queue(s: *Session, c: *Client, bytes: []const u8) void { - if (c.out.items.len > out_backlog) return s.close(c, .backlog); - s.account(); - if (c.fd < 0) return; // the fattest peer was this one - c.out.appendSlice(s.gpa, bytes) catch return s.close(c, .oom); - s.flush(c); - } - - fn account(s: *Session) void { - var total: usize = 0; - var worst: ?*Client = null; - var worst_bytes: usize = 0; - for (&s.clients) |*c| { - if (c.fd < 0) continue; - const held = c.in.items.len + c.out.items.len; - total += held; - if (held > worst_bytes) { - worst_bytes = held; - worst = c; - } - } - if (total <= session_backlog) return; - if (worst) |c| s.close(c, .backlog); - } - - fn flush(s: *Session, c: *Client) void { - var off: usize = 0; - while (off < c.out.items.len) { - const n = libc.send(c.fd, c.out.items.ptr + off, c.out.items.len - off, nosignal); - if (n < 0) switch (libc.errno(n)) { - .INTR => continue, - .AGAIN => break, - else => return s.close(c, .write), - }; - if (n == 0) break; - off += @intCast(n); - } - if (off == 0) return; - if (off == c.out.items.len) { - c.out.clearRetainingCapacity(); - return retire(s.gpa, &c.out); - } - std.mem.copyForwards(u8, c.out.items, c.out.items[off..]); - c.out.items.len -= off; - } - - fn refuse(s: *Session, c: *Client, why: wire.Refusal) void { - s.refuseFd(c.fd, why); - s.close(c, .refused); - } - - fn refuseFd(_: *Session, fd: c_int, why: wire.Refusal) void { - var buf: [wire.header_len + 1]u8 = undefined; - // unreachable: a refusal is a header and one byte, which buf is sized to - const bytes = wire.encodeServer(&buf, .{ .refuse = why }) catch unreachable; - var off: usize = 0; - while (off < bytes.len) { - const n = libc.send(fd, bytes.ptr + off, bytes.len - off, nosignal); - if (n < 0 and libc.errno(n) == .INTR) continue; - if (n <= 0) break; // it left before hearing why; nothing to do - off += @intCast(n); - } - } - - fn close(s: *Session, c: *Client, why: Closed) void { - if (c.fd < 0) return; - log.debug("frontend detached: {t}", .{why}); - _ = libc.close(c.fd); - c.in.deinit(s.gpa); - c.out.deinit(s.gpa); - c.mirror.deinit(s.gpa); - const gone = s.slotOf(c); - if (s.origin) |i| if (i == gone) { - s.origin = null; - }; - c.* = .{}; - } -}; - -/// SIGTERM, SIGINT or SIGHUP: quit as Exit does, through the loop, so its -/// sockets are unlinked on the way out rather than left for the next -/// session to trip on. The handler only sets the flag and wakes the loop. -var quit_signalled = std.atomic.Value(bool).init(false); -var quit_wake: c_int = -1; - -fn onQuitSignal(_: std.posix.SIG) callconv(.c) void { - quit_signalled.store(true, .release); - if (quit_wake >= 0) _ = libc.send(quit_wake, "q", 1, nosignal); -} - -/// The screen of a detached session no frontend has sized. -pub const detached_cols = 160; -pub const detached_rows = 50; - -pub fn run(init: std.process.Init, opts: pardes.Options, name: []const u8) !void { - // A name a live session holds: said, exit 1, before anything is made - // (no error trace, no second session racing the first for the socket). - if (comptime supported) { - var path_buf: [path_max]u8 = undefined; - if (sessionPath(&path_buf, name)) |path| if (ninep_io.statNoFollow(path) != null and alive(path)) { - var said: [path_max + 64]u8 = undefined; - const text = 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"; - std.Io.File.stderr().writeStreamingAll(init.io, text) catch {}; - std.process.exit(1); - }; - } - const gpa = init.gpa; - const allocs = pardes.memory.init(gpa); - defer pardes.memory.deinit(); - var options = opts; - // No frontend yet, so no size to take: one roomy enough for a script's - // panes (80x24 left pane/new refused after a few); `size` sets another, - // and an attaching frontend its own. - options.cols = detached_cols; - options.rows = detached_rows; - options.detached_session = true; - options.image_allocator = allocs.image; - options.pdf_allocator = allocs.pdf; - options.tree_sitter_allocator = allocs.tree_sitter; - options.frame_allocator = allocs.frame; - - pardes.image.start(init.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(); - } - - const core = 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); - } else try pardes.Pardes.init(allocs.pardes, options); - var session: Session = .{ - .gpa = gpa, - .worker_gpa = allocs.lsp, - .io = init.io, - .core = core, - .cols = options.cols, - .rows = options.rows, - .prompt_rcs = host_io.Shell.prepare(), - .test_clock = host_io.testClock(), - }; - defer session.core.deinit(); - defer session.deinit(); - try session.initAsync(); - if (!session.listen(name)) { - try std.Io.File.stderr().writeStreamingAll(init.io, "pardes: could not bind a detached session socket\n"); - std.process.exit(1); - } - - session.ninep = ninep_io.listen(init.io, gpa, core, opts.ninep_name, name, opts.ninep_tcp, opts.ninep_quic); - if (session.ninep == null) return error.ListenFailed; - session.ninep.?.wake_ctx = &session; - session.ninep.?.wake = Session.wakeNinep; - - const h = session.host(); - core.host = h; - while (core.nextEffect()) |effect| core.perform(effect); - session.in_loop = true; - quit_wake = session.wake[1]; - const on_quit: std.posix.Sigaction = .{ .handler = .{ .handler = onQuitSignal }, .mask = std.posix.sigemptyset(), .flags = 0 }; - for ([_]std.posix.SIG{ .HUP, .INT, .TERM }) |sig| std.posix.sigaction(sig, &on_quit, null); - defer quit_wake = -1; - while (!session.core.quit and !quit_signalled.load(.acquire)) { - pardes.turn.bump(&pardes.turn.restores); - try session.core.pump(h); - if (session.core.quit) break; - if (session.core.takeRestore()) |path| restore: { - const bytes = filesystem.readRestore(gpa, path, session.core.settings.dump_dir.get()) catch |err| { - session.core.reportError(session.core.active, "Restore", err); - break :restore; - }; - defer gpa.free(bytes); - session.restore(bytes, path) catch |err| session.core.reportError(session.core.active, "Restore", err); - } - } -} - -test "a link looked at with no frontend attached opens no browser, and says so" { - const gpa = std.testing.allocator; - var s: Session = .{ - .gpa = gpa, - .worker_gpa = gpa, - .io = std.testing.io, - .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), - .cols = 40, - .rows = 12, - }; - defer s.core.deinit(); - defer s.deinit(); - Session.openLink(&s, "https://example.com/x"); - try std.testing.expectEqualStrings("https://example.com/x", s.core.fallback.link.items); - const pane = s.core.panes[s.core.active].?; - try std.testing.expectEqualStrings("look: https://example.com/x: no frontend attached to open it in a browser", pane.msg[0..pane.msg_len]); -} - -test "detached queued results preserve current requests and are discarded before Restore" { - const gpa = std.testing.allocator; - var s: Session = .{ - .gpa = gpa, - .worker_gpa = gpa, - .io = std.testing.io, - .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), - .cols = 40, - .rows = 12, - }; - defer s.core.deinit(); - defer s.deinit(); - try s.initAsync(); - while (s.core.nextEffect()) |_| {} - _ = try s.core.setTestFile("saved body\n"); - s.core.lspRequest(0, .status, ""); - const old_id = s.core.lsp_wait.?.id; - s.core.lspRequest(0, .status, ""); - const current_id = s.core.lsp_wait.?.id; - s.inbox.lsp_id = current_id; - s.inbox.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } }; - // The inbox turns the superseded answer away. - s.inbox.push(.{ .lsp = .{ .id = old_id, .rows = try gpa.dupe(u8, "old result\n") } }); - try std.testing.expectEqual(@as(usize, 0), s.inbox.completion_len); - _ = s.drainCompletions(true); - try std.testing.expectEqual(current_id, s.inbox.lsp_task.?.id); - try std.testing.expectEqual(current_id, s.core.lsp_wait.?.id); - - try dump.dumpState(s.core); - const saved = try gpa.dupe(u8, s.core.dump_out.?); - defer gpa.free(saved); - while (s.core.nextEffect()) |_| {} - s.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "queued before restore\n") } }); - const outputs = try gpa.alloc([]u8, 1); - outputs[0] = try gpa.dupe(u8, "old filter output\n"); - try std.testing.expect(s.pipe_tasks.add(.{ .id = 77, .future = .{ .any_future = null, .result = {} } })); - s.inbox.push(.{ .pipe = .{ .id = 77, .success = true, .outputs = outputs } }); - host_io.Inbox.lspStatus(&s.inbox, "old status"); - try s.restore(saved, "test"); - try std.testing.expect(s.inbox.lsp_task == null); - try std.testing.expectEqual(@as(usize, 0), s.pipe_tasks.len); - try std.testing.expect(!s.drainCompletions(true)); - try std.testing.expectEqualStrings("saved body\n", s.core.panes[0].?.file.?.content); - - s.core.lspRequest(0, .status, ""); - const restored_id = s.core.lsp_wait.?.id; - try std.testing.expect(restored_id > current_id); - s.inbox.lsp_id = restored_id; - s.inbox.lsp_task = .{ .id = restored_id, .future = .{ .any_future = null, .result = {} } }; - s.inbox.push(.{ .lsp = .{ .id = current_id, .rows = try gpa.dupe(u8, "late old result\n") } }); - _ = s.drainCompletions(true); - try std.testing.expectEqual(restored_id, s.inbox.lsp_task.?.id); - try std.testing.expectEqual(restored_id, s.core.lsp_wait.?.id); - s.inbox.push(.{ .lsp = .{ .id = restored_id, .rows = &.{} } }); - const host = s.host(); - host.vtable.wait_input.?(host.ctx, 1); - try std.testing.expect(s.inbox.lsp_task == null); - try std.testing.expect(s.core.lsp_wait == null); -} - -test "a detached session's terminal pane takes its shell with it when it closes" { - const gpa = std.testing.allocator; - var s: Session = .{ - .gpa = gpa, - .worker_gpa = gpa, - .io = std.testing.io, - .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), - .cols = 40, - .rows = 12, - }; - defer s.core.deinit(); - defer s.deinit(); - // A stand-in shell: it waits for a signal, and a hangup ends it. - const pid = libc.fork(); - if (pid == 0) while (true) { - _ = libc.poll(&[0]libc.pollfd{}, 0, -1); - }; - try std.testing.expect(pid > 0); - var fds: [2]c_int = undefined; - try std.testing.expectEqual(@as(c_int, 0), libc.pipe(&fds)); - defer _ = libc.close(fds[1]); - s.ptys[3] = .{ .fd = fds[0], .pid = pid }; - const host = s.host(); - host.vtable.close_pty.?(host.ctx, 3); - try std.testing.expect(s.ptys[3].fd < 0); - const deadline = std.Io.Clock.awake.now(std.testing.io).toMilliseconds() + 2000; - while (std.Io.Clock.awake.now(std.testing.io).toMilliseconds() < deadline) { - s.harvest(); - const running = s.ptys[3].pid != 0 or for (s.retired_shells) |shell| { - if (shell.pid == pid) break true; - } else false; - if (!running) break; - _ = libc.poll(&[0]libc.pollfd{}, 0, 5); - } - try std.testing.expect(s.ptys[3].pid == 0); - for (s.retired_shells) |shell| try std.testing.expect(shell.pid != pid); -} - -test "detached worker setup failure completes requests without changing document bytes" { - const gpa = std.testing.allocator; - var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); - var s: Session = .{ - .gpa = gpa, - .worker_gpa = failing.allocator(), - .io = std.testing.io, - .core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }), - .cols = 40, - .rows = 12, - }; - defer s.core.deinit(); - defer s.deinit(); - try s.initAsync(); - while (s.core.nextEffect()) |_| {} - const pane = try s.core.setTestFile("one\n"); - s.core.host = s.host(); - s.core.lspRequest(0, .status, ""); - while (s.core.nextEffect()) |effect| s.core.perform(effect); - try std.testing.expect(s.core.lsp_wait == null); - try std.testing.expect(s.inbox.lsp_task == null); - - pane.body.cur_col = 2; - pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true }; - s.core.update(.{ .key = .{ .cp = '|' } }); - s.core.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - s.core.update(.{ .key = .{ .cp = pardes.Key.enter } }); - try std.testing.expect(s.core.pipe.wait != null); - while (s.core.nextEffect()) |effect| s.core.perform(effect); - try std.testing.expect(s.core.pipe.wait == null); - try std.testing.expectEqual(@as(usize, 0), s.pipe_tasks.len); - try std.testing.expectEqualStrings("one\n", pane.file.?.content); - try std.testing.expect(failing.has_induced_failure); -} - -pub const setCloexec = ninep_io.setCloexec; - -pub fn setNonblock(fd: c_int) bool { - const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0)); - if (flags < 0) return false; - var o: libc.O = @bitCast(@as(u32, @bitCast(flags))); - o.NONBLOCK = true; - return libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))) >= 0; -} - -pub const nosignal: u32 = if (darwin) 0 else libc.MSG.NOSIGNAL; - -pub const poll_in: i16 = @intCast(libc.POLL.IN); -pub const poll_out: i16 = @intCast(libc.POLL.OUT); -pub const poll_hup: i16 = @intCast(libc.POLL.HUP); -pub const poll_err: i16 = @intCast(libc.POLL.ERR); -pub const poll_nval: i16 = @intCast(libc.POLL.NVAL); - -fn retire(gpa: std.mem.Allocator, list: *std.ArrayListUnmanaged(u8)) void { - if (list.items.len != 0 or list.capacity <= idle_retain) return; - list.clearAndFree(gpa); -} - -pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:0]const u8 { - if (name.len == 0) return null; - if (std.mem.indexOfAny(u8, name, "/\x00") != null) return null; - return std.fmt.bufPrintSentinel(buf, "{s}/" ++ prefix ++ "{s}.sock", .{ dir, name }, 0) catch null; -} - -const prefix = "pardes-detached-"; - -pub const path_max = sun_path_len; - -pub fn sessionPath(buf: *[path_max]u8, name: []const u8) ?[:0]const u8 { - if (comptime !supported) return null; - var dir_buf: [sun_path_len:0]u8 = undefined; - const dir = ninep_io.socketDir(&dir_buf) orelse return null; - return socketPath(buf, dir, name); -} - -pub fn vetted(path: [:0]const u8) bool { - if (comptime !supported) return false; - var dir_buf: [sun_path_len:0]u8 = undefined; - const dir = ninep_io.socketDir(&dir_buf) orelse return false; - if (!ours(ninep_io.statNoFollow(dir) orelse return false, s_ifdir)) return false; - return ours(ninep_io.statNoFollow(path) orelse return false, s_ifsock); -} - -const s_ifmt: u32 = 0o170000; -const s_ifdir: u32 = 0o040000; -const s_ifsock: u32 = 0o140000; - -fn ours(st: ninep_io.FileFacts, kind: u32) bool { - if (st.mode & s_ifmt != kind) return false; - if (st.uid != libc.getuid()) return false; - return st.mode & 0o077 == 0; -} - -fn alive(path: [:0]const u8) bool { - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - if (path.len + 1 > addr.path.len) return true; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); - if (fd < 0) return true; - defer _ = libc.close(fd); - ninep_io.setCloexec(fd); - _ = setNonblock(fd); - const rc = libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))); - if (rc == 0) return true; - return libc.errno(rc) != .CONNREFUSED; -} - -fn sweep(dir: [:0]const u8) void { - const d = libc.opendir(dir) orelse return; - defer _ = libc.closedir(d); - while (libc.readdir(d)) |ent| { - const name = std.mem.sliceTo(&ent.name, 0); - if (!std.mem.startsWith(u8, name, prefix) or !std.mem.endsWith(u8, name, ".sock")) continue; - var path_buf: [sun_path_len:0]u8 = undefined; - const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{s}", .{ dir, name }, 0) catch continue; - if (!alive(path)) _ = libc.unlink(path); - } -} diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 0e141686..337211b7 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -1,32 +1,67 @@ -//! Detached input and frames use fixed-width little-endian fields and explicit tags. -//! Native struct layout never reaches the wire. +//! The framed stream a replica speaks (docs/next-daemon.md, phase 5), on +//! the daemon's `pardes-replica.sock` and then its session's: fixed-width +//! little-endian fields and explicit tags, the hello and its version, a +//! ping, and the input Events. Native struct layout never reaches the wire. +//! The cell frames that went the other way, and the frontends that drew +//! them, went with phase 3. const std = @import("std"); +const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); -const limits = @import("../memory.zig").limits; -const ShaderBuild = @import("../ShaderBuild.zig"); -const Scene = pardes.config.Runtime.Scene; -const ShaderAnimation = pardes.config.Runtime.ShaderAnimation; +const config = @import("pardes_config"); -/// 7: tag layers gained the `.notice` kind, so a frame can carry a tag value -/// an older frontend would reject mid-session. The greeting is where that has -/// to be refused, which is what this number is for. -/// 8: one layer type: a tag band carries all its rows (not a layer per -/// line), and a frame carries the core's placed regions after its layers. -pub const version: u16 = 12; +/// 19: a client joining again names the view it had in its hello, and a +/// bye says whether it joins again for it (views.away); +/// 18: a replica's input is stamped with its guess of the session's +/// clocks, the time its step reads (phase 6's instant interaction notes); +/// 17: a pointer's hit carries the place it was made on (Layer.Place) and +/// a tag hit its line, which the step resolves it by, not by its frame; +/// 16: a far client says so, the session sends it its blobs' hashes before +/// the rest and only the blobs it asks for, all of it in zstd (phase 6's +/// content cache and zstd notes), and a client says its frontend (a window +/// 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 = 19; -/// The Chrome palette is carried field by field as its declaration lists -/// them, so any change to it changes the wire: bump `version`, then this. -const chrome_layout: u64 = 0xbe608bed776ff502; -comptime { - @setEvalBranchQuota(10_000); - var hash = std.hash.Fnv1a_64.init(); - const info = @typeInfo(pardes.Chrome).@"struct"; - for (info.field_names, info.field_types) |name, T| { - hash.update(name); - hash.update(@typeName(T)); - } - if (hash.final() != chrome_layout) @compileError(std.fmt.comptimePrint("pardes.Chrome changed (layout 0x{x}): bump wire.version, then chrome_layout", .{hash.final()})); -} +/// 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), the walker codec's assumptions +/// (pointer width, byte order) and the tape's shape (tape.shape: what each +/// input and each answer of the host's is laid out as). Not the OS: a Linux +/// client of a FreeBSD session is the main case, and what differs there is +/// the host's, taped, in a shape every OS shares (an error by its name, a +/// stat as fs.OsStat). Nor the build mode: a Debug build steps as a +/// release one does (regexp.zig's one budget). +/// Nor the terminal's build against the window's: their cores step alike, +/// the GUI's own words by the view's frontend (builtins.zig `here`), so a +/// window replicates a terminal's session and a terminal a window's. +/// 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{s} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}\n{s}", + .{ + version, config.version, config.commit orelse "unstamped", + family, config.syntax_highlighting, config.syntax_zig_grammar, + config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf, + config.theme_animation, config.workspace_tag, config.embed_sources, + config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize), + builtin.cpu.arch.endian(), config.deps, pardes.tape.shape, + }, + )); +}; /// EndOfStream: the message ends early; WriteFailed: no room for it. /// ReadFailed is in only because std.Io.Reader says so: a fixed one never @@ -38,20 +73,26 @@ pub const max_rows: u16 = 128; pub const max_payload: u32 = 16 << 20; pub const header_len = 5; // tag:u8, payload length:u32le -const cell_max = 1 + 1 + 7 + 4 + 4 + 1 + 1 + 1; -const run_header = 4 + 2; // start:u32, count:u16 -const run_max = std.math.maxInt(u16); -const frame_head = 1 + 2 + 2 + 6 + 4 + 4; - -// Worst case: every cell changed, each in its own run. -pub fn frameBound(cols: u16, rows: u16) usize { - return header_len + frame_head + @as(usize, cols) * @as(usize, rows) * (run_header + cell_max); -} - // Stable wire numbers; gaps are retired tags. Unknown tags are rejected by the decoder. pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, + ping = 0x03, + /// A replica's word about a terminal's state (src/screen_state.zig): the + /// delta it holds, or rows it asks for. Opaque here. + screen_ack = 0x04, + /// What is the session's digest, in stream order (a replica checks + /// itself against it once quiet). + check = 0x05, + /// A client's first word after its hello: whether it is far (through + /// ssh), so that it holds blobs by their hash and reads zstd. + far = 0x06, + /// The join's blobs a far client lacks, a bit each in `blobs` order. + want = 0x07, + /// The session's clocks (realtime and monotonic, ns) as the client + /// guessed them for the input that follows: its time, which the step + /// reads (src/replica.zig). + stamp = 0x08, key = 0x10, mouse = 0x11, @@ -63,45 +104,10 @@ 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, }; -// Session control precedes display-local effects. -pub const ServerTag = enum(u8) { - welcome = 0x01, - refuse = 0x02, - frame = 0x03, - quit = 0x04, - /// One frontend is done, and the session is NOT over. The `Detach` word, - /// routed back to the frontend whose keystroke ran it (server.zig ORIGIN, - /// ELSE PRIMARY, the same rule `read_clipboard` takes and for the same - /// reason). Every other frontend, the core and the pane shells are - /// untouched, so leaving is success rather than a failure to report. - detach = 0x05, - - set_clipboard = 0x10, - read_clipboard = 0x11, - open_link = 0x12, - /// The post chain this frontend draws its frames through: sent on - /// attach and whenever it or a file's SPIR-V changes. - post = 0x13, -}; - -/// Why the server hung up on a connect. Sent as a `refuse` and followed by a -/// close, so a frontend can say something specific instead of "connection -/// closed". -pub const Refusal = enum(u8) { - /// `Hello.version` is not `version`. The frontend and the core are two - /// builds of pardes. - version = 0x01, - /// Every client slot is taken (see server.zig `max_clients`). - full = 0x02, - /// The core has already quit; this session is ending. - quitting = 0x03, -}; - -const ColorTag = enum(u8) { default = 0x00, index = 0x01, rgb = 0x02 }; -const UlTag = enum(u8) { off = 0x00, single = 0x01, double = 0x02, curly = 0x03, dotted = 0x04, dashed = 0x05 }; -const FontTag = enum(u8) { body = 0x00, tagline = 0x01 }; const ButtonTag = enum(u8) { left = 0x00, middle = 0x01, @@ -116,24 +122,6 @@ const ButtonTag = enum(u8) { }; const KindTag = enum(u8) { press = 0x00, release = 0x01, motion = 0x02, drag = 0x03 }; -/// A full frame resets the receiver's grid to unpainted cells and then applies -/// its runs; a diff applies its runs on top of what is already there. One bit -/// of semantics and one code path, and it is what makes a full frame of a -/// mostly-empty grid cheap. -pub const FrameKind = enum(u8) { full = 0x01, diff = 0x02 }; -const FrameHeader = enum(u8) { full = 0x01, diff = 0x02, full_link = 0x03, diff_link = 0x04 }; - -/// Bit per `CellStyle` bool, packed into one byte. Bit 7 is unassigned and a -/// set bit 7 is a decode error: it is a byte this protocol cannot mean. -const attr_bold: u8 = 1 << 0; -const attr_dim: u8 = 1 << 1; -const attr_italic: u8 = 1 << 2; -const attr_blink: u8 = 1 << 3; -const attr_reverse: u8 = 1 << 4; -const attr_invisible: u8 = 1 << 5; -const attr_strikethrough: u8 = 1 << 6; -const attr_reserved: u8 = 1 << 7; - // --------------------------------------------------------------------------- // messages // --------------------------------------------------------------------------- @@ -143,12 +131,126 @@ const attr_reserved: u8 = 1 << 7; /// when the rest of the layout is the part that changed. pub const Hello = struct { version: u16 = version, - /// This frontend's grid. Never zero — see `Cursor` for why zero is refused - /// rather than clamped. - cols: u16, - rows: u16, + /// The client's `semantics`: the daemon refuses it unless the session's + /// is the same. + semantics: u64 = semantics, + /// Which session (its 9P name, as `--attach=` names it). + session: []const u8, + /// The view it had, joining again for it (a divergence's rejoin, an + /// Upgrade's): the daemon hands it on with the connection. 0xff: none. + view: ?u8 = null, +}; + +/// A session's name on the wire, as the daemon keeps it. +pub const max_name = 128; +/// The longest hello there is, header and all. +pub const hello_max = header_len + 2 + 8 + 2 + max_name + 1; + +/// Daemon or session -> client. +pub const ServerTag = enum(u8) { + /// The hello is accepted and the connection is the session's now: its pid + /// and name. + welcome = 0x01, + /// Refused, said in words; the connection ends. + refused = 0x02, + /// A ping's value back. + pong = 0x03, + /// A piece of the join's dump (src/dump.zig): the welcome says how long + /// it is whole. + dump = 0x04, + /// A unit of the authority's stream (src/replica.zig): its origin, seq, + /// clocks and tape. + unit = 0x05, + /// A terminal's delta, or rows asked for (src/screen_state.zig), applied + /// before the next unit. Opaque here. + screen = 0x06, + /// The session's digest (u64le), after every unit before it. + check = 0x07, + /// Part of the join's list of blobs, to a far client: each one's place + /// in the state, its length and its BLAKE3 hash (src/replica.zig). + blobs = 0x08, + /// A piece of a far client's stream from here on, zstd-compressed: the + /// messages in it are framed as these are. Opaque here. + zstd = 0x09, +}; + +pub const ServerMsg = union(ServerTag) { + /// Who the client is now (its id and view in the session), how long the + /// dump that follows is, and the session's offset from UTC (a replica + /// guesses the session's time of day). A far client is told how many + /// blobs the dump holds (`blobs`). + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, blobs: u32 = 0 }, + refused: []const u8, + pong: u64, + dump: []const u8, + unit: []const u8, + screen: []const u8, + check: u64, + blobs: []const u8, + zstd: []const u8, }; +/// A message's header, for a payload sent on its own (a unit, a dump's +/// piece): no copy of it into a message buffer. +pub fn header(tag: u8, len: usize) [header_len]u8 { + var h: [header_len]u8 = undefined; + h[0] = tag; + std.mem.writeInt(u32, h[1..5], @intCast(len), .little); + return h; +} + +/// The other way: what a client is told, its slices borrowing `payload`. +pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { + var r: std.Io.Reader = .fixed(payload); + const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) { + .welcome => .{ .welcome = .{ + .pid = try r.takeInt(u32, .little), + .session = try r.take(try r.takeInt(u16, .little)), + .client = try r.takeInt(u16, .little), + .view = try r.takeByte(), + .dump_len = try r.takeInt(u64, .little), + .gmtoff = try r.takeInt(i32, .little), + .digest = try r.takeInt(u64, .little), + .semantics = try r.takeInt(u64, .little), + .blobs = try r.takeInt(u32, .little), + } }, + .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, + .pong => .{ .pong = try r.takeInt(u64, .little) }, + .dump => return .{ .dump = payload }, + .unit => return .{ .unit = payload }, + .screen => return .{ .screen = payload }, + .check => .{ .check = try r.takeInt(u64, .little) }, + .blobs => return .{ .blobs = payload }, + .zstd => return .{ .zstd = payload }, + }; + try end(&r); + return msg; +} + +pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { + var w: std.Io.Writer = .fixed(out); + const at = try beginMessage(&w, @backingInt(std.meta.activeTag(msg))); + switch (msg) { + .welcome => |h| { + try w.writeInt(u32, h.pid, .little); + try putSlice16(&w, h.session); + try w.writeInt(u16, h.client, .little); + try w.writeByte(h.view); + try w.writeInt(u64, h.dump_len, .little); + try w.writeInt(i32, h.gmtoff, .little); + try w.writeInt(u64, h.digest, .little); + try w.writeInt(u64, h.semantics, .little); + try w.writeInt(u32, h.blobs, .little); + }, + .refused => |words| try putSlice16(&w, words), + .pong => |v| try w.writeInt(u64, v, .little), + .dump, .unit, .screen, .blobs, .zstd => |bytes| try w.writeAll(bytes), + .check => |v| try w.writeInt(u64, v, .little), + } + try finishMessage(&w, at); + return w.buffered(); +} + /// `Hello.version` alone, read out of the still-undecoded payload. /// /// Separate from `decodeClient` because of the guarantee the fixed offset above @@ -166,223 +268,23 @@ pub fn helloVersion(payload: []const u8) Error!u16 { return r.takeInt(u16, .little); } -pub const Welcome = struct { - version: u16 = version, - /// Which client slot this connection got. Carried because it is what the - /// server's own diagnostics name, so both sides say the same number. - slot: u8, - /// The session's grid as of this attach — the smallest common one, which - /// may be smaller than the `Hello` asked for. See server.zig `geometry`. - cols: u16, - rows: u16, -}; - -pub const Cursor = struct { x: u16, y: u16, bar: bool }; - -/// One frame, head decoded and runs left encoded. The runs are NOT expanded -/// into a slice of cells here: a frame of the largest grid is 1.6 MiB, this -/// union is passed by value, and the receiver already owns the grid the runs -/// belong in. `apply` is the bounds-checked walk. -pub const Frame = struct { - layers: []const u8 = &.{}, - pointer_shape: pardes.Surface.PointerShape = .arrow, - kind: FrameKind, - cols: u16, - rows: u16, - cursor: ?Cursor, - nruns: u32, - runs: []const u8, - - pub fn applyLayers(f: Frame, gpa: std.mem.Allocator, layers: *[pardes.MAX_PANES]pardes.Layer, tags: *[pardes.MAX_TAG_LAYERS]pardes.Layer, regions: *std.ArrayListUnmanaged(pardes.Region), chrome: *pardes.Chrome) !void { - regions.clearRetainingCapacity(); - chrome.* = .{}; - for (layers) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - for (tags) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - errdefer { - for (layers) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - for (tags) |*layer| { - gpa.free(layer.cells); - layer.* = .{}; - } - regions.clearRetainingCapacity(); - chrome.* = .{}; - } - if (f.layers.len == 0) return; - var r: std.Io.Reader = .fixed(f.layers); - const count = try r.takeInt(u16, .little); - if (count > layers.len) return error.Overlong; - for (layers[0..count]) |*layer| { - layer.kind = .body; - layer.id = try r.takeInt(u16, .little); - layer.serial = try r.takeInt(u32, .little); - layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - layer.cols = try r.takeInt(u16, .little); - layer.rows = try r.takeInt(u16, .little); - layer.context_rows = try r.takeInt(u16, .little); - if (layer.id >= pardes.MAX_PANES or layer.cols == 0 or layer.cols > f.cols or - layer.rows == 0 or layer.rows > 2 * max_rows or layer.context_rows > layer.rows or layer.context_rows > limits.wrap_rows or - layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or - layer.viewport.y > f.rows or layer.viewport.h > f.rows - layer.viewport.y) return error.BadValue; - for (0..layer.context_rows) |row| { - if (try getBool(&r)) layer.context_separators.set(row); - } - const cursor = try getCursor(&r, layer.cols, layer.rows); - layer.cursor = if (cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null; - layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); - for (layer.cells) |*cell| cell.* = try decodeCell(&r); - } - const tag_count = try r.takeInt(u16, .little); - if (tag_count > tags.len) return error.Overlong; - for (tags[0..tag_count]) |*layer| { - layer.kind = try getTag(&r, pardes.TagKind); - layer.id = try r.takeInt(u16, .little); - layer.serial = try r.takeInt(u32, .little); - layer.viewport = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - layer.cols = try r.takeInt(u16, .little); - layer.rows = try r.takeInt(u16, .little); - for (&layer.bg) |*channel| channel.* = try r.takeByte(); - if (layer.cols == 0 or layer.viewport.w == 0 or layer.rows == 0 or layer.rows > pardes.MAX_TAG_ROWS or - layer.viewport.h != layer.rows or - layer.viewport.x > f.cols or layer.viewport.w > f.cols - layer.viewport.x or - layer.viewport.y >= f.rows or layer.viewport.h > f.rows - layer.viewport.y or (switch (layer.kind) { - .workspace => layer.id != 0, - .column => layer.id >= pardes.MAX_COLS, - .pane, .notice => layer.id >= pardes.MAX_PANES, - .body => true, - })) return error.BadValue; - const cursor = try getCursor(&r, layer.cols, layer.rows); - layer.cursor = if (cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null; - layer.cells = try gpa.alloc(pardes.Cell, @as(usize, layer.cols) * layer.rows); - for (layer.cells) |*cell| cell.* = try decodeCell(&r); - } - const region_count = try r.takeInt(u16, .little); - if (region_count > pardes.MAX_REGIONS) return error.Overlong; - try regions.ensureTotalCapacity(gpa, region_count); - for (0..region_count) |_| { - var region: pardes.Region = .{ .kind = try getTag(&r, pardes.Region.Kind), .rect = undefined }; - region.owner = try r.takeByte(); - region.serial = try r.takeInt(u32, .little); - region.rect = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .w = try r.takeInt(u16, .little), .h = try r.takeInt(u16, .little) }; - const flags = try r.takeByte(); - if (flags > 3) return error.BadValue; - region.active = flags & 1 != 0; - region.dirty = flags & 2 != 0; - region.thumb_y = try r.takeInt(u16, .little); - region.thumb_h = try r.takeInt(u16, .little); - region.notice = try r.takeByte(); - region.clip = try r.takeInt(u16, .little); - region.lift = @bitCast(try r.takeInt(u32, .little)); - region.line = @bitCast(try r.takeInt(u32, .little)); - // A springy flavour overshoots, and a wind-up dips below. - if (!(region.lift >= -1 and region.lift <= 2)) return error.BadValue; - if (region.rect.x > f.cols or region.rect.w > f.cols - region.rect.x or - region.rect.y > f.rows or region.rect.h > f.rows - region.rect.y or - region.clip > region.rect.w or region.thumb_y > region.rect.h or region.thumb_h > region.rect.h - region.thumb_y) return error.BadValue; - regions.appendAssumeCapacity(region); - } - const info = @typeInfo(pardes.Chrome).@"struct"; - inline for (info.field_names, info.field_types) |name, T| { - const rgb = &@field(chrome, name); - if (T == ?[3]u8) { - if (try getBool(&r)) { - rgb.* = .{ 0, 0, 0 }; - for (&rgb.*.?) |*channel| channel.* = try r.takeByte(); - } - } else if (T == u8) { - rgb.* = try r.takeByte(); - } else if (T == u32) { - rgb.* = try r.takeInt(u32, .little); - } else if (T == f32) { - rgb.* = @bitCast(try r.takeInt(u32, .little)); - if (!std.math.isFinite(rgb.*)) return error.BadValue; - } else if (T == [8]f32) { - for (rgb) |*value| { - value.* = @bitCast(try r.takeInt(u32, .little)); - if (!std.math.isFinite(value.*)) return error.BadValue; - } - } else for (rgb) |*channel| channel.* = try r.takeByte(); - } - try end(&r); - } - - /// Paint this frame into `grid`, which must be exactly `cols * rows` cells - /// — the receiver resizes on a geometry change before applying, and a grid - /// of the wrong size is a receiver bug, not a wire condition. - /// - /// Every run is range-checked against the grid before a single cell is - /// written, so a run claiming to start past the end writes nothing. - pub fn apply(f: Frame, grid: []pardes.Cell) Error!void { - if (grid.len != @as(usize, f.cols) * @as(usize, f.rows)) return error.BadValue; - if (f.kind == .full) @memset(grid, .{}); - var r: std.Io.Reader = .fixed(f.runs); - var i: u32 = 0; - while (i < f.nruns) : (i += 1) { - const start = try r.takeInt(u32, .little); - const count = try r.takeInt(u16, .little); - // A zero-length run is not something `encodeFrameLayers` emits, and - // accepting one would let a peer spend the run budget saying - // nothing. - if (count == 0) return error.BadValue; - // THE bounds check. Written as `count > len - start` rather than - // `start + count > len` because the sum of two attacker-chosen - // 32-bit numbers is the classic way this check is bypassed. - if (start > grid.len or count > grid.len - start) return error.Overlong; - for (grid[start..][0..count]) |*c| c.* = try decodeCell(&r); - } - try end(&r); - } -}; - /// Frontend -> core, decoded. The `Event`'s slices BORROW the payload buffer, /// exactly like the pty chunks the tty host hands to `update`: valid for that /// one call and no longer. pub const ClientMsg = union(enum) { hello: Hello, - bye, + /// The client's end: `again`, it joins again for its view at once. + bye: struct { again: bool = false }, + /// A round trip's probe, answered `pong` with the same value. + ping: u64, + screen_ack: []const u8, + check, + far: bool, + want: []const u8, + stamp: [2]i64, event: pardes.Event, }; -/// Core -> frontend, decoded. Slices borrow the payload buffer the same way. -pub const ServerMsg = union(enum) { - welcome: Welcome, - refuse: Refusal, - frame: Frame, - /// The session is over. Sent before the listener closes so a frontend can - /// exit rather than report a broken pipe. - quit, - /// One frontend is done, and the session is NOT over — see `ServerTag`. - detach, - - set_clipboard: []const u8, - read_clipboard, - open_link: []const u8, - post: Post, -}; - -/// The session's post chain, as a GUI attached to it draws it: its passes in -/// order, each a bundled scene at its level or a Shadertoy file's SPIR-V -/// (compiled by the session, ShaderBuild.zig: a frontend reads no disk and -/// runs no program), and when it animates on its own. Slices borrow the -/// payload. -pub const Post = struct { - animation: ShaderAnimation = .on, - passes: [ShaderBuild.max]ShaderBuild.Pass = @splat(.{}), - len: u8 = 0, - - pub fn list(p: *const Post) []const ShaderBuild.Pass { - return p.passes[0..p.len]; - } -}; - // --------------------------------------------------------------------------- // primitives // --------------------------------------------------------------------------- @@ -491,367 +393,12 @@ pub fn framed(buf: []const u8) Error!?Framed { } // --------------------------------------------------------------------------- -// cells -// --------------------------------------------------------------------------- - -fn putColor(w: *std.Io.Writer, c: pardes.Color) Error!void { - switch (c) { - .default => try w.writeByte(@backingInt(ColorTag.default)), - .index => |i| { - try w.writeByte(@backingInt(ColorTag.index)); - try w.writeByte(i); - }, - .rgb => |v| { - try w.writeByte(@backingInt(ColorTag.rgb)); - try w.writeAll(&v); - }, - } -} - -fn getColor(r: *std.Io.Reader) Error!pardes.Color { - return switch (try getTag(r, ColorTag)) { - .default => .default, - .index => .{ .index = try r.takeByte() }, - .rgb => .{ .rgb = (try r.take(3))[0..3].* }, - }; -} - -const Ul = @FieldType(pardes.CellStyle, "ul"); - -/// The four enum mappings — underline, font role, mouse button, mouse kind — -/// and `clientTag`/`serverTag` further down all have one shape: the WIRE -/// member of the same NAME. So the byte on the socket is still `@intFromEnum` -/// of an explicitly numbered enum in THIS file and never of a core type — -/// reordering `CellStyle.ul` changes nothing — and adding a variant to the -/// core without adding one here does not compile: -/// -/// error: enum 'wire.UlTag' has no member named 'wavy' -/// -/// which is the whole property the six hand-written switches this replaced -/// existed for, in a form that cannot fall out of step. A variant that has to -/// travel under a DIFFERENT name than the core's gets an arm of its own before -/// the `inline else`, exactly as `clientTag` keeps the machine-local reports. -fn putUl(w: *std.Io.Writer, u: Ul) Error!void { - try w.writeByte(switch (u) { - inline else => |t| @backingInt(@field(UlTag, @tagName(t))), - }); -} - -fn getUl(r: *std.Io.Reader) Error!Ul { - return switch (try getTag(r, UlTag)) { - inline else => |t| @field(Ul, @tagName(t)), - }; -} - -fn putFont(w: *std.Io.Writer, f: pardes.FontRole) Error!void { - try w.writeByte(switch (f) { - inline else => |t| @backingInt(@field(FontTag, @tagName(t))), - }); -} - -fn getFont(r: *std.Io.Reader) Error!pardes.FontRole { - return switch (try getTag(r, FontTag)) { - inline else => |t| @field(pardes.FontRole, @tagName(t)), - }; -} - -/// How many bytes `putCell` will write. Exact, because `encodeFrameLayers` weighs it -/// against `run_header` to decide whether to coalesce a run across it. -fn cellSize(c: *const pardes.Cell) usize { - if (c.default) return 1; - return 1 + 1 + c.len + colorSize(c.style.fg) + colorSize(c.style.bg) + 1 + 1 + 1; -} - -fn colorSize(c: pardes.Color) usize { - return switch (c) { - .default => 1, - .index => 2, - .rgb => 4, - }; -} - -/// An unpainted cell is ONE byte: `default` means "the shell renders the -/// terminal's default cell" (surface.zig `Cell`), so its text and style are not -/// merely equal to the defaults, they are not part of the frame at all. -fn putCell(w: *std.Io.Writer, c: *const pardes.Cell) Error!void { - try w.writeByte(@intFromBool(c.default)); - if (c.default) return; - try w.writeByte(c.len); - try w.writeAll(c.grapheme()); - const s = c.style; - try putColor(w, s.fg); - try putColor(w, s.bg); - var attrs: u8 = 0; - if (s.bold) attrs |= attr_bold; - if (s.dim) attrs |= attr_dim; - if (s.italic) attrs |= attr_italic; - if (s.blink) attrs |= attr_blink; - if (s.reverse) attrs |= attr_reverse; - if (s.invisible) attrs |= attr_invisible; - if (s.strikethrough) attrs |= attr_strikethrough; - try w.writeByte(attrs); - try putUl(w, s.ul); - try putFont(w, s.font_role); -} - -fn decodeCell(r: *std.Io.Reader) Error!pardes.Cell { - if (try getBool(r)) return .{}; - var c: pardes.Cell = .{ .default = false }; - const len = try r.takeByte(); - // `Cell.text` is 7 bytes and `grapheme()` slices to `len`. A zero-length - // grapheme is a cell with nothing to draw and no way to advance a column. - if (len == 0 or len > c.text.len) return error.BadValue; - c.len = len; - @memcpy(c.text[0..len], try r.take(len)); - c.style.fg = try getColor(r); - c.style.bg = try getColor(r); - const attrs = try r.takeByte(); - if (attrs & attr_reserved != 0) return error.BadValue; - c.style.bold = attrs & attr_bold != 0; - c.style.dim = attrs & attr_dim != 0; - c.style.italic = attrs & attr_italic != 0; - c.style.blink = attrs & attr_blink != 0; - c.style.reverse = attrs & attr_reverse != 0; - c.style.invisible = attrs & attr_invisible != 0; - c.style.strikethrough = attrs & attr_strikethrough != 0; - c.style.ul = try getUl(r); - c.style.font_role = try getFont(r); - return c; -} - -// --------------------------------------------------------------------------- -// frames -// --------------------------------------------------------------------------- - -fn putCursor(w: *std.Io.Writer, cursor: ?Cursor, cols: u16, rows: u16) Error!void { - // Fixed six bytes whether or not there is a cursor. An optional field - // would save five bytes on a message that is already hundreds, and cost a - // branch on both sides of the wire. - try w.writeByte(@intFromBool(cursor != null)); - const c = cursor orelse Cursor{ .x = 0, .y = 0, .bar = false }; - // Refused here as well as on decode, so this side cannot build a frame its - // own decoder rejects: a dropped frame (sendFrame logs it) leaves a stale - // screen, and a refused one takes the frontend's connection with it. - if (c.x >= cols or c.y >= rows) return error.BadValue; - try w.writeInt(u16, c.x, .little); - try w.writeInt(u16, c.y, .little); - try w.writeByte(@intFromBool(c.bar)); -} - -fn getCursor(r: *std.Io.Reader, cols: u16, rows: u16) Error!?Cursor { - const present = try getBool(r); - const c: Cursor = .{ .x = try r.takeInt(u16, .little), .y = try r.takeInt(u16, .little), .bar = try getBool(r) }; - // THE cursor bounds check. Every other field of a frame is checked against - // the grid and these two were not, and they are the two a frontend indexes - // with directly — `Surface.at(cursor.x, cursor.y)` on a grid this frame - // says is 4x2 is an out-of-bounds write in EVERY frontend, not a wrong - // glyph. Checked whether or not the cursor is present, because the absent - // case is six bytes of padding this encoder writes as zero and a peer that - // fills them with anything else is not speaking this protocol. - if (c.x >= cols or c.y >= rows) return error.BadValue; - return if (present) c else null; -} - -pub fn layersBound(layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region) usize { - var n: usize = 6; - for (layers) |layer| if (layer.rows != 0) { - n += 26 + layer.context_rows + layer.cells.len * cell_max; - }; - for (tags) |layer| if (layer.rows != 0) { - n += 28 + layer.cells.len * cell_max; - }; - return n + regions.len * region_len + chrome_len; -} - -const region_len = 1 + 1 + 4 + 8 + 1 + 2 + 2 + 1 + 2 + 4 + 4; -/// The palette: each colour three bytes, a theme's optional ones a flag -/// before them. -const chrome_len = @typeInfo(pardes.Chrome).@"struct".field_names.len * 4 + 8 * 4; - -/// Encode one frame of `cells`, as a DIFF against `prev` when `prev` is the -/// same grid this receiver last acknowledged, and as a FULL frame otherwise. -/// -/// A late joiner and a resize are the same case and are handled by the same -/// test: nothing on the far side is comparable to this grid, so `prev` is -/// empty or a different length and the frame becomes full. -/// -/// Why a diff at all. Measured against a real `pardes --detach` at 80x24 with -/// the default theme, which paints every cell and gives most of them an rgb -/// pair (14 bytes a cell rather than the 8 an uncoloured one costs): -/// * a full frame is 21965-26699 bytes, depending on what is on screen, -/// * a frame that changes nothing is 20 — the head, and no runs at all, -/// * a one-row change (`Msg hi`) is 54 bytes in one run, and a wordier one -/// 166, -/// * and an IDLE session sends nothing whatever, because the core is asleep -/// in `poll(2)` and produces no frame until something happens. -/// So the diff is worth roughly 500x on the traffic a session actually -/// generates. The byte-count test below asserts the arithmetic on the board's -/// own 56x14 grid with uncoloured cells, where it is exact: 6298 against 56. -/// This transport exists for a frontend on the far end of a slow link, and -/// shipping the Surface every frame would be 3 MiB/s at the animation tick. -pub fn encodeFrameLayers(out: []u8, cols: u16, rows: u16, cursor: ?Cursor, pointer_shape: pardes.Surface.PointerShape, cells: []const pardes.Cell, prev: []const pardes.Cell, layers: []const pardes.Layer, tags: []const pardes.Layer, regions: []const pardes.Region, chrome: pardes.Chrome) Error![]const u8 { - // The protocol's ceiling, enforced by the ENCODER too, and BEFORE the - // assert below so a caller can be told rather than tripped. `max_payload` - // is derived from these two, so a larger grid is a frame this decoder - // refuses as Overlong — and sendFrame's log line already claims that this - // is the error it is catching, which was true of nothing until here. - if (cols == 0 or cols > max_cols or rows == 0 or rows > max_rows) return error.Overlong; - std.debug.assert(cells.len == @as(usize, cols) * @as(usize, rows)); - const full = prev.len != cells.len; - var w: std.Io.Writer = .fixed(out); - const at = try beginMessage(&w, @backingInt(ServerTag.frame)); - const header: FrameHeader = if (pointer_shape == .link) - (if (full) .full_link else .diff_link) - else - (if (full) .full else .diff); - try w.writeByte(@backingInt(header)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, cursor, cols, rows); - const layers_len_at = w.end; - try w.writeInt(u32, 0, .little); - const layers_start = w.end; - if (layers.len != 0 or tags.len != 0 or regions.len != 0) { - var count: u16 = 0; - for (layers) |layer| if (layer.rows != 0) { - count += 1; - }; - try w.writeInt(u16, count, .little); - for (layers) |layer| { - if (layer.rows == 0) continue; - if (layer.context_rows > limits.wrap_rows or layer.context_rows > layer.rows) return error.BadValue; - try w.writeInt(u16, layer.id, .little); - try w.writeInt(u32, layer.serial, .little); - try w.writeInt(u16, layer.viewport.x, .little); - try w.writeInt(u16, layer.viewport.y, .little); - try w.writeInt(u16, layer.viewport.w, .little); - try w.writeInt(u16, layer.viewport.h, .little); - try w.writeInt(u16, layer.cols, .little); - try w.writeInt(u16, layer.rows, .little); - try w.writeInt(u16, layer.context_rows, .little); - for (0..layer.context_rows) |row| try w.writeByte(@intFromBool(layer.context_separators.isSet(row))); - try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); - for (layer.cells) |*cell| try putCell(&w, cell); - } - var tag_count: u16 = 0; - for (tags) |layer| if (layer.rows != 0) { - tag_count += 1; - }; - try w.writeInt(u16, tag_count, .little); - for (tags) |layer| { - if (layer.rows == 0) continue; - if (layer.cells.len != @as(usize, layer.cols) * layer.rows) return error.BadValue; - try w.writeByte(@backingInt(layer.kind)); - try w.writeInt(u16, layer.id, .little); - try w.writeInt(u32, layer.serial, .little); - try w.writeInt(u16, layer.viewport.x, .little); - try w.writeInt(u16, layer.viewport.y, .little); - try w.writeInt(u16, layer.viewport.w, .little); - try w.writeInt(u16, layer.viewport.h, .little); - try w.writeInt(u16, layer.cols, .little); - try w.writeInt(u16, layer.rows, .little); - for (layer.bg) |channel| try w.writeByte(channel); - try putCursor(&w, if (layer.cursor) |c| .{ .x = c.x, .y = c.y, .bar = c.bar } else null, layer.cols, layer.rows); - for (layer.cells) |*cell| try putCell(&w, cell); - } - try w.writeInt(u16, @intCast(regions.len), .little); - for (regions) |region| { - try w.writeByte(@backingInt(region.kind)); - try w.writeByte(region.owner); - try w.writeInt(u32, region.serial, .little); - try w.writeInt(u16, region.rect.x, .little); - try w.writeInt(u16, region.rect.y, .little); - try w.writeInt(u16, region.rect.w, .little); - try w.writeInt(u16, region.rect.h, .little); - try w.writeByte(@as(u8, @intFromBool(region.active)) | @as(u8, @intFromBool(region.dirty)) << 1); - try w.writeInt(u16, region.thumb_y, .little); - try w.writeInt(u16, region.thumb_h, .little); - try w.writeByte(region.notice); - try w.writeInt(u16, region.clip, .little); - try w.writeInt(u32, @bitCast(region.lift), .little); - try w.writeInt(u32, @bitCast(region.line), .little); - } - const info = @typeInfo(pardes.Chrome).@"struct"; - inline for (info.field_names, info.field_types) |name, T| { - const rgb = @field(chrome, name); - if (T == ?[3]u8) { - try w.writeByte(@intFromBool(rgb != null)); - if (rgb) |value| for (value) |channel| try w.writeByte(channel); - } else if (T == u8) { - try w.writeByte(rgb); - } else if (T == u32) { - try w.writeInt(u32, rgb, .little); - } else if (T == f32) { - try w.writeInt(u32, @bitCast(rgb), .little); - } else if (T == [8]f32) { - for (rgb) |value| try w.writeInt(u32, @bitCast(value), .little); - } else for (rgb) |channel| try w.writeByte(channel); - } - } - std.mem.writeInt(u32, w.buffer[layers_len_at..][0..4], @intCast(w.end - layers_start), .little); - const nruns_at = w.end; - try w.writeInt(u32, 0, .little); - - var nruns: u32 = 0; - var i: usize = 0; - while (i < cells.len) { - if (!sendCell(cells, prev, full, i)) { - i += 1; - continue; - } - const start = i; - var run_end = i + 1; - i += 1; - // `i - start` and not `run_end - start`, because the gap lookahead - // below moves `i` ahead of `run_end` by up to `run_header - 1` before - // the next iteration adopts it: bounding the cursor bounds the run, - // and bounding the run afterwards would not. - while (i < cells.len and i - start < run_max) { - if (sendCell(cells, prev, full, i)) { - run_end = i + 1; - i += 1; - continue; - } - // A gap. Re-sending cells the far side already has is cheaper than - // a second run header whenever their encoded size adds up to less - // than one — true for short stretches of unpainted cells at a byte - // each, and false as soon as one painted cell (eight bytes at its - // smallest) is in the way. So the lookahead can never need to pass - // `run_header - 1` cells. - var gap: usize = 0; - var j = i; - while (j < cells.len and gap < run_header) : (j += 1) { - if (sendCell(cells, prev, full, j)) break; - gap += cellSize(&cells[j]); - } - if (j >= cells.len or gap >= run_header) break; - i = j; - } - try w.writeInt(u32, @intCast(start), .little); - try w.writeInt(u16, @intCast(run_end - start), .little); - for (cells[start..run_end]) |*c| try putCell(&w, c); - nruns += 1; - i = run_end; - } - std.mem.writeInt(u32, w.buffer[nruns_at..][0..4], nruns, .little); - try finishMessage(&w, at); - return w.buffered(); -} - -fn sendCell(cells: []const pardes.Cell, prev: []const pardes.Cell, full: bool, i: usize) bool { - // Full: the receiver reset the grid, so unpainted cells are already right. - // Diff: anything the receiver cannot already be showing. - if (full) return !cells[i].default; - return !cells[i].visuallyEqual(&prev[i]); -} - -// --------------------------------------------------------------------------- // frontend -> core // --------------------------------------------------------------------------- /// Which tag a message travels under: the `ClientTag` of the same NAME, so a -/// new `Event` variant does not compile until it has a number here. See -/// `putUl` for the error it produces and why this is not a written-out table. +/// new `Event` variant does not compile until it has a number here, rather +/// than a written-out table that can drift. fn clientTag(msg: ClientMsg) ClientTag { return switch (msg) { .event => |ev| switch (ev) { @@ -885,6 +432,17 @@ fn getMetrics(r: *std.Io.Reader) Error!?pardes.RowMetrics { return m; } +fn putPlace(w: *std.Io.Writer, at: pardes.Layer.Place) Error!void { + for ([_]u16{ at.viewport.x, at.viewport.y, at.viewport.w, at.viewport.h, at.cols, at.rows, at.context_rows }) |n| try w.writeInt(u16, n, .little); +} + +fn getPlace(r: *std.Io.Reader) Error!pardes.Layer.Place { + var n: [7]u16 = undefined; + for (&n) |*x| x.* = try r.takeInt(u16, .little); + if (n[6] > n[5]) return error.BadValue; + return .{ .viewport = .{ .x = n[0], .y = n[1], .w = n[2], .h = n[3] }, .cols = n[4], .rows = n[5], .context_rows = n[6] }; +} + fn putBodyHit(w: *std.Io.Writer, hit: ?pardes.Mouse.BodyHit) Error!void { try w.writeByte(@intFromBool(hit != null)); if (hit) |h| { @@ -894,12 +452,13 @@ fn putBodyHit(w: *std.Io.Writer, hit: ?pardes.Mouse.BodyHit) Error!void { try w.writeInt(u32, @bitCast(h.pixel_x), .little); try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); + try putPlace(w, h.place); } } fn getBodyHit(r: *std.Io.Reader) Error!?pardes.Mouse.BodyHit { if (!try getBool(r)) return null; - const hit: pardes.Mouse.BodyHit = .{ .serial = try r.takeInt(u32, .little), .row = try r.takeInt(u16, .little), .col = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue }; + const hit: pardes.Mouse.BodyHit = .{ .serial = try r.takeInt(u32, .little), .row = try r.takeInt(u16, .little), .col = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue, .place = try getPlace(r) }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; return hit; } @@ -911,9 +470,11 @@ fn putTagHit(w: *std.Io.Writer, hit: ?pardes.TagHit) Error!void { try w.writeInt(u16, h.id, .little); try w.writeInt(u32, h.serial, .little); try w.writeInt(u16, h.col, .little); + try w.writeInt(u16, h.line, .little); try w.writeInt(u32, @bitCast(h.pixel_x), .little); try w.writeInt(u32, @bitCast(h.pixel_y), .little); try putMetrics(w, h.metrics); + try putPlace(w, h.place); } } @@ -924,9 +485,11 @@ fn getTagHit(r: *std.Io.Reader) Error!?pardes.TagHit { .id = try r.takeInt(u16, .little), .serial = try r.takeInt(u32, .little), .col = try r.takeInt(u16, .little), + .line = try r.takeInt(u16, .little), .pixel_x = try getF32(r), .pixel_y = try getF32(r), .metrics = (try getMetrics(r)) orelse return error.BadValue, + .place = try getPlace(r), }; if (!std.math.isFinite(hit.pixel_x) or !std.math.isFinite(hit.pixel_y)) return error.BadValue; return hit; @@ -938,10 +501,17 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { switch (msg) { .hello => |h| { try w.writeInt(u16, h.version, .little); - try w.writeInt(u16, h.cols, .little); - try w.writeInt(u16, h.rows, .little); + try w.writeInt(u64, h.semantics, .little); + if (h.session.len > max_name) return error.Overlong; + try putSlice16(&w, h.session); + try w.writeByte(h.view orelse 0xff); }, - .bye => {}, + .bye => |b| try w.writeByte(@intFromBool(b.again)), + .ping => |v| try w.writeInt(u64, v, .little), + .screen_ack, .want => |bytes| try w.writeAll(bytes), + .check => {}, + .far => |far| try w.writeByte(@intFromBool(far)), + .stamp => |s| for (s) |v| try w.writeInt(i64, v, .little), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -986,6 +556,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, }, @@ -999,10 +574,24 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { const msg: ClientMsg = switch (std.enums.fromInt(ClientTag, tag) orelse return error.BadTag) { .hello => .{ .hello = .{ .version = try r.takeInt(u16, .little), - .cols = try getCols(&r), - .rows = try getRows(&r), + .semantics = try r.takeInt(u64, .little), + .session = blk: { + const n = try r.takeInt(u16, .little); + if (n > max_name) return error.BadValue; + break :blk try r.take(n); + }, + .view = switch (try r.takeByte()) { + 0xff => null, + else => |k| k, + }, } }, - .bye => .bye, + .bye => .{ .bye = .{ .again = try getBool(&r) } }, + .ping => .{ .ping = try r.takeInt(u64, .little) }, + .screen_ack => return .{ .screen_ack = payload }, + .check => .check, + .far => .{ .far = try getBool(&r) }, + .want => return .{ .want = payload }, + .stamp => .{ .stamp = .{ try r.takeInt(i64, .little), try r.takeInt(i64, .little) } }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -1049,91 +638,27 @@ 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; } -// --------------------------------------------------------------------------- -// core -> frontend -// --------------------------------------------------------------------------- - -/// ...and the same rule in the same shape: the `ServerTag` of the same name. -fn serverTag(msg: ServerMsg) ServerTag { - return switch (msg) { - inline else => |_, t| @field(ServerTag, @tagName(t)), - }; -} - -/// Every server message EXCEPT a frame, which has its own encoder because its -/// payload is a walk of two grids rather than a value (see `encodeFrameLayers`). -pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { - var w: std.Io.Writer = .fixed(out); - const at = try beginMessage(&w, @backingInt(serverTag(msg))); - switch (msg) { - .welcome => |v| { - try w.writeInt(u16, v.version, .little); - try w.writeByte(v.slot); - try w.writeInt(u16, v.cols, .little); - try w.writeInt(u16, v.rows, .little); - }, - .refuse => |why| try w.writeByte(@backingInt(why)), - // A frame's runs are not a value this union can hold; the server calls - // encodeFrameLayers directly and this arm exists so the switch stays - // exhaustive over ServerMsg. - .frame => return error.BadValue, - .quit, .detach => {}, - .set_clipboard => |t| try putSlice32(&w, t), - .read_clipboard => {}, - .open_link => |u| try putSlice16(&w, u), - .post => |p| { - try w.writeByte(@backingInt(p.animation)); - try w.writeByte(p.len); - for (p.list()) |pass| { - // 0 a file, else a bundled scene and one. - try w.writeByte(if (pass.scene) |scene| @as(u8, @backingInt(scene)) + 1 else 0); - try w.writeByte(pass.level); - try w.writeByte(@intFromBool(pass.animated)); - try w.writeInt(u32, pass.revision, .little); - try putSlice16(&w, pass.path); - try putSlice32(&w, pass.spirv); - } - }, - } - try finishMessage(&w, at); - return w.buffered(); -} - /// Slack over a message's variable payload, covering every fixed field any -/// message here has plus its own header. One loose constant rather than a -/// field-by-field count: a bound that is 32 bytes generous costs one `memcpy` -/// worth of nothing, and a bound that is one byte tight is a bug that only -/// shows up on the message nobody tested. +/// message here has plus its own header. const msg_slack = header_len + 64; -/// An upper bound on `encodeServer`'s output, so a caller sizes its buffer +/// An upper bound on `encodeClient`'s output, so a caller sizes its buffer /// once instead of guessing and retrying. -pub fn serverBound(msg: ServerMsg) usize { - return msg_slack + switch (msg) { - .welcome, .refuse, .quit, .detach, .read_clipboard => 0, - // A frame is bounded by its grid, not by this: see `frameBound`. - .frame => |f| frameBound(f.cols, f.rows), - .set_clipboard => |t| t.len, - .open_link => |u| u.len, - .post => |p| blk: { - var n: usize = 0; - for (p.list()) |pass| n += 13 + pass.path.len + pass.spirv.len; - break :blk n; - }, - }; -} - -/// ...and the same for the frontend's side of the wire. pub fn clientBound(msg: ClientMsg) usize { return msg_slack + switch (msg) { - .hello, .bye => 0, + .hello => |h| h.session.len, + .bye, .ping, .check, .far, .stamp => 0, + .screen_ack, .want => |bytes| bytes.len, .event => |ev| switch (ev) { - .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, + // Its hits' places (14 bytes each) past the slack. + .mouse => 28, + .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus, .frontend => 0, .key => |k| k.text.len, .paste => |b| b.len, .command => |line| line.len, @@ -1143,76 +668,6 @@ pub fn clientBound(msg: ClientMsg) usize { }; } -pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { - var r: std.Io.Reader = .fixed(payload); - const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) { - .welcome => .{ .welcome = .{ - .version = try r.takeInt(u16, .little), - .slot = try r.takeByte(), - .cols = try getCols(&r), - .rows = try getRows(&r), - } }, - .refuse => .{ .refuse = try getTag(&r, Refusal) }, - .frame => blk: { - const header = try getTag(&r, FrameHeader); - const kind: FrameKind = switch (header) { - .full, .full_link => .full, - .diff, .diff_link => .diff, - }; - const cols = try getCols(&r); - const rows = try getRows(&r); - const cursor = try getCursor(&r, cols, rows); - const layers = try getSlice32(&r); - const nruns = try r.takeInt(u32, .little); - // One run per cell is the worst an encoder emits, so a bigger - // count cannot be describing this grid. Checked HERE rather than - // in `apply`'s loop so the number is refused before it is used to - // bound anything. - if (nruns > @as(u32, cols) * @as(u32, rows)) return error.Overlong; - const runs = r.buffered(); - r.tossBuffered(); - break :blk .{ .frame = .{ - .layers = layers, - .kind = kind, - .pointer_shape = if (header == .full_link or header == .diff_link) .link else .arrow, - .cols = cols, - .rows = rows, - .cursor = cursor, - .nruns = nruns, - .runs = runs, - } }; - }, - .quit => .quit, - .detach => .detach, - .set_clipboard => .{ .set_clipboard = try getSlice32(&r) }, - .read_clipboard => .read_clipboard, - .open_link => .{ .open_link = try r.take(try r.takeInt(u16, .little)) }, - .post => blk: { - var p: Post = .{ .animation = try getTag(&r, ShaderAnimation) }; - p.len = try r.takeByte(); - if (p.len > ShaderBuild.max) return error.Overlong; - for (p.passes[0..p.len]) |*pass| { - const kind = try r.takeByte(); - pass.* = .{ - .scene = if (kind == 0) null else std.enums.fromInt(Scene, kind - 1) orelse return error.BadValue, - .level = try r.takeByte(), - .animated = switch (try r.takeByte()) { - 0 => false, - 1 => true, - else => return error.BadValue, - }, - .revision = try r.takeInt(u32, .little), - .path = try r.take(try r.takeInt(u16, .little)), - .spirv = try getSlice32(&r), - }; - } - break :blk .{ .post = p }; - }, - }; - try end(&r); - return msg; -} - // --------------------------------------------------------------------------- // tests // --------------------------------------------------------------------------- @@ -1233,13 +688,6 @@ fn roundClient(buf: []u8, msg: ClientMsg) !ClientMsg { return decodeClient(f.tag, f.payload); } -fn roundServer(buf: []u8, msg: ServerMsg) !ServerMsg { - const bytes = try encodeServer(buf, msg); - const f = (try framed(bytes)).?; - try testing.expectEqual(bytes.len, f.total); - return decodeServer(f.tag, f.payload); -} - test "detached wire: every Event variant round-trips" { var buf: [4096]u8 = undefined; @@ -1251,12 +699,25 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqual(@as(u8, 0x1d), @backingInt(ClientTag.pointer_leave)); { - const got = try roundClient(&buf, .{ .hello = .{ .cols = 80, .rows = 24 } }); + const got = try roundClient(&buf, .{ .hello = .{ .session = "work" } }); try testing.expectEqual(version, got.hello.version); - try testing.expectEqual(@as(u16, 80), got.hello.cols); - try testing.expectEqual(@as(u16, 24), got.hello.rows); + try testing.expectEqual(semantics, got.hello.semantics); + try testing.expectEqualStrings("work", got.hello.session); + } + try testing.expect((try roundClient(&buf, .{ .bye = .{ .again = true } })).bye.again); + try testing.expectEqual(@as(?u8, 2), (try roundClient(&buf, .{ .hello = .{ .session = "work", .view = 2 } })).hello.view); + try testing.expectEqual(@as(u64, 0xfeed_beef_0000_0001), (try roundClient(&buf, .{ .ping = 0xfeed_beef_0000_0001 })).ping); + { + // The other way: framed the same, its tag the ServerTag's number. + const bytes = try encodeServer(&buf, .{ .welcome = .{ .pid = 42, .session = "work" } }); + const f = (try framed(bytes)).?; + try testing.expectEqual(@as(u8, 0x01), f.tag); + try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload); + const back = (try decodeServer(f.tag, f.payload)).welcome; + try testing.expectEqualStrings("work", back.session); + try testing.expectEqual(@as(u32, 42), back.pid); + try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); } - try testing.expectEqual(ClientMsg.bye, try roundClient(&buf, .bye)); { const key: pardes.Key = .{ .cp = pardes.Key.page_down, .text = "ü", .ctrl = true, .alt = false, .shift = true }; @@ -1311,50 +772,6 @@ test "detached wire: every Event variant round-trips" { ); } -test "detached wire: every server message round-trips" { - var buf: [4096]u8 = undefined; - - { - const got = (try roundServer(&buf, .{ .welcome = .{ .slot = 2, .cols = 56, .rows = 14 } })).welcome; - try testing.expectEqual(version, got.version); - try testing.expectEqual(@as(u8, 2), got.slot); - try testing.expectEqual(@as(u16, 56), got.cols); - try testing.expectEqual(@as(u16, 14), got.rows); - } - for (std.enums.values(Refusal)) |why| - try testing.expectEqual(why, (try roundServer(&buf, .{ .refuse = why })).refuse); - try testing.expectEqual(ServerMsg.quit, try roundServer(&buf, .quit)); - try testing.expectEqual(ServerMsg.detach, try roundServer(&buf, .detach)); - try testing.expectEqualStrings("yank", (try roundServer(&buf, .{ .set_clipboard = "yank" })).set_clipboard); - try testing.expectEqual(ServerMsg.read_clipboard, try roundServer(&buf, .read_clipboard)); - try testing.expectEqualStrings("https://x", (try roundServer(&buf, .{ .open_link = "https://x" })).open_link); - var chain: Post = .{ .animation = .always, .len = 2 }; - chain.passes[0] = .{ .scene = .bloom, .level = 3 }; - chain.passes[1] = .{ .path = "~/wave.glsl", .spirv = "\x03\x02\x23\x07spirv", .revision = 7, .animated = true }; - const back = (try roundServer(&buf, .{ .post = chain })).post; - try testing.expectEqual(ShaderAnimation.always, back.animation); - try testing.expectEqual(@as(u8, 2), back.len); - try testing.expectEqual(Scene.bloom, back.passes[0].scene.?); - try testing.expectEqual(@as(u8, 3), back.passes[0].level); - try testing.expect(back.passes[1].scene == null); - try testing.expectEqualStrings("~/wave.glsl", back.passes[1].path); - try testing.expectEqualStrings("\x03\x02\x23\x07spirv", back.passes[1].spirv); - try testing.expectEqual(@as(u32, 7), back.passes[1].revision); - try testing.expect(back.passes[1].animated and !back.passes[0].animated); -} - -test "detached wire: only the display's own effects are on the wire" { - // Two guards, because the mistake has two directions. The exhaustive - // switch fails to COMPILE if a machine-local effect is added back, which - // is the direction that matters: it would hand a frontend work that must - // outlive it. The count fails if one of the three is dropped, which would - // silently leave a clipboard or a link unanswered on every frontend. - try testing.expectEqual(@as(usize, 9), std.enums.values(ServerTag).len); - for (std.enums.values(ServerTag)) |t| switch (t) { - .welcome, .refuse, .frame, .quit, .detach, .set_clipboard, .read_clipboard, .open_link, .post => {}, - }; -} - test "detached wire: a session is never told to do a frontend's remembering" { // The mirror of the test above, and of client.zig's "a frontend is never // asked to fork, write, or watch". That one pins what may reach a FRONTEND; @@ -1367,20 +784,27 @@ test "detached wire: a session is never told to do a frontend's remembering" { // Adding a name here is meant to be an ARGUMENT, not a formality, and the // bar is one sentence: A HUMAN DID IT. A keystroke, a click, a pinch, a // window resized, a paste, a command line executed, a pointer leaving the - // window, the window's focus — plus the two session words that say who is speaking. A pty's + // window, the window's focus — plus the session words that say who is speaking, time the link and end it. A pty's // output, a worker's answer, a watched file's new bytes and an animation // tick all fail that bar the same way: nobody did them, a machine reported // them, and the machine that reports them is the one already holding the // core. const allowed = [_][]const u8{ - // Who this connection is, and that it is finished. - "hello", "bye", + // Who this connection is, its round trip, and that it is finished, + // and which terminal state its replica holds (a protocol's word, + // as a ping is). + "hello", "bye", "ping", "screen_ack", "check", + // ...and how far it is, and which of the join's blobs it lacks. + "far", "want", + // ...and when its next input was, by the session's clocks. + "stamp", // ...and everything a person can do to a window. - "key", "mouse", - "resize", "paste", - "command", "pdf_scroll", - "pinch", "touch_scroll", + "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 @@ -1422,223 +846,7 @@ test "detached wire: a session is never told to do a frontend's remembering" { try testing.expectEqual(allowed.len, accepted); } -/// A grid with something in every corner: a default cell, a plain ASCII cell, -/// an indexed pair, an rgb pair with every attribute on, and a multi-byte -/// grapheme — the five shapes `putCell` branches on. -fn sampleGrid(cells: []pardes.Cell) void { - @memset(cells, .{}); - cells[0] = .{ .text = "x".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[1] = .{ - .text = "y".* ++ @as([6]u8, @splat(0)), - .len = 1, - .default = false, - .style = .{ .fg = .{ .index = 3 }, .bg = .{ .index = 250 }, .bold = true, .ul = .curly }, - }; - cells[2] = .{ - .text = "→".* ++ @as([4]u8, @splat(0)), - .len = 3, - .default = false, - .style = .{ - .fg = .{ .rgb = .{ 1, 2, 3 } }, - .bg = .{ .rgb = .{ 250, 251, 252 } }, - .bold = true, - .dim = true, - .italic = true, - .blink = true, - .reverse = true, - .invisible = true, - .strikethrough = true, - .ul = .dashed, - .font_role = .tagline, - }, - }; - cells[cells.len - 1] = .{ .text = "z".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; -} - -fn expectGridEqual(want: []const pardes.Cell, have: []const pardes.Cell) !void { - try testing.expectEqual(want.len, have.len); - // `visuallyEqual` and not `std.meta.eql`: `Cell.text` past `len` is - // scratch left by an earlier grapheme, the decoder does not invent it, and - // pardes.zig says in as many words that it must never manufacture a diff. - for (want, have, 0..) |*a, *b, i| if (!a.visuallyEqual(b)) { - std.debug.print("cell {d} differs: {any} vs {any}\n", .{ i, a.*, b.* }); - return error.CellMismatch; - }; -} - -test "detached wire: a full frame carries the grid, a diff carries the change" { - const cols: u16 = 56; - const rows: u16 = 14; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const mirror = try gpa.alloc(pardes.Cell, n); - defer gpa.free(mirror); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - sampleGrid(cells); - @memset(mirror, .{}); - - // FULL: nothing on the far side is comparable, which is the late joiner - // and the resize both. - const full_bytes = try encodeFrameLayers(buf, cols, rows, .{ .x = 3, .y = 4, .bar = true }, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); - { - const f = (try framed(full_bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.full, msg.kind); - try testing.expectEqual(cols, msg.cols); - try testing.expectEqual(rows, msg.rows); - try testing.expectEqual(@as(u16, 3), msg.cursor.?.x); - try testing.expectEqual(@as(u16, 4), msg.cursor.?.y); - try testing.expect(msg.cursor.?.bar); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } - - // DIFF: two cells move. The mirror is already in sync, so this is what a - // steady-state frame looks like. - const before = try gpa.dupe(pardes.Cell, cells); - defer gpa.free(before); - cells[100] = .{ .text = "q".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[0] = .{}; // ...and one goes back to unpainted, which a diff must say - const diff_bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{}); - { - const f = (try framed(diff_bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.diff, msg.kind); - try testing.expectEqual(@as(?Cursor, null), msg.cursor); - try testing.expectEqual(@as(u32, 2), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } - - // An unchanged frame is the head and nothing else: no runs, and the - // receiver keeps what it has. This is what makes an idle session silent. - { - const idle = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, cells, &.{}, &.{}, &.{}, .{}); - try testing.expectEqual(@as(usize, header_len + frame_head), idle.len); - const f = (try framed(idle)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(@as(u32, 0), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - } -} - -test "detached wire: the largest grid is one cell past a run, and survives it" { - // `max_cols * max_rows` is 65536 and a run's `count` is a u16, so THE ONE - // grid this protocol calls legal at both bounds is the one grid whose full - // frame cannot be described by a single run. It reached nobody while a - // frontend sent its window raw — a display that big had its hello refused - // before any frame was composed — and it is the ordinary case now that a - // frontend clamps to exactly this. In a safe build the `@intCast` panicked - // and took the session down; in a fast one it wrote a zero-length run the - // frontend answered with `BadValue`. - const cols = max_cols; - const rows = max_rows; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const mirror = try gpa.alloc(pardes.Cell, n); - defer gpa.free(mirror); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - // Every cell painted, which is what `render` produces and therefore what a - // full frame always is: one run, if a run could be that long. - for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - @memset(mirror, .{}); - - const bytes = try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{}); - const f = (try framed(bytes)).?; - const msg = (try decodeServer(f.tag, f.payload)).frame; - try testing.expectEqual(FrameKind.full, msg.kind); - // Split, and split as late as it can be: 65535 cells then 1. - try testing.expectEqual(@as(u32, 2), msg.nruns); - try msg.apply(mirror); - try expectGridEqual(cells, mirror); - // ...at exactly the two headers the split costs and not one byte more. - // `bytes.len <= frameBound(...)` would prove nothing here — the buffer IS - // `frameBound` bytes, so an over-run is `WriteFailed` above, not a false - // assertion here — and what wants pinning is that the second run is the - // whole of the cost. - try testing.expectEqual( - @as(usize, header_len + frame_head + 2 * run_header + n * 8), - bytes.len, - ); -} - -test "detached wire: the diff is worth having, in bytes, on the board's grid" { - // The numbers quoted in `encodeFrameLayers`'s comment, asserted so the claim - // cannot rot. 56x14 is the ESP32-P4 board's default grid (esp32p4.zig). - const cols: u16 = 56; - const rows: u16 = 14; - const n = @as(usize, cols) * rows; - const gpa = testing.allocator; - - const cells = try gpa.alloc(pardes.Cell, n); - defer gpa.free(cells); - const buf = try gpa.alloc(u8, frameBound(cols, rows)); - defer gpa.free(buf); - - // Every cell painted with a plain ASCII glyph and default colors, which is - // what a pardes frame overwhelmingly is: eight bytes a cell. - for (cells) |*c| c.* = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const full = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, &.{}, &.{}, &.{}, &.{}, .{})).len; - try testing.expectEqual(@as(usize, header_len + frame_head + run_header + n * 8), full); - try testing.expectEqual(@as(usize, 6302), full); - - const before = try gpa.dupe(pardes.Cell, cells); - defer gpa.free(before); - // A keystroke: one glyph replaced, and the cursor's old and new cells - // repainted. Three cells, adjacent enough to coalesce into two runs. - cells[300] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[301] = .{ .text = "c".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[500] = .{ .text = "d".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - const diff = (try encodeFrameLayers(buf, cols, rows, null, .arrow, cells, before, &.{}, &.{}, &.{}, .{})).len; - try testing.expectEqual(@as(usize, header_len + frame_head + 2 * run_header + 3 * 8), diff); - try testing.expectEqual(@as(usize, 60), diff); - // The whole reason this codec exists rather than shipping the Surface. - try testing.expect(full / diff > 100); -} - -test "detached wire: a run is coalesced across a gap only when that is cheaper" { - const cols: u16 = 8; - const rows: u16 = 1; - var cells: [8]pardes.Cell = @splat(.{}); - var prev: [8]pardes.Cell = @splat(.{}); - var buf: [512]u8 = undefined; - - // Two changes with three UNPAINTED cells between them. Re-sending those is - // three bytes; a second run header is six. One run. - cells[0] = .{ .text = "a".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[4] = .{ .text = "b".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - { - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try testing.expectEqual(@as(u32, 1), (try decodeServer(f.tag, f.payload)).frame.nruns); - } - // Now put a PAINTED cell in the gap that both sides already agree about. - // Eight bytes to re-send against six for a header: two runs. - const painted: pardes.Cell = .{ .text = "-".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; - cells[2] = painted; - prev[2] = painted; - { - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try testing.expectEqual(@as(u32, 2), (try decodeServer(f.tag, f.payload)).frame.nruns); - } - // Whichever it chose, the receiver ends up with the same grid. - var mirror: [8]pardes.Cell = prev; - const f = (try framed(try encodeFrameLayers(&buf, cols, rows, null, .arrow, &cells, &prev, &.{}, &.{}, &.{}, .{}))).?; - try (try decodeServer(f.tag, f.payload)).frame.apply(&mirror); - try expectGridEqual(&cells, &mirror); -} - -test "detached wire: a truncated frame is refused at every length" { +test "detached wire: a truncated message is refused at every length" { var buf: [4096]u8 = undefined; // Every prefix of a real message. The framing must say "not yet" for the @@ -1659,7 +867,6 @@ test "detached wire: a truncated frame is refused at every length" { try testing.expectEqual(@as(?Framed, null), try framed(copy[0..cut])); // A payload with bytes LEFT OVER is refused too: it is not this message. try testing.expectError(error.Trailing, decodeClient(@backingInt(ClientTag.pointer_leave), "x")); - try testing.expectError(error.Trailing, decodeServer(@backingInt(ServerTag.quit), "x")); } test "detached wire: an unknown tag is refused, never guessed" { @@ -1667,11 +874,8 @@ test "detached wire: an unknown tag is refused, never guessed" { // the session tags and the input tags. All three are the same answer. for ([_]u8{ 0x00, 0x0f, 0x1f, 0xff }) |tag| { try testing.expectError(error.BadTag, decodeClient(tag, "")); - try testing.expectError(error.BadTag, decodeServer(tag, "")); } - // A tag NESTED in a payload gets the same treatment: a color, an - // underline style, a mouse button, a refusal reason. - try testing.expectError(error.BadTag, decodeServer(@backingInt(ServerTag.refuse), &.{0x7f})); + // A tag NESTED in a payload gets the same treatment: a mouse button. try testing.expectError(error.BadTag, decodeClient( @backingInt(ClientTag.mouse), &.{ 0xff, 0x00, 0, 0, 0, 0, 0 }, @@ -1701,140 +905,6 @@ test "detached wire: an over-long length prefix is refused before it is believed try testing.expectError(error.Overlong, decodeClient(@backingInt(ClientTag.paste), &paste)); } -test "detached wire: a frame that lies about its runs cannot walk out of the grid" { - const cols: u16 = 4; - const rows: u16 = 2; - var grid: [8]pardes.Cell = @splat(.{}); - - // start = 6, count = 4 on an 8-cell grid: the check that matters, and the - // one an `start + count > len` spelling would miss on overflow. - var runs: [10]u8 = undefined; - std.mem.writeInt(u32, runs[0..4], 6, .little); - std.mem.writeInt(u16, runs[4..6], 4, .little); - runs[6] = 1; - const f: Frame = .{ .kind = .diff, .cols = cols, .rows = rows, .cursor = null, .nruns = 1, .runs = runs[0..7] }; - try testing.expectError(error.Overlong, f.apply(&grid)); - - // A start past the end entirely, and the 32-bit wrap. - std.mem.writeInt(u32, runs[0..4], std.math.maxInt(u32) - 1, .little); - std.mem.writeInt(u16, runs[4..6], 4, .little); - try testing.expectError(error.Overlong, (Frame{ - .kind = .diff, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 1, - .runs = runs[0..7], - }).apply(&grid)); - - // A zero-length run says nothing and is not something the encoder emits. - std.mem.writeInt(u32, runs[0..4], 0, .little); - std.mem.writeInt(u16, runs[4..6], 0, .little); - try testing.expectError(error.BadValue, (Frame{ - .kind = .diff, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 1, - .runs = runs[0..6], - }).apply(&grid)); - - // A run count larger than the grid has cells is refused at DECODE, before - // it is used to bound the walk. - var head: [frame_head]u8 = undefined; - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, null, cols, rows); - try w.writeInt(u32, 0, .little); // no body layers - try w.writeInt(u32, 9, .little); - try testing.expectError(error.Overlong, decodeServer(@backingInt(ServerTag.frame), w.buffered())); - - // ...and a grid of the wrong size is the receiver's own bug, not a frame - // it should paint half of. - var small: [4]pardes.Cell = @splat(.{}); - try testing.expectError(error.BadValue, (Frame{ - .kind = .full, - .cols = cols, - .rows = rows, - .cursor = null, - .nruns = 0, - .runs = "", - }).apply(&small)); -} - -test "detached wire: link pointer survives full and unchanged grid frames" { - const cells = [_]pardes.Cell{.{}}; - var buffer: [256]u8 = undefined; - for ([_]pardes.Surface.PointerShape{ .link, .arrow, .link }) |shape| { - for ([_]bool{ true, false }) |full| { - const bytes = try encodeFrameLayers(&buffer, 1, 1, null, shape, &cells, if (full) &.{} else &cells, &.{}, &.{}, &.{}, .{}); - const packet = (try framed(bytes)).?; - const frame = (try decodeServer(packet.tag, packet.payload)).frame; - try std.testing.expectEqual(shape, frame.pointer_shape); - try std.testing.expectEqual(if (full) FrameKind.full else FrameKind.diff, frame.kind); - if (!full) try std.testing.expectEqual(@as(u32, 0), frame.nruns); - } - } -} - -test "detached wire: a cursor outside the grid is refused, not painted" { - // The one field of a frame a frontend indexes with rather than copies: - // tty.zig moves the terminal's own cursor to `cursor.x`/`cursor.y`, and - // the ESP32-P4 panel writes the cell there. A frame that says 4x2 and puts - // the cursor at (9,0) is an out-of-bounds write in every frontend, so it - // has to be a decode error and not a clamp — a clamped cursor is a wrong - // screen that nobody reports. - const cols: u16 = 4; - const rows: u16 = 2; - var head: [frame_head]u8 = undefined; - for ([_][2]u16{ .{ cols, 0 }, .{ 0, rows }, .{ 0xffff, 0xffff } }) |xy| { - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - // Written by hand: `putCursor` now refuses this too, which is the - // other half of the same fix and is asserted below. - try w.writeByte(@intFromBool(true)); - try w.writeInt(u16, xy[0], .little); - try w.writeInt(u16, xy[1], .little); - try w.writeByte(@intFromBool(false)); - try w.writeInt(u32, 0, .little); - try testing.expectError( - error.BadValue, - decodeServer(@backingInt(ServerTag.frame), w.buffered()), - ); - } - // The last cell IS in the grid, and a frame carrying it decodes. - { - var w: std.Io.Writer = .fixed(&head); - try w.writeByte(@backingInt(FrameKind.diff)); - try w.writeInt(u16, cols, .little); - try w.writeInt(u16, rows, .little); - try putCursor(&w, .{ .x = cols - 1, .y = rows - 1, .bar = true }, cols, rows); - try w.writeInt(u32, 0, .little); // no body layers - try w.writeInt(u32, 0, .little); - const got = (try decodeServer(@backingInt(ServerTag.frame), w.buffered())).frame; - try testing.expectEqual(@as(u16, cols - 1), got.cursor.?.x); - try testing.expectEqual(@as(u16, rows - 1), got.cursor.?.y); - } - // ...and the ENCODER refuses to build one, so a core bug is a dropped - // frame with a log line rather than a frame every frontend hangs up over. - var cells: [8]pardes.Cell = @splat(.{}); - var buf: [512]u8 = undefined; - try testing.expectError( - error.BadValue, - encodeFrameLayers(&buf, cols, rows, .{ .x = cols, .y = 0, .bar = false }, .arrow, &cells, &.{}, &.{}, &.{}, &.{}, .{}), - ); - // A grid past the protocol's own ceiling is refused by the encoder for the - // same reason: `max_payload` is derived from it, so the decoder would. - try testing.expectError( - error.Overlong, - encodeFrameLayers(&buf, max_cols + 1, 1, null, .arrow, cells[0..0], &.{}, &.{}, &.{}, &.{}, .{}), - ); -} - test "detached wire: values a field cannot mean are refused" { // A bool is 0 or 1. `2` used to be "true" in every hand-written codec that // ever silently accepted a corrupt stream. @@ -1854,18 +924,17 @@ test "detached wire: values a field cannot mean are refused" { @backingInt(ClientTag.pdf_scroll), &.{pardes.MAX_PANES}, )); - // A zero-column grid would collapse a shared session; an over-wide one is - // past what this protocol carries. - try testing.expectError(error.BadValue, decodeClient( - @backingInt(ClientTag.hello), - &.{ 1, 0, 0, 0, 24, 0 }, - )); + // A session name longer than the daemon keeps one: refused, never cut + // to another session's name. { - var hello: [6]u8 = undefined; + var hello: [12 + max_name + 1]u8 = @splat('x'); std.mem.writeInt(u16, hello[0..2], version, .little); - std.mem.writeInt(u16, hello[2..4], max_cols + 1, .little); - std.mem.writeInt(u16, hello[4..6], 24, .little); + std.mem.writeInt(u16, hello[10..12], max_name + 1, .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.hello), &hello)); + std.mem.writeInt(u16, hello[10..12], max_name, .little); + hello[hello.len - 1] = 0xff; + try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), &hello)).hello.session.len); + try testing.expect(header_len + hello.len == hello_max); } // A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps. { @@ -1875,28 +944,10 @@ test "detached wire: values a field cannot mean are refused" { std.mem.writeInt(u32, &pinch, @bitCast(std.math.inf(f32)), .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.touch_scroll), &pinch)); } - // A cell with the reserved attribute bit set, and one with an impossible - // grapheme length. Both are bytes this protocol has no meaning for. - { - var grid: [1]pardes.Cell = @splat(.{}); - // default=0, len=1, 'x', fg default, bg default, attrs=0x80, ul, font - var runs = [_]u8{ 0, 0, 0, 0, 1, 0, 0, 1, 'x', 0, 0, 0x80, 0, 0 }; - const f: Frame = .{ .kind = .diff, .cols = 1, .rows = 1, .cursor = null, .nruns = 1, .runs = &runs }; - try testing.expectError(error.BadValue, f.apply(&grid)); - runs[11] = 0; - runs[7] = 8; // len past Cell.text - try testing.expectError(error.BadValue, f.apply(&grid)); - runs[7] = 0; // ...and a grapheme of no bytes at all - try testing.expectError(error.BadValue, f.apply(&grid)); - } } test "detached wire: max_payload bounds every message this protocol can build" { - // The derivation in `max_payload`'s comment, asserted: the worst frame - // this protocol admits fits, so a receiver sized for max_payload can - // always hold one. - try testing.expect(frameBound(max_cols, max_rows) <= max_payload); - // ...and a 4 MiB paste, which is the tty frontend's own cap. + // A 4 MiB paste, which is the tty's own cap, fits. try testing.expect((4 << 20) + header_len + 4 <= max_payload); // A slice past the width of its own length prefix is refused by the // ENCODER, rather than written and rejected at the far end. A command line @@ -1914,47 +965,12 @@ test "detached wire: max_payload bounds every message this protocol can build" { try testing.expectError(error.WriteFailed, encodeClient(&small, .{ .event = .{ .paste = "0123456789" } })); } -test "detached wire carries compact body rows and exact pointer metrics" { - const gpa = std.testing.allocator; - var cells: [8]pardes.Cell = @splat(.{}); +test "detached wire carries exact pointer metrics" { var logical: [12]pardes.Cell = @splat(.{}); - logical[10] = .{ .text = "z".* ++ @as([6]u8, @splat(0)), .len = 1, .default = false }; var layer: pardes.Layer = .{ .id = 0, .serial = 42, .viewport = .{ .x = 0, .y = 0, .w = 2, .h = 4 }, .cols = 2, .rows = 6, .context_rows = 2, .cells = &logical, .cursor = .{ .x = 0, .y = 5, .bar = true } }; layer.context_separators.set(0); var tag_cells: [3]pardes.Cell = @splat(.{}); - tag_cells[2] = logical[10]; const tag: pardes.Layer = .{ .kind = .column, .id = 1, .viewport = .{ .x = 0, .y = 0, .w = 2, .h = 1 }, .cols = 3, .rows = 1, .bg = .{ 11, 22, 33 }, .cells = &tag_cells, .cursor = .{ .x = 2, .y = 0, .bar = true } }; - const placed = [_]pardes.Region{.{ .kind = .rail, .owner = 3, .serial = 42, .rect = .{ .x = 0, .y = 1, .w = 2, .h = 3 }, .active = true, .dirty = true, .thumb_y = 1, .thumb_h = 2, .clip = 1, .lift = 0.5 }}; - const out = try gpa.alloc(u8, frameBound(2, 4) + layersBound(&.{layer}, &.{tag}, &placed)); - defer gpa.free(out); - const palette: pardes.Chrome = .{ .page = .{ 1, 2, 3 }, .border = .{ 4, 5, 6 }, .tag_focus_bg = .{ 7, 8, 9 }, .scroll_thumb = .{ 10, 11, 12 }, .rule_px = 3, .tag_rule = .{ 13, 14, 15 }, .rail_px = 12, .grip_border = .{ 16, 17, 18 }, .grip_focus_ring = .{ 19, 20, 21 }, .grip_dirty = .{ 22, 23, 24 }, .lift_style = 2, .cursor_glide = 3, .cursor_alpha = 0.5, .cursor_idle_ms = 1234, .cursor_quad = .{ 1, 2, 3.5, 2, 3.5, 3, 1, 3 } }; - const encoded = try encodeFrameLayers(out, 2, 4, null, .link, &cells, &cells, &.{layer}, &.{tag}, &placed, palette); - const packet = (try framed(encoded)).?; - const frame = (try decodeServer(packet.tag, packet.payload)).frame; - var decoded: [pardes.MAX_PANES]pardes.Layer = @splat(.{}); - var tags: [pardes.MAX_TAG_LAYERS]pardes.Layer = @splat(.{}); - defer for (&tags) |*item| gpa.free(item.cells); - defer for (&decoded) |*item| gpa.free(item.cells); - try frame.apply(&cells); - var regions: std.ArrayListUnmanaged(pardes.Region) = .empty; - defer regions.deinit(gpa); - var chrome: pardes.Chrome = .{}; - try frame.applyLayers(gpa, &decoded, &tags, ®ions, &chrome); - try std.testing.expectEqualDeep(placed[0..], regions.items); - try std.testing.expectEqualDeep(palette, chrome); - try std.testing.expectEqual(@as(u16, 6), decoded[0].rows); - try std.testing.expect(decoded[0].context_separators.isSet(0)); - try std.testing.expect(!decoded[0].context_separators.isSet(1)); - try std.testing.expect(decoded[0].hasContextBorderAfter(0)); - try std.testing.expect(decoded[0].hasContextBorderAfter(1)); - try std.testing.expectEqualStrings("z", decoded[0].cells[10].grapheme()); - try std.testing.expectEqual(@as(u16, 5), decoded[0].cursor.?.y); - try std.testing.expectEqual(tag.kind, tags[0].kind); - try std.testing.expectEqual(tag.id, tags[0].id); - try std.testing.expectEqual(tag.bg, tags[0].bg); - try std.testing.expectEqual(tag.cols, tags[0].cols); - try std.testing.expectEqual(@as(u16, 2), tags[0].cursor.?.x); - try std.testing.expectEqualStrings("z", tags[0].cells[2].grapheme()); const pointer = layer.bodyHit(4, 27, 8, 16, 6, 8).?; const tag_pointer = tag.tagHit(11, 8, 8, 16, 4, 8).?; const message: ClientMsg = .{ .event = .{ .mouse = .{ .col = 0, .row = 1, .button = .left, .kind = .press, .body_hit = pointer, .tag_hit = tag_pointer } } }; @@ -1965,22 +981,4 @@ test "detached wire carries compact body rows and exact pointer metrics" { try std.testing.expectEqualDeep(pointer, mouse.body_hit.?); try std.testing.expectEqualDeep(tag_pointer, mouse.tag_hit.?); try std.testing.expect(mouse_encoded.len <= clientBound(message)); - var truncated = frame; - truncated.layers = frame.layers[0 .. frame.layers.len - 1]; - try std.testing.expectError(error.EndOfStream, truncated.applyLayers(gpa, &decoded, &tags, ®ions, &chrome)); - for (decoded) |item| { - try std.testing.expectEqual(@as(u16, 0), item.rows); - try std.testing.expectEqual(@as(usize, 0), item.cells.len); - try std.testing.expectEqual(@as(usize, 0), item.context_separators.count()); - } - for (tags) |item| { - try std.testing.expectEqual(@as(u16, 0), item.rows); - try std.testing.expectEqual(@as(usize, 0), item.cells.len); - } - try std.testing.expectEqual(@as(usize, 0), regions.items.len); - const no_layers = try encodeFrameLayers(out, 2, 4, null, .arrow, &cells, &cells, &.{}, &.{}, &.{}, .{}); - const cleared_packet = (try framed(no_layers)).?; - const cleared = (try decodeServer(cleared_packet.tag, cleared_packet.payload)).frame; - try cleared.applyLayers(gpa, &decoded, &tags, ®ions, &chrome); - try std.testing.expectEqual(@as(u16, 0), decoded[0].rows); } diff --git a/src/draw.zig b/src/draw.zig index e141ce31..8503197c 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 } }); } } @@ -135,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); @@ -226,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, @@ -521,6 +519,10 @@ fn resolved(color: Color, default: ?[3]u8, palette: ?[16][3]u8) ?[3]u8 { } pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { + const taped = pardes.tape.enter(p, .render); + defer pardes.tape.leave(p, taped); + panes.terminal.drawing = true; + defer panes.terminal.drawing = false; panes.File.refreshHighlights(p); const s = &p.surface; const ncells = @as(usize, p.screen_w) * p.screen_h; @@ -606,9 +608,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); } @@ -961,6 +960,9 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: } try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*); + // A terminal panned to its cursor in a view shorter than it: an arrow + // in the gutter's first body row says rows are above (views.zig). + if (pane.pan > 0) s.set(r.x, p.bodyTop(pane, r), "↑", .{ .fg = .{ .rgb = chrome.box } }); // A query's places' ticks, each in the track's cell of its row: chrome, // painted with the rail (a pixel shell draws its own from the same list). for (s.rail_ticks[0..s.nrail_ticks]) |tick| { @@ -976,7 +978,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]; @@ -1736,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); @@ -1911,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"); @@ -1950,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; @@ -1994,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".*)))); @@ -2077,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 } } }); @@ -2127,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()) |_| {} @@ -2286,12 +2287,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| { @@ -2325,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); @@ -2378,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); @@ -2414,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); @@ -2448,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); @@ -2477,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/dump.zig b/src/dump.zig index 0da2513f..5f5f5870 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -1,20 +1,14 @@ const std = @import("std"); const builtin = @import("builtin"); const limits = @import("memory.zig").limits; - -// scoped, not bare std.log: main.zig's logFn drops the unscoped .default scope -// wholesale (ghostty and uucode log there too), and a corrupt dump's parse -// diagnostic is the only thing that says WHICH field went bad. -const log = std.log.scoped(.dump); const pardes = @import("pardes.zig"); -const tagline = @import("tagline.zig"); -const layout = @import("layout.zig"); +const walk = @import("walk.zig"); +const Handover = @import("Handover.zig"); const filesystem = @import("fs.zig"); +const layout = @import("layout.zig"); const ctlfs = @import("ninep/tree.zig"); const exec = @import("exec.zig"); -const body_layer = @import("body_layer.zig"); const colors = @import("colors.zig"); -const themes = colors.themes; const config = @import("config.zig"); const pdf_enabled = pardes.pdf_enabled; const platform = pardes.platform; @@ -22,15 +16,14 @@ const isolated = pardes.isolated; const terminal_panes = pardes.terminal_panes; const MAX_PANES = pardes.MAX_PANES; const MAX_COLS = pardes.MAX_COLS; -const column_weight_unit = layout.column_weight_unit; -const max_column_weight = layout.max_column_weight; const effect_path_cap = pardes.effect_path_cap; const Options = pardes.Options; const Pardes = pardes.Pardes; +const views = pardes.views; /// Where the next dump goes. $PARDES_DUMP wins verbatim (the snapshot harness /// pins it for deterministic goldens); else $XDG_DATA_HOME|~/.local/share -/// /pardes/pardes-<utc>.zon — timestamped so dumps never overwrite each other. +/// /pardes/pardes-<utc>.dump — timestamped so dumps never overwrite each other. /// Creates the directory and any parent missing (a fresh HOME has no /// ~/.local/share); a failure surfaces at open. pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { @@ -53,15 +46,15 @@ pub fn outPath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { return uniqueIn(buf, dir, base); } -/// `<dir>/<base>.zon`, or `<base>-2.zon` and on when two dumps land in one +/// `<dir>/<base>.dump`, or `<base>-2.dump` and on when two dumps land in one /// second: a dump never overwrites another. fn uniqueIn(buf: *[1024:0]u8, dir: []const u8, base: []const u8) ?[:0]const u8 { var n: usize = 1; while (n < 1000) : (n += 1) { const path = (if (n == 1) - std.fmt.bufPrintSentinel(buf, "{s}/{s}.zon", .{ dir, base }, 0) + std.fmt.bufPrintSentinel(buf, "{s}/{s}.dump", .{ dir, base }, 0) else - std.fmt.bufPrintSentinel(buf, "{s}/{s}-{d}.zon", .{ dir, base, n }, 0)) catch return null; + std.fmt.bufPrintSentinel(buf, "{s}/{s}-{d}.dump", .{ dir, base, n }, 0)) catch return null; if (comptime !pardes.hosted) return path; if (std.c.access(path.ptr, std.c.F_OK) != 0) return path; } @@ -76,9 +69,9 @@ test "two dumps in one second get names of their own" { var buf: [1024:0]u8 = undefined; const first = try std.testing.allocator.dupe(u8, uniqueIn(&buf, dir, "pardes-x").?); defer std.testing.allocator.free(first); - try std.testing.expect(std.mem.endsWith(u8, first, "/pardes-x.zon")); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-x.zon", .data = "" }); - try std.testing.expect(std.mem.endsWith(u8, uniqueIn(&buf, dir, "pardes-x").?, "/pardes-x-2.zon")); + try std.testing.expect(std.mem.endsWith(u8, first, "/pardes-x.dump")); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-x.dump", .data = "" }); + try std.testing.expect(std.mem.endsWith(u8, uniqueIn(&buf, dir, "pardes-x").?, "/pardes-x-2.dump")); } /// The host's environment; a freestanding core (web, the board) has none /// and no libc to ask, so only a DumpDir with no `~/` names a directory there. @@ -136,955 +129,443 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { return null; } -pub const magic = "pardes-dump"; -pub const version: u32 = 1; -pub const max_panes: usize = MAX_PANES; -pub const max_cols: usize = MAX_COLS; -/// Output arguments are typed in the same bounded one-line tag storage. Keep -/// the schema limit named independently so a dump reader can validate it -/// without importing the output-pane implementation. The bound itself is -/// `limits.max_tag_tail` — the one place a board-shaped capacity is chosen. -pub const max_origin_arg: usize = limits.max_tag_tail; - -pub const Size = struct { - cols: u16, - rows: u16, -}; - -pub const Cursor = struct { - col: u16 = 0, - row: u16 = 0, -}; - -pub const PaneKind = enum { - terminal, - file, - image, -}; - -pub const Terminal = struct { - cwd: []const u8 = "", - /// The shell it runs when not the configured one (`Tty bash`); empty, - /// the configured one, as in dumps from before. - shell: []const u8 = "", - /// A command pane's line and how it ended (null: unknown, or still - /// running when dumped); empty for an interactive terminal. - command: []const u8 = "", - status: ?u8 = null, - /// The rendered scrollback and screen as plain text, one row per line. - stream: []const u8 = "", - stream_b64: []const u8 = "", - /// `stream_b64` is the emulator's primary screen and scrollback as clean - /// VT (ghostty's formatter), which replays to what the screen showed. - /// Absent (older dumps), it is the raw tail of the pty's output, which - /// replays faithfully only if it holds the whole session at one size. - stream_is_screen: bool = false, - cursor: Cursor = .{}, - /// Petscii on: its kitty graphics drawn as glyph art by choice. - petscii: bool = false, -}; - -pub const LocationRow = struct { - mark_occurrence: bool = true, - source_column: usize = 0, - kind: enum { match, context, preview }, - declaration: bool = false, - path: []const u8, - line: usize, - col: usize = 0, - end_line: usize = 0, - end_col: usize = 0, - location_end: usize, - code_start: usize, - colors_b64: []const u8 = "", -}; - -pub const File = struct { - path: []const u8 = "", - /// The text, in older dumps plain and base64 both; now base64 alone, - /// and only for a pane whose text is not its file's on disk (unsaved, - /// an output, a file never written): a clean file's is read from disk. - content: []const u8 = "", - content_b64: []const u8 = "", - dirty: bool = false, - /// An unsaved pane's: the hash of the file on disk its text was read - /// against. A Restore that finds another there says it changed, and - /// Save asks before overwriting it. - disk_hash: ?u64 = null, - /// Its file was on disk when dumped: one gone by the Restore is said - /// deleted, one never written (Config's new file) is not. True in - /// older dumps. - on_disk: bool = true, - tree_context: bool = false, - location_rows: []const LocationRow = &.{}, - // Builtin names remain stable when enum ordinals change. - origin: []const u8 = "", - origin_arg: []const u8 = "", - mini_source: []const u8 = "", - mini_colors_b64: []const u8 = "", - /// Its dot, q0 and q1 in bytes, as acme's dump keeps a window's; null - /// in older dumps. - dot: ?[2]u32 = null, - /// A Zerox twin: the index, in `panes`, of the earlier pane whose - /// buffer it shares, its text kept there once; null for none. - twin_of: ?u32 = null, -}; - -pub const ImagePalette = enum { - commodore, - terminal, -}; +/// What a load makes a terminal's emulator with, when not the loader's +/// own: a screen-state replica's are the C heap's (src/replica.zig). +pub var terminal_gpa: ?std.mem.Allocator = null; -pub const Image = struct { - path: []const u8 = "", - bytes_b64: []const u8 = "", - // Defaults are the renderer's historical state, so version-1 dumps which - // predate these fields remain readable without a schema fork. - petscii: bool = false, - palette: ImagePalette = .commodore, - ascii: bool = true, - /// A PDF's PdfFit and PdfTint, by their tag words (`width`, `filtered`); - /// empty, the defaults, as in dumps from before. - pdf_fit: []const u8 = "", - pdf_tint: []const u8 = "", -}; +// ---- a whole session to and from a dump (acme keeps this in rows.c) ---- +// +// A dump (src/walk.zig) is everything the session is but what is the +// host's, the frame's or worked out again: its panes with their text and +// undo, terminals with their screens, history and kitty images (ghostty's +// snapshot), registers, places, tags, layout and settings. It crosses +// builds: its fields are matched by name. +// +// Loading is two halves. `load` makes the core the dump was and nothing +// else (a replica joining a session takes that, phase 5); `revivePane` and +// `reviveSession` give it to this host: new shells, watches, PDFs opened. -pub const Pane = struct { - kind: PaneKind, - tag: []const u8, - body: []const u8, - scroll: usize = 0, - cols: u16 = 0, - rows: u16 = 0, - vweight: f32 = 1, - collapsed: bool = false, - /// Its serial when dumped (0 in older dumps): a Restore logs - /// `restored <old> <new>` for it. - serial: u32 = 0, - /// Exact editable tail when the user touched it. Null retains the live - /// default; a present empty slice deliberately restores an empty tail. - tag_tail: ?[]const u8 = null, - /// The index in `panes` of the pane this one is attached to (layout.zig, - /// "Attached panes"); null when it is attached to none, and in older dumps. - attached_to: ?usize = null, - terminal: ?Terminal = null, - file: ?File = null, - image: ?Image = null, -}; +/// A session's dump: its core, everything it is but what is the host's, +/// the frame's or worked out again (`replica` in each type, src/walk.zig), +/// then its 9P mounts, which live with the 9P server's own state, which of +/// its panes' files were on disk when it was made (one gone by its load is +/// said deleted: its text is nowhere else now), its theme's name (the +/// setting is a place in this build's ring of themes, another build's +/// ring is another), and the views not live (views.zig: the core holds +/// the live one's). +const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8, []const views.Dumped }); +const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8, []const views.Dumped }); +const OnDisk = [MAX_PANES]bool; -pub const Column = struct { - /// Its serial when dumped (0 in older dumps): a Restore logs - /// `restoredcol <old> <new>` for it. - serial: u32 = 0, - tag: ?[]const u8 = null, - weight: f32 = 1, - panes: []const usize = &.{}, -}; - -pub const Mount = struct { name: []const u8, dial: []const u8 }; - -/// What a grip carried out of its window (Handover.zig): one pane with the -/// panes attached to it, or a whole column. A session's own dump is none. -pub const Carried = enum { pane, column }; - -pub const State = struct { - magic: []const u8 = magic, - version: u32 = version, - screen: Size, - active: usize = 0, - topbar: []const u8 = "", - topbar_custom: ?[]const u8 = null, - theme: []const u8 = "dark", - locations_config: @import("locations.zig").Config = .{}, - tree_context_tag_style: bool = true, - columns: []const Column = &.{}, - panes: []const Pane = &.{}, - mounts: []const Mount = &.{}, - /// The settings that differ from a fresh session's, each a line as the - /// root ctl takes it (`Placement pardes`); the theme and the locations - /// config are kept on their own, and the font is the frontend's. Absent - /// in older dumps. - settings: []const []const u8 = &.{}, - /// Incl's directories, in order; null in older dumps (the defaults). - incl: ?[]const []const u8 = null, - /// A dump a grip carried to another window; null for a session's own. - carried: ?Carried = null, -}; - -/// A header is a text like any other: it may hold newlines, but not a -/// carriage return or a NUL. -fn validateTag(bar: []const u8) !void { - if (bar.len > limits.max_tag_tail or std.mem.indexOfAny(u8, bar, "\r\x00") != null or !std.unicode.utf8ValidateSlice(bar)) return error.BadDumpTag; +fn onDisk(pane: *const pardes.panes.Pane) bool { + if (comptime !pardes.hosted) return false; + const f = pane.file orelse return false; + if (f.output != null) return false; + const local = filesystem.localPath(f.path) orelse return false; + return exec.kindOf(local) != null; } -pub fn validate(state: State) !void { - if (state.topbar_custom) |bar| try validateTag(bar); - for (state.columns) |column| if (column.tag) |bar| try validateTag(bar); - if (!std.mem.eql(u8, state.magic, magic)) return error.BadDumpMagic; - if (state.version != version) return error.BadDumpVersion; - if (state.panes.len == 0 or state.panes.len > max_panes) return error.BadDumpPanes; - if (state.columns.len == 0 or state.columns.len > max_cols) return error.BadDumpColumns; - if (state.active >= state.panes.len) return error.BadDumpActive; - if (state.mounts.len > 8) return error.BadDumpMounts; - for (state.mounts, 0..) |mount, i| { - if (mount.name.len == 0 or mount.name.len > 255 or mount.dial.len == 0 or - std.mem.indexOfScalar(u8, mount.dial, 0) != null) return error.BadDumpMounts; - if (std.mem.eql(u8, mount.name, ".") or std.mem.eql(u8, mount.name, "..") or - std.mem.eql(u8, mount.name, "os") or std.mem.eql(u8, mount.name, "self")) return error.BadDumpMounts; - for (mount.name) |c| if (!std.ascii.isAlphanumeric(c) and c != '_' and c != '-' and c != '.') return error.BadDumpMounts; - for (state.mounts[0..i]) |previous| if (std.mem.eql(u8, mount.name, previous.name)) return error.BadDumpMounts; - } - for (state.columns) |col| { - if (!std.math.isFinite(col.weight) or col.weight <= 0) return error.BadDumpColumns; - // An empty column is a column, as in acme's dump (rows.c:639). - if (col.panes.len > max_panes) return error.BadDumpColumns; - for (col.panes) |pane| { - if (pane >= state.panes.len) return error.BadDumpPaneRef; - } - } - for (state.panes) |pane| { - if (!std.math.isFinite(pane.vweight) or pane.vweight <= 0) - return error.BadDumpPaneWeight; - if (pane.scroll > std.math.maxInt(i32)) return error.BadDumpPaneScroll; - if (pane.tag_tail) |tail| if (tail.len > limits.max_tag_tail) - return error.BadDumpTagTail; - if (pane.file) |file| if (file.origin_arg.len > max_origin_arg) - return error.BadDumpOriginArg; - if (pane.file) |file| { - if (file.mini_source.len > 4096 or std.mem.indexOfScalar(u8, file.mini_source, 0) != null or - (file.mini_source.len == 0 and file.mini_colors_b64.len != 0) or - (file.mini_source.len != 0 and !std.mem.eql(u8, file.origin, "Mini"))) return error.BadDumpMini; - } - switch (pane.kind) { - .terminal => if (pane.terminal == null) return error.BadDumpPaneKind, - .file => if (pane.file == null) return error.BadDumpPaneKind, - .image => if (pane.image == null) return error.BadDumpPaneKind, - } - } +pub fn dumpState(p: *Pardes) !void { + const bytes = try dumpBytes(p); + if (p.dump_out) |d| p.gpa.free(d); + p.dump_out = bytes; + p.emit(.write_dump); } -test "oversized recognized output argument is rejected before restore" { - var oversized: [max_origin_arg + 1]u8 = @splat('x'); - const pane_ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &pane_ids }}; - const panes = [_]Pane{.{ - .kind = .file, - .tag = "+Search", - .body = "", - .file = .{ - .path = "/tmp/+Search", - .origin = "Find", - .origin_arg = &oversized, - }, - }}; - const state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; +/// The session as a dump, gpa-owned. +pub fn dumpBytes(p: *Pardes) ![]u8 { + return dumpWith(p, null); +} - // Serialize without writeFile: that writer correctly rejects the state - // too, while this exercises the hostile bytes a restore actually reads. - var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); - defer encoded.deinit(); - try std.zon.stringify.serialize(state, .{}, &encoded.writer); - try std.testing.expectError( - error.BadDumpOriginArg, - readZon(std.testing.allocator, encoded.written(), "oversized-origin-arg"), - ); +/// A dump for a screen-state replica's join (src/replica.zig): its blobs' +/// bytes left out and noted in `deferred` (they are the session's, alive +/// while pinned: memory.zig `pin`), its terminals' emulators left out (the +/// first delta brings each afresh). What is left is the session's mutable +/// state, made in its step. +pub fn dumpJoin(p: *Pardes, deferred: *std.ArrayListUnmanaged(walk.Deferred)) ![]u8 { + slim_terminals = true; + defer slim_terminals = false; + return dumpWith(p, deferred); } -pub const Parsed = struct { - value: State, - arena: std.heap.ArenaAllocator, +/// `dumpJoin`'s: a terminal dumps its size, not its emulator. +pub var slim_terminals = false; - pub fn deinit(parsed: *Parsed) void { - parsed.arena.deinit(); +fn dumpWith(p: *Pardes, deferred: ?*std.ArrayListUnmanaged(walk.Deferred)) ![]u8 { + var d = try Session.init(p.gpa); + errdefer d.deinit(); + d.deferred = deferred; + try d.put(Pardes, p); + const mounts: []const filesystem.Mount = p.fs.mounts.items; + try d.put([]const filesystem.Mount, &mounts); + var on_disk: OnDisk = @splat(false); + for (p.panes, &on_disk) |slot, *on| if (slot) |pane| { + on.* = onDisk(pane); + }; + try d.put(OnDisk, &on_disk); + const theme: []const u8 = colors.themes[@min(p.settings.theme, colors.themes.len - 1)].name; + try d.put([]const u8, &theme); + const others = try views.dumped(p, p.gpa); + defer { + for (others) |o| p.gpa.free(o.panes); + p.gpa.free(others); } -}; + const kept: []const views.Dumped = others; + try d.put([]const views.Dumped, &kept); + return d.finish(); +} -/// Whether `bytes` are a dump a Restore could load, saying nothing: a +/// Whether `bytes` are a dump a Restore could read, saying nothing: a /// Restore asks before it warns about unsaved text or replaces anything. -/// Whether `bytes` are a dump; a ZON failure's line and reason are left in -/// `why`, ended by a 0. +/// Why not is left in `why`, ended by a 0. pub fn check(gpa: std.mem.Allocator, bytes: []const u8, why: []u8) !void { - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - const source = try arena.allocator().dupeSentinel(u8, bytes, 0); - var diag: std.zon.parse.Diagnostics = undefined; - const state = std.zon.parse.fromSlice(State, .{ .gpa = gpa, .arena = arena.allocator(), .source = source, .diagnostics = &diag }) catch |err| { - const said = pardes.Messages.zonWhy(&diag, why[0 .. why.len - 1]); - if (said.ptr != why.ptr) @memcpy(why[0..said.len], said); + var l = SessionLoad.init(gpa, bytes) catch |err| { + const said = std.fmt.bufPrint(why[0 .. why.len - 1], "{s}", .{switch (err) { + error.BadDump => "it does not begin as one", + error.EndOfDump => "it is cut short", + else => @errorName(err), + }}) catch ""; why[said.len] = 0; return err; }; - try validate(state); + l.deinit(); } -pub fn readZon(gpa: std.mem.Allocator, bytes: []const u8, label: []const u8) !Parsed { - var parsed: Parsed = .{ .value = undefined, .arena = .init(gpa) }; - errdefer parsed.deinit(); - const arena = parsed.arena.allocator(); - const source = try gpa.dupeSentinel(u8, bytes, 0); - defer gpa.free(source); - - var diag: std.zon.parse.Diagnostics = undefined; - parsed.value = std.zon.parse.fromSlice(State, .{ .gpa = gpa, .arena = arena, .source = source, .diagnostics = &diag }) catch |err| { - if (err == error.ParseZon) log.err("{f}", .{diag.fmt(label)}); - return err; - }; - try validate(parsed.value); - return parsed; -} - -pub fn encodeBytes(alloc: std.mem.Allocator, bytes: []const u8) ![]const u8 { - const enc = std.base64.standard.Encoder; - const out = try alloc.alloc(u8, enc.calcSize(bytes.len)); - return enc.encode(out, bytes); +/// The core the dump `bytes` is, and nothing more: no shell started, no +/// file watched, nothing asked of a host (`revive` does that). What another +/// build's dump could not give is said in `notes` when given. `opts` are +/// this process's (its allocators, paths and name), not the dump's. +pub fn load(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?*std.ArrayList(u8)) !*Pardes { + return loadKnowing(gpa, opts, bytes, notes, null); } -pub fn decodeBytes(alloc: std.mem.Allocator, b64: []const u8) ![]u8 { - const dec = std.base64.standard.Decoder; - const n = try dec.calcSizeForSlice(b64); - const out = try alloc.alloc(u8, n); - errdefer alloc.free(out); - try dec.decode(out, b64); - return out; -} - -test "dump zon roundtrip" { - const gpa = std.testing.allocator; - const tty_b64 = try encodeBytes(gpa, "old\nhello\nworld"); - defer gpa.free(tty_b64); - const file_b64 = try encodeBytes(gpa, "alpha\nbeta\n"); - defer gpa.free(file_b64); - const panes = [_]Pane{ - .{ - .kind = .terminal, - .tag = "nm /tmp Del", - .body = "hello\nworld", - .scroll = 1, - .cols = 40, - .rows = 10, - .terminal = .{ .cwd = "/tmp", .stream = "old\nhello\nworld", .stream_b64 = tty_b64, .cursor = .{ .col = 3, .row = 2 } }, - }, - .{ - .kind = .file, - .tag = "nm /tmp/a.txt Save Del", - .body = " 1 alpha", - .cols = 20, - .rows = 5, - .file = .{ .path = "/tmp/a.txt", .content = "alpha\nbeta\n", .content_b64 = file_b64, .dirty = true }, - }, - }; - const col_panes = [_]usize{ 0, 1 }; - const columns = [_]Column{.{ .weight = 1, .panes = &col_panes }}; - const state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .topbar = "Kill Newcol Dump", - .theme = "dark", - .columns = &columns, - .panes = &panes, +/// `load`, with which panes' files were on disk when it was dumped. +fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?*std.ArrayList(u8), on_disk: ?*OnDisk) !*Pardes { + const p = try Pardes.create(gpa, opts); + var l = SessionLoad.init(gpa, bytes) catch |err| { + p.deinit(); + return err; }; - - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - var result = try readZon(gpa, out.written(), "roundtrip"); - defer result.deinit(); - const parsed = result.value; - try std.testing.expectEqual(@as(usize, 2), parsed.panes.len); - try std.testing.expectEqualStrings("dark", parsed.theme); - try std.testing.expectEqualStrings("old\nhello\nworld", parsed.panes[0].terminal.?.stream); - try std.testing.expectEqualStrings("alpha\nbeta\n", parsed.panes[1].file.?.content); - try std.testing.expect(parsed.panes[1].file.?.dirty); - { - const bytes = try decodeBytes(gpa, parsed.panes[0].terminal.?.stream_b64); - defer gpa.free(bytes); - try std.testing.expectEqualStrings("old\nhello\nworld", bytes); + defer l.deinit(); + // A load that fails leaves nothing: what it read is freed, and the + // core with it, its fields never torn down one by one. + errdefer { + l.rollback(); + p.fs.deinit(gpa); + gpa.destroy(p); } - { - const bytes = try decodeBytes(gpa, parsed.panes[1].file.?.content_b64); - defer gpa.free(bytes); - try std.testing.expectEqualStrings("alpha\nbeta\n", bytes); + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = terminal_gpa }; + try l.into(Pardes, p); + const read = l.shared.items.len; + const mounts = try l.get([]const filesystem.Mount); + defer { + for (mounts) |m| { + gpa.free(m.name); + gpa.free(m.dial); + } + gpa.free(mounts); + // Freed here: no rollback of them. + l.shared.shrinkRetainingCapacity(read); } -} - -test "omitted dump defaults roundtrip without borrowing input" { - const gpa = std.testing.allocator; - const fixture = - \\.{ - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .columns = .{.{ .panes = .{0, 1, 2} }}, - \\ .panes = .{ - \\ .{ .kind = .terminal, .tag = "terminal", .body = "", .terminal = .{} }, - \\ .{ .kind = .file, .tag = "file", .body = "", .file = .{ .path = "file.zig", .content = "const café = 1;" } }, - \\ .{ .kind = .image, .tag = "image", .body = "", .image = .{} }, - \\ }, - \\} - ; - const input = try gpa.dupe(u8, fixture); - defer gpa.free(input); - var parsed = try readZon(gpa, input, "omitted-defaults"); - defer parsed.deinit(); - @memset(input, 'x'); - const value = parsed.value; - try std.testing.expectEqualStrings(magic, value.magic); - try std.testing.expectEqualStrings("dark", value.theme); - try std.testing.expectEqualStrings("", value.topbar); - try std.testing.expectEqual(@as(usize, 0), value.mounts.len); - try std.testing.expectEqual(@as(usize, 3), value.panes.len); - try std.testing.expectEqualStrings("file.zig", value.panes[1].file.?.path); - try std.testing.expectEqualStrings("const café = 1;", value.panes[1].file.?.content); - try std.testing.expect(!value.panes[1].file.?.dirty); - try std.testing.expect(value.panes[2].image.?.ascii); - - var encoded: std.Io.Writer.Allocating = .init(gpa); - defer encoded.deinit(); - try std.zon.stringify.serialize(value, .{}, &encoded.writer); - var again = try readZon(gpa, encoded.written(), "default-roundtrip"); - defer again.deinit(); - try std.testing.expectEqualDeep(value, again.value); -} - -test "dump parser releases its arena at every allocation failure" { - const Check = struct { - fn run(gpa: std.mem.Allocator) !void { - var parsed = try readZon(gpa, - \\.{ - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .columns = .{.{ .panes = .{0} }}, - \\ .panes = .{.{ .kind = .file, .tag = "file", .body = "", .file = .{ .content = "owned" } }}, - \\} - , "allocation-cleanup"); - defer parsed.deinit(); - try std.testing.expectEqualStrings("owned", parsed.value.panes[0].file.?.content); + const was = try l.get(OnDisk); + if (on_disk) |out| out.* = was; + const theme = try l.get([]const u8); + defer gpa.free(theme); + // This build's place for that theme; one it has not, its first. + p.settings.theme = for (colors.themes, 0..) |t, i| { + if (std.mem.eql(u8, t.name, theme)) break i; + } else if (p.settings.theme < colors.themes.len) p.settings.theme else 0; + // A mount this process was started with stands; one of the same name + // to elsewhere is refused, never silently put over it. + for (mounts) |m| { + const existing = for (p.fs.mounts.items) |e| { + if (std.mem.eql(u8, e.name, m.name)) break e; + } else null; + if (existing) |e| { + if (!std.mem.eql(u8, e.dial, m.dial)) return error.MountConflict; + continue; } - }; - try std.testing.checkAllAllocationFailures(std.testing.allocator, Check.run, .{}); -} - -test "dump mount validation bounds names ownership inputs and duplicates" { - const panes = [_]Pane{.{ .kind = .file, .tag = "", .body = "", .file = .{} }}; - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - var state: State = .{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - try validate(state); - for ([_]Mount{ - .{ .name = "peer", .dial = "/tmp/peer.sock" }, - .{ .name = "build-1.local", .dial = "tcp!127.0.0.1!5640" }, - }) |mount| { - state.mounts = &.{mount}; - try validate(state); + p.fs.mount(gpa, m.name, m.dial) catch |err| l.note("mount {s}: {t}", .{ m.name, err }); } - for ([_]Mount{ - .{ .name = "", .dial = "/tmp/peer.sock" }, - .{ .name = ".", .dial = "/tmp/peer.sock" }, - .{ .name = "..", .dial = "/tmp/peer.sock" }, - .{ .name = "os", .dial = "/tmp/peer.sock" }, - .{ .name = "self", .dial = "/tmp/peer.sock" }, - .{ .name = "two/parts", .dial = "/tmp/peer.sock" }, - .{ .name = "two parts", .dial = "/tmp/peer.sock" }, - .{ .name = "peer", .dial = "" }, - .{ .name = "peer", .dial = "unix!/tmp/peer\x00.sock" }, - }) |mount| { - state.mounts = &.{mount}; - try std.testing.expectError(error.BadDumpMounts, validate(state)); + if (l.notes.items.len > 0) { + // A pane another build could not give is left out: so is its + // place in the layout. One whose terminal it could not give is a + // terminal still. + dropMissing(p); + for (p.panes) |slot| if (slot) |pane| try ensureKind(p, pane); + if (notes) |n| try n.appendSlice(gpa, l.notes.items); } - const duplicate = Mount{ .name = "peer", .dial = "/tmp/peer.sock" }; - state.mounts = &.{ duplicate, duplicate }; - try std.testing.expectError(error.BadDumpMounts, validate(state)); - var name: [256]u8 = @splat('x'); - state.mounts = &.{.{ .name = name[0..255], .dial = "/tmp/peer.sock" }}; - try validate(state); - state.mounts = &.{.{ .name = &name, .dial = "/tmp/peer.sock" }}; - try std.testing.expectError(error.BadDumpMounts, validate(state)); - const mounts = [_]Mount{ - .{ .name = "a", .dial = "/tmp/a" }, - .{ .name = "b", .dial = "/tmp/b" }, - .{ .name = "c", .dial = "/tmp/c" }, - .{ .name = "d", .dial = "/tmp/d" }, - .{ .name = "e", .dial = "/tmp/e" }, - .{ .name = "f", .dial = "/tmp/f" }, - .{ .name = "g", .dial = "/tmp/g" }, - .{ .name = "h", .dial = "/tmp/h" }, - .{ .name = "i", .dial = "/tmp/i" }, + // The views not live, last: what it moves into the core is never + // rolled back from under it, nothing after it failing. + const at = l.shared.items.len; + const others = l.get([]const views.Dumped) catch |err| switch (err) { + // A dump made before views were in it. + error.EndOfDump => &.{}, + else => return err, }; - state.mounts = mounts[0..8]; - try validate(state); - state.mounts = &mounts; - try std.testing.expectError(error.BadDumpMounts, validate(state)); -} - -test "version-one image records default old fields and roundtrip new state" { - const gpa = std.testing.allocator; - const legacy = - \\.{ - \\ .magic = "pardes-dump", - \\ .version = 1, - \\ .screen = .{ .cols = 80, .rows = 24 }, - \\ .active = 0, - \\ .topbar = "", - \\ .theme = "dark", - \\ .columns = .{.{ .panes = .{0} }}, - \\ .panes = .{.{ - \\ .kind = .image, - \\ .tag = "img /tmp/old.ppm New Del", - \\ .body = "", - \\ .image = .{ .path = "/tmp/old.ppm", .bytes_b64 = "" }, - \\ }}, - \\} - ; - var old = try readZon(gpa, legacy, "legacy-image"); - defer old.deinit(); - const old_image = old.value.panes[0].image.?; - try std.testing.expect(!old_image.petscii); - try std.testing.expectEqual(ImagePalette.commodore, old_image.palette); - try std.testing.expect(old_image.ascii); - try std.testing.expect(old.value.panes[0].tag_tail == null); - - const pane = Pane{ - .kind = .image, - .tag = "img petscii:on palette:terminal ascii:off /tmp/new.ppm", - .body = "", - .tag_tail = "", - .image = .{ - .path = "/tmp/new.ppm", - .petscii = true, - .palette = .terminal, - .ascii = false, - }, - }; - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - const panes = [_]Pane{pane}; - const state = State{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - var out: std.Io.Writer.Allocating = .init(gpa); - defer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - var parsed = try readZon(gpa, out.written(), "new-image"); - defer parsed.deinit(); - const restored = parsed.value.panes[0].image.?; - try std.testing.expect(restored.petscii); - try std.testing.expectEqual(ImagePalette.terminal, restored.palette); - try std.testing.expect(!restored.ascii); - try std.testing.expect(parsed.value.panes[0].tag_tail != null); - try std.testing.expectEqualStrings("", parsed.value.panes[0].tag_tail.?); -} - -test "validation bounds pane restore state" { - const ids = [_]usize{0}; - const columns = [_]Column{.{ .panes = &ids }}; - var panes = [_]Pane{.{ - .kind = .terminal, - .tag = "term", - .body = "", - .terminal = .{}, - }}; - const state = State{ - .screen = .{ .cols = 80, .rows = 24 }, - .columns = &columns, - .panes = &panes, - }; - try validate(state); - - panes[0].vweight = 0; - try std.testing.expectError(error.BadDumpPaneWeight, validate(state)); - panes[0].vweight = std.math.inf(f32); - try std.testing.expectError(error.BadDumpPaneWeight, validate(state)); - panes[0].vweight = 1; - - panes[0].scroll = @as(usize, @intCast(std.math.maxInt(i32))) + 1; - try std.testing.expectError(error.BadDumpPaneScroll, validate(state)); - panes[0].scroll = 0; - - var oversized_tail: [limits.max_tag_tail + 1]u8 = @splat('x'); - panes[0].tag_tail = &oversized_tail; - try std.testing.expectError(error.BadDumpTagTail, validate(state)); - panes[0].tag_tail = oversized_tail[0..limits.max_tag_tail]; - try validate(state); + try views.undumped(p, others); + for (others) |o| gpa.free(o.panes); + gpa.free(others); + l.shared.shrinkRetainingCapacity(at); + return p; } -// ---- a whole editor to and from a dump (acme keeps this in rows.c) ---- - -pub fn dumpState(p: *Pardes) !void { - const bytes = try dumpBytes(p, null, null); - if (p.dump_out) |d| p.gpa.free(d); - p.dump_out = bytes; - p.emit(.write_dump); +/// A pane that is none of its kinds (a terminal kept slim, or one another +/// build's dump could not give): a terminal, a new emulator for its shell. +fn ensureKind(p: *Pardes, pane: *pardes.panes.Pane) !void { + if (pane.terminal != null or pane.file != null or pane.image != null or pane.hasPdf()) return; + const fresh = try pardes.panes.terminal.create(p.gpa, @max(1, pane.cols), @max(1, pane.rows)); + pane.terminal = fresh.terminal; + fresh.terminal = null; + p.gpa.destroy(fresh); } -/// The dump of the panes `keep` marks (every pane for null), gpa-owned: a -/// whole session's, or what a grip carries to another window (Handover.zig), -/// `carried` saying which. Only the columns holding a kept pane are in it, -/// and a carried pane's column tag is its old column's no longer. -pub fn dumpBytes(p: *Pardes, keep: ?*const [MAX_PANES]bool, carried: ?Carried) ![]u8 { - const arena = p.scratch.allocator(); - var slot_to_pane: [MAX_PANES]?usize = @splat(null); - var dump_panes: [MAX_PANES]Pane = undefined; - var panes_len: usize = 0; - for (p.panes, 0..) |slot, id| { - const pane = slot orelse continue; - if (keep) |k| if (!k[id]) continue; - slot_to_pane[id] = panes_len; - dump_panes[panes_len] = try dumpPane(p, arena, pane); - panes_len += 1; - } - // An attachment names its owner by its place in the dump: serials are - // handed out again on a Restore. - for (0..p.panes.len) |id| { - const i = slot_to_pane[id] orelse continue; - const owner = layout.ownerOf(p, id) orelse continue; - // An attachment carried without its owner stands on its own. - dump_panes[i].attached_to = slot_to_pane[owner]; - } - // Zerox's twins: the text once, in the first; each other names it. - for (p.panes, 0..) |slot, id| { - const f = (slot orelse continue).file orelse continue; - if (f.twin == 0) continue; - const i = slot_to_pane[id] orelse continue; - for (p.panes[0..id], 0..) |earlier, j_id| { - const of = (earlier orelse continue).file orelse continue; - if (of.twin != f.twin) continue; - const j = slot_to_pane[j_id] orelse break; - if (dump_panes[i].file) |*df| { - df.twin_of = @intCast(j); - df.content_b64 = ""; - } - break; - } - } - - var columns: [MAX_COLS]Column = undefined; - var columns_len: usize = 0; - var column_ids: [MAX_COLS][MAX_PANES]usize = undefined; +/// The panes a column lists that are not there (another build's dump left +/// them out) taken out of it, and the keyboard on one that is. +fn dropMissing(p: *Pardes) void { for (0..p.ncol) |c| { - var ids_len: usize = 0; - for (0..p.col_n[c]) |k| if (slot_to_pane[p.col_panes[c][k]]) |compact| { - column_ids[c][ids_len] = compact; - ids_len += 1; - }; - if (keep != null and ids_len == 0) continue; - columns[columns_len] = .{ - .serial = layout.columnSerial(p, c), - .tag = if (carried == .pane) null else p.col_tags[c].own, - .weight = @as(f32, @floatFromInt(p.col_weight[c])) / @as(f32, @floatFromInt(column_weight_unit)), - .panes = column_ids[c][0..ids_len], + var n: usize = 0; + for (p.col_panes[c][0..p.col_n[c]]) |id| if (id < MAX_PANES and p.panes[id] != null) { + p.col_panes[c][n] = id; + n += 1; }; - columns_len += 1; - } - - var mounts: [filesystem.max_mounts]Mount = undefined; - for (p.fs.mounts.items, 0..) |mount, i| mounts[i] = .{ .name = mount.name, .dial = mount.dial }; - const state: State = .{ - .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, - .active = slot_to_pane[p.active] orelse 0, - .topbar = config.topbar_str, - .topbar_custom = p.global_tag.own, - .theme = p.theme().name, - .locations_config = p.locations_config, - .tree_context_tag_style = p.settings.tree_context_tag_style, - .columns = columns[0..columns_len], - .panes = dump_panes[0..panes_len], - .mounts = mounts[0..p.fs.mounts.items.len], - .settings = try changedSettings(p, arena), - .incl = p.incl.items, - .carried = carried, - }; - try validate(state); - var out: std.Io.Writer.Allocating = .init(p.gpa); - errdefer out.deinit(); - try std.zon.stringify.serialize(state, .{ .whitespace = true }, &out.writer); - return out.toOwnedSlice(); -} - -/// One pane as a dump keeps it, its strings in `arena`: a Restore's, and a -/// closed pane's in the store (Store.zig), which Panes brings back. -pub fn dumpPane(p: *Pardes, arena: std.mem.Allocator, pane: *pardes.panes.Pane) !Pane { - const tag = try tagline.tagText(p, arena, pane); - const body = try body_layer.bodyText(p, arena, pane); - const scroll: usize = @intCast(@max(0, pane.scroll())); - var dp: Pane = if (pane.file) |*f| try pardes.panes.File.dumpPane( - p, - arena, - pane, - f, - tag, - body, - scroll, - if (f.output) |o| pardes.panes.Output.word(o.from) else "", - if (f.output) |*o| o.arg() else "", - ) else if (pane.hasPdf()) blk: { - if (comptime !pdf_enabled) unreachable; - const pv = &pane.pdf.?; - break :blk try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, pv.page, pv.path, &.{}); - } else if (pane.image) |iv| - try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, scroll, iv.path, iv.raw) - else - try pardes.panes.terminal.dumpPane(pane, arena, tag, body, scroll); - dp.tag_tail = pane.tag.own; - if (dp.file) |*df| { - const dot = ctlfs.pane.dotOf(pane); - df.dot = .{ dot.q0, dot.q1 }; - } - dp.serial = pane.serial; - return dp; -} - -/// The setting lines that differ from a fresh session's, as the root ctl -/// reads them, less the ones a dump keeps apart. -fn changedSettings(p: *Pardes, arena: std.mem.Allocator) ![]const []const u8 { - var now: std.Io.Writer.Allocating = .init(arena); - var fresh: std.Io.Writer.Allocating = .init(arena); - const defaults: config.Runtime = .{}; - try pardes.ctlfs.ctl.writeSettingsOf(p, &p.settings, &now.writer); - try pardes.ctlfs.ctl.writeSettingsOf(p, &defaults, &fresh.writer); - var out: std.ArrayList([]const u8) = .empty; - var lines = std.mem.splitScalar(u8, now.written(), '\n'); - while (lines.next()) |line| { - if (line.len == 0) continue; - const word = line[0 .. std.mem.indexOfScalar(u8, line, ' ') orelse line.len]; - if (std.mem.eql(u8, word, "Theme") or std.mem.eql(u8, word, "Font")) continue; - var same = std.mem.splitScalar(u8, fresh.written(), '\n'); - while (same.next()) |other| { - if (std.mem.eql(u8, other, line)) break; - } else try out.append(arena, line); - } - return out.items; -} - -/// Applies a dump's setting lines, each as the root ctl would. -fn applySettings(p: *Pardes, lines: []const []const u8) void { - for (lines) |line| { - const sp = std.mem.indexOfScalar(u8, line, ' '); - // A ThemeFile is a request the host answers by reading the file, as - // a /ctl write of it is: asked again here, not dropped. - if (std.mem.eql(u8, line[0 .. sp orelse line.len], "ThemeFile")) { - if (sp) |s| colors.requestThemeFile(p, 0, line[s + 1 ..]); - continue; - } - const setting = config.Runtime.find(line[0 .. sp orelse line.len]) orelse continue; - _ = p.settings.apply(setting, if (sp) |s| line[s + 1 ..] else null); - if (setting.action == .tab) config.tab_now = p.settings.tab; + p.col_n[c] = n; } + if (p.active >= MAX_PANES or p.panes[p.active] == null) p.active = for (p.panes, 0..) |slot, i| { + if (slot != null) break i; + } else 0; } /// 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 { +pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { + const taped = pardes.tape.enter(p, .{ .restore = .{ .bytes = 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); + 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); 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) { - var rec: [32]u8 = undefined; - pardes.ctlfs.events.notePath(replacement, "restored", std.fmt.bufPrint(&rec, "{d} {d}", .{ was, pane.serial }) catch continue); - }; - for (old.cols[0..replacement.ncol], 0..) |was, c| if (was != 0) { - var rec: [32]u8 = undefined; - pardes.ctlfs.events.notePath(replacement, "restoredcol", std.fmt.bufPrint(&rec, "{d} {d}", .{ was, layout.columnSerial(replacement, c) }) catch continue); - }; return replacement; } -pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8) !*Pardes { - return initDump(gpa, opts, zon_bytes, null, null); +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, 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. -const OldSerials = struct { - panes: [MAX_PANES]u32 = @splat(0), - cols: [MAX_COLS]u32 = @splat(0), -}; - -fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previous: ?*const Pardes, old_serials: ?*OldSerials) !*Pardes { +/// A window of its own for panes a grip carried out of another (Handover): +/// those panes and nothing else, its settings this process's. +fn carriedWindow(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { const p = try Pardes.create(gpa, opts); errdefer p.deinit(); + _ = try Handover.adopt(p, bytes); + try reviveSession(p, null, false); + return p; +} + +/// 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-<pid>.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| { + 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) { + const first = notes.items[0 .. std.mem.indexOfScalar(u8, notes.items, '\n') orelse notes.items.len]; + var buf: [320]u8 = undefined; + p.setMessage(p.active, std.fmt.bufPrint(&buf, "a dump of another build: {s}", .{first}) catch "a dump of another build: not all of it was read"); + } + return p; +} + +/// 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. 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. + if (!resumed) views.only(p); if (previous) |old| { - p.next_serial = old.next_serial; - // Columns as panes: a restored one takes a fresh serial, never one - // a column of the old session had, living or deleted. - p.next_column_serial = old.next_column_serial; - p.lsp_seq = old.lsp_seq; - p.pipe.seq = old.pipe.seq; + // Never a serial or a request number the old session gave out. + p.next_serial = @max(p.next_serial, old.next_serial); + p.next_column_serial = @max(p.next_column_serial, old.next_column_serial); + p.lsp_seq = @max(p.lsp_seq, old.lsp_seq); + p.pipe.seq = @max(p.pipe.seq, old.pipe.seq); + // The host's facts, as this host told them. p.cell_pixels = old.cell_pixels; p.row_metrics = old.row_metrics; p.native_images = old.native_images; - // The workspace tag's Restore word names the dump it came from. - if (old.last_dump) |d| p.last_dump = try gpa.dupe(u8, d); - // Recent goes on across a Restore, the old session's own list. p.recent = old.recent.clone(p.recentGpa()); - // The same session: its closed panes go on being its own. p.store = old.store.clone(p.recentGpa()); } else p.loadRecent(); - var parsed = try readZon(gpa, zon_bytes, "load"); - defer parsed.deinit(); - const st = parsed.value; - p.locations_config = st.locations_config; - p.settings.tree_context_tag_style = st.tree_context_tag_style; - applySettings(p, st.settings); - if (st.incl) |dirs| { - for (p.incl.items) |dir| p.gpa.free(dir); - p.incl.clearRetainingCapacity(); - for (dirs) |dir| if (dir.len > 0 and dir[0] == '/' and std.mem.indexOfScalar(u8, dir, 0) == null) { - const copy = p.gpa.dupe(u8, dir) catch continue; - p.incl.append(p.gpa, copy) catch p.gpa.free(copy); - }; + config.tab_now = p.settings.tab; + // A ThemeFile is a request the host answers by reading the file again; + // until it has (and for good if it fails) the theme the dump had shows. + if (p.theme_file_path.get().len > 0) { + var path: [limits.host_path_cap]u8 = undefined; + const name = p.theme_file_path.get(); + @memcpy(path[0..name.len], name); + colors.requestThemeFile(p, 0, path[0..name.len]); } - // A dump naming a ThemeFile asks the host to load it again: until it - // has, and for good if it fails, the theme this session showed goes on - // (a failed ThemeFile changes nothing, a Restore's included), unless - // the dump names a Theme of its own. - if (previous) |old| if (p.theme_file_path.get().len > 0) { - const names_theme = for (st.settings) |line| { - if (std.mem.startsWith(u8, line, "Theme ")) break true; - } else false; - try colors.carryTheme(p, old, !names_theme); + for (p.panes) |slot| if (slot) |pane| { + pane.fs.unannounced = true; }; - for (st.mounts) |mount| { - var already_mounted = false; - for (p.fs.mounts.items) |existing| { - if (!std.mem.eql(u8, mount.name, existing.name)) continue; - if (!std.mem.eql(u8, mount.dial, existing.dial)) return error.MountConflict; - already_mounted = true; - break; - } - if (!already_mounted) try p.fs.mount(gpa, mount.name, mount.dial); - } - - for (themes, 0..) |t, i| { - if (std.mem.eql(u8, t.name, st.theme)) p.settings.theme = @intCast(i); - } - for (st.panes, 0..) |src, i| { - if (i >= MAX_PANES) break; - if (old_serials) |o| o.panes[i] = src.serial; - const pane = try restorePane(p, i, src); - pane.vweight = src.vweight; - pane.collapsed = src.collapsed; - } - p.ncol = @min(st.columns.len, MAX_COLS); - p.col_serial = @splat(0); - if (st.topbar_custom) |bar| p.global_tag.own = try p.gpa.dupe(u8, bar); - for (st.columns[0..p.ncol], 0..) |col, c| { - if (old_serials) |o| o.cols[c] = col.serial; - if (col.tag) |bar| p.col_tags[c].own = try p.gpa.dupe(u8, bar); - const scaled = @as(f64, @floatCast(col.weight)) * @as(f64, @floatFromInt(column_weight_unit)); - const bounded = @min(scaled, @as(f64, @floatFromInt(max_column_weight))); - p.col_weight[c] = @max(1, @as(u64, @intFromFloat(@round(bounded)))); - p.col_n[c] = @min(col.panes.len, MAX_PANES); - for (col.panes[0..p.col_n[c]], 0..) |pid, k| p.col_panes[c][k] = pid; - } - // A dump's pane i is restored into slot i: its owner's serial now. - for (st.panes[0..@min(st.panes.len, MAX_PANES)], 0..) |src, i| { - const owner = src.attached_to orelse continue; - if (owner >= MAX_PANES or owner == i) continue; - const pane = p.panes[i] orelse continue; - pane.attached_to = (p.panes[owner] orelse continue).serial; - } - p.active = @min(st.active, MAX_PANES - 1); + // 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; _ = p.takeHaptic(); // see init: a restored session is not a gesture try p.fs.log.buf.ensureTotalCapacityPrecise(p.gpa, p.fs.log.cap); ctlfs.events.announce(p); - return p; } -/// Dump pane `src` made a pane again in slot `i`, installed (a fresh -/// serial) and not yet placed: a Restore's, and a closed pane Panes brings -/// back (Store.zig). -pub fn restorePane(p: *Pardes, i: usize, src: Pane) !*pardes.panes.Pane { - const gpa = p.gpa; - const pane: *pardes.panes.Pane = switch (src.kind) { - .terminal => terminal: { - const t = src.terminal.?; - // A host that runs shells gets them back: the old history - // above a new shell in the directory it was in. The web - // shell and an isolated build only ever replay it. - // A command pane comes back finished, showing what it said - // and how it ended: its command is not run again. - const live = comptime (terminal_panes and platform != .web and !isolated); - const revive = live and t.cwd.len <= effect_path_cap and t.command.len == 0; - const restored = try pardes.panes.terminal.restore(p, src, revive); - p.installPane(i, restored); - try restored.setOwnedCwd(t.cwd); - if (t.shell.len > 0) restored.shell = try gpa.dupe(u8, t.shell); - if (t.command.len > 0) { - restored.command = try gpa.dupe(u8, t.command); - restored.command_done = true; - restored.command_status = t.status; - } - if (revive) p.emitSpawn(i, restored.serial, t.cwd); - if (t.petscii and pardes.panes.terminal.petscii(restored) == false) pardes.panes.terminal.togglePetscii(restored); - break :terminal restored; - }, - .file => file: { - const restored = try pardes.panes.File.restore(p, i, src); - // A twin: on the earlier pane's buffer again. - if (src.file.?.twin_of) |j| if (j < i) pardes.panes.File.relink(p, j, i, src.file.?.dot); - break :file restored; - }, - .image => restore_image: { - const im = src.image.?; - if (pardes.panes.Pdf.isPath(im.path)) { - var raw: []u8 = if (im.bytes_b64.len > 0) - try decodeBytes(gpa, im.bytes_b64) - else - &.{}; - errdefer if (raw.len > 0) gpa.free(raw); - if (comptime pdf_enabled) { - if (pardes.panes.Pdf.openPane(p, i, im.path, src.scroll + 1, .last) catch null) |restored| { - if (raw.len > 0) gpa.free(raw); - raw = &.{}; - // Its fit and tint as they were (PdfFit, PdfTint). - const pv = &restored.pdf.?; - if (std.meta.stringToEnum(@TypeOf(pv.fit), im.pdf_fit)) |fit| if (fit != pv.fit) pardes.panes.Pdf.toggleFit(restored); - if (std.meta.stringToEnum(@TypeOf(pv.tint), im.pdf_tint)) |tint| while (pv.tint != tint) pardes.panes.Pdf.toggleTint(restored); - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - break :restore_image restored; - } - } - // If the source path is unavailable, retain its bytes - // as an ordinary file rather than dropping the pane. - const path = try gpa.dupe(u8, im.path); - errdefer gpa.free(path); - const content = if (raw.len > 0) raw else try gpa.dupe(u8, ""); - raw = &.{}; - errdefer if (content.len > 0) gpa.free(content); - const history = try pardes.panes.File.History.create(gpa); - errdefer gpa.destroy(history); - const restored = try p.newDocPane(i); - restored.file = .{ .path = path, .content = content, .history = history }; - restored.body.cur_pinned = true; - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - p.emit(.{ .watch = .{ .pane = @intCast(i), .on = true, .mode = .baseline_disk } }); - break :restore_image restored; - } - break :restore_image try pardes.panes.Image.restore(p, i, src); - }, +/// A loaded pane given to this host: a terminal a new shell in its old +/// one's directory under its history, a file watched again (one changed on +/// disk since comes back as it is now, one unsaved is marked changed), a +/// PDF opened again. +fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void { + if (pane.terminal != null) { + // A command pane comes back finished, showing what it said: its + // command is not run again. A host that runs shells starts one + // under the history in the old one's directory; the web shell and + // an isolated build only show it. + if (pane.command != null) { + pane.command_done = true; + return; + } + if (comptime !(terminal_panes and platform != .web and !isolated)) return; + if (pane.cwdSlice().len > effect_path_cap) return; + pardes.panes.terminal.reviveShell(pane); + p.emitSpawn(id, pane.serial, pane.cwdSlice()); + return; + } + if (comptime pdf_enabled) if (pane.pdf != null) { + pardes.panes.Pdf.reopen(p, pane) catch |err| { + p.reportError(id, "Restore", err); + p.removePane(id, null) catch {}; + }; + return; }; - tagline.restoreDumpTail(p, pane, src); + const f = if (pane.file) |*file| file else return; + if (f.output != null or filesystem.localPath(f.path) == null) return; + p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + // Its file gone from disk since the dump: its text is nowhere else, so + // it is said deleted, and Del, Exit and Restore ask first. One never on + // disk (a name not yet saved) comes back as it was. + // (A host with no disk of its own, the web's, asks nothing.) + if (comptime pardes.hosted) if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); +} + +// ---- one pane to and from a dump: the store's, a handover's ---- + +/// Pane `pane` alone as a dump (`gpa`'s): what it shares with another +/// pane is its own in it (a directory it follows), and nothing says where +/// it was. `slim`: without a terminal's history, a file's undo, or a clean +/// file's text, which is read from its file again (a store's record past +/// its bound). +pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![]u8 { + // The fields it changes put back after, not the whole pane (88 KB) kept + // aside: a store record is made at every close. + const cwd = pane.cwd; + const attached_to = pane.attached_to; + const terminal = pane.terminal; + defer { + pane.cwd = cwd; + pane.attached_to = attached_to; + pane.terminal = terminal; + } + if (cwd == .inherited) { + // Read before the union is written (its tag is set first). + const dir = pane.cwdSlice(); + pane.cwd = .{ .owned = @constCast(dir) }; + } + pane.attached_to = null; + if (!slim) return walk.save(pardes.panes.Pane, gpa, pane); + pane.terminal = null; + const f = if (pane.file) |*file| file else return walk.save(pardes.panes.Pane, gpa, pane); + const history = f.history; + const content = f.content; + defer { + f.history = history; + f.content = content; + } + var empty: pardes.panes.File.History = .{}; + f.history = ∅ + if (f.output == null and f.revision == f.saved_revision and filesystem.localPath(f.path) != null) f.content = &.{}; + return walk.save(pardes.panes.Pane, gpa, pane); +} + +/// A pane `savePane` made, installed in free slot `id` with a serial of +/// this session's and not yet placed, and given to the host as a loaded +/// session's are (`revive`). +pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane { + var l = try walk.Load(&.{pardes.panes.Pane}).init(p.gpa, bytes); + defer l.deinit(); + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = pardes.panes.terminal.emulatorGpa(p.gpa) }; + const pane = try p.gpa.create(pardes.panes.Pane); + errdefer { + l.rollback(); + p.gpa.destroy(pane); + } + pane.* = try l.get(pardes.panes.Pane); + // The slots and serials of the session it was in name nothing here. + pane.search_pane = null; + pane.search_row = null; + pane.command_from = 0; + pane.places.from = 0; + pane.collapsed = false; + pane.prompt_view = p.view; + if (pane.file) |*f| if (f.content.len == 0 and (f.saved_len orelse 0) > 0) { + // A clean file kept slim: its text is its file's. + if (filesystem.read(p, f.path)) |text| { + f.content = text; + } else |_| {} + }; + try ensureKind(p, pane); + p.installPane(id, pane); + revivePane(p, id, pane, false); return pane; } -test "a dump keeps the settings that differ from a fresh session's, and a restore applies them" { +test "a dump keeps the session's settings, and a restore applies them" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); @@ -1092,10 +573,6 @@ test "a dump keeps the settings that differ from a fresh session's, and a restor try std.testing.expect(p.executeBuiltinLine(p.active, "Verbose off")); try std.testing.expect(p.executeBuiltinLine(p.active, "JumpScope all")); try dumpState(p); - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"Placement pardes\"") != null); - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"JumpScope all\"") != null); - // What is left at its default is not written. - try std.testing.expect(std.mem.indexOf(u8, p.dump_out.?, "\"BootShell keep\"") == null); const restored = try initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); defer restored.deinit(); try std.testing.expectEqual(config.Runtime.Placement.pardes, restored.settings.placement); @@ -1186,3 +663,73 @@ test "a pane restored with unsaved text is asked about by Exit, Restore, Del and try std.testing.expect(!restored.quit and restored.panes[id] != null); } } + +test "a loaded terminal's history stays above its banner, the cursor where it was" { + 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 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + term.feedOutput(p, p.panes[0].?, "one\r\ntwo\r\n$ "); + const before = term.gridCursor(p.panes[0].?); + try dumpState(p); + const loaded = try load(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, p.dump_out.?, null); + defer loaded.deinit(); + try std.testing.expectEqual(before, term.gridCursor(loaded.panes[0].?)); + const q = try initFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, p.dump_out.?); + defer q.deinit(); + const text = try term.screenTextAlloc(q.panes[0].?, gpa); + defer gpa.free(text); + try std.testing.expect(std.mem.indexOf(u8, text, "one\ntwo\n") != null); + 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 }); + defer p.deinit(); + const pane = try p.setTestFile("one\ntwo\n"); + _ = ctlfs.pane.spliceBody(p, pane, 0, 0, "unsaved ").?; + try std.testing.expect(p.executeBuiltinLine(p.active, "Zerox")); + const bytes = try dumpBytes(p); + defer gpa.free(bytes); + const q = try Pardes.create(gpa, p.opts); + defer q.deinit(); + var l = try SessionLoad.init(gpa, bytes); + defer l.deinit(); + try std.testing.expect(l.same); + l.same = false; + try l.into(Pardes, q); + try std.testing.expectEqualStrings("", l.notes.items); + try std.testing.expectEqual(walk.digest(Pardes, p), walk.digest(Pardes, q)); +} diff --git a/src/echo.zig b/src/echo.zig new file mode 100644 index 00000000..e25953e0 --- /dev/null +++ b/src/echo.zig @@ -0,0 +1,842 @@ +//! Predicted echo for a replica's terminal panes, mosh's way +//! (docs/next-daemon.md, "Phase 6: predicted echo notes"). A key typed into +//! a terminal goes to the authority, and its echo comes back as screen state +//! a round trip later: this shows it at once. +//! +//! Presentation only. The overlay is the replica's own (`Pardes.echo`, a +//! `.local` field the rollback copy skips), painted over a terminal's body +//! after it is drawn (`paint`, from body_layer.renderBody, which the tty and +//! the GUI share). The core's emulator stays the authority's, and no digest +//! sees any of this. +//! +//! What is predicted: a printable character at the cursor, backspace, and +//! left and right, in tty mode on the primary screen at the live bottom. Any +//! other input this replica could not apply itself (Enter, Tab, a chord, a +//! paste, a click) stops prediction until the authority has confirmed it and +//! its echo has had `grace` to come back: predictions start again from the +//! screen's own cursor. So a key typed after Tab waits a round trip; it is +//! never drawn where the completion was not. +//! +//! Judged as mosh does, once the authority confirmed the key and `grace` +//! passed: at a delta, a cell the screen shows goes, and one it does not is +//! a misprediction, and every prediction goes. With no delta (nothing +//! changed: no echo), a frame judges it `stale` later. Shown at once at a +//! shell's prompt (pardes's OSC 133 marks say its line editor echoes); +//! elsewhere only once an echo was confirmed there since the last key that +//! was not predicted (`cat`, a shell without marks), and underlined; never +//! after a shown misprediction until an echo is confirmed again. A password +//! prompt or a program that ignores input confirms none, so nothing typed +//! there is ever shown. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const terminal = pardes.panes.terminal; +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const Surface = pardes.Surface; +const Key = pardes.Key; + +const ms = std.time.ns_per_ms; +/// After the authority confirmed a key, how long its echo has to come back +/// before a delta that does not show it makes a misprediction (mosh's +/// ECHO_TIMEOUT): the shell's echo, then the authority's next frame. +// ponytail: fixed; a loaded host's later echo makes a drawn character go and +// come back. The echo delay measured per terminal would be the bound. +pub const grace_ns = 50 * ms; +/// With no delta at all, how much longer a frame waits to judge a key. +pub const stale_ns = 250 * ms; +const never = std.math.maxInt(u64); +/// Cells predicted at once: a line typed a round trip ahead. +const max = 64; + +/// A cell predicted: `cp` at column `x` of absolute row `row` (the +/// authority's numbering, screen_state's), a space for a cell backspace +/// cleared; `was` what the screen showed there before; `seq` the key's. +pub const Cell = struct { x: u16, row: u64, cp: u21, was: u21, seq: u32, due: u64 = never }; +pub const Cursor = struct { x: u16, row: u64, seq: u32, due: u64 = never }; + +pub const Stats = struct { + /// Keys into a terminal predicted, and not. + predicted: u32 = 0, + unpredicted: u32 = 0, + /// Cells the screen showed when judged; cells and cursors it did not. + confirmed: u32 = 0, + mispredicted: u32 = 0, + cursor_missed: u32 = 0, +}; + +pub const Echo = struct { + /// Off: nothing predicted (the harness's baseline). + on: bool = true, + /// The harness's clock (ns); the monotonic one when null. + now: ?u64 = null, + /// Its client's view, and the terminal it predicts into. + view: u8 = 0, + pane: u8 = 0, + serial: u32 = 0, + /// Each terminal's last delta: its active area's first row, absolute. + tops: [pardes.MAX_PANES]u64 = @splat(0), + cells: [max]Cell = undefined, + n: u8 = 0, + cursor: ?Cursor = null, + /// The newest of its client's inputs the authority confirmed. + acked: u32 = 0, + /// The newest input not predicted, and when its echo is due: nothing is + /// predicted until then. `wait_key`: it was a key forwarded to this + /// terminal (no other pane's), so the keys typed into it meanwhile wait + /// in `deferred`, to be predicted over the screen that shows its doing. + wait: u32 = 0, + wait_due: u64 = 0, + wait_key: bool = false, + deferred: [max]Deferred = undefined, + nd: u8 = 0, + /// An echo confirmed since the last key not predicted: shown off a + /// prompt too. A shown misprediction since the last confirmed echo: + /// shown nowhere. + trusted: bool = false, + doubt: bool = false, + /// What it draws changed: the client draws a frame. + changed: bool = false, + stats: Stats = .{}, + /// Each key's prediction as it is made, when set (the harness's; on + /// the C heap). + log: ?*std.ArrayListUnmanaged(Predicted) = null, + + pub const Predicted = struct { seq: u32, cell: ?Cell, cursor: Cursor }; + + fn time(e: *const Echo) u64 { + return e.now orelse pardes.FrameStats.nowNs(); + } + + const Deferred = struct { seq: u32, kind: ?Kind, due: u64 = never }; + + fn drop(e: *Echo) void { + if (e.n > 0 or e.cursor != null) e.changed = true; + e.n = 0; + e.cursor = null; + } + + /// Every prediction goes, and the keys that made them may still be on + /// their way: the keys typed next wait for the newest of them to show, + /// as keys typed behind one not predicted do (`deferred`). Predicted + /// at once from the screen's cursor, they would land where those keys' + /// echo is about to (found at 207 ms, where three keys are in flight: + /// one drop, and every key after it misjudged, drawn nowhere). + fn lose(e: *Echo) void { + // The cursor after the newest key: its `due` is that key's, never + // until the authority has it. + const newest = e.cursor; + e.drop(); + const cur = newest orelse return; + if (e.waiting() or e.time() >= cur.due) return; + e.wait = cur.seq; + e.wait_key = true; + e.nd = 0; + e.wait_due = cur.due; + } + + fn waiting(e: *const Echo) bool { + return e.acked < e.wait or e.time() < e.wait_due; + } + + fn waitFor(e: *Echo, seq: u32, key: bool) void { + e.wait = seq; + e.wait_due = never; + e.wait_key = key; + e.nd = 0; + } + + /// Its client's input `seq`, after the replica took it: `applied`, + /// it was applied here at once (so it was no key into a terminal). + pub fn typed(e: *Echo, c: *Pardes, view: u8, ev: pardes.Event, seq: u32, applied: bool) void { + e.view = view; + if (applied or comptime !terminal.enabled) return; + switch (ev) { + // ponytail: a motion or focus report a program asked for could + // move its cursor; the judgement catches that. + .mouse => |m| if (m.kind == .motion) return, + .pointer_leave, .focus => return, + else => {}, + } + const id = keyboardPane(c, view) orelse { + // Another pane's, or not a key: where the keys after it go is + // not known here until the authority has it. ponytail: so they + // wait for it too, each, until a pause. + e.waitFor(seq, false); + return; + }; + if (id != e.pane or c.panes[id].?.serial != e.serial) { + e.drop(); + e.nd = 0; + e.pane = @intCast(id); + e.serial = c.panes[id].?.serial; + } + const kind = kindOf(ev); + const key = forwarded(c, ev); + // A key that is not one of those predicted says nothing of the + // program the keys after it reach (Enter: a password prompt's). + if (kind == null) e.trusted = false; + if (!e.on) { + e.stats.unpredicted += 1; + return e.waitFor(seq, key); + } + // Behind keys still deferred: after them, in order. + e.catchUp(c); + if (e.waiting() or e.nd > 0) { + if (e.wait_key and key and e.nd < max) { + e.deferred[e.nd] = .{ .seq = seq, .kind = kind }; + e.nd += 1; + return; + } + e.stats.unpredicted += 1; + return e.waitFor(seq, false); + } + if (kind) |k| if (e.predict(c.panes[id].?, k, seq)) { + e.stats.predicted += 1; + e.changed = true; + return; + }; + e.stats.unpredicted += 1; + e.waitFor(seq, key); + } + + /// The wait over (its key's doing on the screen), the keys deferred + /// behind it that the screen does not show yet are predicted over it, + /// in order; those it shows are not. One the authority has whose echo + /// may be on its way puts it off until its grace has passed. + fn catchUp(e: *Echo, c: *Pardes) void { + if (e.nd == 0 or e.waiting()) return; + const now = e.time(); + var i: usize = 0; + while (i < e.nd and e.deferred[i].due <= now) i += 1; + if (i < e.nd and e.deferred[i].due != never) return; + e.stats.unpredicted += @intCast(i); + const pane = c.panes[e.pane] orelse return e.waitFor(e.wait, false); + if (pane.serial != e.serial) return e.waitFor(e.wait, false); + while (i < e.nd) : (i += 1) { + const d = e.deferred[i]; + if (d.kind) |k| if (e.predict(pane, k, d.seq)) { + e.stats.predicted += 1; + e.changed = true; + continue; + }; + // The next to wait for, the rest behind it. + e.stats.unpredicted += 1; + const rest = e.nd - i - 1; + std.mem.copyForwards(Deferred, e.deferred[0..rest], e.deferred[i + 1 .. e.nd]); + e.wait = d.seq; + e.wait_due = never; + e.wait_key = true; + e.nd = @intCast(rest); + return; + } + e.nd = 0; + } + + /// Whether a key `kind` into terminal `pane` was predicted. + fn predict(e: *Echo, pane: *Pane, kind: Kind, seq: u32) bool { + const state = pane.terminal orelse return false; + const vt = &state.vt; + if (vt.screens.active_key != .primary or !terminal.atBottom(pane) or !vt.modes.get(.cursor_visible)) return false; + const top = e.tops[e.pane]; + const screen = vt.screens.active; + const cur: Cursor = e.cursor orelse .{ .x = @intCast(screen.cursor.x), .row = top + screen.cursor.y, .seq = 0 }; + if (cur.row < top or cur.row - top >= vt.rows or e.n == max) return false; + var next: Cursor = .{ .x = cur.x, .row = cur.row, .seq = seq }; + var cell: ?Cell = null; + switch (kind) { + .char => |cp| { + // The last column wraps as a program pleases; a cell that + // holds another character is a line edited in its middle, + // or a suggestion (fish's) being left: the shell redraws + // what is right of it. ponytail: mosh's shift of the rest + // would cover bash's middle, and mispredict fish's. + if (cur.x + 1 >= vt.cols) return false; + const under = e.cpAt(state, cur.x, cur.row); + if (!blank(under) and under != cp) return false; + cell = .{ .x = cur.x, .row = cur.row, .cp = cp, .was = under, .seq = seq }; + next.x += 1; + }, + // At the end of a line only: in its middle a line editor moves + // the rest left. + .backspace => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row) or !blank(e.cpAt(state, cur.x, cur.row))) return false; + cell = .{ .x = cur.x - 1, .row = cur.row, .cp = ' ', .was = e.cpAt(state, cur.x - 1, cur.row), .seq = seq }; + next.x -= 1; + }, + .left => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row)) return false; + next.x -= 1; + }, + // Over typed text only: past its end, fish takes the right + // arrow for its suggestion (another colour). + .right => { + if (cur.x + 1 >= vt.cols or !e.isText(state, cur.x, cur.row)) return false; + next.x += 1; + }, + } + if (cell) |want| e.put(want); + e.cursor = next; + if (e.log) |l| l.append(std.heap.c_allocator, .{ .seq = seq, .cell = cell, .cursor = next }) catch {}; + return true; + } + + fn put(e: *Echo, want: Cell) void { + for (e.cells[0..e.n]) |*have| if (have.x == want.x and have.row == want.row) { + const was = have.was; + have.* = want; + have.was = was; + return; + }; + e.cells[e.n] = want; + e.n += 1; + } + + fn mine(e: *const Echo, x: u16, row: u64) ?*const Cell { + for (e.cells[0..e.n]) |*have| if (have.x == x and have.row == row) return have; + return null; + } + + /// What the screen shows at a cell as predicted so far. + fn cpAt(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + if (e.mine(x, row)) |have| return have.cp; + return e.screenCp(state, x, row); + } + + /// What the screen itself shows there; off its active area, a + /// codepoint no key types. + fn screenCp(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + const ci = e.screenCell(state, x, row) orelse return 0x10ffff; + return ci.cell.codepoint(); + } + + fn screenCell(e: *const Echo, state: *terminal.State, x: u16, row: u64) ?terminal.PageCell { + if (comptime !terminal.enabled) return null; + const top = e.tops[e.pane]; + if (row < top or row - top >= state.vt.rows) return null; + return state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = @intCast(row - top) } }); + } + + /// A prompt's input there, or a character predicted. + fn isInput(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + return ci.cell.semantic_content == .input; + } + + /// Typed text there: a character predicted, or a prompt's input in the + /// colour of the cell before it. + fn isText(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + if (ci.cell.semantic_content != .input or blank(ci.cell.codepoint())) return false; + if (x == 0) return true; + const before = e.screenCell(state, x - 1, row) orelse return false; + return before.cell.style_id == ci.cell.style_id; + } + + /// The authority confirmed its client's input `seq`. + pub fn confirmed(e: *Echo, c: *Pardes, seq: u32) void { + e.acked = seq; + const due = e.time() + grace_ns; + for (e.deferred[0..e.nd]) |*d| if (d.seq <= seq and d.due == never) { + d.due = due; + }; + for (e.cells[0..e.n]) |*cell| if (cell.seq <= seq and cell.due == never) { + cell.due = due; + }; + if (e.cursor) |*cur| if (cur.seq <= seq and cur.due == never) { + cur.due = due; + }; + if (e.wait <= seq and e.wait_due == never) { + // The authority stepped a key this did not predict, after every + // one it did: its deltas from here show that key's doing, which + // no prediction knew of. They go, judged neither way. + e.wait_due = due; + e.drop(); + } + e.catchUp(c); + } + + /// Terminal `id`'s delta was applied, its active area's first row now + /// `top`. + pub fn delta(e: *Echo, c: *Pardes, id: u8, top: u64) void { + if (id == e.pane and top != e.tops[id] and (e.n > 0 or e.cursor != null)) { + // Scrolled: the rows predicted are others now. + e.tops[id] = top; + return e.lose(); + } + e.tops[id] = top; + if (id == e.pane) e.judge(c, 0); + e.catchUp(c); + } + + /// Once a frame: what is stale judged. Whether to draw. + pub fn frame(e: *Echo, c: *Pardes) bool { + if (e.n > 0 or e.cursor != null) e.judge(c, stale_ns); + e.catchUp(c); + defer e.changed = false; + return e.changed; + } + + fn judge(e: *Echo, c: *Pardes, slack: u64) void { + if (comptime !terminal.enabled) return; + if (e.n == 0 and e.cursor == null) return; + const now = e.time(); + const pane = c.panes[e.pane] orelse return e.drop(); + if (pane.serial != e.serial) return e.drop(); + const state = pane.terminal orelse return e.drop(); + if (state.vt.screens.active_key != .primary) return e.drop(); + const shown = e.visible(c); + var i: usize = 0; + while (i < e.n) { + const cell = e.cells[i]; + if (now < cell.due) { + i += 1; + continue; + } + if (!blank(cell.cp) and e.screenCp(state, cell.x, cell.row) != cell.cp) { + if (now < cell.due + slack) { + i += 1; + continue; + } + e.stats.mispredicted += 1; + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + const cur = state.vt.screens.active.cursor; + std.debug.print("echo: key {d}'s {u} at {d},{d} not shown ({u} there) {d} ms past due; top {d}, the screen's cursor {d},{d}, acked {d}\n", .{ cell.seq, cell.cp, cell.x, cell.row, e.screenCp(state, cell.x, cell.row), (now - cell.due) / ms, e.tops[e.pane], cur.x, cur.y, e.acked }); + } + if (shown) e.doubt = true; + e.trusted = false; + return e.lose(); + } + // Shown, or a space (mosh: too easy to show by chance to vouch). + // A key before the last one not predicted vouches for nothing + // after it (a line's characters for the password prompt its + // Enter brought up). + if (!blank(cell.cp)) { + e.stats.confirmed += 1; + if (cell.was != cell.cp and cell.seq > e.wait) { + e.trusted = true; + e.doubt = false; + } + } + e.cells[i] = e.cells[e.n - 1]; + e.n -= 1; + e.changed = true; + } + const cur = e.cursor orelse return; + if (now < cur.due) return; + const screen = state.vt.screens.active; + if (screen.cursor.x == cur.x and e.tops[e.pane] + screen.cursor.y == cur.row) { + e.cursor = null; + e.changed = true; + } else if (now >= cur.due + slack) { + e.stats.cursor_missed += 1; + e.lose(); + } + } + + /// Whether its predictions are drawn now. + pub fn visible(e: *const Echo, c: *const Pardes) bool { + if (e.doubt) return false; + if (e.trusted) return true; + const pane = c.panes[e.pane] orelse return false; + return atPrompt(pane); + } + + /// Over terminal pane `id`'s body, as renderBody drew it: the + /// characters predicted and the cursor after them. + pub fn paint(e: *const Echo, p: *const Pardes, s: *Surface, pane: *const Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16, active: bool) void { + if (comptime !terminal.enabled) return; + if (e.n == 0 and e.cursor == null) return; + if (id != e.pane or pane.serial != e.serial or p.view != e.view or pane.body.mode != .tty) return; + if (!e.visible(p)) return; + const state = pane.terminal orelse return; + const sb = state.vt.screens.active.pages.scrollbar(); + const top = e.tops[id]; + // Off a prompt, the trust is inferred: underlined, as mosh does. + const flag = !atPrompt(pane); + const at = struct { + fn f(sb_: @TypeOf(sb), pan: u16, h: u16, top_: u64, row: u64) ?u16 { + if (row < top_ or row - top_ >= sb_.len) return null; + const screen_row = sb_.total - sb_.len + (row - top_); + if (screen_row < sb_.offset + pan) return null; + const y = screen_row - sb_.offset - pan; + return if (y < h) @intCast(y) else null; + } + }.f; + for (e.cells[0..e.n]) |cell| { + const y = at(sb, pane.pan, body_h, top, cell.row) orelse continue; + if (cell.x >= tw) continue; + const dst = s.at(tx + cell.x, body_y + y); + // Typed text continues the text before it (mosh's heuristic). + var style = dst.style; + if (cell.x > 0) { + const before = s.at(tx + cell.x - 1, body_y + y); + if (!before.default and !blank(before.text[0])) style = before.style; + } + if (flag and !blank(cell.cp)) style.ul = .single; + dst.default = false; + dst.text[0] = @intCast(cell.cp); + dst.len = 1; + dst.style = style; + } + if (!active or pane.focus != .body) return; + const cur = e.cursor orelse return; + const y = at(sb, pane.pan, body_h, top, cur.row) orelse return; + if (cur.x < tw) s.cursor = .{ .x = tx + cur.x, .y = body_y + y }; + } +}; + +const Kind = union(enum) { char: u21, backspace, left, right }; + +fn kindOf(ev: pardes.Event) ?Kind { + const k = switch (ev) { + .key => |k| k, + else => return null, + }; + if (k.ctrl or k.alt) return null; + switch (k.cp) { + Key.backspace => return .backspace, + Key.left => return .left, + Key.right => return .right, + else => {}, + } + // As forwardKey sends it. ponytail: ASCII only; a width table would + // let the rest in. + const cp: u21 = if (k.text.len == 1) + k.text[0] + else if (k.text.len == 0 and k.cp >= 'a' and k.cp <= 'z' and k.shift) + k.cp - 'a' + 'A' + else if (k.text.len == 0) + k.cp + else + return null; + return if (cp >= 0x20 and cp < 0x7f) .{ .char = cp } else null; +} + +/// Whether key `ev` goes to the terminal's program as it is (forwardKey's) +/// and not to pardes: Esc backs out of the pane, the tty chord leaves tty +/// mode, Ctrl-V pastes the register. +fn forwarded(c: *const Pardes, ev: pardes.Event) bool { + const k = switch (ev) { + .key => |k| k, + else => return false, + }; + if (k.cp == Key.escape) return false; + return !(k.ctrl and (k.cp == c.tty_toggle or k.cp == 'v' or k.cp == 'V')); +} + +fn blank(cp: u21) bool { + return cp == 0 or cp == ' '; +} + +/// At a shell's prompt, reading its input: pardes's marks (a B and no C +/// since) and the cursor's own, on the primary screen. A shell without +/// pardes's marks is trusted only once its echo is confirmed. +fn atPrompt(pane: *const Pane) bool { + if (comptime !terminal.enabled) return false; + const state = pane.terminal orelse return false; + return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.promptInputReady(pane); +} + +/// The terminal view `view`'s keys go to, in tty mode: none when they go +/// elsewhere (a prompt, a header, window mode, another pane). +fn keyboardPane(c: *const Pardes, view: u8) ?usize { + const live = c.view == view; + const part = if (live) null else &(c.parked[view] orelse return null).view; + const id = if (part) |v| v.active else c.active; + if (if (part) |v| v.window_pick != null or v.header_focus else c.window_pick != null or c.header_focus) return null; + const pane = c.panes[id] orelse return null; + if (!pane.isTerminal() or pane.terminal == null or pane.prompt != .none) return null; + const mode = if (live) pane.body.mode else pane.pv[view].body.mode; + const focus = if (live) pane.focus else pane.pv[view].focus; + return if (mode == .tty and focus == .body) id else null; +} + +// ---- tests: one authority and one replica, its shell played by hand ---- + +const testing = std.testing; +const replica = @import("replica.zig"); + +const Rig = struct { + a: *replica.Authority, + r: *replica.Replica, + screens: replica.Screens, + peer: u16, + shell: u8, + /// What the authority wrote its shell's pty. + written: std.ArrayListUnmanaged(u8) = .empty, + now: u64 = 1_000 * ms, + + fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + const g: *Rig = @ptrCast(@alignCast(ctx.?)); + g.written.appendSlice(testing.allocator, bytes) catch {}; + } + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite }; + + fn init(g: *Rig) !void { + const gpa = testing.allocator; + g.written = .empty; + g.now = 1_000 * ms; + g.a = try replica.Authority.init(gpa, .{ .cols = 60, .rows = 16, .boot = .tty_shell }, .{ .ctx = g, .vtable = &vtable }, true); + g.screens = .init(gpa); + g.peer = try g.screens.addPeer(); + g.r = try replica.Replica.init(gpa, 1, try g.a.unit(0, 0), true); + g.shell = for (g.a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + g.r.echo.now = g.now; + } + + fn deinit(g: *Rig) void { + g.r.deinit(); + g.screens.deinit(g.a.core); + g.a.deinit(); + g.written.deinit(testing.allocator); + } + + fn later(g: *Rig, dt: u64) void { + g.now += dt; + g.r.echo.now = g.now; + } + + /// The shell prints `bytes`; the replica gets the unit and the delta. + fn prints(g: *Rig, bytes: []const u8) !void { + g.a.output(.{ .output = .{ .pane = g.shell, .bytes = bytes } }); + try g.sync(); + } + + fn sync(g: *Rig) !void { + const gpa = testing.allocator; + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.settle(); + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try g.screens.flush(g.a.core, g.peer, &msgs); + for (msgs.items) |m| if (g.r.screen(m)) |reply| { + _ = try g.screens.take(g.a.core, g.peer, reply); + }; + } + + /// The replica's client types `k`; the message for the authority. + fn key(g: *Rig, k: Key) ![]u8 { + return testing.allocator.dupe(u8, try g.r.input(.{ .key = k })); + } + + /// The authority takes it, and the replica its unit. + fn take(g: *Rig, msg: []u8) !void { + defer testing.allocator.free(msg); + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.confirm(try g.a.take(msg)); + g.r.settle(); + } + + /// What the replica's screen shows at the shell's cursor row, drawn, + /// and whether the cell at `x` is underlined. + fn drawn(g: *Rig, arena: *std.heap.ArenaAllocator, x: u16) !struct { cp: u8, ul: bool, cursor: ?u16 } { + _ = arena.reset(.retain_capacity); + const c = g.r.core(); + c.needs_frame = true; + const s = try c.render(arena.allocator()); + const pane = c.panes[g.shell].?; + const body = s.region(.body, g.shell).?.rect; + const y = body.y + terminal.gridCursor(pane).y; + const cell = s.at(body.x + x, y); + return .{ .cp = cell.text[0], .ul = cell.style.ul != .off, .cursor = if (s.cursor) |cur| if (cur.y == y) cur.x - body.x else null else null }; + } +}; + +const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + +test "keys typed at a shell's prompt are drawn at once, and the authority's echo takes their place" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const before = try g.drawn(&arena, 2); + try testing.expectEqual(@as(?u16, 2), before.cursor); + // Typed: shown at once, the cursor past it, and nothing underlined at + // a prompt. + const l = try g.key(.{ .cp = 'l', .text = "l" }); + const s = try g.key(.{ .cp = 's', .text = "s" }); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.predicted); + var shown = try g.drawn(&arena, 2); + try testing.expectEqual(@as(u8, 'l'), shown.cp); + try testing.expect(!shown.ul); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + try testing.expectEqual(@as(u8, 's'), (try g.drawn(&arena, 3)).cp); + // The authority's core draws no overlay of its own. + try testing.expect(g.a.core.echo == null); + // The authority takes them, its shell echoes, the delta lands after the + // grace: confirmed, and the screen's own cells and cursor shown. + try g.take(l); + try g.take(s); + try testing.expectEqualStrings("ls", g.written.items); + g.later(grace_ns); + try g.prints("ls"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expect(g.r.echo.cursor == null); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.confirmed); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, 's'), shown.cp); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + // Backspace at the end of the line, shown at once. + const bs = try g.key(.{ .cp = Key.backspace }); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, ' '), shown.cp); + try testing.expectEqual(@as(?u16, 3), shown.cursor); + try g.take(bs); + g.later(grace_ns); + try g.prints("\x08\x1b[K"); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.mispredicted + g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} + +test "a password prompt shows nothing typed, and a full-screen program is not predicted" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt ++ "sudo ls\r\n\x1b]133;C;aid=pardes\x07[sudo] password: "); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const x = terminal.gridCursor(g.r.core().panes[g.shell].?).x; + const k = try g.key(.{ .cp = 'h', .text = "h" }); + // Predicted, and not drawn: nothing has confirmed an echo here. + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k); + // No echo, so no delta: a frame lets it go once it is stale. + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + // The next key still shows nothing. + const k2 = try g.key(.{ .cp = 'i', .text = "i" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k2); + // vim: the alternate screen, never predicted. + try g.prints("\r\n\x1b]133;D;1;aid=pardes\x07" ++ prompt ++ "vim\r\n\x1b]133;C;aid=pardes\x07\x1b[?1049h\x1b[H~"); + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + const before = g.r.echo.stats.predicted; + try g.take(try g.key(.{ .cp = 'i', .text = "i" })); + try testing.expectEqual(before, g.r.echo.stats.predicted); +} + +test "keys still on their way when their predictions go: the next waits for their echo, never predicted where it is about to land" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + // Three keys typed a round trip ahead; the authority has the first, + // and its shell echoes something else: every prediction goes. + const a = try g.key(.{ .cp = 'a', .text = "a" }); + const b = try g.key(.{ .cp = 'b', .text = "b" }); + const c = try g.key(.{ .cp = 'c', .text = "c" }); + try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted); + try g.take(a); + g.later(grace_ns); + try g.prints("A"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + // The next key is not predicted at the screen's cursor, where b's + // echo lands: it waits for b and c. + const d = try g.key(.{ .cp = 'd', .text = "d" }); + try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try g.take(b); + try g.take(c); + g.later(grace_ns); + try g.prints("bc"); + // Their echo shown: d is predicted after it. + try testing.expectEqual(@as(u32, 4), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, 1), g.r.echo.n); + try testing.expectEqual(@as(u16, 5), g.r.echo.cells[0].x); + try g.take(d); + g.later(grace_ns); + try g.prints("d"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expect(!g.r.echo.doubt); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); +} + +test "a misprediction takes every prediction back and shows none until an echo is confirmed; after Tab nothing is predicted until its echo" { + if (comptime !terminal.enabled) return error.SkipZigTest; + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // A line editor that echoes something else (vi's command mode): shown, + // then taken back. + const a = try g.key(.{ .cp = 'a', .text = "a" }); + try testing.expectEqual(@as(u8, 'a'), (try g.drawn(&arena, 2)).cp); + try g.take(a); + g.later(grace_ns); + try g.prints("A"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expect(g.r.echo.doubt); + try testing.expectEqual(@as(u8, 'A'), (try g.drawn(&arena, 2)).cp); + // Doubted: predicted, not drawn, until one is confirmed. + const b = try g.key(.{ .cp = 'b', .text = "b" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, 3)).cp); + try g.take(b); + g.later(grace_ns); + try g.prints("b"); + try testing.expect(!g.r.echo.doubt); + const c = try g.key(.{ .cp = 'c', .text = "c" }); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // Tab: not predicted, and neither is what follows until the authority + // has it and its echo has had its grace. + const tab = try g.key(.{ .cp = Key.tab }); + const d = try g.key(.{ .cp = 'd', .text = "d" }); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.unpredicted); + try g.take(c); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // The authority has Tab (and the key after it): what was predicted + // before it goes, the screen's own shown. + try g.take(tab); + try g.take(d); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + try g.prints("c" ++ "ompleted d"); + // `d`, typed behind Tab, the screen showed by then: not predicted. + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.unpredicted); + const e = try g.key(.{ .cp = 'e', .text = "e" }); + try testing.expectEqual(@as(u8, 'e'), (try g.drawn(&arena, 15)).cp); + try g.take(e); + g.later(grace_ns); + try g.prints("e"); + // A key typed behind Tab that the authority does not have yet is + // predicted once Tab's doing is on the screen, where it now goes. + const tab2 = try g.key(.{ .cp = Key.tab }); + const f = try g.key(.{ .cp = 'f', .text = "f" }); + try g.take(tab2); + try g.prints("ff"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 1), g.r.echo.n); + try testing.expectEqual(@as(u8, 'f'), (try g.drawn(&arena, 18)).cp); + try g.take(f); + g.later(grace_ns); + try g.prints("f"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} diff --git a/src/exec.zig b/src/exec.zig index c14a648c..af312c45 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -409,13 +409,24 @@ 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); - if (p.fs.socket_path.len == 0) + 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; +} + fn spawnTtyWithMount(p: *Pardes, from: usize, v9fs: bool) ?*Pane { // No pane asking (an empty window's tag): the session's directory, as // New's scratch is. @@ -483,10 +494,14 @@ pub fn isFile(path: []const u8) bool { /// What `path` is on this host, null when it is not there; the turn out /// while it asks (it may be inside a mount this editor serves). pub fn kindOf(path: []const u8) ?std.Io.File.Kind { + // A freestanding build has no host to ask. + if (comptime !pardes.hosted) return null; 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; } @@ -1005,7 +1020,7 @@ pub fn applyStartupConfig(p: *Pardes) void { /// setting lines: a command in it (`Newcol`) is the session's start, and /// would not be right twice. pub fn reapplyInitFile(p: *Pardes, path: []const u8, text: []const u8) void { - const init_path = p.opts.startup_config_path orelse return; + const init_path = p.startupPath() orelse return; if (!std.mem.eql(u8, init_path, path)) return; applyInitText(p, text, true); } @@ -1029,7 +1044,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; @@ -1053,6 +1068,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); @@ -1116,7 +1137,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 +1182,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/file_watch.zig b/src/file_watch.zig index ab902e5b..f640fa5f 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -5,9 +5,10 @@ //! is their generation. Large PDFs use metadata which changes for in-place and //! rename-over saves, avoiding a second whole-document allocation. //! -//! Two kernels, one transaction: inotify on linux, kqueue on macos. `init`, -//! `wait`, `stop`, `drain`, `markDir` and `unmarkDir` are the whole of that -//! difference, and nothing above them knows which one it is on. +//! Two kernels, one transaction: inotify on linux (and FreeBSD 15's, the +//! same events), kqueue on macos. `init`, `wait`, `stop`, `drain`, `markDir` +//! and `unmarkDir` are the whole of that difference, and nothing above them +//! knows which one it is on. const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; @@ -81,7 +82,7 @@ pub const Table = [shader_slot + pardes.config.Runtime.Post.max]?Watch; /// empty and `reloadPane` returns false for every slot. The core then keeps its /// own record of what was asked and simply never gets a `file_changed`, which /// is what a host with no watcher has always done here. -pub const supported = builtin.os.tag == .linux or builtin.os.tag == .macos; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or builtin.os.tag == .macos; /// The wake filter's `ident` on macos. `ident` is a directory descriptor for /// every other filter on this queue, and no descriptor is `maxInt(usize)`, so @@ -98,7 +99,7 @@ const wake_ident: usize = std.math.maxInt(usize); /// /// `polled` is the only flag that survives the port. On linux it adds /// IN_NONBLOCK, because a host that finds this descriptor ready in its own -/// `poll(2)` (detached/server.zig) must be able to stop draining it, while a +/// `poll(2)` must be able to stop draining it, while a /// host that parks a thread in `wait` (tty.zig, gui.zig) wants the blocking /// read. On macos neither host needs either flag: a kqueue is not inherited /// across `fork(2)`, so there is no CLOEXEC to ask for, and whether a wait @@ -113,6 +114,8 @@ pub fn init(polled: bool) c_int { linux.IN.CLOEXEC | linux.IN.NONBLOCK else linux.IN.CLOEXEC), + // FreeBSD's IN_CLOEXEC and IN_NONBLOCK are its O_ flags. + .freebsd => return libc.inotify_init1(if (polled) 0x00100000 | 0x4 else 0x00100000), .macos => { const kq = libc.kqueue(); if (kq < 0) return -1; @@ -193,7 +196,7 @@ pub fn stop(fd: c_int) void { pub fn drain(fd: c_int) bool { if (fd < 0) return false; switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { var buf: [4096]u8 = undefined; var seen = false; while (libc.read(fd, &buf, buf.len) > 0) seen = true; @@ -227,7 +230,7 @@ pub fn drain(fd: c_int) bool { /// and inode, because each EVFILT_VNODE filter needs a descriptor of its own. fn markDir(fd: c_int, dir_z: [:0]const u8, watches: *const Table) c_int { switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { // CLOSE_WRITE coalesces one writer's writes; MOVED_TO and CREATE // cover rename-over and delete-then-recreate saves; DELETE and // MOVED_FROM a file going away, said at once (File.deleted), not @@ -366,7 +369,7 @@ fn unmark(fd: c_int, old: Watch, drop_dir: bool) void { } if (!drop_dir) return; switch (builtin.os.tag) { - .linux => _ = libc.inotify_rm_watch(fd, old.wd), + .linux, .freebsd => _ = libc.inotify_rm_watch(fd, old.wd), .macos => _ = libc.close(old.wd), else => {}, } @@ -666,30 +669,27 @@ pub fn identify(io: std.Io, path: []const u8) !Identity { }; } -test "restored file watches preserve snapshots and accept later disk changes" { +test "a restored file is watched again: one clean takes what its file became since the dump, one unsaved keeps its text" { if (comptime !supported or !filesystem.platform_has_fs) return; const io = std.testing.io; const gpa = std.testing.allocator; - for ([_]struct { dirty: bool, exists: bool }{ - .{ .dirty = false, .exists = true }, - .{ .dirty = true, .exists = true }, - .{ .dirty = true, .exists = false }, - }) |case| { + for ([_]bool{ false, true }) |dirty| { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - if (case.exists) try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "disk baseline\n" }); + try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "dumped\n" }); var path_buf: [256]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/watched", .{tmp.sub_path}); - const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + const source = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer source.deinit(); + const id: u8 = @intCast(source.freeSlot().?); + const opened = try pardes.panes.File.open(source, id, path, 0); + if (dirty) _ = pardes.ctlfs.pane.spliceBody(source, opened, 0, 0, "unsaved ").?; + try pardes.dump.dumpState(source); + // Changed on disk between the dump and its load. + try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "changed since\n" }); + const core = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, source.dump_out.?); defer core.deinit(); - while (core.nextEffect()) |_| {} - const id: u8 = @intCast(core.freeSlot().?); - const pane = try pardes.panes.File.restore(core, id, .{ - .kind = .file, - .tag = "", - .body = "", - .file = .{ .path = path, .content = "restored snapshot\n", .dirty = case.dirty }, - }); + const pane = core.panes[id].?; const fd = init(true); if (fd < 0) return error.NoWatcher; defer _ = libc.close(fd); @@ -698,36 +698,16 @@ test "restored file watches preserve snapshots and accept later disk changes" { var armed = false; while (core.nextEffect()) |effect| switch (effect) { .watch => |watch| if (watch.pane == id and watch.on) { - try std.testing.expectEqual(.baseline_disk, watch.mode); - try std.testing.expect(!applyEffect(core, io, fd, &watches, id, watch.on, watch.mode)); + _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode); armed = true; }, else => {}, }; try std.testing.expect(armed and watches[id] != null); - // A clean pane's text is its file's, read at the Restore: a dump - // that kept other text (an older dump) is not what comes back. - const restored: []const u8 = if (case.dirty) "restored snapshot\n" else "disk baseline\n"; - try std.testing.expectEqualStrings(restored, pane.file.?.content); - try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); - try std.testing.expectEqual(case.dirty, pane.file.?.revision != pane.file.?.saved_revision); - _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings(restored, pane.file.?.content); - - try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "later disk save\n" }); - _ = reloadChanged(core, io, gpa, &watches); - // A snapshot restored with unsaved text keeps it: a later disk - // change is said, never loaded over it (File.changed). - if (case.dirty) { - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + if (dirty) { + try std.testing.expectEqualStrings("unsaved dumped\n", pane.file.?.content); try std.testing.expect(pane.file.?.disk_newer != null); - continue; - } - try std.testing.expectEqualStrings("later disk save\n", pane.file.?.content); - pardes.panes.File.undo(core, pane); - try std.testing.expectEqualStrings(restored, pane.file.?.content); - _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings(restored, pane.file.?.content); + } else try std.testing.expectEqualStrings("changed since\n", pane.file.?.content); } } @@ -171,12 +171,8 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .release) return .{ .tag = req.tag }; const io = std.Io.Threaded.global_single_threaded.io(); - const stat = stat: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT); - }; - const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; + const stat = pardes.tape.site(.os_stat, null, osStat, .{ io, path }) catch return Reply.fail(req.tag, E.NOENT); + const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = stat.mtime }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, .open => { @@ -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,80 @@ 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), } } +/// What /os keeps of a stat, as the tape carries it to a replica: the same +/// bytes on every OS, which std.Io.File.Stat is not (a FreeBSD one's +/// `nlink` and `permissions` are another width than a Linux one's). +pub const OsStat = struct { kind: std.Io.File.Kind, size: u64, mtime: u32 }; + +fn osStat(io: std.Io, path: []const u8) !OsStat { + const stat = try statPath(io, path, .{}); + return .{ .kind = stat.kind, .size = stat.size, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; +} + +/// A directory's listing under /os, the entries from `off` until `size` +/// bytes of them are staged, into `listing`: the syscalls of a readdir. +fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 { + 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; +} + +pub const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; + +/// The syscalls of an /os read, write or truncate: the reply, or the bytes +/// read into `buf`. +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 +704,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 {}; } @@ -708,7 +727,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")); } @@ -797,6 +817,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")); } @@ -845,8 +871,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) }; } @@ -903,9 +931,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.readMount, .{ p.gpa, mount.dial, remote_path, path, limit }); } return error.FileNotFound; } @@ -993,7 +1019,7 @@ fn writeOut(p: *pardes.Pardes, dial: ?[]const u8, path: []const u8, bytes: []con defer p.gpa.free(copy); pardes.turn.rest(); defer pardes.turn.wake(); - if (dial) |d| return ninep_io.Client.write(p.gpa, d, path, copy); + if (dial) |d| return ninep_io.Client.writeMount(p.gpa, d, path, copy); return writeFile(path, copy); } @@ -1232,8 +1258,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, @@ -1524,6 +1552,7 @@ pub fn unmount(p: *Pardes, name: []const u8) !void { (path.len == prefix.len or path[prefix.len] == '/')) return error.MountInUse; } const removed = p.fs.mounts.orderedRemove(index); + if (comptime pardes.hosted) ninep_io.Client.forget(removed.dial); p.gpa.free(removed.name); p.gpa.free(removed.dial); return; diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 01272b41..d3acf454 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -22,7 +22,10 @@ const c_heap = @import("c_heap"); const ninep_io = @import("../9p_io.zig"); -const detached_client = @import("../detached/client.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 @@ -670,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 }; @@ -1109,7 +1117,8 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { core.deinit(); core = replacement; shell.core = replacement; - try std.testing.expect(core.panes[0].?.serial != old_serial); + // The dump's pane, its serial too; its old shell stopped all the same. + try std.testing.expectEqual(old_serial, core.panes[0].?.serial); try std.testing.expectEqual(@as(usize, 0), queue.waiting); try std.testing.expect(queue.readers[0] == null); try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); @@ -1434,8 +1443,6 @@ const Gui = struct { tagline_percent: u8, tagline_width: u32, tagline_height: u32, - attached_layers: ?*const [pardes.MAX_PANES]pardes.Layer = null, - attached_tag_layers: ?*const [pardes.MAX_TAG_LAYERS]pardes.Layer = null, cell_w: u32, cell_h: u32, ascent: i32, @@ -1533,6 +1540,14 @@ const Gui = struct { applied_window_opacity: u8 = 100, soft_renderer: ?*c.SDL_Renderer = null, soft_texture: ?*c.SDL_Texture = null, + /// What a window is opened again with (reopenWindow): the flags (not + /// hidden), the present mode, its size when it was closed. + win_flags: c.SDL_WindowFlags = 0, + present_mode: c.SDL_GPUPresentMode = c.SDL_GPU_PRESENTMODE_VSYNC, + window_w: c_int = 1120, + window_h: c_int = 720, + /// The window runNative destroys at the end: the one open then, if any. + window_slot: ?*?*c.SDL_Window = null, soft_tex_w: u32 = 0, soft_tex_h: u32 = 0, @@ -1544,6 +1559,68 @@ const Gui = struct { deck: deck.Deck = .{}, }; +/// The Gui's fields that are each window's own, one a view (views.zig): +/// the window and its presenting, its scroll and glide, its scene and post +/// chain, its pointer, its blink, its images as presented, its capture. The +/// device, the atlas, the font and the pipelines are every window's. The +/// Gui holds the live window's; the others wait in `Shell.windows`. +const window_fields = [_][:0]const u8{ + "window", "soft_renderer", "soft_texture", "soft_tex_w", "soft_tex_h", + "window_w", "window_h", "scroll_pane", "scroll_rect", "scroll_body_y", + "scroll_body_h", "scroll_held_px", "scroll_col", "scroll_row", "scroll_delta", + "scroll_lag", "scroll_edge", "scroll_edge_len", "scroll_notch", "program_wheel", + "glide", "pace_ns", "sel_glow", "hover_glow", "frame_arrival_ns", + "blink_due", "cursor_quad_alpha", "scene_tex", "scene_tex_w", "scene_tex_h", + "scene_failures", "scene_target_failed", "post", "pointer_present", "pointer_shape", + "pointer_mapped", "pointer_cell", "presented_images", "prepared_images", "touch", + "live_ctrl", "live_alt", "applied_window_opacity", "capture_tex", "capture_tex_w", + "capture_tex_h", "capture_xfer", "capture_xfer_size", "captured", "capture_dir", +}; + +const GuiWindow = blk: { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + var types: [window_fields.len]type = undefined; + var attrs: [window_fields.len]std.lang.Type.Struct.FieldAttributes = undefined; + for (window_fields, 0..) |name, i| { + const at = for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else @compileError("gui: Gui has no field " ++ name); + types[i] = st.field_types[at]; + attrs[i] = .{ .default_value_ptr = st.field_attrs[at].default_value_ptr }; + } + break :blk @Struct(.auto, null, &window_fields, &types, &attrs); +}; + +fn copyWindow(dst: anytype, src: anytype) void { + inline for (window_fields) |name| @field(dst, name) = @field(src, name); +} + +/// The live window's share of `g` as a window starts, but its SDL window +/// (reopenWindow makes that). +fn freshWindow(g: *Gui) void { + @setEvalBranchQuota(100_000); + const st = @typeInfo(Gui).@"struct"; + inline for (window_fields) |name| { + const at = comptime for (st.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else unreachable; + if (comptime st.field_attrs[at].defaultValue(st.field_types[at])) |d| @field(g, name) = d; + } +} + +/// What a window held of its own let go (closeWindow lets the window go). +fn releaseWindowParts(g: *Gui, gpa: std.mem.Allocator) void { + if (g.scene_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); + if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); + g.post.deinit(gpa, g.device); + g.glide.deinit(gpa); + gpa.free(g.scroll_edge); + g.presented_images.deinit(gpa); + g.prepared_images.deinit(gpa); +} + fn setGuiFontName(g: *Gui, fallback: []const u8) void { const name = if (c.ui_font_name(g.font)) |name_z| std.mem.span(name_z) else fallback; const len = @min(name.len, g.font_name.len); @@ -1893,18 +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=<socket>:<session>` (and `<host>:<session>`, +/// 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, attach: ?[]const u8) !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) { - if (attach != null) { - log.err("--attach needs a window; PARDES_TEST_GRID is headless", .{}); - return error.AttachNeedsWindow; - } - 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"); @@ -1950,6 +2032,9 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u // opacity would dim the text as well. SDL requires this flag at creation. const surface_opacity = native_wayland; if (surface_opacity) win_flags |= c.SDL_WINDOW_TRANSPARENT; + const reopen_flags = win_flags; + // `--detach`: a window never shown, closed once the session is up. + if (headless) win_flags |= c.SDL_WINDOW_HIDDEN; // `--geometry=WxH[+X+Y]`, as X11 programs take it. var want_w: c_int = 1120; var want_h: c_int = 720; @@ -1969,7 +2054,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u log.err("SDL_CreateWindow: {s}", .{c.SDL_GetError()}); return error.SdlInit; }; - defer c.SDL_DestroyWindow(window); + var window_slot: ?*c.SDL_Window = window; + defer if (window_slot) |open| c.SDL_DestroyWindow(open); if (want_at) |at| _ = c.SDL_SetWindowPosition(window, at[0], at[1]); updateDisplayScale(window); if (native_wayland) log.info("Wayland opacity: background-only transparent surface", .{}); @@ -2173,6 +2259,11 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u .demo_next = if (env.get("PARDES_FRAMES_DEMO")) |name| (if (pardes.FrameStats.named(name) != null) @as(?usize, 0) else null) else null, .surface_opacity = surface_opacity, .soft_renderer = soft_renderer, + .win_flags = reopen_flags, + .present_mode = present_mode, + .window_w = want_w, + .window_h = want_h, + .window_slot = &window_slot, }; // The pipelines, textures and buffers the Gui holds, whatever they were // regrown to; the image ones are released below. @@ -2221,10 +2312,54 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, attach: ?[]const u // 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 (attach) |requested| return attachRequested(gpa, &g, requested); - var attached: ?detached_client.Client = null; - try localSession(init, &g, opts_in, test_mode, &attached); - if (attached) |*client| return attachedLoop(gpa, &g, client); + if (dial) |to| return remoteSession(init, &g, opts_in, test_mode, to); + try localSession(init, &g, opts_in, test_mode, headless); +} + +/// Detach: the window closed (and with it its swapchain or renderer); the +/// device, the atlas and the pipelines stay for the next one. +fn closeWindow(g: *Gui) void { + _ = c.SDL_GetWindowSize(g.window, &g.window_w, &g.window_h); + _ = c.SDL_StopTextInput(g.window); + if (g.soft_renderer) |renderer| { + if (g.soft_texture) |texture| c.SDL_DestroyTexture(texture); + g.soft_texture = null; + g.soft_tex_w = 0; + g.soft_tex_h = 0; + c.SDL_DestroyRenderer(renderer); + g.soft_renderer = null; + } else c.SDL_ReleaseWindowFromGPUDevice(g.device, g.window); + c.SDL_DestroyWindow(g.window); + if (g.window_slot) |slot| if (slot.* == g.window) { + slot.* = null; + }; +} + +/// `pardes-gui --attach`: a window again, as the first was made, at the +/// size the last one had. False when the display will not give one. +fn reopenWindow(g: *Gui) bool { + const window = c.SDL_CreateWindow("pardes", g.window_w, g.window_h, g.win_flags) orelse return false; + if (g.soft_present) { + g.soft_renderer = c.SDL_CreateRenderer(window, null) orelse { + c.SDL_DestroyWindow(window); + return false; + }; + } else { + if (!c.SDL_ClaimWindowForGPUDevice(g.device, window)) { + c.SDL_DestroyWindow(window); + return false; + } + _ = c.SDL_SetGPUSwapchainParameters(g.device, window, c.SDL_GPU_SWAPCHAINCOMPOSITION_SDR, g.present_mode); + } + g.window = window; + if (g.window_slot) |slot| slot.* = window; + updateDisplayScale(window); + // ponytail: another view's window asks the hit test of whichever + // window's share the Gui holds then (the workspace tag sits in the same + // place in every view); a test per window if one moves it. + if (!g.capture) _ = c.SDL_SetWindowHitTest(window, hitTest, g); + _ = c.SDL_StartTextInput(window); + return true; } fn localSession( @@ -2232,7 +2367,7 @@ fn localSession( g: *Gui, opts_in: pardes.Options, test_mode: bool, - attached: *?detached_client.Client, + headless: bool, ) !void { const io = init.io; const gpa = init.gpa; @@ -2261,7 +2396,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); @@ -2340,8 +2477,18 @@ 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 + // 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]); }; @@ -2352,6 +2499,16 @@ 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-<k>; view 0 live after. + 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(); + }; + defer shell.closeOtherWindows(); if (test_mode) setStdinRaw() catch {}; shell.threads_ok = true; @@ -2359,26 +2516,28 @@ fn localSession( defer pardes.lsp.setStatusSink(null, null); host_io.setExitWake(&queue, Queue.wakeForExit); defer host_io.setExitWake(null, null); + if (headless) { + shell.closeTheWindow(); + // Nothing else may wake the first wait. + 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(&shell, deliverNote); if (core.quit) break; // a session that ended does not restore into one - if (core.takeAttach()) |req| { - const geom = windowCells(g); - const outcome = detached_client.attempt(gpa, req.name, geom.cols, geom.rows); - switch (outcome) { - .greeted => |client| { - attached.* = client; - break; - }, - else => { - var mbuf: [256]u8 = undefined; - core.setMessage(req.pane, attachFailure(&mbuf, outcome, req.name)); - }, - } - } 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; @@ -2390,6 +2549,11 @@ fn localSession( }; pipe_tasks.cancelAll(io); queue.discardCompletions(); + // ponytail: a restored session is one view, the live window's: + // the others close. + shell.closeOtherWindows(); + shell.gview = 0; + shell.windowed = 1; shell.stopPtys(); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( inotify_fd, @@ -2426,198 +2590,327 @@ fn localSession( } } -fn attachFailure(buf: []u8, outcome: detached_client.Attempt, requested: []const u8) []const u8 { - return switch (outcome) { - .greeted => unreachable, - .no_session => if (requested.len != 0) - std.fmt.bufPrint(buf, "Attach: no detached session called '{s}'", .{requested}) catch - "Attach: no detached session under that name" - else - "Attach: nothing is detached (start one with `pardes --detach`)", - .ambiguous => |n| std.fmt.bufPrint(buf, "Attach: {d} detached sessions; say which one", .{n}) catch - "Attach: several detached sessions; say which one", - .refused => |why| switch (why) { - .version => "Attach: that session speaks a different wire version — it is another build of pardes", - .full => "Attach: that session already has every frontend slot taken", - .quitting => "Attach: that session is ending", - }, - .silent => "Attach: that session took the connection and never greeted us", - .lost => |err| std.fmt.bufPrint(buf, "Attach: lost the connection ({t})", .{err}) catch - "Attach: lost the connection", - }; -} +/// 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 attachRequested(gpa: std.mem.Allocator, g: *Gui, requested: []const u8) !void { - const geom = windowCells(g); - const outcome = detached_client.attempt(gpa, requested, geom.cols, geom.rows); - switch (outcome) { - .greeted => |greeted| { - var client = greeted; - return attachedLoop(gpa, g, &client); - }, - else => { - var mbuf: [256]u8 = undefined; - log.err("{s}", .{attachFailure(&mbuf, outcome, requested)}); - return switch (outcome) { - .no_session => error.NoSession, - .ambiguous => error.AmbiguousSession, - .refused => error.Refused, - .silent => error.NoGreeting, - .lost => |err| err, - .greeted => unreachable, - }; - }, + fn deinit(cl: *Client) void { + cl.pending.deinit(cl.gpa); + cl.texts.deinit(); + cl.inbox.deinit(cl.gpa); + cl.taken.deinit(cl.gpa); } -} -fn attachedResize(client: *detached_client.Client, g: *const Gui, cols: u16, rows: u16) !void { - try client.send(.{ .event = .{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = .{ - .body_w = @intCast(g.cell_w), - .body_h = @intCast(g.cell_h), - .tagline_w = @intCast(g.tagline_width), - .tagline_h = @intCast(g.tagline_height), - } } } }); -} - -fn attachedLoop(gpa: std.mem.Allocator, g: *Gui, client: *detached_client.Client) !void { - defer client.detach(); - _ = c.SDL_StartTextInput(g.window); + 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 {}; + } - var in: Input = .{ .client = client }; - g.attached_layers = &client.body_layers; - g.attached_tag_layers = &client.tag_layers; - var dirty = false; - var geom = windowCells(g); - try attachedResize(client, g, geom.cols, geom.rows); - while (true) { - const link = client.wait(detached_client.poll_ms); - while (true) { - const msg = (try client.next()) orelse break; - switch (msg) { - .welcome => {}, - .refuse => |why| { - log.err("session refused an already-greeted frontend: {t}", .{why}); - return error.Refused; - }, - .frame => dirty = true, - .quit => return, - .detach => return, - .set_clipboard => |text| putClipboard(gpa, text), - .read_clipboard => if (takeClipboard()) |text| { - defer c.SDL_free(text.ptr); - in.post(.{ .paste = text }); - }, - .open_link => |url| look.openLink(url), - .post => dirty = true, - } + /// What the window's input made, applied at once and sent. + fn feed(cl: *Client) void { + for (cl.pending.items) |ev| { + const r = cl.link.r; + const predicted = r.echo.stats.predicted; + cl.link.input(ev); + // PARDES_TEST_LATENCY: the input, and whether it shows at once + // (applied, or its echo predicted), for the frame that shows it. + latencyNote("I {d} {d} {d}\n", .{ r.next_seq - 1, monoNs(), @intFromBool(r.applied == r.pending.items.len or r.echo.stats.predicted > predicted) }); } - try link; + cl.pending.clearRetainingCapacity(); + _ = cl.texts.reset(.retain_capacity); + } - var sev = std.mem.zeroes(c.SDL_Event); - while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &sev); - pollGamepad(g, &in); - if (in.lost) |err| return err; - if (in.quit) return; + /// 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; + } - const now = windowCells(g); - if (now.cols != geom.cols or now.rows != geom.rows) { - geom = now; - try attachedResize(client, g, now.cols, now.rows); + /// 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); + // And after a unit, the first of its own inputs not confirmed. + if (msg == .unit) { + const r = cl.link.r; + latencyNote("U {d} {d}\n", .{ monoNs(), if (r.pending.items.len > 0) r.pending.items[0].seq else r.next_seq }); + } } - // The session's chain, its files compiled there. - g.post.sync(gpa, g.device, g.swapchain_format, client.post.list(), client.post_key, null); - if (!dirty) { - // As a local window does: a chain that moves on its own redraws - // alone, once a refresh. - if (g.post.animating(client.post.animation) and g.scene_tex != null and - c.SDL_GetTicksNS() -| g.post.last_ns + std.time.ns_per_ms >= refreshNs(g)) redrawPost(g, gpa); - continue; + cl.taken.clearRetainingCapacity(); + if (ended) { + cl.link.quit = true; + cl.link.gone = true; } - dirty = false; - paintAttached(g, gpa, client); } -} -fn paintAttached(g: *Gui, gpa: std.mem.Allocator, client: *detached_client.Client) void { - var surface = attachedSurface(client); - _ = renderFrame( - g, - gpa, - null, - &surface, - false, - ) catch |err| blk: { - log.err("render: {t}", .{err}); - break :blk false; - }; + /// 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; + // Its render's end is marked as it presents (`present`). + core.frames.mark_render = core.frames.mark_update; + r.draw(.{ .ctx = s, .f = present }); + cl.link.owed = core.present_skipped; + core.frames.record(pardes.FrameStats.nowNs(), false, cl.link.owed); + core.present_skipped = skipped; + 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); } -/// The session's frame as a surface: its cells, layers, regions and palette, -/// so its chrome is drawn from them as a local one's is. -fn attachedSurface(client: *const detached_client.Client) pardes.Surface { - var surface: pardes.Surface = .{ - .cols = client.cols, - .rows = client.rows, - .cells = client.grid.items, - .cursor = if (client.cursor) |cu| .{ .x = cu.x, .y = cu.y, .bar = cu.bar } else null, - .pointer_shape = client.pointer_shape, - .body_layers = client.body_layers, - .tag_layers = client.tag_layers, - .chrome = client.chrome, +/// 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=<where>:<session>`: 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); + } }; - const placed = client.regions.items[0..@min(client.regions.items.len, surface.regions.len)]; - @memcpy(surface.regions[0..placed.len], placed); - surface.nregions = @intCast(placed.len); - return surface; -} + 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, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); -test "an attached frontend draws the session's chrome from its frame alone" { - const gpa = std.testing.allocator; - const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); - defer core.deinit(); - // A gui-shaped core, whose tags and notices are layers. - core.update(.{ .resize = .{ .cols = 80, .rows = 24, .row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 } } }); - _ = try core.newShell(1, ""); - try std.testing.expect(pardes.layout.splitColumn(core, 0, 1, false)); - core.settings.focus_tint = true; - try std.testing.expect(core.executeBuiltinLine(0, "Msg a notice with a rule under it")); - core.sync(); - // Past its slide into place, where its rule shows. - for (0..30) |_| core.update(.tick); - const s = try core.render(core.scratch.allocator()); + 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)); - const wire = @import("../detached/wire.zig"); - var client: detached_client.Client = .{ .gpa = gpa }; + var client: Client = .{ .link = .{ .r = r, .fd = joined.fd }, .io = io, .gpa = gpa, .texts = .init(gpa) }; defer client.deinit(); - var welcome: [64]u8 = undefined; - try client.in.appendSlice(gpa, try wire.encodeServer(&welcome, .{ .welcome = .{ .slot = 0, .cols = s.cols, .rows = s.rows } })); - const out = try gpa.alloc(u8, wire.frameBound(s.cols, s.rows) + wire.layersBound(&s.body_layers, &s.tag_layers, s.regionList())); - defer gpa.free(out); - try client.in.appendSlice(gpa, try wire.encodeFrameLayers(out, s.cols, s.rows, null, s.pointer_shape, s.cells, &.{}, &s.body_layers, &s.tag_layers, s.regionList(), s.chrome)); - _ = try client.next(); - _ = try client.next(); - const attached = attachedSurface(&client); - - var g: Gui = undefined; - g.cell_w = 10; - g.cell_h = 20; - g.tagline_width = 6; - g.tagline_height = 12; - g.decor = .empty; - g.glide = .{}; - g.scroll_pane = null; - g.scroll_lag = 0; - defer g.decor.deinit(gpa); - var groups = makeGroups(&.{}, false); - try buildDecor(&g, gpa, s, &groups, fixedCellLayout(&g), 800, 480, true); - const local = try gpa.dupe(CellInstance, g.decor.items); - defer gpa.free(local); - try std.testing.expect(local.len > 8); - try std.testing.expect(groups.items[groups.notices].decor_count != 0); - try buildDecor(&g, gpa, &attached, &groups, fixedCellLayout(&g), 800, 480, true); - try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(local), std.mem.sliceAsBytes(g.decor.items)); + 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, r.diverged != null); + // 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, r.diverged == null, joined.view) orelse Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); + if (have) |h| h.left(); + r.deinit(); + r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + 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; @@ -2691,6 +2984,7 @@ fn runGrid(init: std.process.Init, opts_in: pardes.Options) !void { .inotify_fd = -1, .watches = &watches, .fs = fs, + .grid = true, }; defer shell.shutdownPtys(); const host = shell.host(); @@ -2785,6 +3079,24 @@ const StdinFeed = struct { // A mouse event at window pixels, built as SDL would and run // through dispatch, so a driver exercises the real pointer path // (pixel to cell, tag and body hits). SGR mouse carries only cells. + // ESC]777;view;<k>BEL: what follows is window k's (k from 1), + // as an event of that window's makes its view live (route). + const view_prefix = "\x1b]777;view;"; + if (std.mem.startsWith(u8, f.buf[seq_start..len], view_prefix)) { + const body = f.buf[seq_start + view_prefix.len .. len]; + const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { + std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); + f.fill = len - seq_start; + break; + }; + const k = std.fmt.parseInt(u8, body[0..end], 10) catch 0; + if (in.shell) |sh| if (k >= 1 and pardes.views.isOpen(sh.core, k - 1)) { + sh.core.toView(k - 1); + sh.follow(); + }; + seq_start += view_prefix.len + end + 1; + continue; + } const mouse_prefix = "\x1b]777;mouse;"; if (std.mem.startsWith(u8, f.buf[seq_start..len], mouse_prefix)) { const body = f.buf[seq_start + mouse_prefix.len .. len]; @@ -3125,21 +3437,36 @@ test "the window's function and editing keys reach a terminal as its cursor keys const Input = struct { core: ?*pardes.Pardes = null, - client: ?*detached_client.Client = null, + /// 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, - lost: ?anyerror = null, fn post(in: *Input, ev: pardes.Event) void { - if (in.core) |core| return core.update(ev); - const client = in.client orelse return; - if (in.lost != null) return; - client.send(.{ .event = ev }) catch |err| switch (err) { - error.Overlong, error.WriteFailed => {}, - else => in.lost = err, + 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.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; } @@ -3169,15 +3496,24 @@ 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), else => {}, } switch (sev.type) { - c.SDL_EVENT_QUIT, c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), - c.SDL_EVENT_WINDOW_FOCUS_GAINED => in.post(.{ .focus = true }), + c.SDL_EVENT_QUIT => { + in.shell = null; + in.close(); + }, + c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), + c.SDL_EVENT_WINDOW_FOCUS_GAINED => { + in.post(.{ .focus = true }); + 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 => { g.pointer_present = false; @@ -3196,8 +3532,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; } @@ -3526,18 +3868,6 @@ fn gridCellAtPixels(g: *const Gui, core: ?*const pardes.Pardes, x: f32, y: f32) break; } }; - if (core == null) if (g.attached_layers) |layers| for (layers) |*layer| { - if (layer.bodyHit(x, y, @floatFromInt(g.cell_w), @floatFromInt(g.cell_h), @floatFromInt(g.tagline_width), @floatFromInt(g.tagline_height))) |body_hit| { - point.body_hit = body_hit; - break; - } - }; - if (core == null) if (g.attached_tag_layers) |layers| for (layers) |*layer| { - if (layer.tagHit(x, y, @floatFromInt(g.cell_w), @floatFromInt(g.cell_h), @floatFromInt(g.tagline_width), @floatFromInt(g.tagline_height))) |hit| { - point.tag_hit = hit; - break; - } - }; return point; } @@ -3637,6 +3967,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; @@ -3727,10 +4069,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; }; @@ -3740,7 +4083,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, @@ -3805,7 +4148,17 @@ fn pollGamepad(g: *Gui, in: *Input) void { const Shell = struct { core: *pardes.Pardes, + /// The window's, while one is open; null while detached (`parked` has + /// it) and in the headless test grid. gui: ?*Gui = null, + parked: ?*Gui = null, + /// The headless test grid (runGrid): its own loop, no window ever. + grid: bool = false, + /// Shells a dead session left (Adopt), each waiting for the spawn of + /// the terminal pane made for it. + adoptions: [pardes.MAX_PANES]Adoption = undefined, + adoptions_len: usize = 0, + adopted: usize = 0, io: std.Io, gpa: std.mem.Allocator, fs: ?*ninep_io.Listener = null, @@ -3837,6 +4190,28 @@ const Shell = struct { /// The tick a paced frame is sampled at (waitInput): the clock the /// core is advanced to while anything moves. paced_ns: ?u64 = null, + /// Each other view's window, while another's is in the Gui (views.zig), + /// and the view whose window the Gui holds (`follow`). + windows: [pardes.views.max]GuiWindow = undefined, + gview: u8 = 0, + /// 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(); @@ -3863,6 +4238,7 @@ const Shell = struct { } _ = libc.close(pt.fd); pt.fd = -1; + daemon.closed(pt.pid); const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); host_io.reapRetired(&pt.pid, &pt.kill_at, now); if (pt.pid == 0) { @@ -3917,7 +4293,302 @@ const Shell = struct { } fn host(s: *Shell) pardes.Host { - return .{ .ctx = s, .vtable = if (s.gui == null) &grid_vtable else &vtable }; + return .{ .ctx = s, .vtable = if (s.grid) &grid_vtable else &vtable }; + } + + /// Detach: the window closed, the session left running with no + /// 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 (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(); + } + + /// The Gui holds the window of the core's live view. + fn follow(s: *Shell) void { + const g = s.gui orelse return; + const k = s.core.view; + if (s.gview == k) return; + // 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; + } + + /// An event of another view's window: that view goes live. + fn route(s: *Shell, sev: *const c.SDL_Event) void { + if (!pardes.views.several(s.core)) return; + const g = s.gui orelse return; + const w = c.SDL_GetWindowFromEvent(sev) orelse return; + // 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(); + }; + } + + /// `pardes-gui --attach` while a window is open: another window, a view + /// 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 = 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); + if (!reopenWindow(g)) { + log.err("--attach: no window: {s}", .{c.SDL_GetError()}); + copyWindow(g, &s.windows[live]); + return s.core.closeView(k); + } + if (s.test_mode) { + // Its own grid, its captures under view-<k> (PARDES_TEST_VIEWS). + // ponytail: the path is kept for the session, a few bytes. + const cols: u32 = if (std.c.getenv("PARDES_TEST_COLS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 80 else 80; + const rows: u32 = if (std.c.getenv("PARDES_TEST_ROWS")) |v| std.fmt.parseInt(u16, std.mem.span(v), 10) catch 24 else 24; + _ = c.SDL_SetWindowSize(g.window, @intCast(cols * g.cell_w + g.capture_pad), @intCast(rows * g.cell_h + g.capture_pad)); + _ = c.SDL_SyncWindow(g.window); + g.capture = true; + g.capture_dir = std.fmt.allocPrint(s.gpa, "{s}/view-{d}", .{ s.windows[live].capture_dir, k }) catch ""; + if (g.capture_dir.len > 0) std.Io.Dir.cwd().createDirPath(s.io, g.capture_dir) catch {}; + } + s.gview = k; + s.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; + s.core.toView(k); + s.follow(); + 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; + for (0..s.windows.len) |i| { + const k: u8 = @intCast(i); + if (k == s.gview or !pardes.views.isOpen(s.core, k)) continue; + var live: GuiWindow = undefined; + copyWindow(&live, g); + copyWindow(g, &s.windows[k]); + closeWindow(g); + releaseWindowParts(g, s.gpa); + copyWindow(g, &live); + } + } + + fn closeTheWindow(s: *Shell) void { + const g = s.gui orelse return; + closeWindow(g); + s.parked = g; + s.gui = null; + s.presented = false; + s.surface = null; + // 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); + } + + fn openTheWindow(s: *Shell) void { + const g = s.parked orelse return; + if (!reopenWindow(g)) { + log.err("--attach: no window: {s}", .{c.SDL_GetError()}); + return daemon.setState(true); + } + s.gui = g; + s.parked = null; + s.peers.alone_unattended = false; + s.core.unattended = false; + s.core.needs_frame = true; + resetScroll(g); + daemon.setState(false); + } + + fn watched(ctx: ?*anyopaque) bool { + return shellOf(ctx).gui != null; + } + + fn adopt(ctx: ?*anyopaque, pane: u8) void { + const s = shellOf(ctx); + if (!daemon.kept()) return s.core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session"); + s.adopted = 0; + 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(); + // 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; + 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(); + 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; a replica + /// client's, kept for its rejoin (views.away), none. + fn resumeWindows(s: *Shell) void { + const live = s.core.view; + s.gview = live; + for (0..pardes.views.max) |i| { + const k: u8 = @intCast(i); + if (k != live and pardes.views.isOpen(s.core, k) and !pardes.views.isAway(s.core, k)) s.openAnotherWindow(k); + s.windowed |= @as(u8, 1) << @intCast(live); + } + 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). + fn adoptShell(s: *Shell, pid: libc.pid_t, fd: c_int) void { + const core = s.core; + if (pid == 0) { + var buf: [96]u8 = undefined; + return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); + } + if (s.adoptions_len < s.adoptions.len) { + s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; + s.adoptions_len += 1; + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; + s.adopted += 1; + return; + } + s.adoptions_len -= 1; + } + _ = libc.close(fd); + var buf: [96]u8 = undefined; + core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell"); } const vtable: pardes.Host.VTable = .{ @@ -3926,6 +4597,10 @@ const Shell = struct { .present = present, .post_present = postPresent, .poll_frame = pollFrame, + .watched = watched, + .detach = detachWindow, + .adopt = adopt, + .upgrade = upgradeAsked, .spawn = spawnPane, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -3957,6 +4632,10 @@ const Shell = struct { v.post_present = gridPostPresent; v.set_clipboard = null; v.read_clipboard = null; + v.watched = null; + v.detach = null; + v.adopt = null; + v.upgrade = null; break :vt v; }; @@ -3976,6 +4655,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| { @@ -3988,10 +4668,7 @@ const Shell = struct { if (s.gens[e.pane] == e.gen) eof: { if (e.failure) |err| { s.closePty(e.pane); - if (s.core.panes[e.pane]) |pane| { - pane.body.mode = .normal; - s.core.reportError(e.pane, "terminal reader", err); - } + s.core.readerFailed(e.pane, err); break :eof; } const pt = if (s.ptys[e.pane]) |*pt| pt else { @@ -4051,6 +4728,14 @@ const Shell = struct { }, .files_changed => check_files = true, .fs_ready => {}, + .window => { + if (s.gui != null) s.openAnotherWindow(null) else s.openTheWindow(); + s.saw_event = true; + }, + .adopted => |a| { + s.adoptShell(a.pid, a.fd); + s.saw_event = true; + }, }; if (check_files) s.shaders.recheck(); if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches)) @@ -4077,7 +4762,11 @@ fn shellOf(ctx: ?*anyopaque) *Shell { fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = shellOf(ctx); const core = s.core; - var in: Input = .{ .core = core }; + var in: Input = .{ .core = core, .shell = s }; + s.follow(); + 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 @@ -4139,9 +4828,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 @@ -4169,16 +4862,28 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // each would repeat hover and selection work the next undoes. var motion: ?c.SDL_Event = null; while (true) { - if (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + if (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 { - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } motion = null; - dispatch(g, &in, &sev); + if (sev.type == c.SDL_EVENT_MOUSE_MOTION) { + motion = sev; + } else { + s.route(&sev); + dispatch(g, &in, &sev); + } } if (!c.SDL_PollEvent(&sev)) break; } - if (motion) |*m| dispatch(g, &in, m); + if (motion) |*m| { + s.route(m); + dispatch(g, &in, m); + } } runDemo(g, &in, core); // Paced: what came before the tick is in; wait out the rest of it, @@ -4196,14 +4901,21 @@ 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) { - dispatch(g, &in, &sev); - while (c.SDL_PollEvent(&sev)) dispatch(g, &in, &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. const until = c.SDL_GetTicksNS() + 100 * std.time.ns_per_us; @@ -4214,7 +4926,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { } s.paced_ns = if (paced) tick else null; if (paced) g.pace_ns = tick; - } + } else waitDetached(core, timeout_ms); if (s.test_mode) { const r = s.feed.pump(s.gpa, &in, s.gui) catch { core.quit = true; @@ -4229,8 +4941,28 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { if (s.gui) |g| pollGamepad(g, &in); } +/// Detached: no window, asleep until the queue's wake (an SDL user event: +/// a pty's output, the daemon's window request), the core's next wake, or a +/// quit (SDL makes SIGTERM and SIGINT one). With nobody to dismiss them, +/// messages go on the clock (the pump's, after this wait), which wakes it. +fn waitDetached(core: *pardes.Pardes, timeout_ms: u32) void { + var ms: c_int = if (timeout_ms != 0) @intCast(@min(timeout_ms, std.math.maxInt(c_int))) else -1; + for (core.panes) |slot| if (slot) |pane| if (pane.msg_len > 0) { + ms = if (ms < 0) 100 else @min(ms, 100); + break; + }; + var sev = std.mem.zeroes(c.SDL_Event); + pardes.turn.rest(); + var got = c.SDL_WaitEventTimeout(&sev, ms); + pardes.turn.wake(); + while (got) : (got = c.SDL_PollEvent(&sev)) { + if (sev.type == c.SDL_EVENT_QUIT) core.quit = true; + } +} + fn pollFrame(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); s.reconcilePtys(); const core = s.core; if (s.fs) |f| if (f.tick().pending) s.queue.push(.fs_ready); @@ -4241,6 +4973,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); @@ -4411,16 +5153,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 { @@ -4463,6 +5197,9 @@ test "background opacity acknowledges success and rolls back unsupported surface fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const s = shellOf(ctx); const core = s.core; + // A replica client draws outside the pump, which marks it there. + if (s.remote != null) core.frames.mark_render = pardes.FrameStats.nowNs(); + s.follow(); const g = s.gui orelse return; frame_started_ns = monoNs(); const frame = @constCast(surface); @@ -4488,7 +5225,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; @@ -4496,17 +5235,26 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { fn postPresent(ctx: ?*anyopaque) void { const s = shellOf(ctx); + s.follow(); const g = s.gui orelse return; // No image (a minimized window) shows no tracks; time still passes and // the pump advances it all the same. 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(); } @@ -4545,6 +5293,11 @@ fn gridPostPresent(ctx: ?*anyopaque) void { fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { const s = shellOf(ctx); s.reap(); + for (s.adoptions[0..s.adoptions_len], 0..) |a, i| if (a.pane == pane) { + std.mem.copyForwards(Adoption, s.adoptions[i .. s.adoptions_len - 1], s.adoptions[i + 1 .. s.adoptions_len]); + s.adoptions_len -= 1; + return takeShell(s, pane, a); + }; // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = s.ptys[pane] != null; @@ -4571,6 +5324,49 @@ fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { }; } +const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 }; + +/// An adopted shell, running already, as pane `pane`'s: told the size twice +/// (one it never had, then the pane's), which makes it redraw. +fn takeShell(s: *Shell, pane: u8, a: Adoption) void { + s.closePty(pane); + s.gens[pane] +%= 1; + s.ptys[pane] = .{ .fd = a.fd, .pid = a.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{} }; + var exe_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + s.core.acknowledgeShell(pane, host_io.processExe(a.pid, &exe_buf) orelse "sh", host_io.marksPrompts(a.pid)); + var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(pane, wd); + const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; + _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); + if (s.core.panes[pane]) |pn| ptyResize(s, pane, @max(pn.cols, 1), @max(pn.rows, 1)); + if (!s.threads_ok) return; + s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = a.fd, .flags = .{ .nonblocking = false } }, pane, s.gens[pane]) catch |err| { + s.closePty(pane); + return s.core.reportError(pane, "terminal reader", err); + }; +} + +/// The daemon's thread: what it says, onto the queue (drainQueue). +fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { + 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 + // before it is sent, so only an old one's would be here. + if (a.tty >= 0) _ = libc.close(a.tty); + if (a.stub >= 0) daemon.refuseStub(a.stub, "a window session takes no terminal"); + if (a.tty < 0) q.push(.window); + }, + .adopted => |a| { + 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 } }); + }, + // Joined at the next step boundary (Peers, any thread). + .replica => |j| s.peers.handed(j.fd, j.view), + } +} + fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { const s = shellOf(ctx); if (s.ptys[pane]) |pt| if (pt.fd >= 0) { @@ -4704,6 +5500,7 @@ test "the headless grid host round-trips a yank back as a paste" { .pipe_tasks = &pipe_tasks, .inotify_fd = -1, .watches = &watches, + .grid = true, }; core.host = shell.host(); try std.testing.expect(core.host.vtable == &Shell.grid_vtable); @@ -7654,8 +8451,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; @@ -7663,8 +8466,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 @@ -9400,7 +10202,9 @@ fn rulePx(chrome: *const pardes.Chrome) f32 { /// by which every size given in logical pixels is drawn, as acme scales its /// Border, ButtonBorder and Scrollgap (scalesize). Cells, glyphs and what is /// measured in them are already the display's pixels and are not scaled. -/// ponytail: one window per process, so one global; per window if ever two. +/// ponytail: one per process: a session's windows (views.zig) on screens +/// of two densities draw at the density of the last one opened or moved; +/// a field of `window_fields` when that matters. var display_scale: f32 = 1; /// The window's display scale, or PARDES_TEST_SCALE (a harness forcing 2x); diff --git a/src/host_io.zig b/src/host_io.zig index af284e84..b1ed8b4b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -24,7 +24,17 @@ pub const Host = struct { /// client attached draws none (a /screen read draws its own), and /// the frame owed is drawn when one attaches. Absent: always. watched: ?*const fn (ctx: ?*anyopaque) bool = null, + /// Detach: the terminal or window let go, the session left running + /// for the daemon to attach again (daemon.zig). 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, + /// Whether a newer pardes is installed at the path this process + /// started from, to be said once (upgrade.zig): asked at a focus-in. + newer_build: ?*const fn (ctx: ?*anyopaque) bool = null, spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null, pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null, @@ -66,7 +76,7 @@ pub fn testClock() ?u64 { } pub const Fallback = struct { - pub const dump_path = "pardes.dump.zon"; + pub const dump_path = "pardes.dump"; gpa: std.mem.Allocator, files: std.StringHashMapUnmanaged([]u8) = .empty, @@ -143,6 +153,11 @@ pub const Inbox = struct { handed: struct { seq: u32, ok: bool, words: []u8 }, files_changed, fs_ready, + /// The daemon asks for the window again (`pardes-gui --attach`). + window, + /// A shell a dead session left, its pty's master (Adopt); pid 0 is + /// the last of them told. + adopted: struct { pid: libc.pid_t, fd: c_int }, pub fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void { switch (m) { @@ -155,7 +170,10 @@ pub const Inbox = struct { }, .search => |b| b.deinit(gpa), .handed => |h| gpa.free(h.words), - .eof, .files_changed, .fs_ready => {}, + .adopted => |a| if (a.fd >= 0) { + _ = libc.close(a.fd); + }, + .eof, .files_changed, .fs_ready, .window => {}, } } }; @@ -962,8 +980,10 @@ pub const Shell = struct { while (std.c.readdir(dir)) |ent| { const name = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&ent.name)), 0); const pid = rcPid(name) orelse continue; - if (pid == std.c.getpid()) continue; - if (std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue; + // This pid's are this process's, but in an upgraded one: those + // were its old image's (upgrade.zig), and its own come after. + if (pid == std.c.getpid() and @import("upgrade.zig").resumed == null) continue; + if (pid != std.c.getpid() and std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue; if (name.len >= 64 or n == dead.len) continue; @memcpy(dead[n][0..name.len], name); dead_len[n] = name.len; @@ -2097,6 +2117,9 @@ pub fn forkShell( }; } ninep_io.setCloexec(master); + // The daemon keeps a dup: this session killed outright never hangs the + // shell up (daemon.zig). + @import("daemon.zig").pty(pid, master); if (core) |c| c.acknowledgeShell(pane, std.mem.span(spawn.path), one_line == null and spawn.argv[1] != null); return .{ .file = .{ .handle = master, .flags = .{ .nonblocking = false } }, .pid = pid }; } @@ -2208,6 +2231,30 @@ const proc_vnodepathinfo = extern struct { const PROC_PIDVNODEPATHINFO: c_int = 9; extern "c" fn proc_pidinfo(pid: c_int, flavor: c_int, arg: u64, buffer: *anyopaque, buffersize: c_int) c_int; +/// Whether an adopted shell marks its prompts: one pardes started with its +/// prompt files (bash, zsh, fish), which a pane's runs wait on. +pub fn marksPrompts(pid: libc.pid_t) bool { + var buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + const exe = std.fs.path.basename(processExe(pid, &buf) orelse return false); + for ([_][]const u8{ "bash", "zsh", "fish" }) |shell| if (std.mem.eql(u8, exe, shell)) return true; + return false; +} + +/// The program `pid` runs, where the system says (an adopted shell's, for +/// its pane's Tty word); null elsewhere. +pub fn processExe(pid: libc.pid_t, buf: []u8) ?[]const u8 { + if (comptime builtin.os.tag == .freebsd) { + const n = kernProc(pid, 12, buf) orelse return null; // KERN_PROC_PATHNAME + return if (n > 1) buf[0 .. n - 1] else null; + } + if (comptime builtin.os.tag != .linux) return null; + var pbuf: [64]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{pid}, 0) catch return null; + const n = libc.readlink(path, buf.ptr, buf.len); + if (n <= 0 or n >= buf.len) return null; + return buf[0..@intCast(n)]; +} + pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { switch (builtin.os.tag) { .linux => { @@ -2233,10 +2280,28 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { @memcpy(buf[0..path.len], path); return buf[0..path.len]; }, + .freebsd => { + // A struct kinfo_file (sys/user.h), its last 1024 bytes the path. + var kf: [1392]u8 align(8) = undefined; + const n = kernProc(pid, 42, &kf) orelse return null; // KERN_PROC_CWD + if (n <= 368) return null; + const path = std.mem.sliceTo(kf[368..n], 0); + if (path.len == 0 or path.len >= buf.len) return null; + @memcpy(buf[0..path.len], path); + return buf[0..path.len]; + }, else => return null, } } +/// FreeBSD's kern.proc.<what>.<pid> into `buf`: the bytes it wrote. +fn kernProc(pid: libc.pid_t, what: c_int, buf: []u8) ?usize { + const mib = [_]c_int{ 1, 14, what, pid }; // CTL_KERN, KERN_PROC + var len: usize = buf.len; + if (libc.sysctl(&mib, mib.len, buf.ptr, &len, null, 0) != 0) return null; + return len; +} + /// Where a terminal is. The process forked for it, while it still runs the /// shell pardes started (`shell_exe`), as ever: a job it runs, in another /// directory (`(cd sub && make)`, a nested pardes), never names the pane. @@ -2443,6 +2508,13 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool { if (fg == shell_pid) return false; return darwin.taken(shell_pid, fg); }, + // ponytail: any foreground job but the shell's own group, so a + // nested interactive shell counts as taken; walk the group + // (KERN_PROC_PGRP) as darwin does if that matters. + .freebsd => { + const fg = tcgetpgrp(master_fd); + return fg > 0 and fg != shell_pid; + }, else => return false, } } @@ -2551,10 +2623,16 @@ pub fn retireShell(pid: libc.pid_t) void { // it -- until the host, told through `takeExited`, reaps it in its own loop // with `exitStatus` and tells the core. -const waitid_p_pid: c_int = 1; -const waitid_wexited: c_int = 4; -const waitid_wnowait: c_int = if (builtin.os.tag == .linux) 0x01000000 else 0x20; -extern "c" fn waitid(idtype: c_int, id: c_uint, infop: *anyopaque, options: c_int) c_int; +const freebsd = builtin.os.tag == .freebsd; +const waitid_p_pid: c_int = if (freebsd) 0 else 1; +const waitid_wexited: c_int = if (freebsd) 16 else 4; +const waitid_wnowait: c_int = switch (builtin.os.tag) { + .linux => 0x01000000, + .freebsd => 8, + else => 0x20, +}; +// FreeBSD's id_t is 64 bits. +extern "c" fn waitid(idtype: c_int, id: if (freebsd) i64 else c_uint, infop: *anyopaque, options: c_int) c_int; var exited: [pardes.MAX_PANES * 2]std.atomic.Value(libc.pid_t) = @splat(.init(0)); var exit_lock: libc.pthread_mutex_t = .{}; @@ -2798,11 +2876,13 @@ pub fn killJob(shell_pid: libc.pid_t, master_fd: c_int) bool { } /// The command name of the tty's foreground job, when that is not the -/// shell: what `pty/run` says is running. Linux only (/proc/<pgrp>/comm). +/// shell: what `pty/run` says is running. /proc/<pgrp>/comm on Linux, the +/// program's name on FreeBSD. pub fn foregroundName(shell_pid: libc.pid_t, master_fd: c_int, buf: []u8) ?[]const u8 { - if (comptime builtin.os.tag != .linux) return null; + if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) return null; const fg = tcgetpgrp(master_fd); if (fg <= 0 or fg == shell_pid) return null; + if (comptime builtin.os.tag == .freebsd) return std.fs.path.basename(processExe(fg, buf) orelse return null); var name: [64:0]u8 = undefined; const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/comm", .{@as(u32, @intCast(fg))}, 0) catch return null; const comm = std.mem.trimEnd(u8, readProc(path, buf) orelse return null, "\n"); diff --git a/src/image.zig b/src/image.zig index e334965a..9163730d 100644 --- a/src/image.zig +++ b/src/image.zig @@ -11,7 +11,6 @@ const pdf_enabled = @import("pardes_config").mupdf; const pardes = @import("pardes.zig"); const config = @import("config.zig"); const filesystem = @import("fs.zig"); -const dump = @import("dump.zig"); // Glyph matching adapted from caioluders/petsciinator. pub const GlyphArt = struct { pub const commodore = [16][3]u8{ @@ -756,6 +755,13 @@ var started = false; extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int; +/// stb_image's header reader; a freestanding build has no stb_image (its +/// decoder is the shim in src/web/zstbi.zig), so no header reads as one. +fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool { + if (comptime !pardes.hosted) return false; + return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1; +} + /// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for /// kitty graphics) wherever ghostty-vt is in the build; stb_image is then /// compiled without them, and without the inflate only its PNG used @@ -788,7 +794,7 @@ fn headerSize(bytes: []const u8) ?Size { var iw: c_int = 0; var ih: c_int = 0; var ic: c_int = 0; - if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; + if (!stbiInfo(bytes, &iw, &ih, &ic)) return null; return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) }; } @@ -829,7 +835,7 @@ pub fn readable(bytes: []const u8) bool { var c: c_int = 0; if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null; - return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1; + return stbiInfo(bytes, &w, &h, &c); } /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the @@ -930,7 +936,14 @@ const GridKey = struct { ascii: bool = true, }; +/// A host's thread; a freestanding build has none to have. +const Decoder = if (pardes.hosted) std.Thread else void; + 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, @@ -944,12 +957,12 @@ pub const State = struct { grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, grid_key: GridKey = .{}, /// The decode under way off the loop (ensureDecoded), and its answer. - decoder: ?std.Thread = null, + decoder: ?Decoder = null, decoded: std.atomic.Value(bool) = .init(false), answer: ?Decoded = null, pub fn deinit(state: *State, gpa: std.mem.Allocator) void { - if (state.decoder) |t| t.join(); + if (comptime pardes.hosted) if (state.decoder) |t| t.join(); if (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); @@ -986,68 +999,6 @@ pub fn create(p: *pardes.Pardes, id: usize, path: []const u8, raw: []u8) !*parde return pane; } -/// Serialize the binary image record used by images and, for dump-schema -/// compatibility, PDFs. Common pane metadata is supplied by the core. -pub fn dumpPane( - p: *pardes.Pardes, - arena: std.mem.Allocator, - pane: *const pardes.Pane, - tag: []const u8, - body: []const u8, - scroll: usize, - path: []const u8, - raw: []const u8, -) !dump.Pane { - // A picture or PDF on disk is read from there at Restore, as a clean - // file's text is: the dump keeps only the bytes of one that is not - // (megabytes of a PDF per dump, and a stale copy, otherwise). - _ = p; - const on_disk = if (comptime !pardes.hosted) false else if (filesystem.localPath(path)) |local| @import("exec.zig").kindOf(local) == .file else false; - const bytes: []const u8 = if (on_disk) &.{} else raw; - return .{ - .kind = .image, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .image = .{ - .path = path, - .bytes_b64 = if (bytes.len > 0) try dump.encodeBytes(arena, bytes) else "", - .petscii = if (pane.image) |state| state.glyph_art else false, - .palette = if (pane.image) |state| switch (state.pmode) { - .commodore => .commodore, - .terminal => .terminal, - } else .commodore, - .ascii = if (pane.image) |state| state.ascii else true, - .pdf_fit = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.fit) else "") else "", - .pdf_tint = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.tint) else "") else "", - }, - }; -} - -pub fn restore(p: *pardes.Pardes, id: usize, src: dump.Pane) !*pardes.Pane { - const saved = src.image.?; - var raw: []u8 = if (saved.bytes_b64.len > 0) - try dump.decodeBytes(p.image_gpa, saved.bytes_b64) - else - &.{}; - errdefer if (raw.len > 0) p.image_gpa.free(raw); - const pane = try create(p, id, saved.path, raw); - raw = &.{}; - pane.image.?.glyph_art = saved.petscii; - pane.image.?.pmode = switch (saved.palette) { - .commodore => .commodore, - .terminal => .terminal, - }; - pane.image.?.ascii = saved.ascii; - pane.cols = @max(1, src.cols); - pane.rows = @max(1, src.rows); - return pane; -} - pub fn toggleGlyphArt(state: *State) void { state.glyph_art = !state.glyph_art; } @@ -1085,11 +1036,11 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![ /// docs' screenshot: all of it once on the frame before). /// ponytail: a thread a picture; a pool if many open at once. fn ensureDecoded(p: *pardes.Pardes, state: *State) void { - if (state.decoder) |t| { + if (comptime pardes.hosted) if (state.decoder) |t| { if (!state.decoded.load(.acquire)) return; t.join(); state.decoder = null; - } + }; if (state.answer) |decoded| { state.answer = null; state.rgba = decoded.rgba; diff --git a/src/locations.zig b/src/locations.zig index cdb228a6..b14b3a2c 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const pardes = @import("pardes.zig"); const look = @import("look.zig"); const syntax = @import("syntax.zig"); @@ -23,14 +24,53 @@ pub const Row = struct { location_end: usize, code_start: usize, colors: []u8 = &.{}, + + /// The replay digest's (src/walk.zig): all of it but its colours. They + /// are painted from what this process has painted (the owner's + /// highlights a frame made, the locations cache: both derived), so a + /// client joined from a dump, or one that drew another view, colours a + /// row its session left plain. They are how a row looks, not where it + /// points; a dump keeps them. + pub fn digest(r: *const Row, h: *pardes.walk.Hasher) void { + inline for (@typeInfo(Row).@"struct".field_names) |name| { + if (comptime std.mem.eql(u8, name, "colors")) continue; + pardes.walk.hash(@FieldType(Row, name), &@field(r, name), h); + } + } }; +/// Raw (no safe build's overwrite): a replica's rollback copy pins a row +/// blob and what its rows own (src/rollback.zig `pinnedDeep`). pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { for (rows) |row| { - gpa.free(row.path); - gpa.free(row.colors); + freeRaw(gpa, row.path); + freeRaw(gpa, row.colors); + } + freeRaw(gpa, rows); +} + +/// A copy of `rows` and all they own, gpa's: rows are a blob (File.State +/// `location_rows`), made again rather than written. +pub fn cloneRows(gpa: std.mem.Allocator, rows: []const Row) ![]Row { + const out = try gpa.alloc(Row, rows.len); + var made: usize = 0; + errdefer { + for (out[0..made]) |r| { + gpa.free(r.path); + gpa.free(r.colors); + } + gpa.free(out); + } + for (rows, out) |r, *o| { + o.* = r; + o.path = try gpa.dupe(u8, r.path); + o.colors = gpa.dupe(u8, r.colors) catch |err| { + gpa.free(o.path); + return err; + }; + made += 1; } - gpa.free(rows); + return out; } /// A list of rows being built, given up: each row's own bytes, then the list. diff --git a/src/look.zig b/src/look.zig index 1a574212..09a22266 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; @@ -1816,7 +1816,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // `@p<serial>` names no open pane: one this session closed comes // back where it was (Store.reopen), any other is a miss, said. if (t.id >= MAX_PANES or p.panes[t.id] == null) { - if (p.store.closedAddress(pl.path)) |serial| if (p.store.reopen(p, serial, id)) return; + if (@TypeOf(p.store).closedAddress(pl.path)) |serial| if (p.store.reopen(p, serial, id)) return; return missed(p, id, "no pane {s}", .{pl.path[0..@min(pl.path.len, 32)]}); } const target = p.panes[t.id].?; diff --git a/src/macos.zig b/src/macos.zig index 4d1d6511..76e4ba19 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -56,7 +56,7 @@ fn logFn( comptime format: []const u8, args: anytype, ) void { - if (scope != .macos and scope != .dump and std.c.getenv("PARDES_LOG") == null) return; + if (scope != .macos and std.c.getenv("PARDES_LOG") == null) return; std.log.defaultLog(level, scope, format, args); } @@ -1105,7 +1105,8 @@ fn drainInbox(st: *State) bool { .search => |b| st.core.update(.{ .search = b }), // The SDL shell's: this one's file watches and 9P are its own. // ponytail: no hand_over host entry on macOS yet, so none comes. - .files_changed, .fs_ready, .handed => {}, + // No daemon on macOS (daemon.supported): neither ever comes. + .files_changed, .fs_ready, .handed, .window, .adopted => {}, } } for (0..pardes.MAX_PANES) |pane| { diff --git a/src/main.zig b/src/main.zig index 4e6b49bb..f9118dfc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,8 @@ const std = @import("std"); 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; @@ -20,7 +22,15 @@ else // the thread starts, so std's 256 KiB made each 9P connection's thread cost // that much resident. A stack-overflow segfault on a thread still printed its // whole symbolized trace from 32 KiB up (16 KiB double-faulted): 64 KiB. -pub const std_options: std.Options = .{ .logFn = logFn, .signal_stack_size = 64 * 1024 }; +// +// FreeBSD answers a write to a terminal that is gone with ENXIO, an errno std +// does not expect: a Debug build traced it to stderr, that same terminal, +// and recursed until the stack ran out (a session whose ssh dropped hung). +pub const std_options: std.Options = .{ + .logFn = logFn, + .signal_stack_size = 64 * 1024, + .unexpected_error_tracing = (std.Options{}).unexpected_error_tracing and builtin.os.tag != .freebsd, +}; fn logFn( comptime level: std.log.Level, @@ -28,13 +38,16 @@ fn logFn( comptime format: []const u8, args: anytype, ) void { - if (scope != .gui and scope != .dump and std.c.getenv("PARDES_LOG") == null) return; + if (scope != .gui and std.c.getenv("PARDES_LOG") == null) return; std.log.defaultLog(level, scope, format, args); } pub const panic = if (is_emscripten) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.FullPanic(struct { fn call(msg: []const u8, ret_addr: ?usize) noreturn { - if (!recovering.swap(true, .seq_cst)) @import("vaxis").recover(); + if (!recovering.swap(true, .seq_cst)) { + pardes.replica.dying(); + @import("vaxis").recover(); + } @import("crash.zig").record(msg); std.debug.defaultPanic(msg, ret_addr); } @@ -44,7 +57,10 @@ var recovering: std.atomic.Value(bool) = .init(false); pub const debug = if (is_emscripten) struct {} else struct { pub fn handleSegfault(addr: ?usize, name: []const u8, opt_ctx: anytype) noreturn { - if (!recovering.swap(true, .seq_cst)) @import("vaxis").recover(); + if (!recovering.swap(true, .seq_cst)) { + pardes.replica.dying(); + @import("vaxis").recover(); + } return std.debug.defaultHandleSegfault(addr, name, opt_ctx); } }; @@ -64,7 +80,7 @@ const help_text = \\ -n <count> initial shell panes: 1 (default) or 3 (classic). \\ A FILE argument boots just that file instead. \\ --tty-toggle <key> use Ctrl-<key> to toggle tty/editor mode - \\ -l <dump.zon> load a dump of another instance (see Dump) + \\ -l <dump> load a dump of another instance (see Dump) \\ -w, --wait inside a pardes pane, hand FILE to the outer \\ session and return only once the pane it opened \\ (or already had) is deleted: 0 then, 1 if the @@ -84,22 +100,35 @@ const help_text = \\ --9p-quic=<dial> also serve at quic!<numeric-ip>!<port> (-Dquic=true). \\ --mount=<name>=<dial> mount a 9P session or Unix/TCP/QUIC endpoint under /n/<name>. \\ Repeat for more mounts; os and self are reserved. - \\ --detach run this session with NO terminal of its own, - \\ serving frontends over a unix socket beside its - \\ 9P socket. The core, the panes and the - \\ undo history outlive every frontend that attaches - \\ --detach=<name> ...named <name> rather than this process's pid, so - \\ a frontend can say which session it wants. One - \\ path component: no '/' and nothing empty - \\ --attach become a frontend of the one detached session - \\ that is running: draw its screen and send it - \\ input, and nothing else — the session owns its - \\ pane shells, its files and its watches. Works in - \\ both the terminal and the SDL window build. - \\ Several frontends may be attached at once and all - \\ see the same screen - \\ --attach=<name> ...of the session called <name>, which is what to - \\ use when more than one is running + \\ --detach run this session with no terminal (no window) + \\ until one attaches; its panes, shells and files + \\ stay. Detach, or a terminal that closes, leaves + \\ any session so. One `pardes --daemon` per user + \\ keeps them (PARDES_DAEMON=off: none) + \\ --detach=<name> ...named <name> rather than this process's pid, + \\ as --9p names it. One path component: no '/' + \\ and nothing empty + \\ --attach show the one detached session here: this + \\ terminal for `pardes`, a window again for + \\ `pardes-gui`, each of its own kind. Returns when + \\ the session detaches or ends + \\ --attach=<name> ...the session called <name> (its --9p or + \\ --detach name, else its pid) + \\ --attach=<host>:<name> a client of session <name> on another machine, + \\ 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, + \\ if not this one's path). Its keys show at once + \\ --attach=<sock>:<name> ...through that socket (a path, with a '/'), + \\ one forwarded already, or this machine's + \\ --ps what the pardes daemon keeps: each session (its + \\ pid, kind, whether attached) and its shells, then + \\ the shells no session holds + \\ --kill=<name> end session <name> and its shells (TERM, then + \\ KILL 2 s later); unsaved changes are lost + \\ --kill-all end every session and every shell the daemon keeps \\ --geometry=WxH[+X+Y] the window's size in points, and where it goes \\ where the display lets a window say (X11) \\ -h, --help show this help and exit @@ -281,10 +310,12 @@ fn badArgs(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn { std.process.exit(1); } -const version_text = if (pardes.commit) |c| +/// The second line is what a replica's build must share with its session's +/// (detached/wire.zig `semantics`). +const version_text = (if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")\n" else - "pardes " ++ pardes.version ++ "\n"; + "pardes " ++ pardes.version ++ "\n") ++ std.fmt.comptimePrint("semantics {x:0>16}\n", .{@import("detached/wire.zig").semantics}); const nested_text = \\pardes: this shell is already inside pardes, and a pardes inside a pardes @@ -297,6 +328,47 @@ const nested_text = // the std.process.Init entry. pub const main = if (is_emscripten) webMain else nativeMain; +// glibc's struct termios is 60 bytes, Zig 0.17's std.c.termios the kernel's +// 44: libc's tcgetattr wrote 16 bytes past every Zig caller's (vaxis's +// makeRaw, the stub's, the GUI's), and a ReleaseSafe build trapped at its +// first terminal. These take libc's place in the process: the kernel's own +// ioctls, which read and write only the 36 bytes both layouts begin with +// (the flags, the line, the kernel's 19 cc), so a caller of either size +// stays within its struct. +comptime { + if (builtin.os.tag == .linux and builtin.abi.isGnu()) { + @export(&kernelTcgetattr, .{ .name = "tcgetattr" }); + @export(&kernelTcsetattr, .{ .name = "tcsetattr" }); + } +} + +fn kernelTcgetattr(fd: c_int, t: *std.os.linux.termios) callconv(.c) c_int { + return libcResult(std.os.linux.tcgetattr(fd, t)); +} + +fn kernelTcsetattr(fd: c_int, how: c_int, t: *const std.os.linux.termios) callconv(.c) c_int { + return libcResult(std.os.linux.tcsetattr(fd, @fromBackingInt(@bitCast(how)), t)); +} + +fn libcResult(rc: usize) c_int { + const e = std.os.linux.errno(rc); + if (e == .SUCCESS) return 0; + std.c._errno().* = @intCast(@backingInt(e)); + return -1; +} + +test "tcgetattr writes nothing past a Zig termios" { + if (comptime !(builtin.os.tag == .linux and builtin.abi.isGnu())) return error.SkipZigTest; + const master = std.c.open("/dev/ptmx", .{ .ACCMODE = .RDWR, .NOCTTY = true, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (master < 0) return error.SkipZigTest; + defer _ = std.c.close(master); + var s: extern struct { t: std.c.termios, canary: [32]u8 } = .{ .t = undefined, .canary = @splat(0xAA) }; + try std.testing.expectEqual(0, std.c.tcgetattr(master, &s.t)); + try std.testing.expectEqualSlices(u8, &@as([32]u8, @splat(0xAA)), &s.canary); + try std.testing.expectEqual(0, std.c.tcsetattr(master, .NOW, &s.t)); + try std.testing.expectEqual(-1, std.c.tcgetattr(-1, &s.t)); +} + comptime { if (is_emscripten) @export(&emscriptenMain, .{ .name = "main" }); } @@ -316,7 +388,7 @@ fn emscriptenMain(argc: c_int, argv: [*]?[*:0]u8) callconv(.c) c_int { // no argv in the browser: options stay default, state comes from the dump, and // there is no session to attach to — a browser tab has no unix socket. fn webMain() !void { - try @import("gui/gui.zig").run(.{}, .{}, null); + try @import("gui/gui.zig").run(.{}, .{}, false); } // init.gpa, what the tty, gui and detached shells allocate from, is their @@ -326,6 +398,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(); @@ -335,6 +409,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), 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; @@ -342,7 +428,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")) { @@ -434,6 +520,14 @@ fn nativeMain(std_init: std.process.Init) !void { attach = ""; } else if (std.mem.startsWith(u8, a, "--attach=")) { attach = a["--attach=".len..]; + } else if (std.mem.eql(u8, a, "--ps")) { + manage(init.io, "list", ""); + } else if (std.mem.startsWith(u8, a, "--kill=")) { + const name = a["--kill=".len..]; + if (name.len == 0) badArgs(init.io, "--kill needs a session's name; --kill-all ends every one", .{}); + manage(init.io, "kill", name); + } else if (std.mem.eql(u8, a, "--kill-all")) { + manage(init.io, "killall", ""); } else if (std.mem.eql(u8, a, "-h") or std.mem.eql(u8, a, "--help")) { try std.Io.File.stdout().writeStreamingAll(init.io, help_text); return; @@ -483,7 +577,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 @@ -664,6 +758,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, "-")) { @@ -713,30 +808,72 @@ fn nativeMain(std_init: std.process.Init) !void { opts.config_dir = found.dir; opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); + if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { + remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); + }; + if (attach) |name| if (!resuming) attachTo(init.io, name); if (detach) |name| { - const named = if (name.len != 0) - name - else - try std.fmt.allocPrint(arena, "{d}", .{@as(u32, @intCast(std.c.getpid()))}); - // Said plainly, never a Zig error trace: a name another session's + // 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). - return @import("detached/server.zig").run(init, opts, named) catch |err| { - var words: [128]u8 = undefined; + if (name.len != 0 and !resuming and ninep_io.taken(name)) { var said: [512]u8 = undefined; - const why = if (err == error.ListenFailed) - "its socket is taken: another session (a plain one is named by its pid) holds that name; choose another" - else - pardes.Messages.errorWords(err, &words); - std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&said, "pardes: --detach={s}: {s}\n", .{ named, why }) catch "pardes: --detach failed\n") catch {}; + 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); - }; + } + if (opts.ninep_name.len == 0) opts.ninep_name = name; + // Nobody's size yet: one roomy enough for a script's panes; `size` + // sets another, and an attached terminal or window its own. + opts.cols = 160; + opts.rows = 50; } - if (attach != null and pardes.platform != .tty and pardes.platform != .gui) { - try std.Io.File.stderr().writeStreamingAll(init.io, "pardes: --attach needs the tty or gui shell\n"); - std.process.exit(1); + const kind: daemon.Kind = if (pardes.platform == .gui) .gui else .tty; + var term: @import("tty/tty.zig").Term = if (detach != null) .none else .open; + 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: { + // A terminal's session is forked off its stub, which stays the + // job the shell waits on: Detach (or the terminal's end) ends + // the job and leaves the session running (daemon.forkSession). + // The daemon is reached or started first, from this small + // process, and the terminal opened while it is this one's. + const conn = daemon.connect(true) orelse break :fork; + const fd = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true, .NOCTTY = true }, @as(std.c.mode_t, 0)); + if (fd < 0) { + daemon.startPending(true); + _ = std.c.close(conn); + break :fork; + } + // Started in the background, the job stops here until it is + // brought to the foreground, as one making its terminal raw does. + // std.posix's termios, not std.c's: glibc's is the bigger struct. + if (std.posix.tcgetattr(fd)) |tio| std.posix.tcsetattr(fd, .NOW, tio) catch {} else |_| {} + daemon.forkSession(conn) orelse { + daemon.startPending(true); + _ = std.c.close(fd); + _ = std.c.close(conn); + break :fork; + }; + _ = daemon.register(conn, .tty, opts.ninep_name, false); + term = .{ .fd = .{ .fd = fd } }; + } } + defer daemon.quit(); + errdefer daemon.tellStubExit(1); switch (pardes.platform) { - .tty => @import("tty/tty.zig").run(init, opts, attach) catch |err| switch (err) { + .tty => @import("tty/tty.zig").run(init, opts, term) catch |err| switch (err) { // No controlling terminal (a detached pty, a daemon): said, not // an error trace. error.NoDevice => { @@ -745,9 +882,253 @@ fn nativeMain(std_init: std.process.Init) !void { }, else => return err, }, - .gui => try @import("gui/gui.zig").run(init, opts, attach), + .gui => try @import("gui/gui.zig").run(init, opts, headless), .web, .macos, .esp32p4 => unreachable, } + daemon.tellStubExit(0); +} + +extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; +extern "c" fn atexit(f: *const fn () callconv(.c) void) c_int; + +/// `pardes --ps`: what the per-user daemon keeps, as a tree; `--kill=<name>` +/// and `--kill-all`: those sessions ended with their shells, a TERM and, +/// for one still there 2 s later, a KILL. +fn manage(io: std.Io, verb: []const u8, name: []const u8) noreturn { + const Say = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: " ++ fmt ++ "\n", args) catch "pardes: failed\n") catch {}; + std.process.exit(1); + } + }; + if (comptime !daemon.supported) Say.no(io, "this system has no pardes daemon", .{}); + var words_buf: [300]u8 = undefined; + const words = if (std.mem.eql(u8, verb, "kill")) + std.fmt.bufPrint(&words_buf, "kill {s}", .{name}) catch Say.no(io, "that name is too long", .{}) + else + verb; + var buf: [4096]u8 = undefined; + const said = daemon.ask(words, &buf) orelse Say.no(io, "no pardes daemon is running", .{}); + if (std.mem.startsWith(u8, said, "no ")) Say.no(io, "{s}", .{said[3..]}); + if (std.mem.eql(u8, verb, "list")) { + var out: [8192]u8 = undefined; + var w: std.Io.Writer = .fixed(&out); + var lines = std.mem.tokenizeScalar(u8, said, '\n'); + var orphans = false; + while (lines.next()) |line| { + var it = std.mem.tokenizeScalar(u8, line, ' '); + const what = it.next() orelse continue; + const pid = it.next() orelse continue; + var comm_buf: [64]u8 = undefined; + const comm = commOf(pid, &comm_buf); + if (std.mem.eql(u8, what, "daemon")) { + w.print("pardes daemon {s}\n", .{pid}) catch break; + } else if (std.mem.eql(u8, what, "session")) { + const kind = it.next() orelse ""; + const state = it.next() orelse ""; + w.print(" session {s} {s} ({s}, {s})\n", .{ std.mem.trim(u8, it.rest(), " "), pid, kind, state }) catch break; + } else if (std.mem.eql(u8, what, "shell")) { + w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + } else if (std.mem.eql(u8, what, "orphan")) { + if (!orphans) w.writeAll(" shells no session holds (Adopt takes them, --kill-all ends them)\n") catch break; + orphans = true; + w.print(" shell {s} {s}\n", .{ pid, comm }) catch break; + } + } + std.Io.File.stdout().writeStreamingAll(io, w.buffered()) catch {}; + std.process.exit(0); + } + var pids: [64]std.c.pid_t = undefined; + var n: usize = 0; + var it = std.mem.tokenizeScalar(u8, said, ' '); + _ = it.next(); // ok + while (it.next()) |p| if (n < pids.len) { + pids[n] = std.fmt.parseInt(std.c.pid_t, p, 10) catch continue; + n += 1; + }; + // Gone once the daemon no longer lists them: a session's link ends with + // its process, a zombie's too, which kill(pid, 0) would call alive. + const Gone = struct { + fn all(list: []const std.c.pid_t, ms: u32) bool { + var waited: u32 = 0; + while (true) : (waited += 20) { + var reply: [4096]u8 = undefined; + const now = daemon.ask("list", &reply) orelse return true; + var lines = std.mem.tokenizeScalar(u8, now, '\n'); + const left = while (lines.next()) |line| { + var fields = std.mem.tokenizeScalar(u8, line, ' '); + if (!std.mem.eql(u8, fields.next() orelse "", "session")) continue; + const pid = std.fmt.parseInt(std.c.pid_t, fields.next() orelse "", 10) catch continue; + if (std.mem.indexOfScalar(std.c.pid_t, list, pid) != null) break true; + } else false; + if (!left) return true; + if (waited >= ms) return false; + const ts: std.c.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&ts, null); + } + } + }; + if (!Gone.all(pids[0..n], 2000)) { + for (pids[0..n]) |p| _ = std.c.kill(p, .KILL); + if (!Gone.all(pids[0..n], 1000)) Say.no(io, "a session would not end, even killed", .{}); + } + std.process.exit(0); +} + +/// The command `pid` runs, as /proc says it (Linux); empty where it does not. +fn commOf(pid: []const u8, buf: *[64]u8) []const u8 { + var path_buf: [64]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{s}/comm", .{pid}, 0) catch return ""; + const fd = std.c.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (fd < 0) return ""; + defer _ = std.c.close(fd); + const got = std.c.read(fd, buf, buf.len); + if (got <= 0) return ""; + return std.mem.trimEnd(u8, buf[0..@intCast(got)], "\n"); +} + +/// 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=<where>:<name>` (`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 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; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + if (std.mem.indexOfScalar(u8, where, '/') != null) { + clientOf(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); + std.process.exit(0); + } + // ssh -L <here>:<there> <host>, until the socket here answers. + var here_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); + const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch Say.no(io, "TMPDIR is too long for a socket's path", .{}); + var there_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; + var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse Say.no(io, "no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{}); + var forward_buf: [2 * ninep_io.sun_path_len + 64]u8 = undefined; + const there = std.fmt.bufPrintSentinel(&there_buf, "{s}/pardes-replica.sock", .{dir}, 0) catch unreachable; + const forward = std.fmt.bufPrintSentinel(&forward_buf, "{s}:{s}", .{ here, there }, 0) catch unreachable; + var host_buf: [256]u8 = undefined; + const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch Say.no(io, "that host's name is too long", .{}); + var argv: [48:null]?[*:0]const u8 = @splat(null); + var n: usize = 0; + var words_buf: [1024]u8 = undefined; + // Not `ssh -C`: what the session sends a far client is zstd already + // (replica.zig), and zlib over it costs both ends' time for nothing. + // ponytail: what the client sends goes as it is, keys and a paste + // alike; a big paste through a slow link is the ceiling, and would be + // an envelope of its own. $PARDES_SSH, when set, is the whole command. + var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh"), ' '); + var at: usize = 0; + while (words.next()) |w| { + if (n >= 40 or at + w.len + 1 > words_buf.len) Say.no(io, "PARDES_SSH has too many words", .{}); + @memcpy(words_buf[at..][0..w.len], w); + words_buf[at + w.len] = 0; + argv[n] = @ptrCast(&words_buf[at]); + n += 1; + at += w.len + 1; + } + // A terminal asked for and a command that waits, not -N: only an + // interactive connection gets TCP_NODELAY from ssh and sshd, and with + // Nagle each key in a burst waited for the last one's ACK, a round trip. + for ([_][*:0]const u8{ "-tt", "-q", "-o", "ExitOnForwardFailure=yes", "-L", forward.ptr, host.ptr, "exec sleep 2147483647" }) |a| { + argv[n] = a; + n += 1; + } + _ = std.c.unlink(here); + const pid = std.c.fork(); + if (pid < 0) Say.no(io, "no fork for ssh", .{}); + if (pid == 0) { + // A client killed outright takes its ssh with it. + daemon.hupOnStubDeath(true); + const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDONLY }, @as(std.c.mode_t, 0)); + if (null_fd >= 0) _ = std.c.dup2(null_fd, 0); + _ = execvp(argv[0].?, &argv); + std.c._exit(127); + } + // Ended at the client's exit, whatever path it takes there (its own + // words on a refused redial exit from inside it). ponytail: a client + // that crashes leaves its socket's file behind; its ssh goes. + const Ssh = struct { + var child: std.c.pid_t = 0; + var path: [:0]const u8 = ""; + fn end() callconv(.c) void { + if (child > 0) { + _ = std.c.kill(child, .TERM); + _ = std.c.waitpid(child, null, 0); + } + _ = std.c.unlink(path); + } + }; + Ssh.child = pid; + Ssh.path = here; + _ = atexit(Ssh.end); + var waited: u32 = 0; + while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) { + var status: c_int = 0; + if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) { + Ssh.child = 0; + Say.no(io, "ssh to {s} ended before its forward was up", .{where}); + } + if (waited > 3000) Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); + const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&ts, null); + } + clientOf(init, opts, here, name, true) catch |err| Say.no(io, "{t}", .{err}); + std.process.exit(0); +} + +/// `pardes --attach[=name]` in a terminal: this terminal handed (through +/// the daemon) to the detached session of that name, or the only one, and +/// this process its stub until the session leaves it. `pardes-gui +/// --attach`: the session asked to open its window again, and done. +fn attachTo(io: std.Io, name: []const u8) noreturn { + var words: [600]u8 = undefined; + const Say = struct { + fn no(o: std.Io, why: []const u8) noreturn { + var buf: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: {s}\n", .{why}) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + if (comptime !daemon.supported) Say.no(io, "this system has no pardes daemon to attach through"); + if (pardes.platform == .gui) switch (daemon.attach(.gui, name, &.{}, &words)) { + .ok => std.process.exit(0), + .no => |why| Say.no(io, why), + }; + const fd = std.c.open("/dev/tty", .{ .ACCMODE = .RDWR, .CLOEXEC = true, .NOCTTY = true }, @as(std.c.mode_t, 0)); + if (fd < 0) Say.no(io, "no terminal to attach: /dev/tty will not open"); + if (std.posix.tcgetattr(fd)) |tio| std.posix.tcsetattr(fd, .NOW, tio) catch {} else |_| {} + var pipe: [2]std.c.fd_t = undefined; + if (std.c.pipe(&pipe) != 0) Say.no(io, "no pipe for the session to say when it leaves"); + ninep_io.setCloexec(pipe[0]); + ninep_io.setCloexec(pipe[1]); + switch (daemon.attach(.tty, name, &.{ fd, pipe[1] }, &words)) { + .ok => |pid| { + _ = std.c.close(fd); + _ = std.c.close(pipe[1]); + daemon.stubWait(pid, false, pipe[0]); + }, + .no => |why| Say.no(io, why), + } } fn parseCtrlKey(raw: []const u8) ?u21 { @@ -774,8 +1155,7 @@ test { _ = @import("detached/wire.zig"); _ = @import("ShaderBuild.zig"); _ = @import("vaxis_input.zig"); - _ = @import("detached/server.zig"); - _ = @import("detached/client.zig"); + _ = @import("daemon.zig"); _ = @import("9p_io.zig"); _ = @import("9p_io.zig").Client; _ = @import("host_io.zig"); diff --git a/src/memory.zig b/src/memory.zig index f7775615..cd29308f 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const walk = @import("walk.zig"); const config = @import("pardes_config"); const board = config.platform == .esp32p4; @@ -418,7 +419,85 @@ pub fn dupeUnfilled(allocator: Allocator, bytes: []const u8) Allocator.Error![]u /// Frees without the safe-build overwrite `free` does, for text-sized buffers /// freed on every edit. pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { - if (bytes.len > 0) allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); + if (bytes.len == 0) return; + // A blob's digest word goes with it (walk.zig). + walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(u8))) return; + allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); +} + +/// `freeUnfilled` for a slice of any element: a text's line index, made +/// anew at every edit too (and pinned by a replica's rollback copy, +/// src/rollback.zig, which a safe build's overwrite would spoil). +pub fn freeRaw(allocator: Allocator, items: anytype) void { + const E = @typeInfo(@TypeOf(items)).pointer.child; + const bytes = std.mem.sliceAsBytes(items); + if (bytes.len == 0) return; + walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(E))) return; + allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); +} + +// ---- a join's pins (src/replica.zig) ---- +// +// A replica client's join dumps the session's mutable state in its step and +// leaves its blobs out (walk.Deferred); its writer thread streams them after. +// A blob is never written in place and is freed raw (above), so until the +// writer has sent it, its free is put off: `pin` it, `unpin` it after. +var pins_lock: std.atomic.Value(bool) = .init(false); +var pins: std.AutoHashMapUnmanaged(usize, u32) = .empty; +var pin_count: std.atomic.Value(usize) = .init(0); +const PutOff = struct { allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment }; +var put_off: std.ArrayListUnmanaged(PutOff) = .empty; + +fn lockPins() void { + while (pins_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); +} + +/// The blob at `bytes.ptr` kept alive until it is unpinned as many times. +pub fn pin(bytes: []const u8) void { + lockPins(); + defer pins_lock.store(false, .release); + const got = pins.getOrPut(std.heap.page_allocator, @intFromPtr(bytes.ptr)) catch return; + got.value_ptr.* = if (got.found_existing) got.value_ptr.* + 1 else 1; + pin_count.store(pins.count(), .release); +} + +/// One pin of the blob at `bytes.ptr` let go: its free, put off, made now +/// the last one goes. +pub fn unpin(bytes: []const u8) void { + const addr = @intFromPtr(bytes.ptr); + lockPins(); + const n = pins.getPtr(addr) orelse { + pins_lock.store(false, .release); + return; + }; + n.* -= 1; + var due: ?PutOff = null; + if (n.* == 0) { + _ = pins.remove(addr); + pin_count.store(pins.count(), .release); + for (put_off.items, 0..) |f, i| if (@intFromPtr(f.bytes.ptr) == addr) { + due = put_off.swapRemove(i); + break; + }; + } + pins_lock.store(false, .release); + if (due) |f| { + // A word made for it while it was pinned (walk.makeWords) goes too. + walk.forget(addr); + f.allocator.rawFree(@constCast(f.bytes), f.alignment, @returnAddress()); + } +} + +/// Whether the free of `bytes` is put off (it is pinned). +fn pinned(allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment) bool { + if (pin_count.load(.acquire) == 0) return false; + lockPins(); + defer pins_lock.store(false, .release); + if (!pins.contains(@intFromPtr(bytes.ptr))) return false; + put_off.append(std.heap.page_allocator, .{ .allocator = allocator, .bytes = bytes, .alignment = alignment }) catch return false; + return true; } /// Gives free memory back to the system, the arenas' and libc's, inside its @@ -442,7 +521,8 @@ pub fn trim() void { /// 71 MiB that way. Only the region given, never the whole process (a prctl /// would reach every shell and server pardes starts). pub fn sparse(region: []u8) void { - if (comptime builtin.os.tag == .linux) advise(region, std.posix.MADV.NOHUGEPAGE); + if (comptime builtin.os.tag != .linux) return; + advise(region, std.posix.MADV.NOHUGEPAGE); } /// madvise over the whole pages inside `region`. @@ -552,3 +632,18 @@ test "memory limits preserve desktop capacities" { try std.testing.expectEqual(256 * MiB, limits.arena.image); try std.testing.expectEqual(if (config.mupdf) 512 * MiB else 64 * KiB, limits.arena.pdf); } + +test "a pinned blob's free is put off until its last pin goes" { + const gpa = std.testing.allocator; + const b = try gpa.alloc(u8, 8192); + @memset(b, 7); + pin(b); + pin(b); + freeUnfilled(gpa, b); + // Still the blob's: a join's writer reads it. + try std.testing.expectEqual(@as(u8, 7), b[100]); + unpin(b); + try std.testing.expectEqual(@as(u8, 7), b[8000]); + // The last pin: freed now (the testing allocator says if not). + unpin(b); +} diff --git a/src/mouse.zig b/src/mouse.zig index 2ff4fef3..b7307f2d 100644 --- a/src/mouse.zig +++ b/src/mouse.zig @@ -59,6 +59,9 @@ pub const Drag = union(enum) { /// This gesture began over a usable native PDF raster. The payload is /// zero-bit when PDF support is absent. pdf: panes.Pdf.PointerDrag = .{}, + /// Where its body was in the frontend's frame at its last hit there: + /// a sweep that leaves it is clamped back onto it (`bodyHitForPane`). + body: ?Layer.Place = null, }, fn button(drag: Drag) ?Mouse.Button { @@ -288,22 +291,29 @@ pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clam supplied.metrics.tagline_w == 0 or supplied.metrics.tagline_h == 0) return null; var mapped_hit = supplied; if (clamp) { + // Onto the body the sweep is in: the place of its last hit there + // (the drag's `body`), the hit's own while it is over it. + if (supplied.serial != pane.serial) mapped_hit.place = (if (p.drag == .select) p.drag.select.body else null) orelse return null; mapped_hit.serial = pane.serial; - const layer = for (&p.surface.body_layers) |*layer| { - if (layer.rows > 0 and layer.serial == pane.serial) break layer; - } else return null; + if (mapped_hit.place.rows == 0) return null; + const v = mapped_hit.place.viewport; const bw: f32 = @floatFromInt(mapped_hit.metrics.body_w); const bh: f32 = @floatFromInt(mapped_hit.metrics.body_h); - const x0 = @as(f32, @floatFromInt(layer.viewport.x)) * bw; - const y0 = @as(f32, @floatFromInt(layer.viewport.y)) * bh; - const x1 = x0 + @as(f32, @floatFromInt(layer.viewport.w)) * bw; - const y1 = y0 + @as(f32, @floatFromInt(layer.viewport.h)) * bh; + const x0 = @as(f32, @floatFromInt(v.x)) * bw; + const y0 = @as(f32, @floatFromInt(v.y)) * bh; + const x1 = x0 + @as(f32, @floatFromInt(v.w)) * bw; + const y1 = y0 + @as(f32, @floatFromInt(v.h)) * bh; mapped_hit.pixel_x = std.math.clamp(mapped_hit.pixel_x, x0, @max(x0, x1 - 0.001)); mapped_hit.pixel_y = std.math.clamp(mapped_hit.pixel_y, y0, @max(y0, y1 - 0.001)); } return p.reprojectBodyHit(mapped_hit); } +fn bodyPlace(hit: ?Mouse.BodyHit, pane: *const Pane) ?Layer.Place { + const h = hit orelse return null; + return if (h.serial == pane.serial) h.place else null; +} + pub fn sameBodyCell(a: ?Mouse.BodyHit, b: ?Mouse.BodyHit) bool { const first = a orelse return b == null; const second = b orelse return false; @@ -380,7 +390,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| { @@ -514,6 +524,8 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { current.kind = previous.kind; current.id = previous.id; current.serial = previous.serial; + // ...and its place, which a clamp brings the pointer back onto. + current.place = previous.place; }; } // A drag can leave the compact text viewport. Preserve its metrics so @@ -535,7 +547,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) { @@ -805,6 +817,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { .id = id, .button = config.select_button, .ctrl = m.ctrl, + .body = bodyPlace(m.body_hit, pane), .pdf = panes.Pdf.pointerStart( p, pane, @@ -862,6 +875,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { .select = .{ .id = id, .button = m.button, + .body = bodyPlace(m.body_hit, pane), // A native body gesture stays native even when its // point later misses a letterbox or selection. .pdf = panes.Pdf.pointerStart( @@ -931,7 +945,9 @@ 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| { + if (supplied.serial == pane.serial) s.body = supplied.place; const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return; pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER; pane.sel[b].r1 = @as(i32, mapped_hit.row) + tag_rows; @@ -989,10 +1005,17 @@ fn reportGesture( operand: exec.PointerOperand, chord_arg: ?[]const u8, ) bool { - if ((p.panes[id] orelse return false).fs.readers == 0) return false; return reportRange(p, id, cmd, text, on_tag, gestureRange(p, id, text, on_tag, operand), chord_arg); } +/// Whether a script reads pane `id`'s events, and so takes its looks and +/// execs: the 9P server's, the authority's alone (a replica has its +/// answer, `tape.authority`). +fn readsEvents(p: *Pardes, id: usize) bool { + const pane = p.panes[id] orelse return false; + return pane.fs.readers != 0; +} + /// An event reader told of a look or an exec of `text` at `range` of the /// tag or body, by a click or a key alike: false when the pane has none. pub fn reportRange( @@ -1005,7 +1028,7 @@ pub fn reportRange( chord_arg: ?[]const u8, ) bool { const scripted = p.panes[id] orelse return false; - if (scripted.fs.readers == 0) return false; + if (!pardes.tape.authority(readsEvents, .{ p, id })) return false; const is_look = cmd == config.look_cmd; const action: ctlfs.events.Action = if (is_look) (if (on_tag) .tag_look else .body_look) @@ -1019,7 +1042,9 @@ pub fn reportRange( if (is_look) { if (!named and std.mem.indexOfAny(u8, text, "/.:") != null) flag |= ctlfs.events.flag_filename; } else if (chord_arg != null) flag |= ctlfs.events.flag_chorded; - if (!ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text)) return false; + // Taken whether or not this process's server has the reader: the + // session's said it has (a replica's has none). + _ = ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text); // A chorded exec's argument follows as two records, its text and where // it came from (`file:#q0,#q1`, empty when that is not known), as acme // sends them (exec.c:182-188); the pane keeps it for the write-back. @@ -2214,3 +2239,114 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .release })); } + +const test_metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 }; + +/// A session and a replica joined to it from its dump: digest for digest +/// the same, and only the session has drawn a frame (its surface is its +/// own, `.local`). What a step does with one input must not depend on that. +fn sessionAndReplica(gpa: std.mem.Allocator, opts: pardes.Options) !struct { a: *Pardes, b: *Pardes } { + const a = try Pardes.init(gpa, opts); + errdefer a.deinit(); + a.update(.{ .resize = .{ .cols = opts.cols, .rows = opts.rows, .row_metrics = test_metrics } }); + a.sync(); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + errdefer b.deinit(); + var frame: std.heap.ArenaAllocator = .init(gpa); + defer frame.deinit(); + _ = try a.render(frame.allocator()); + try expectSame(a, b, "before the press"); + return .{ .a = a, .b = b }; +} + +/// The two cores' digests equal, else the fields (and pane fields) that differ. +fn expectSame(a: *const Pardes, b: *const Pardes, when: []const u8) !void { + if (pardes.walk.digest(Pardes, a) == pardes.walk.digest(Pardes, b)) return; + const names = comptime pardes.walk.fieldNames(Pardes); + var da: [names.len]u64 = undefined; + var db: [names.len]u64 = undefined; + _ = pardes.walk.fieldDigests(Pardes, a, &da); + _ = pardes.walk.fieldDigests(Pardes, b, &db); + for (names, da, db) |name, x, y| if (x != y) std.debug.print("{s}: {s} differs\n", .{ when, name }); + const pane_names = comptime pardes.walk.fieldNames(pardes.Pane); + for (a.panes, b.panes, 0..) |pa, pb, id| { + const x = pa orelse continue; + const y = pb orelse continue; + var pda: [pane_names.len]u64 = undefined; + var pdb: [pane_names.len]u64 = undefined; + _ = pardes.walk.fieldDigests(pardes.Pane, x, &pda); + _ = pardes.walk.fieldDigests(pardes.Pane, y, &pdb); + for (pane_names, pda, pdb) |name, u, v| if (u != v) std.debug.print("{s}: pane {d}.{s} differs\n", .{ when, id, name }); + } + return error.DigestsDiffer; +} + +fn layerCenter(layer: *const Layer, col: u16, comptime tag: bool) [2]f32 { + const w: f32 = if (tag) test_metrics.tagline_w else test_metrics.body_w; + const h: f32 = if (tag) test_metrics.tagline_h else test_metrics.body_h; + return .{ + @as(f32, @floatFromInt(layer.viewport.x)) * test_metrics.body_w + (@as(f32, @floatFromInt(col)) + 0.5) * w, + @as(f32, @floatFromInt(layer.viewport.y)) * test_metrics.body_h + 0.5 * h, + }; +} + +// A frontend's pointer hit is its own frame's: a step resolves it on the +// place the hit carries (Layer.Place), never on this process's last frame +// (`surface`, `.local`), so a session and its replica take one input alike +// (the variant hunt's, 2026-10-30, after a window client's tag hover). +test "a tag press is the same state whichever frame this process drew last" { + const gpa = std.testing.allocator; + const two = try sessionAndReplica(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer two.a.deinit(); + defer two.b.deinit(); + const layer = for (two.a.surface.tagLayers()) |*l| { + if (l.rows != 0 and l.kind == .pane) break l; + } else return error.NoPaneTag; + const at = layerCenter(layer, 2, true); + const hit = layer.tagHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit; + const press: pardes.Event = .{ .mouse = .{ .button = .left, .kind = .press, .col = layer.viewport.x + 2, .row = layer.viewport.y, .tag_hit = hit } }; + two.a.update(press); + two.b.update(press); + try expectSame(two.a, two.b, "after the press"); +} + +// The same through a body hit (edit.pointerTextSelection, a sweep's drag), +// on a compact body layer (TreeContext), which a pixel shell inverts. +test "a body press, drag and release are the same state whichever frame this process drew last" { + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer a.deinit(); + const pane = try a.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][16]u8, @splat(" // body\n".*)))) ++ " }\n};\n"); + a.gpa.free(pane.file.?.path); + pane.file.?.path = try a.gpa.dupe(u8, "/test.zig"); + if (!pardes.panes.File.supportsContext(pane)) return error.SkipZigTest; + a.update(.{ .resize = .{ .cols = 60, .rows = 16, .row_metrics = test_metrics } }); + pane.file.?.tree_context = true; + pane.file.?.scroll = 8; + a.sync(); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + defer b.deinit(); + var frame: std.heap.ArenaAllocator = .init(gpa); + defer frame.deinit(); + _ = try a.render(frame.allocator()); + // The join agrees, the frame drawn and all. + try expectSame(a, b, "before the press"); + const layer = for (&a.surface.body_layers) |*l| { + if (l.rows > 0 and l.serial == pane.serial) break l; + } else return error.NoBodyLayer; + const at = layerCenter(layer, 8, false); + const hit = layer.bodyHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit; + const col: u16 = @intFromFloat(at[0] / test_metrics.body_w); + const row: u16 = @intFromFloat(at[1] / test_metrics.body_h); + for ([_]pardes.Mouse.Kind{ .press, .drag, .release }) |kind| { + const ev: pardes.Event = .{ .mouse = .{ .button = .left, .kind = kind, .col = col, .row = row, .body_hit = hit } }; + a.update(ev); + b.update(ev); + } + try expectSame(a, b, "after the press"); +} diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index d0d9b0b4..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'); @@ -2075,8 +2089,7 @@ test "Zerox twins dump their text once and come back on one buffer from a Restor try pardes.dump.dumpState(p); const text = p.dump_out.?; // The text is kept once though three panes show it. - const encoded = try pardes.dump.encodeBytes(p.scratch.allocator(), p.panes[0].?.file.?.content); - try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, encoded)); + try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, p.panes[0].?.file.?.content)); const restored = try pardes.dump.restore(p, text, "/tmp/pardes.dump.zon"); defer restored.deinit(); var files: [3]*pardes.panes.File.State = undefined; @@ -2838,7 +2851,7 @@ test "a Restore of a file that is no dump fails the write before any warning, an const root_ctl = @backingInt(tree.TopFile.ctl); const refused = wr(p, root_ctl, line); try testing.expectEqual(E.IO, refused.errno()); - try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: line 1: ") != null); + try testing.expect(std.mem.indexOf(u8, refused.reply.ename, "not a pardes dump: it does not begin as one") != null); try testing.expect(p.restore_req == null); try testing.expect(p.panes[0].?.discard_warned == null); @@ -2856,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 @@ -4676,9 +4689,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. - if (comptime @import("builtin").os.tag == .linux) - 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 @@ -4787,8 +4799,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")); } @@ -4897,46 +4912,6 @@ test "Petscii, Palette and Ascii take the state their tag word names, set rather try testing.expect(th.logHas(p, "Petscii takes on or off")); } -test "a Restore reads a clean file from disk; an unsaved one whose file changed since is said so, and its first Save asks" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "one\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/a.txt", .{dir}); - const p = try Pardes.init(gpa, .{ .file = path, .tty_only = true, .cols = 80, .rows = 24 }); - defer p.deinit(); - // Clean: the dump keeps no text, and the newer disk is what comes back. - try pardes.dump.dumpState(p); - try testing.expect(std.mem.indexOf(u8, p.dump_out.?, "b25lCg==") == null); // "one\n" in base64 - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "two\n" }); - { - const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); - defer restored.deinit(); - const f = pane_files.fileOf(restored.panes[0].?).?; - try testing.expectEqualStrings("two\n", f.content); - try testing.expectEqual(f.saved_revision, f.revision); - } - // Unsaved: its text comes back, the disk changed under it is said, and - // Save asks once before overwriting it. - const f = pane_files.fileOf(p.panes[0].?).?; - const edited = try gpa.dupe(u8, "edited\n"); - pardes.panes.File.setContent(p, f, edited); - try testing.expect(f.revision != f.saved_revision); - try pardes.dump.dumpState(p); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.txt", .data = "three\n" }); - const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); - defer restored.deinit(); - const rf = pane_files.fileOf(restored.panes[0].?).?; - try testing.expectEqualStrings("edited\n", rf.content); - try testing.expect(rf.disk_newer != null); - pardes.exec.saveFile(restored, 0); - try testing.expect(th.logHas(restored, "modified on disk since read (Save again to overwrite)")); -} - test "a Rename the server answered for this file alone, with other open files saying the name, is previewed and says so" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); @@ -5037,8 +5012,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")); @@ -5082,25 +5059,6 @@ test "a command or a terminal refused for room is refused before anything is log } } -test "an unsaved pane from a dump that says nothing of its disk asks at its first Save" { - if (comptime !pardes.hosted) return; - const gpa = testing.allocator; - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(testing.io, .{ .sub_path = "old.txt", .data = "disk\n" }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; - var path_buf: [4200]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "{s}/old.txt", .{dir}); - const p = try withFile(gpa, "x\n"); - defer p.deinit(); - const id = p.freeSlot().?; - const pane = try pardes.panes.File.restore(p, id, .{ .kind = .file, .tag = "", .body = "", .file = .{ .path = path, .content = "unsaved\n", .dirty = true } }); - try testing.expect(pane.file.?.disk_newer != null); - pardes.exec.saveFile(p, id); - try testing.expect(th.logHas(p, "modified on disk since read (Save again to overwrite)")); -} - test "a Mount of a socket path that is not there says no such socket, ENOENT" { if (comptime !pardes.hosted) return; const p = try withFile(testing.allocator, "x\n"); diff --git a/src/ninep/events.zig b/src/ninep/events.zig index 78788c3c..f24c6120 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -1305,27 +1305,23 @@ test "a Dump written and a Restore made are in the log, with their files" { var want: [64]u8 = undefined; try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "{s} /tmp/pardes.dump.zon\n", .{what})) != null); } - // The replacement's last says which serial each pane had. - const log = try freezeLog(restored); - defer restored.gpa.free(log.bytes); - var want: [64]u8 = undefined; - try testing.expect(std.mem.indexOf(u8, log.bytes, try std.fmt.bufPrint(&want, "restore /tmp/pardes.dump.zon\nrestored {d} {d}\n", .{ p.panes[0].?.serial, restored.panes[0].?.serial })) != null); - // ...and each column's: restoredcol <old> <new>. - try testing.expect(std.mem.endsWith(u8, log.bytes, try std.fmt.bufPrint(&want, "restoredcol {d} {d}\n", .{ pardes.layout.columnSerial(p, 0), pardes.layout.columnSerial(restored, 0) }))); + // Its panes and columns are the dump's, serials and all. + try testing.expectEqual(p.panes[0].?.serial, restored.panes[0].?.serial); + try testing.expectEqual(pardes.layout.columnSerial(p, 0), pardes.layout.columnSerial(restored, 0)); } -test "a Restore gives columns fresh serials, past every serial the old session handed out" { +test "after a Restore a new pane or column takes a serial past every one the old session handed out" { const p = try withFile(testing.allocator, "one\n"); defer p.deinit(); - const first = pardes.layout.columnSerial(p, 0); - // Serials spent on columns since deleted. - p.next_column_serial += 5; - const spent = p.next_column_serial; try dump.dumpState(p); + // Serials spent since the dump, on panes and columns since deleted. + p.next_serial += 5; + p.next_column_serial += 5; + const spent = p.next_serial; + const spent_col = p.next_column_serial; const restored = try dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); defer restored.deinit(); - for (0..restored.ncol) |c| try testing.expect(pardes.layout.columnSerial(restored, c) >= spent); - try testing.expect(pardes.layout.columnSerial(restored, 0) != first); + try testing.expect(restored.next_serial >= spent and restored.next_column_serial >= spent_col); } test "a pane named before it is announced has its new under that name, and no rename before it" { 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/pty.zig b/src/ninep/pty.zig index d80f7175..ee8c618e 100644 --- a/src/ninep/pty.zig +++ b/src/ninep/pty.zig @@ -86,7 +86,7 @@ pub fn writeCtl(p: *Pardes, req: Req, id: usize) Reply { if (comptime pardes.hosted) if (!apply and std.mem.eql(u8, line, "exec")) if (p.panes[id]) |pane| { const bin = pane.shell orelse p.shellBin(); var why: [320]u8 = undefined; - if (@import("../host_io.zig").Shell.refusal(bin[0..@min(bin.len, 200)], &why)) |refused| + if (pardes.tape.value(.shell, @import("../host_io.zig").Shell.refusal, .{ bin[0..@min(bin.len, 200)], &why })) |refused| return tree.failText(req.tag, E.NOENT, std.fmt.bufPrint(&p.fs.ename, "exec: {s}", .{refused}) catch "exec: no such shell"); }; if (!verb(p, id, line, apply)) return tree.failText(req.tag, E.INVAL, if (tooFewRows(line)) e_winsize_rows else if (outOfRange(line)) e_winsize_range else e_bad_pty_ctl); @@ -378,9 +378,11 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void { /// Answers a run whose command ended with `status`: the header and the /// output it printed. fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void { - const printed = pardes.panes.terminal.commandOutput(pane, p.gpa, output_rows) catch + // As the authority's emulator has it (taped): a screen-state replica's + // is its deltas', which a lagging client's can be states behind, its + // pins in pages it never had (ghostty's leftWrap overflowed on one). + const printed = pardes.tape.value(.prompt, printedBy, .{ pane, p.scratch.allocator() }) orelse return answer(p, slot, "error out of memory", .{}); - defer p.gpa.free(printed.text); // Keep the tail, cut at a line start so the first line kept is whole. var keep: []const u8 = printed.text; if (keep.len > output_cap) { @@ -409,6 +411,14 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void { answer(p, slot, "exit {s}", .{status}); } +pub const Printed = struct { text: []const u8, whole: bool }; + +/// What pane `pane`'s command printed (terminal.commandOutput), in `arena`. +fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed { + const out = pardes.panes.terminal.commandOutput(pane, arena, output_rows) catch return null; + return .{ .text = out.text, .whole = out.whole }; +} + /// The shell itself exited (a run's `exit 3`, or one typed): a run waiting /// on the command it ran answers with the shell's status and what it /// printed, and the log says `exit` before the pane's `del`. Unknown, the diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 7b107ea7..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; @@ -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..5bc24b08 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -659,6 +659,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, semantic: modal.Normal.Action) void t.select = false; // typed again by a macro, Esc is only helix's if (p.macro.replaying > 0) return; + tagline.exitHeader(p); // out of a header too, as Shift-Esc return exec.runBuiltin(p, .Last, p.active, "", null); }, .goto => |go| switch (go.target) { @@ -713,6 +714,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)))); @@ -1015,6 +1017,8 @@ pub fn handlePdfNormal(p: *Pardes, pane: *Pane, key: Key) void { if (result.page_changed) panes.Pdf.resetPageChrome(pane); switch (result.host) { .none => {}, + // typed again by a macro, Esc is only the page's + .last => if (p.macro.replaying == 0) exec.runBuiltin(p, .Last, p.active, "", null), .leader => { p.leader_on = true; p.leader_n = 0; diff --git a/src/panes.zig b/src/panes.zig index 51710d08..095e215f 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -1,6 +1,7 @@ const std = @import("std"); const vaxis = @import("vaxis"); const pardes = @import("pardes.zig"); +const views = @import("views.zig"); const layout = @import("layout.zig"); const config = @import("config.zig"); const Pardes = pardes.Pardes; @@ -8,7 +9,6 @@ const modal = @import("modal.zig"); const filesystem = @import("fs.zig"); const syntax = @import("syntax.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const limits = @import("memory.zig").limits; const builtins = @import("builtins.zig"); const effect_sources = @import("effect_sources.zig"); @@ -24,6 +24,18 @@ 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; the other views' share of it (views.zig). + pub const replica = .{ + .local = .{ "pv", "pv_known" }, + .derived = .{ "wrap_line", "wrap_col", "wrap_pad", "wrap_n", "body_wrapped", "context_rows", "body_rows", "body_visible_rows", "context_row_limit", "geometry_key", "geometry_len", "notices", "pan" }, + .authority = .{"fs"}, + .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, + // A picture's and a PDF's memory is their own allocators' (a dump + // loaded makes it there). + .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" }, .{ "terminal", "terminal" } }, + }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; @@ -87,6 +99,7 @@ pub const Pane = struct { pub const Cwd = union(enum) { none, inherited: *Pane, owned: []u8 }; pub const Focus = enum { body, tag }; + pub const SessionView = struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 }; terminal: ?*terminal.State = null, gpa: std.mem.Allocator, serial: u32 = 0, @@ -128,7 +141,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 +263,15 @@ 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`). + /// Digested and dumped, not made again: a key's move reads it before its + /// step's `sync` (Text.halfPageMove's scroll), and a core loaded from a + /// dump has had none (Replica.initDump). + view: SessionView = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -278,6 +305,22 @@ pub const Pane = struct { /// body layer reserves exactly `len` rows for them, the way it already /// reserves rows for sticky context headers. notices: Notices = .{}, + /// Each other view's share of this pane, while it is not live + /// (views.zig): its cursor, scroll and place in that view's layout. + pv: [views.max]views.PaneView = undefined, + /// The views `pv` holds a share for (a pane retired before a view came + /// has none of it). + pv_known: u8 = 0, + /// The view that last typed into it: a terminal shown at two sizes takes + /// that view's (Pardes.sync). + sized_by: u8 = 0, + /// A terminal shown in a view shorter than it (one that does not size + /// it): the rows its top is passed over by, so its cursor stays in + /// sight (terminal.panFor); 0 everywhere else. + pan: u16 = 0, + /// The view whose step a `.name` prompt is (tagline.draftName): only + /// that view's focus leaving the pane drops it. + prompt_view: u8 = 0, pub const MessagePhase = enum { entering, shown, lingering, leaving }; /// One message line's animation: falling in, shown, lingering after the @@ -292,6 +335,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 = .{}, @@ -511,10 +555,11 @@ pub const Pane = struct { pub fn scroll(pane: *Pane) i32 { if (pane.file) |f| return @intCast(f.scroll); if (comptime Pdf.enabled) if (pane.pdf) |pv| return @intCast(pv.text_scroll); - return pane.surfRow(terminal.gridOffset(pane)); + return pane.surfRow(terminal.gridOffset(pane) + pane.pan); } pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } { + 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 +568,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 +618,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 c7ae6704..edab4a58 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -61,6 +61,13 @@ 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 screen_state = if (terminal_panes) @import("screen_state.zig") else struct {}; +pub const views = @import("views.zig"); +pub const rollback = @import("rollback.zig"); +pub const replica = @import("replica.zig"); +pub const echo = @import("echo.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -165,6 +172,10 @@ pub const Turn = struct { /// a step. Nests: a directory listing that resolves each entry yields /// inside a yield, and only the outermost one lets go. pub fn yield(t: *Turn) void { + // A replica makes no syscall: each is a site of the tape's. One that + // is none stops a speculation and diverges a replay (it joins + // again), never the process (a window client's `Tty fish`, H17). + if (tape.replaying()) tape.stray(); const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; @@ -468,6 +479,10 @@ pub const Loc = struct { }; test { + _ = @import("rollback.zig"); + _ = @import("tape.zig"); + _ = @import("replica.zig"); + _ = @import("echo.zig"); _ = @import("modal.zig"); _ = @import("tagline.zig"); _ = @import("hunks.zig"); @@ -491,6 +506,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"); @@ -504,6 +520,9 @@ test { _ = @import("Finder.zig"); _ = @import("Tables.zig"); _ = @import("Handover.zig"); + _ = @import("walk.zig"); + _ = screen_state; + _ = @import("views.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -538,53 +557,8 @@ fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 { return if (len) |n| buf[0..n] else null; } -fn drainForAttach(p: *Pardes) ?AttachRequest { - while (p.nextEffect()) |effect| p.perform(effect); - return p.takeAttach(); -} - -test "Attach asks for a session and tears nothing down" { - if (comptime !can_attach) return; - const gpa = std.testing.allocator; - const p = try Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - - // Bare means whichever session is there, and that is an EMPTY name rather - // than an absent request: the frontend still has to go and look. - try std.testing.expect(p.executeBuiltinLine(0, "Attach")); - const bare = drainForAttach(p) orelse return error.NoAttachAsked; - try std.testing.expectEqualStrings("", bare.name); - try std.testing.expectEqual(@as(u8, 0), bare.pane); - - // ...and the named form carries its tail, the way `Theme <name>` does. - try std.testing.expect(p.executeBuiltinLine(0, "Attach work")); - const named = drainForAttach(p) orelse return error.NoAttachAsked; - try std.testing.expectEqualStrings("work", named.name); - try std.testing.expect(p.takeAttach() == null); // taken once, then gone - - // The word only ASKS. A core that tore itself down here could not be handed - // back intact when the connect fails, and that is the entire guarantee. - try std.testing.expect(!p.quit); - try std.testing.expect(p.panes[0] != null); -} - -test "Attach in a detached session: bare is wrong #args, a name no session has says so, and nothing is asked" { - if (comptime !can_attach) return; - const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .detached_session = true }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const pane = p.panes[0].?; - try std.testing.expect(p.executeBuiltinLine(0, "Attach")); - try std.testing.expect(drainForAttach(p) == null); - try std.testing.expectStringStartsWith(pane.msg[0..pane.msg_len], "wrong #args"); - try std.testing.expect(p.executeBuiltinLine(0, "Attach zz-no-such-session-qq")); - try std.testing.expect(drainForAttach(p) == null); - try std.testing.expectEqualStrings("Attach: zz-no-such-session-qq: no such session", pane.msg[0..pane.msg_len]); -} - -test "Detach asks the frontend to leave, and says so when there is nothing to leave" { - if (comptime !can_attach) return; // compiled out where nothing polls `takeAttach` — see `can_attach` +test "Detach asks the host to let its terminal go, and says so when there is nothing to leave" { + if (comptime !can_attach) return; // the word is compiled out elsewhere: see `can_attach` const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); defer p.deinit(); @@ -804,6 +778,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; @@ -811,6 +786,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. @@ -828,10 +804,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); @@ -882,7 +860,7 @@ test "Togglettymode builtin and keyboard chord share mode transitions including try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); try std.testing.expect(p.executeBuiltinLine(0, "Togglettymode")); try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); - p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } }); + p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); // The tag has its own mode: the body keeps tty while the tag is typed into. tagline.enterTag(p, pane); @@ -892,7 +870,7 @@ test "Togglettymode builtin and keyboard chord share mode transitions including try std.testing.expectEqual(Text.Mode.normal, pane.body.mode); try std.testing.expect(pane.focus == .body); tagline.enterTag(p, pane); - p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } }); + p.update(.{ .key = .{ .cp = p.tty_toggle, .ctrl = true } }); try std.testing.expectEqual(Text.Mode.tty, pane.body.mode); try std.testing.expect(pane.focus == .body); @@ -2063,14 +2041,6 @@ test "image dump restores source bytes renderer choices and exact custom tail" { pane.tag.own = try p.gpa.dupe(u8, " Keep Del"); try dump.dumpState(p); - var first_dump = try dump.readZon(gpa, p.dump_out.?, "image-first-dump"); - defer first_dump.deinit(); - const first = first_dump.value; - try std.testing.expect(first.panes[0].image.?.petscii); - try std.testing.expectEqual(dump.ImagePalette.terminal, first.panes[0].image.?.palette); - try std.testing.expect(!first.panes[0].image.?.ascii); - try std.testing.expectEqualStrings(" Keep Del", first.panes[0].tag_tail.?); - const restored = try dump.initFromDump(gpa, .{ .tty_only = true }, p.dump_out.?); defer restored.deinit(); const restored_pane = restored.panes[0].?; @@ -2081,18 +2051,11 @@ test "image dump restores source bytes renderer choices and exact custom tail" { try std.testing.expectEqualStrings(" Keep Del", tagline.curTail(restored_pane)); try std.testing.expectEqualSlices(u8, source, restored_pane.image.?.raw); + // And dumped again, its bytes still there. try dump.dumpState(restored); - var parsed = try dump.readZon(gpa, restored.dump_out.?, "image-redump"); - defer parsed.deinit(); - const redump = parsed.value; - const encoded = redump.panes[0].image.?.bytes_b64; - const decoded = try dump.decodeBytes(gpa, encoded); - defer gpa.free(decoded); - try std.testing.expectEqualSlices(u8, source, decoded); - try std.testing.expect(redump.panes[0].image.?.petscii); - try std.testing.expectEqual(dump.ImagePalette.terminal, redump.panes[0].image.?.palette); - try std.testing.expect(!redump.panes[0].image.?.ascii); - try std.testing.expectEqualStrings(" Keep Del", redump.panes[0].tag_tail.?); + const again = try dump.load(gpa, .{ .tty_only = true }, restored.dump_out.?, null); + defer again.deinit(); + try std.testing.expectEqualSlices(u8, source, again.panes[0].?.image.?.raw); } test "a Look on a directory does not type ls into an occupied terminal" { @@ -2394,25 +2357,30 @@ test "owned cwd EOF failure retains the pane and Del retries its close" { } test "owned cwd restore either copies the directory or preserves the old core" { + // No terminal: ghostty 1.4's snapshot decode leaks when an allocation + // inside it fails, and this fails every allocation of a Restore in turn + // (what pardes's own load made is freed, src/walk.zig rollback). var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{}); - const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true }); + const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true, .file = "/nonexistent/pardes-owned-cwd" }); defer p.deinit(); - p.setCwd(0, "/retained/terminal/directory"); + p.newScratchBelow(0); + const id = p.active; + try p.panes[id].?.setOwnedCwd("/retained/terminal/directory"); try dump.dumpState(p); - const original = p.panes[0].?; + const original = p.panes[id].?; var completed = false; for (0..256) |failure| { allocator.has_induced_failure = false; allocator.fail_index = allocator.alloc_index + failure; const replacement = dump.restore(p, p.dump_out.?, "test") catch { try std.testing.expect(allocator.has_induced_failure); - try std.testing.expectEqual(original, p.panes[0].?); + try std.testing.expectEqual(original, p.panes[id].?); try std.testing.expectEqualStrings("/retained/terminal/directory", original.cwdSlice()); continue; }; defer replacement.deinit(); - try std.testing.expectEqualStrings("/retained/terminal/directory", replacement.panes[0].?.cwdSlice()); - try std.testing.expectEqual(original, p.panes[0].?); + try std.testing.expectEqualStrings("/retained/terminal/directory", replacement.panes[id].?.cwdSlice()); + try std.testing.expectEqual(original, p.panes[id].?); if (!allocator.has_induced_failure) { completed = true; break; @@ -2903,7 +2871,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")); @@ -2967,8 +2937,7 @@ test "restored terminals keep monotonic nonzero pane identities" { const gpa = std.testing.allocator; const source = try Pardes.init(gpa, .{ .shells = 3 }); defer source.deinit(); - // Filter is a fresh presentation default, not emulator/dump state. Even a - // pane toggled off in the source session comes back with the default on. + // Filter is the pane's, and a dump keeps it: one toggled off comes back off. source.panes[1].?.tty_filter = false; try dump.dumpState(source); @@ -2981,7 +2950,7 @@ test "restored terminals keep monotonic nonzero pane identities" { for (restored.panes, 0..) |slot, id| { const pane = slot orelse continue; if (!pane.isTerminal()) continue; - try std.testing.expect(pane.tty_filter); + try std.testing.expectEqual(id != 1, pane.tty_filter); try std.testing.expect(pane.serial != 0); for (serials[0..n]) |seen| try std.testing.expect(pane.serial != seen); ids[n] = id; @@ -3629,6 +3598,7 @@ pub const Mouse = struct { pixel_x: f32, pixel_y: f32, metrics: RowMetrics, + place: Layer.Place = .{}, }; // sel indexing relies on left/middle/right being 0/1/2 pub const Button = enum { left, middle, right, wheel_up, wheel_down, wheel_left, wheel_right, none, back, forward }; @@ -3706,6 +3676,9 @@ pub const CellPixels = if (pdf_enabled) struct { h: u16 = 16, } else struct {}; +/// Said at a focus-in when the Host's `newer_build` answers yes (upgrade.zig). +pub const newer_build_words = "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"; + pub const Event = union(enum) { key: Key, mouse: Mouse, @@ -3750,12 +3723,15 @@ 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, }; -pub const attach_name_max = 256; - -pub const AttachRequest = struct { pane: u8, name: []const u8 }; +/// 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 }; @@ -3807,12 +3783,12 @@ pub const Effect = union(enum) { /// Write the build-time theme ring below the per-user config directory. /// The pane receives the completion/error message from the native host. dump_themes: struct { pane: u8 }, - attach: struct { pane: u8, name: Buf(attach_name_max) }, detach: struct { pane: u8 }, quit, 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; @@ -4366,6 +4342,10 @@ pub const Boot = enum { pub const Options = struct { tty_only: bool = false, + /// The first view's frontend is a window (`Pardes.windowed`), as a + /// window's build says: an option, taped with the rest, so that a + /// window's build replaying a terminal's session starts as it did. + windowed: bool = platform == .gui, /// The layout to boot, when a host means one outright rather than leaving /// it to be read off the options below. `--tty` names `.tty`; a core /// booted for a test names nothing and gets `.tty_shell`. @@ -4387,9 +4367,6 @@ pub const Options = struct { /// window opens one as big as that, Handover.zig). geometry: ?[]const u8 = null, nested: bool = false, - /// The core of a `--detach` session (detached/server.zig): it has no - /// terminal of its own, so Attach there is refused with why. - detached_session: bool = false, startup_config: ?[]const u8 = null, ninep_name: []const u8 = "", ninep_tcp: ?[]const u8 = null, @@ -4451,7 +4428,10 @@ pub const Pardes = struct { // layout: columns own x by weight; panes own y by vweight within a column. ncol: usize = 0, col_weight: [MAX_COLS]u64 = @splat(column_weight_unit), - col_panes: [MAX_COLS][MAX_PANES]usize = undefined, + // Zeroed, not undefined: a digest takes the whole of it, a release + // build's heap leaves its unused slots whatever was there before, and + // two replicas must agree on them (src/replica.zig). + col_panes: [MAX_COLS][MAX_PANES]usize = @splat(@splat(0)), col_n: [MAX_COLS]usize = @splat(0), /// Each column's tag and the workspace's: Texts like a pane's tag, with /// no prefix, showing a default until someone edits them. @@ -4587,6 +4567,9 @@ 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). + /// The live view's frontend is a window (Event.frontend): what it draws, + /// and the words it takes (builtins.zig `here`). It starts as + /// `Options.windowed` says. windowed: bool = false, /// The last plain B1 click, for acme's double-click (text.c:881, /// textselect: a second click at the same point within 500 ms). @@ -4737,11 +4720,28 @@ pub const Pardes = struct { /// a Restore builtin wants this dump loaded into the live session; the /// shell consumes it via takeRestore each frame (restore contents stay host-fed) restore_req: ?[]const u8 = null, - restore_buf: [1024]u8 = undefined, - attach_req: ?AttachRequest = null, - attach_buf: [attach_name_max]u8 = undefined, + restore_buf: [1024]u8 = @splat(0), surface: Surface = .{}, + /// A replica's predicted echo (src/echo.zig), drawn over its terminals: + /// presentation, its own, in no digest and no rollback copy. Null on a + /// session's own core. + echo: ?*echo.Echo = null, + /// The view the fields above are of: whose layout, focus, pointer and + /// panes' cursors and scroll (views.zig). `views_open` says which views + /// there are, one a window or terminal; each not live is in `parked`. + view: u8 = 0, + views_open: u8 = 1, + /// The open views kept for their clients' rejoin (views.away). + views_away: u8 = 0, + parked: [views.max]?*views.Parked = @splat(null), + /// The views no frontend here draws: a replica client's, which its own + /// process draws (src/replica.zig). A pump skips them. + undrawn: u8 = 0, + /// The Ctrl key into a terminal's tty mode and out (`--tty-toggle`): the + /// session's, taken from its options when it is made, so a dump carries + /// it to a replica, whose own options are its process's. + tty_toggle: u21 = config.tty_toggle_default, /// per-update scratch (paneCursorLines, selection text); reset each update scratch: std.heap.ArenaAllocator, /// The arena each frame's Surface text is built in, reset by `pump`. Hosts @@ -4756,6 +4756,34 @@ 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", + "echo", + // Drained by every pump before its digest; a replica's queues + // are its own. + "effects", "effects_head", "effects_len", "in_q", "in_head", "in_len", + // The views not live: written whenever one goes out, so the + // live one's slot is stale (views.zig). + "parked", "undrawn", + // How this process was started, and the last dump it made for + // its host to write: a session loaded elsewhere has its own. + "opts", "dump_out", + }, + .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), and + // the files of a host with no file system of its own. + .authority = .{ "host", "fs", "store", "recent", "fallback" }, + .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; @@ -4778,6 +4806,8 @@ pub const Pardes = struct { .pdf_gpa = opts.pdf_allocator orelse gpa, .tree_sitter_gpa = opts.tree_sitter_allocator orelse gpa, .opts = opts, + .windowed = opts.windowed, + .tty_toggle = opts.tty_toggle, .col_tags = @splat(.{ .what = .column, .gpa = gpa }), .global_tag = .{ .what = .workspace, .gpa = gpa }, .fs = .{}, @@ -4794,12 +4824,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.?; @@ -4932,7 +4976,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); } @@ -4954,7 +5000,10 @@ pub const Pardes = struct { /// Recent keeps it. fn noteClosed(p: *Pardes, pane: *const Pane) void { const d = recentPlace(pane) orelse return; - if (p.recent.get(d.path) == null and !recentKeeps(d.path)) return; + // The host asked first, always: what Recent holds is the + // authority's, and a step that asked only when it did not know the + // file would ask on one side and not the other. + if (!recentKeeps(d.path) and p.recent.get(d.path) == null) return; p.recent.closedSel(p.recentGpa(), d.path, d.line, d.col, d.aline, d.acol); } @@ -4989,13 +5038,45 @@ pub const Pardes = struct { }; } - /// The recent entry a closed jump names, by its path's hash. - pub fn recentByHash(p: *const Pardes, hash: u64) ?recent_files.Entry { - for (p.recent.entries.items) |e| if (std.hash.Wyhash.hash(0, e.path) == hash) return e; + /// The session's configuration directory and startup file, as a step + /// reads them: its process's options, so the authority's + /// (`tape.authority`); a replica's are its own process's. + pub fn configDir(p: *Pardes) ?[]const u8 { + return tape.authority(optsConfigDir, .{p}); + } + + pub fn startupPath(p: *Pardes) ?[]const u8 { + return tape.authority(optsStartupPath, .{p}); + } + + fn optsConfigDir(p: *Pardes) ?[]const u8 { + return p.opts.config_dir; + } + + fn optsStartupPath(p: *Pardes) ?[]const u8 { + return p.opts.startup_config_path; + } + + /// The recent entry a closed jump names, by its path's hash, its path + /// in scratch. Recent is the authority's (every window of its user + /// writes it): a replica has its answer (`tape.authority`). + pub fn recentByHash(p: *Pardes, hash: u64) ?recent_files.Entry { + return tape.authority(recentFound, .{ p, hash }); + } + + fn recentFound(p: *Pardes, hash: u64) ?recent_files.Entry { + for (p.recent.entries.items) |e| if (std.hash.Wyhash.hash(0, e.path) == hash) { + var found = e; + found.path = p.scratch.allocator().dupe(u8, e.path) catch return null; + return found; + }; return null; } 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); @@ -5024,6 +5105,7 @@ pub const Pardes = struct { slot.* = null; }; const gpa = p.gpa; + views.deinit(p); for (&p.col_tags) |*t| t.deinit(gpa); p.global_tag.deinit(gpa); p.registers.deinit(gpa); @@ -5056,6 +5138,8 @@ pub const Pardes = struct { /// shell feedback after write_dump: remember where the dump landed (the /// topbar grows a `Restore <path>` 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); } @@ -5083,19 +5167,13 @@ 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; } - /// the shell polls this each frame beside takeRestore: a pending Attach's - /// session and reporting pane, or null - pub fn takeAttach(p: *Pardes) ?AttachRequest { - const a = p.attach_req; - p.attach_req = null; - return a; - } - /// The workspace tag's height: one row while the setting keeps it, as /// acme's row tag is, zero once a native shell takes its commands over. /// Layout and hit testing read this rather than the constant, the way @@ -5150,6 +5228,9 @@ pub const Pardes = struct { for (p.panes, 0..) |slot, id| if (slot == pane) { if (watched) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = @intCast(id) } }); + // Its pty's end and its emulator are the authority's: a replica's + // speculation that closes a terminal waits for it. + if (pane.terminal != null) tape.poison("close a terminal"); ctlfs.events.noteRetire(p, pane); // ...and so do bytes still queued for the pty it no longer has. p.dropPendingWrite(id); @@ -5175,6 +5256,7 @@ pub const Pardes = struct { if (comptime pdf_enabled) if (pane.pdf) |*pv| panes.Pdf.deinitPane(p, pane, pv); if (pane.file) |*file| panes.File.deinit(p, pane, file); if (pane.ovl) |o| p.gpa.free(o.text); + views.tornDown(p, pane); pane.body.deinit(p.gpa); pane.tag.deinit(p.gpa); pane.input.deinit(p.gpa); @@ -5209,6 +5291,7 @@ pub const Pardes = struct { pane.serial = p.next_serial; p.rects[id] = .{}; p.panes[id] = pane; + views.installed(p, pane); panes.terminal.adoptSettings(p, pane); // /log hears of it once the update that made it ends, when it has // its file, name and place (`ctlfs.events.announce`). @@ -5449,6 +5532,8 @@ pub const Pardes = struct { /// The shell reports the spawned pane's working directory (and later cwd /// changes it observes, e.g. via /proc/<pid>/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; @@ -5477,7 +5562,39 @@ pub const Pardes = struct { return if (selected.len == 0) config.defaultShell() else selected; } + /// A shell a dead session left, taken in (Adopt): a terminal pane made + /// for it as the Tty word makes one, its greeting off (the shell is up + /// already), whose spawn the host answers with that shell's pty. Its id, + /// or null with no room. An input, as every host write into the core + /// is: a replica of the session makes the same pane. + pub fn adoptShell(p: *Pardes) ?u8 { + const taped = tape.enter(p, .adopt_shell); + defer tape.leave(p, taped); + var newest: u32 = 0; + for (p.panes) |slot| if (slot) |pn| { + newest = @max(newest, pn.serial); + }; + _ = p.executeBuiltinLine(p.active, "Tty"); + for (p.panes, 0..) |slot, id| if (slot) |pn| if (pn.serial > newest and pn.isTerminal()) { + pn.greet = false; + return @intCast(id); + }; + return null; + } + + /// A terminal's reader failed (no end of file: the host closed its pty): + /// its pane takes keys again, and why is said. + pub fn readerFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .reader_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); + const pane = p.panes[id] orelse return; + pane.body.mode = .normal; + p.reportError(id, "terminal reader", err); + } + pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { + const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); + defer tape.leave(p, taped); if (id < MAX_PANES) if (p.panes[id]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; @@ -5500,8 +5617,14 @@ pub const Pardes = struct { p.settings.shell.pending = false; } + /// A window's font, asked and told (this and the three below): in every + /// hosted build, not only a window's, as only a window's host calls + /// them and a terminal's replica of a window's session must step as + /// the window does. pub fn takeFontRequest(p: *Pardes) ?[]const u8 { - if (comptime !font_picker) return null; + if (comptime !hosted) 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; @@ -5515,7 +5638,9 @@ pub const Pardes = struct { effective_size_hundredths: u16, unit: config.Runtime.FontSizeUnit, ) bool { - if (comptime !font_picker) return false; + if (comptime !hosted) 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; @@ -5530,7 +5655,9 @@ pub const Pardes = struct { effective_size_hundredths: u16, unit: config.Runtime.FontSizeUnit, ) bool { - if (comptime !font_picker) return false; + if (comptime !hosted) 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, @@ -5546,7 +5673,9 @@ pub const Pardes = struct { } pub fn rejectFont(p: *Pardes) void { - if (comptime !font_picker) return; + if (comptime !hosted) 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; @@ -5573,11 +5702,92 @@ pub const Pardes = struct { /// Something changed the core without going through `update` -- a watched /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. + /// A view of its own for a new client (a second window, a terminal + /// `pardes --attach`ed), as the live one is laid out; null at + /// views.max. Not live until `toView`. + pub fn openView(p: *Pardes) ?u8 { + const taped = tape.enter(p, .open_view); + defer tape.leave(p, taped); + // Every view taken: one kept for a client that has not come back + // for it (views.away) goes first. ponytail: no timer lets one go; + // until its place is wanted it costs a parked slot, and a terminal + // it sized last keeps that size until another view types into it. + if (@popCount(p.views_open) == views.max and p.views_away != 0) { + views.close(p, @ctz(p.views_away)); + p.sync(); + } + // What was queued since the last pump (a leaving client's view + // closed, a 9P request's step) done first, as every client of the + // tape does it here: a join dumps the session next, and a core + // loaded from a dump has no queue (Replica.initDump). + p.settle(); + p.needs_frame = true; + return views.open(p); + } + + /// The client of view `k` gone: its view goes, the others stay. The + /// last one stays the session's own. + pub fn closeView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .close_view = k }); + defer tape.leave(p, taped); + views.close(p, k); + p.sync(); + // What its sync queued (a terminal it sized, sized again) done here, + // in its own unit on every side: the session's next pump is another + // unit, and a replica takes a copy only of a core with nothing + // queued (rollback.Copy.take). + p.settle(); + } + + /// The last client of a session no one else watches gone: its view + /// goes, and the session's own is laid out as it was (views.closeKeeping), + /// which the client's rejoin copies. Settled as `closeView` is. + pub fn leaveView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .leave_view = k }); + defer tape.leave(p, taped); + views.closeKeeping(p, k); + p.sync(); + p.settle(); + } + + /// The client of view `k` gone, to join again for it (an Upgrade's + /// rejoin, a divergence's): its view kept as it is (views.away). + pub fn awayView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .away_view = k }); + defer tape.leave(p, taped); + views.away(p, k); + } + + /// A client back for view `k`, kept for it (`awayView`): its own again, + /// as it left it; false when it was let go meanwhile. Settled as + /// `openView` is, for the join's dump next. + pub fn rejoinView(p: *Pardes, k: u8) bool { + const taped = tape.enter(p, .{ .rejoin_view = k }); + defer tape.leave(p, taped); + if (!views.isAway(p, k)) return false; + p.views_away &= ~(@as(u8, 1) << @intCast(k)); + p.settle(); + p.needs_frame = true; + return true; + } + + /// Into view `k`, before the input its client sent. + pub fn toView(p: *Pardes, k: u8) void { + if (k == p.view) return; + const taped = tape.enter(p, .{ .to_view = k }); + defer tape.leave(p, taped); + views.to(p, k); + } + pub fn invalidate(p: *Pardes) void { + const taped = tape.enter(p, .invalidate); + defer tape.leave(p, taped); 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); @@ -5594,6 +5804,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; @@ -5730,6 +5942,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); @@ -5751,6 +5965,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); } @@ -5763,9 +5979,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); @@ -5775,6 +5989,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. @@ -5792,11 +6013,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; @@ -5818,6 +6041,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; @@ -5860,6 +6085,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; @@ -5981,11 +6208,6 @@ pub const Pardes = struct { }, .theme_file => |t| if (v.watch_theme) |f| f(p.host.ctx, t.generation, t.on), .dump_themes => |d| if (v.dump_themes) |f| f(p.host.ctx, d.pane), - .attach => |a| { - const name = a.name.slice(); - @memcpy(p.attach_buf[0..name.len], name); - p.attach_req = .{ .pane = a.pane, .name = p.attach_buf[0..name.len] }; - }, .detach => |d| if (v.detach) |f| f(p.host.ctx) else @@ -5995,32 +6217,36 @@ 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; f(h.ctx, timeout); } + // Detached, nobody's key dismisses a message: they go on the clock, + // here in the step, where the clock read is taped (the host's wait + // did it, untaped, and a replay kept a message its session had let + // go). + Messages.expireUnattended(p); p.frames.mark_start = FrameStats.nowNs(); if (v.now) |clock| p.advance(clock(h.ctx)); - // What a 9P open wrote in a row goes in once the writes pause. - ctlfs.pane.flushPausedBatch(p); - // ...and what a /pager open was written before its close. - ctlfs.ctl.fillClosedPagers(p); - // ...and the last line of an open closed with no newline after it. - ctlfs.runClosedLines(p); - while (p.nextQueued()) |ev| p.update(ev); - while (p.nextEffect()) |e| p.perform(e); - turn.bump(&turn.epoch); + p.settle(); // A quitting frame has already freed what it would draw. if (p.quit) return; if (v.poll_frame) |f| f(h.ctx); @@ -6034,12 +6260,19 @@ pub const Pardes = struct { // A pty streaming into an unwatched session spent most of its time // drawing what nobody looked at. if (v.watched) |f| if (!f(h.ctx)) return; - _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); - p.frames.mark_update = FrameStats.nowNs(); - p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; - const surface = try p.render(p.frame_arena.allocator()); - p.frames.mark_render = FrameStats.nowNs(); - if (v.present) |f| f(h.ctx, surface); + // The other views first, each into its own window or terminal. + if (views.several(p)) try views.drawOthers(p, h); + // A replica client's view live (its input the last): its own + // process draws it. + if (p.undrawn & (@as(u8, 1) << @intCast(p.view)) == 0) { + _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept }); + p.frames.mark_update = FrameStats.nowNs(); + p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0; + const surface = try p.render(p.frame_arena.allocator()); + p.frames.mark_render = FrameStats.nowNs(); + if (v.present) |f| f(h.ctx, surface); + Stream.askAhead(p); + } // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. const skipped = p.present_skipped; @@ -6051,6 +6284,32 @@ pub const Pardes = struct { p.frames.record(FrameStats.nowNs(), owed or p.continuous(), skipped); } + /// What a pump does between its wait and its frame: the queued inputs + /// and the effects. Also the session at a step boundary, taken now (an + /// Upgrade's dump, upgrade.zig). + pub fn settle(p: *Pardes) void { + // What a 9P open wrote in a row goes in once the writes pause. + ctlfs.pane.flushPausedBatch(p); + // ...and what a /pager open was written before its close. + ctlfs.ctl.fillClosedPagers(p); + // ...and the last line of an open closed with no newline after it. + ctlfs.runClosedLines(p); + while (p.nextQueued()) |ev| p.update(ev); + while (p.nextEffect()) |e| p.perform(e); + turn.bump(&turn.epoch); + } + + /// A replica client's input settled on the authority (src/replica.zig): + /// a pump that waits on nothing, reads no clock and draws nothing, so + /// the client's own core can apply the same at once and expect the + /// authority's tape of it to read the same. The session's own pump + /// draws it after. + pub fn step(p: *Pardes) void { + const taped = tape.enter(p, .step); + defer tape.leave(p, taped); + p.settle(); + } + /// One request of the control filesystem, from whichever thread has the /// turn (`pardes.turn`). Not an event: it neither sweeps nor resets /// anything the editor's own step may be in the middle of using, so it @@ -6058,6 +6317,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)) { @@ -6096,6 +6357,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 @@ -6142,6 +6405,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 => {}, @@ -6210,6 +6474,7 @@ pub const Pardes = struct { .key => |key| { p.can_ask = true; defer p.can_ask = false; + p.typedInto(); p.handleKey(key); }, .mouse => |m| { @@ -6229,12 +6494,23 @@ pub const Pardes = struct { } } }, - .paste => |bytes| edit.applyPaste(p, bytes), + .paste => |bytes| { + p.typedInto(); + edit.applyPaste(p, bytes); + }, .command => |line| _ = p.executeBuiltinLine(p.active, line), .pinch => |scale| p.ov_pinch_scale = scale, .touch_scroll => |delta| p.ov_touch_scroll_delta = delta, - .focus => |focused| p.window_focused = focused, + .focus => |focused| { + p.window_focused = focused; + if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words); + }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), + .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; @@ -6462,7 +6738,7 @@ pub const Pardes = struct { if (header == null and pane.isTerminal() and pane.body.mode == .tty and pane.focus == .body and pane.prompt == .none) { p.leader_on = false; p.ctrl_w_pending = false; - if (key.ctrl and !key.alt and key.cp == p.opts.tty_toggle) + if (key.ctrl and !key.alt and key.cp == p.tty_toggle) return p.toggleTty(p.active); // With ProgramEsc on, bare Escape leaves only at a shell prompt // (Shift-Esc, above, always). @@ -6527,7 +6803,7 @@ pub const Pardes = struct { } return; } - const tty_toggle = key.ctrl and key.cp == p.opts.tty_toggle; + const tty_toggle = key.ctrl and key.cp == p.tty_toggle; if (pane.isTerminal() and tty_toggle) { return p.toggleTty(p.active); } @@ -6707,11 +6983,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; @@ -6729,10 +7008,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, @@ -7075,6 +7360,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) @@ -7250,9 +7537,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); @@ -7261,6 +7550,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); @@ -7269,9 +7566,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 { @@ -7442,10 +7755,12 @@ pub const Pardes = struct { if (i == p.jcur) cur = w; // survives -> lands at w; dies -> the next one does var kept = j; const live = if (j.serial == 0) false else if (p.panes[j.pane]) |pane| pane.serial == j.serial else false; - // A file's pane closed: its entry stays, closed, while the recent - // list knows the file, and a jump to it opens it again. + // A file's pane closed: its entry stays, closed, and a jump to it + // opens it again from Recent, or drops it when Recent has + // forgotten it (`jumpBy`: Recent is the authority's, read when + // it is wanted, not at every step). if (!live) { - if (j.path_hash == 0 or p.recentByHash(j.path_hash) == null) continue; + if (j.path_hash == 0) continue; kept.serial = 0; // Open again in some pane (a look, Recent): the entry is that // pane's, and closed only while no pane has the file. @@ -7545,12 +7860,13 @@ pub const Pardes = struct { // A closed file's entry: opened again at its dot, placed as a look // places it, and the entry is that pane from here on. if (j.serial == 0) { - const e = p.recentByHash(j.path_hash) orelse return; - // Deleted since: its entries go, it is said, and the step goes - // on to the next place, never stuck on one that cannot open. - if (!recentKeeps(e.path)) { + const found = p.recentByHash(j.path_hash); + // Deleted since, or Recent has forgotten it: its entries go (the + // first said), and the step goes on to the next place, never + // stuck on one that cannot open. + if (found == null or !recentKeeps(found.?.path)) { var said: [recent_path_cap + 32]u8 = undefined; - p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); + if (found) |e| p.setMessage(p.active, std.fmt.bufPrint(&said, "{s}: deleted, skipped", .{e.path}) catch "deleted, skipped"); const hash = j.path_hash; var w: usize = 0; var cur = p.jcur; @@ -7568,6 +7884,7 @@ pub const Pardes = struct { // round takes one file's entries away, so it ends. return p.jumpBy(delta); } + const e = found.?; const from = p.active; look.reopenAt(p, from, e.path, .{ .line = @max(1, j.line), .col = if (j.pdf) 0 else @max(1, j.col) }); const opened = p.panes[p.active] orelse return; @@ -7629,6 +7946,7 @@ pub const Pardes = struct { tagline.exitHeader(p); } p.reapPanes(); + if (views.several(p)) views.show(p); layout.compute(p); p.presentation.sync(p); p.trackJump(); @@ -7638,7 +7956,7 @@ pub const Pardes = struct { // hands it back to its body, and a name drafted there is dropped. if (id != p.active or p.header_focus) { pane.focus = .body; - if (pane.prompt == .name) pane.closePrompt(); + if (pane.prompt == .name and pane.prompt_view == p.view) pane.closePrompt(); } // A sweep over the tag is the tag's while it has the keyboard. const swept = pane.sel[sel_slot]; @@ -7656,6 +7974,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 @@ -7712,7 +8034,9 @@ pub const Pardes = struct { // A terminal's pty follows its pane down to a row, whatever the // resize; only a folded one keeps the size it had, and one not // placed yet the grid it was made with. - if ((cols != pane.cols or rows != pane.rows) and (r.h > pane.tag_rows or (pane.isTerminal() and !pane.collapsed and r.h > 0))) { + if ((cols != pane.cols or rows != pane.rows) and (r.h > pane.tag_rows or (pane.isTerminal() and !pane.collapsed and r.h > 0)) and + (!pane.isTerminal() or p.sizes(pane))) + { // doc panes have no pty/emulator grid to reflow; just record // the size so bodyText renders the right number of rows if (pane.isTerminal()) { @@ -7725,15 +8049,19 @@ pub const Pardes = struct { pane.rows = rows; if (pane.file) |*file| file.syntax_dirty = true; } + pane.pan = panes.terminal.panFor(p, pane, r); // A directory pane's entries follow its width, as acme's // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols); 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 // typed (panes.terminal.greeting). - if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (panes.terminal.greeting(pane)) { + if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (tape.value(.prompt, panes.terminal.greeting, .{pane})) { .wait => {}, .never => pane.greet = false, .now => { @@ -7743,6 +8071,28 @@ 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); + } + + /// The view typing goes into is the one its terminal is sized for + /// (`sizes`). + fn typedInto(p: *Pardes) void { + if (p.panes[p.active]) |pane| pane.sized_by = p.view; + } + + /// Whether the live view sizes terminal `pane`: a terminal shown at two + /// sizes takes the size of the view that typed into it last, and any + /// view's once that one is gone. The others show it at that size, a + /// shorter one passed down to its cursor (`Pane.pan`, terminal.panFor). + // ponytail: a panned view's pointer reports to a terminal's program and + // its kitty images are placed as the unpanned rows are. + fn sizes(p: *Pardes, pane: *Pane) bool { + if (pane.sized_by == p.view) return true; + if (views.isOpen(p, pane.sized_by)) return false; + pane.sized_by = p.view; + return true; } pub fn theme(p: *const Pardes) *const Theme { @@ -7848,6 +8198,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; @@ -7855,7 +8207,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; @@ -7905,18 +8260,16 @@ pub const Pardes = struct { return p.haptic; } + /// A frontend's tag hit, if what it names is still there: resolved on + /// the place it carries (Layer.Place), not on this process's frame. pub fn reprojectTagHit(p: *const Pardes, tag_hit: TagHit) ?TagHit { - for (p.surface.tagLayers()) |*layer| { - if (layer.rows == 0 or layer.kind != tag_hit.kind or layer.id != tag_hit.id or layer.serial != tag_hit.serial) continue; - if (layer.kind == .pane) { - if (layer.id >= p.panes.len) return null; - const pane = p.panes[layer.id] orelse return null; - if (pane.serial != layer.serial) return null; - } - if (layer.kind == .column and (layer.id >= p.ncol or p.col_serial[layer.id] != layer.serial)) return null; - return layer.tagHit(tag_hit.pixel_x, tag_hit.pixel_y, @floatFromInt(tag_hit.metrics.body_w), @floatFromInt(tag_hit.metrics.body_h), @floatFromInt(tag_hit.metrics.tagline_w), @floatFromInt(tag_hit.metrics.tagline_h)) orelse continue; + if (tag_hit.kind == .pane) { + if (tag_hit.id >= p.panes.len) return null; + const pane = p.panes[tag_hit.id] orelse return null; + if (pane.serial != tag_hit.serial) return null; } - return null; + if (tag_hit.kind == .column and (tag_hit.id >= p.ncol or p.col_serial[tag_hit.id] != tag_hit.serial)) return null; + return Layer.tagAgain(tag_hit); } /// The pane's first body row: under the tag when it is on top, over it @@ -7936,12 +8289,9 @@ pub const Pardes = struct { return layer.capacity(p.row_metrics); } - pub fn reprojectBodyHit(p: *const Pardes, body_hit: Mouse.BodyHit) ?Mouse.BodyHit { - for (&p.surface.body_layers) |*layer| { - if (layer.rows == 0 or layer.serial != body_hit.serial) continue; - return layer.bodyHit(body_hit.pixel_x, body_hit.pixel_y, @floatFromInt(body_hit.metrics.body_w), @floatFromInt(body_hit.metrics.body_h), @floatFromInt(body_hit.metrics.tagline_w), @floatFromInt(body_hit.metrics.tagline_h)); - } - return null; + /// A frontend's body hit, resolved on the place it carries. + pub fn reprojectBodyHit(_: *const Pardes, body_hit: Mouse.BodyHit) ?Mouse.BodyHit { + return Layer.bodyAgain(body_hit); } // ---- render: build the canonical surface (draw.zig) ---- @@ -8204,8 +8554,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 } }); @@ -8591,6 +8945,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; @@ -8709,6 +9066,21 @@ test "Shift-Esc backs out to the previous pane in every mode, never into raw" { try std.testing.expect(p.active != 0); } +test "Esc in a column or the workspace tag gives the keyboard back to a pane, as Shift-Esc does" { + if (platform == .web) return; + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }); + defer p.deinit(); + p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a pane to go back to + for ([_]?usize{ 0, null }) |column| { + tagline.enterHeader(p, column); + try std.testing.expect(tagline.focusedHeader(p) != null); + const was = p.active; + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(tagline.focusedHeader(p) == null); + try std.testing.expect(p.active != was); + } +} + test "window mode moves an outline and focuses it on Enter, Esc or a label" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 30 }); defer p.deinit(); @@ -9042,7 +9414,7 @@ test "raw tty forwards Ctrl letters and former global shortcuts to the child" { _ = drainWrites(p, &buf); edit.setYank(p, "register text"); for ('a'..'z' + 1) |letter| { - if (letter == p.opts.tty_toggle) continue; // editor/TTY mode shortcut + if (letter == p.tty_toggle) continue; // editor/TTY mode shortcut if (letter == 'v') continue; // the window's paste, asserted just below p.update(.{ .key = .{ .cp = @intCast(letter), .ctrl = true } }); const expected = [_]u8{@intCast(letter - 'a' + 1)}; diff --git a/src/pdf.zig b/src/pdf.zig index 18037bb7..262af8b9 100644 --- a/src/pdf.zig +++ b/src/pdf.zig @@ -700,9 +700,14 @@ test "oriented selection containment delegates to MuPDF quad geometry" { } pub const Document = struct { - handle: *c.pardes_pdf_document, + /// None in a document loaded from a dump until its PDF is opened again + /// (pdf_view.reopen). + handle: ?*c.pardes_pdf_document = null, 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..0745a6fc 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, @@ -902,12 +906,15 @@ pub const State = if (enabled) struct { if (page) |selected_page| state.invalidateRaster(selected_page); } - pub fn cancelChrome(state: *@This(), gpa: std.mem.Allocator) void { + /// Drops the selection and the search; true when there was one. + pub fn cancelChrome(state: *@This(), gpa: std.mem.Allocator) bool { + const had = state.selection != null or state.search_query.len > 0; state.clearDrag(); state.clearSelection(gpa); - if (state.search_query.len == 0) return; + if (state.search_query.len == 0) return had; state.dropSearchQuery(gpa); state.invalidateRaster(state.page); + return had; } fn invalidatePage(state: *@This(), gpa: std.mem.Allocator) void { @@ -1226,6 +1233,7 @@ pub const ScrollResult = struct { active_page: usize }; pub const CellPixels = struct { w: u16, h: u16 }; pub const NormalHost = enum { none, + last, leader, command_line, search, @@ -2472,7 +2480,11 @@ pub fn applyNormal( if (comptime !enabled) return .{}; var result: NormalResult = .{}; switch (semantic) { - .escape => state.cancelChrome(gpa), + // Esc drops a selection or a search the page shows, else goes back + // a pane (text-only, the page shows neither). + .escape => if (!state.cancelChrome(gpa) or !native_images) { + result.host = .last; + }, .move => |move| switch (move.motion) { .down => result.page_changed = moveRows( state, @@ -2836,6 +2848,20 @@ pub fn deinitPane(core: *pardes.Pardes, pane: *pardes.Pane, state: *State) void state.deinit(core.pdf_gpa); } +/// A PDF pane loaded from a dump given its document again (MuPDF's, so +/// never in the dump) from its file; one changed since is laid out anew. +pub fn reopen(core: *pardes.Pardes, pane: *pardes.Pane) !void { + if (comptime !enabled) return; + const state = &(pane.pdf orelse return); + const local = filesystem.localPath(state.path) orelse return error.NonLocalPath; + const bytes = try filesystem.readFile(core.pdf_gpa, local); + defer core.pdf_gpa.free(bytes); + state.document = try Document.openBytes(bytes); + if (state.document.pages != state.page_count or fileKey(state.path, bytes) != state.file) { + try state.reload(core.pdf_gpa); + } +} + pub fn reloadPane( core: *pardes.Pardes, pane: *pardes.Pane, diff --git a/src/regexp.zig b/src/regexp.zig index 6d281859..6c858dea 100644 --- a/src/regexp.zig +++ b/src/regexp.zig @@ -2,7 +2,6 @@ //! sam searches (editors/acme/regx.c). `addr` (src/ninep/addr.zig) and //! normal mode's `s` and `S` (src/normal.zig) both call it. const std = @import("std"); -const builtin = @import("builtin"); const mvzr = @import("mvzr"); /// A compiled pattern and how to run it. @@ -57,8 +56,11 @@ pub const Regex = struct { end_hint: struct { ptr: usize = 0, len: usize = 0, start: usize = 0, hi: usize = 0, end: usize = 0 } = .{}, /// Timed on this machine's ReleaseSafe build: 10M steps of the worst - /// patterns take 45-80 ms, and a Debug build is ten times slower. - pub const budget: u64 = if (builtin.mode == .debug) 4_000_000 else 32_000_000; + /// patterns take 45-80 ms. One budget in every build mode: what a + /// search finds is the core's state, and a Debug replica of a + /// ReleaseSafe session must find what the session does (a Debug build + /// is ten times slower at it, about two seconds). + pub const budget: u64 = 32_000_000; pub const e_long = std.fmt.comptimePrint("bad regular expression: longer than mvzr's {d} operations (about {d} pattern characters)", .{ max_ops, max_ops }); pub const e_anchor = "bad regular expression: in a pattern with \\n, ^ can only come first and $ only just before a \\n"; @@ -555,6 +557,16 @@ test "a search that would backtrack without end runs out of budget, promptly" { try std.testing.expectError(error.TooSlow, chain.find(&line, 0, line.len, line.len)); } +test "a search's answer is every build mode's" { + // `\s*(\w+)\s*=` over a line of letters costs more than linear: over + // 200 of them a Debug build's old budget (4M steps) ran out, TooSlow, + // where a release build's (32M) answered no match, so a Debug replica + // of a ReleaseSafe session diverged from it on one search. + var line: [200]u8 = @splat('a'); + var rx = try Regex.compile("\\s*(\\w+)\\s*="); + try std.testing.expectEqual(@as(?Regex.Match, null), try rx.find(&line, 0, line.len, line.len)); +} + test "the match is the leftmost, however long the line" { // A window cut into the identifier would find `x... =` from its middle. const text = "let " ++ &@as([100]u8, @splat('x')) ++ " = 1"; diff --git a/src/replica.zig b/src/replica.zig new file mode 100644 index 00000000..8cacb075 --- /dev/null +++ b/src/replica.zig @@ -0,0 +1,4248 @@ +//! A session's replicas (docs/next-daemon.md, phase 5 part A): the authority +//! orders every input and records what its core consumed (src/tape.zig); +//! each replica runs a core of its own, applies its own inputs at once, and +//! takes the authority's stream as the truth. +//! +//! A replica's state is the confirmed position plus its own pending inputs, +//! applied on top (a speculation, recorded as the authority would record +//! it). For each unit of the authority's stream, in order: +//! - its own pending head, recorded the same (inputs, host calls and +//! answers): the head goes, nothing is redone (the fast path); +//! - else the rollback copy is put back (src/rollback.zig), the confirmed +//! units since it are replayed, then this one, and the pending inputs +//! are applied again. +//! A speculation that reaches a host call or a syscall (a Host function, +//! a `tape.site`) or an emulator (the authority's) stops: the copy is put +//! back and the input waits for the authority's answer, one RTT late but +//! never wrong. Clock and environment reads (`tape.value`) are made here, +//! and the comparison catches a guess the authority did not share. +//! +//! No transport: messages are bytes in and out. A client's input is its +//! id, a sequence number and the event as detached/wire.zig frames it; a +//! unit is the input's origin (0 for the authority's own: a pty's output, a +//! tick), its sequence number and the authority's tape of it. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const tape = @import("tape.zig"); +const rollback = @import("rollback.zig"); +const walk = @import("walk.zig"); +const wire = @import("detached/wire.zig"); +const screen_state = @import("screen_state.zig"); +const c_heap = @import("c_heap"); +const terminal = pardes.panes.terminal; +const Allocator = std.mem.Allocator; +const Pardes = pardes.Pardes; + +const checked = builtin.mode == .debug or builtin.mode == .safe; +const head_len = 6; // origin or client: u16le, seq: u32le +/// A `stamp` frame (wire.zig): its header and the two clocks. +const stamp_len = wire.header_len + 16; +/// Confirmed units fast-pathed under one copy before it is taken afresh: +/// a rollback replays them all. +const rebase_after = 32; + +fn header(buf: []u8, who: u16, seq: u32) void { + std.mem.writeInt(u16, buf[0..2], who, .little); + std.mem.writeInt(u32, buf[2..6], seq, .little); +} + +/// A unit's head: its origin and seq, then the authority's clocks when it +/// was made (realtime and monotonic, ns), which a replica's guesses of the +/// session's clock start from. +const unit_head = head_len + 16; + +pub const Unit = struct { origin: u16, seq: u32, real_ns: i64 = 0, mono_ns: i64 = 0, tape: []const u8 }; + +pub fn parseUnit(msg: []const u8) ?Unit { + if (msg.len < unit_head) return null; + return .{ + .origin = std.mem.readInt(u16, msg[0..2], .little), + .seq = std.mem.readInt(u32, msg[2..6], .little), + .real_ns = std.mem.readInt(i64, msg[6..14], .little), + .mono_ns = std.mem.readInt(i64, msg[14..22], .little), + .tape = msg[unit_head..], + }; +} + +fn clockNs(id: std.c.clockid_t) i64 { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(id, &ts) != 0) return 0; + return @as(i64, ts.sec) * std.time.ns_per_s + ts.nsec; +} + +fn unitHeader(buf: []u8, origin: u16, seq: u32) void { + header(buf, origin, seq); + std.mem.writeInt(i64, buf[6..14], clockNs(.REALTIME), .little); + std.mem.writeInt(i64, buf[14..22], clockNs(.MONOTONIC), .little); +} + +pub const Input = struct { client: u16, seq: u32, event: pardes.Event, stamp: ?[2]i64 = null }; + +/// A client's input message: its head, a `stamp` frame (when its client +/// guessed the session's clocks), and its event's frame, which the event +/// borrows. +pub fn parseInput(msg: []const u8) ?Input { + if (msg.len < head_len) return null; + var rest = msg[head_len..]; + var f = (wire.framed(rest) catch return null) orelse return null; + var stamp: ?[2]i64 = null; + if (f.tag == @backingInt(wire.ClientTag.stamp)) { + stamp = (wire.decodeClient(f.tag, f.payload) catch return null).stamp; + rest = rest[f.total..]; + f = (wire.framed(rest) catch return null) orelse return null; + } + const ev = switch (wire.decodeClient(f.tag, f.payload) catch return null) { + .event => |ev| ev, + else => return null, + }; + return .{ .client = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .event = ev, .stamp = stamp }; +} + +/// An input's stamp as the authority takes it: the client's guess of its +/// clocks, so the guess its speculation recorded is the step's own; none +/// from a client a second or more off (a stall, a bad round trip), whose +/// step reads the clocks here. +fn stampOf(in: Input) ?tape.Guess { + const s = in.stamp orelse return null; + if (@abs(s[0] - clockNs(.REALTIME)) >= std.time.ns_per_s or @abs(s[1] - clockNs(.MONOTONIC)) >= std.time.ns_per_s) return null; + return .{ .real_ns = s[0], .mono_ns = s[1], .gmtoff = gmtoff() }; +} + +/// The core whose tape is `t`, stepped: tape.on is process-wide, and so +/// are syntax.zig's held trees, so one process can step several. +const Stepping = struct { + tape: ?*tape.Tape, + trees: ?*pardes.syntax.HeldTrees, + + fn begin(t: *tape.Tape, epoch: ?*rollback.Epoch) Stepping { + const was: Stepping = .{ .tape = tape.on, .trees = pardes.syntax.held_trees }; + tape.on = t; + rollback.Epoch.select(epoch); + return was; + } + + fn end(was: Stepping) void { + tape.on = was.tape; + pardes.syntax.held_trees = was.trees; + } +}; + +fn nowNs() u64 { + return pardes.FrameStats.nowNs(); +} + +/// The `p`th percentile of `samples` (sorted here); 100 is the worst. +fn pctOf(samples: []u64, p: usize) u64 { + if (samples.len == 0) return 0; + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return samples[@min(samples.len - 1, samples.len * p / 100)]; +} + +// ---- the authority ---- + +pub const Authority = struct { + gpa: Allocator, + t: tape.Tape, + core: *Pardes, + host: pardes.Host, + out: std.ArrayListUnmanaged(u8) = .empty, + seq: u32 = 0, + /// Each client's view (src/views.zig): its own layout, focus and + /// cursors over the session's text. The first client has the session's. + view_of: [64]u8 = @splat(0), + /// Steps of its own taken since the last unit (`output`): one unit, + /// `pending`, sent before the next input it steps and at a frame. + batched: u32 = 0, + + /// A session booted with `opts`; `unit` is its first, which a replica + /// is made from. + /// `screens`: its terminals cross as screen state (`Screens`), not as + /// their pty's bytes. + pub fn init(gpa: Allocator, options: pardes.Options, host: pardes.Host, screens: bool) !*Authority { + // A replay gives its core every allocator (tape.zig), and a digest + // tells an allocator from none. + var opts = options; + opts.image_allocator = opts.image_allocator orelse gpa; + opts.pdf_allocator = opts.pdf_allocator orelse gpa; + opts.tree_sitter_allocator = opts.tree_sitter_allocator orelse gpa; + opts.frame_allocator = opts.frame_allocator orelse gpa; + const a = try gpa.create(Authority); + errdefer gpa.destroy(a); + a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + a.t.screen_state = screens; + // One word a digest: every pty read is a unit. + a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; + const was = Stepping.begin(&a.t, null); + defer was.end(); + a.core = try Pardes.init(gpa, opts); + a.core.host = host; + a.core.pump(host) catch {}; + return a; + } + + pub fn deinit(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.deinit(); + was.end(); + tape.release(&a.t); + a.out.deinit(a.gpa); + a.gpa.destroy(a); + } + + /// What was recorded since the last unit, as one (the boot's, a setup's + /// done straight on the core). + pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { + a.out.clearRetainingCapacity(); + try a.out.appendNTimes(a.gpa, 0, unit_head); + unitHeader(a.out.items, origin, seq); + try a.out.appendSlice(a.gpa, tape.taken(&a.t)); + tape.clearTaken(&a.t); + return a.out.items; + } + + /// An input of the authority's own (a pty's output, a tick, a setup's + /// command), recorded into the unit being made. + pub fn apply(a: *Authority, ev: pardes.Event) void { + const was = Stepping.begin(&a.t, null); + a.core.update(ev); + a.core.pump(a.host) catch {}; + was.end(); + } + + /// One of the authority's own inputs, as its unit. + pub fn local(a: *Authority, ev: pardes.Event) ![]const u8 { + std.debug.assert(a.batched == 0); // `pending` first: units keep the order steps took + a.apply(ev); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// A step of its own (a pty's output) into the open batch: under a + /// flood a replica gets a unit a frame, not one a read (each unit of the + /// authority's rolls back a replica's pending inputs), and its digest + /// once a unit. + pub fn output(a: *Authority, ev: pardes.Event) void { + a.t.no_digest = true; + defer a.t.no_digest = false; + a.apply(ev); + a.batched += 1; + } + + /// The open batch as a unit, or null: what the host sends before it + /// steps any other input, and once a frame. + pub fn pending(a: *Authority) !?[]const u8 { + if (a.batched == 0) return null; + a.batched = 0; + const was = Stepping.begin(&a.t, null); + tape.digestHere(&a.t); + was.end(); + a.seq += 1; + return try a.unit(0, a.seq); + } + + /// A view of its own for `client`, as its unit; which it is. + pub fn openView(a: *Authority, client: u16) !struct { view: u8, unit: []const u8 } { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse { + was.end(); + return error.TooManyViews; + }; + a.core.pump(a.host) catch {}; + was.end(); + a.view_of[client] = k; + a.seq += 1; + return .{ .view = k, .unit = try a.unit(0, a.seq) }; + } + + /// Client `client`'s view gone (its link ended), as its unit. + pub fn closeView(a: *Authority, client: u16) ![]const u8 { + std.debug.assert(a.batched == 0); + const was = Stepping.begin(&a.t, null); + a.core.closeView(a.view_of[client]); + was.end(); + a.seq += 1; + return a.unit(0, a.seq); + } + + /// One client's input, applied in the order it came, in its view: the + /// unit every replica takes. + pub fn take(a: *Authority, msg: []const u8) ![]const u8 { + std.debug.assert(a.batched == 0); + const in = parseInput(msg) orelse return error.BadInput; + const was = Stepping.begin(&a.t, null); + a.core.toView(a.view_of[in.client]); + a.t.stamp = stampOf(in); + a.core.update(in.event); + a.core.step(); + a.t.stamp = null; + was.end(); + return a.unit(in.client, in.seq); + } +}; + +// ---- terminals as screen state (src/screen_state.zig) ---- +// +// A message of either side is u8 pane, u32 the pane's serial (a slot's +// next pane is another), u8 kind, and the kind's bytes: +// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for, +// or a step read them; +// to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a +// request for rows (u64 lo, u64 hi, absolute); kind 3, a +// delta refused: start this terminal afresh. +// In the authority's stream to a peer, before it steps any input but a +// pty's output, and once a frame: its own steps since the last unit, as one +// (`Authority.pending`), then what the peer is owed of the terminals +// (`Screens.flush`). After a step, before its unit: the rows it read that +// the peer holds as holes (`Screens.fill`, then `filled`). A replica takes +// them in that order between units (`Replica.screen`), so a step it replays +// reads the terminal its authority's step read. + +const kind_delta = 1; +const kind_rows = 2; +const kind_ack = 1; +const kind_reset = 3; + +const ScreenMsg = struct { pane: u8, serial: u32, kind: u8, payload: []const u8 }; + +fn parseScreen(msg: []const u8) ?ScreenMsg { + if (msg.len < 6) return null; + return .{ .pane = msg[0], .serial = std.mem.readInt(u32, msg[1..5], .little), .kind = msg[5], .payload = msg[6..] }; +} + +fn writeScreenHead(out: *std.ArrayListUnmanaged(u8), gpa: Allocator, pane: u8, serial: u32, kind: u8) !void { + var head: [6]u8 = undefined; + head[0] = pane; + std.mem.writeInt(u32, head[1..5], serial, .little); + head[5] = kind; + try out.appendSlice(gpa, &head); +} + +/// The authority's side of its replicas' terminals: a track each terminal +/// pane, and what each peer was sent of it. +pub const Screens = struct { + gpa: Allocator, + enc: screen_state.Encoder, + tracks: [pardes.MAX_PANES]?Tracked = @splat(null), + peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, + free_peers: std.ArrayListUnmanaged(u16) = .empty, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds: what each flush spent encoding, for every peer. + encode_ns: u64 = 0, + + const Tracked = struct { serial: u32, state: *terminal.State, track: screen_state.Track }; + const PeerOf = struct { serial: u32 = 0, peer: screen_state.Peer = .{} }; + + pub fn init(gpa: Allocator) Screens { + return .{ .gpa = gpa, .enc = .init(gpa) }; + } + + /// `core` is the session's, its terminals still there. + pub fn deinit(s: *Screens, core: *Pardes) void { + for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| tr.track.deinit(s.gpa, if (s.live(core, id, tr)) &tr.state.vt else null); + for (s.peers.items) |p| { + for (p) |*pp| pp.peer.deinit(s.gpa); + s.gpa.destroy(p); + } + s.peers.deinit(s.gpa); + s.free_peers.deinit(s.gpa); + s.out.deinit(s.gpa); + s.enc.deinit(); + } + + fn live(_: *const Screens, core: *Pardes, id: usize, tr: *const Tracked) bool { + const pane = core.panes[id] orelse return false; + return pane.serial == tr.serial and pane.terminal == tr.state; + } + + /// A new peer, with nothing yet: its first delta of each terminal + /// starts it afresh. + pub fn addPeer(s: *Screens) !u16 { + if (s.free_peers.pop()) |again| return again; + const p = try s.gpa.create([pardes.MAX_PANES]PeerOf); + p.* = @splat(.{}); + try s.peers.append(s.gpa, p); + return @intCast(s.peers.items.len - 1); + } + + /// Peer `peer` gone: what it was sent forgotten, its number free for + /// the next `addPeer` (a socket's clients come and go). + pub fn dropPeer(s: *Screens, peer: u16) void { + for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa); + s.peers.items[peer].* = @splat(.{}); + s.free_peers.append(s.gpa, peer) catch {}; + } + + /// What `peer` is owed of every terminal, each a message onto `out` + /// (owned): nothing for one it has as it is, or with `bound` in flight. + pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void { + const t0 = nowNs(); + defer s.encode_ns += nowNs() - t0; + for (core.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + const tr = try s.track(core, id); + const pp = &s.peers.items[peer][id]; + if (pp.serial != pane.serial) { + pp.peer.reset(); + pp.serial = pane.serial; + } + var marks: [terminal.marks_len]u8 = undefined; + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_delta); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + if (!try screen_state.encode(&s.enc, &tr.track, &pp.peer, &state.vt, state.changes, terminal.marksOf(state, &marks), &w.writer)) continue; + try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); + } + } + + /// Rows a peer holds as holes of every terminal a step read (its rows, + /// history and all: text mode over a shell), each a rows message onto + /// `out` (owned): sent after the step and before its unit, so the + /// replica's replay reads what the authority's step read. A history row + /// never changes, so each goes once. Then `filled`. + // ponytail: a step that reads a terminal's rows reads all of them (its + // motion surface), so the first after a flood sends its whole history. + pub fn fill(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void { + for (core.panes, 0..) |slot, id| { + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + if (!state.read) continue; + const pp = &s.peers.items[peer][id]; + if (pp.serial != pane.serial or pp.peer.holes.items.len == 0) continue; + const tr = try s.track(core, id); + for (pp.peer.holes.items) |hole| { + var lo = hole[0]; + while (lo < hole[1]) { + const hi = @min(hole[1], lo + 512); + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_rows); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + try screen_state.encodeRows(&s.enc, &tr.track, &state.vt, lo, hi, &w.writer); + try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); + lo = hi; + } + } + pp.peer.holes.clearRetainingCapacity(); + } + } + + /// Every peer is filled: the steps' reads are forgotten. + pub fn filled(_: *Screens, core: *Pardes) void { + for (core.panes) |slot| if (slot) |pane| if (pane.terminal) |state| { + state.read = false; + }; + } + + fn track(s: *Screens, core: *Pardes, id: usize) !*Tracked { + const pane = core.panes[id].?; + if (s.tracks[id]) |*tr| { + if (s.live(core, id, tr)) return tr; + // Its pane closed (its emulator, the pin with it, gone). + tr.track.deinit(s.gpa, null); + } + s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = .init(s.gpa) }; + return &s.tracks[id].?; + } + + /// A peer's message (`Replica.screen`'s, `Replica.screenRequest`'s): + /// an answer for it (owned), or null. + pub fn take(s: *Screens, core: *Pardes, peer: u16, msg: []const u8) !?[]u8 { + const m = parseScreen(msg) orelse return null; + const pane = core.panes[m.pane] orelse return null; + if (pane.serial != m.serial) return null; + const state = pane.terminal orelse return null; + const pp = &s.peers.items[peer][m.pane]; + if (pp.serial != m.serial) return null; + switch (m.kind) { + kind_ack => if (m.payload.len == 4) pp.peer.ack(std.mem.readInt(u32, m.payload[0..4], .little)), + kind_reset => pp.peer.reset(), + kind_rows => { + if (m.payload.len != 16) return null; + const tr = try s.track(core, m.pane); + s.out.clearRetainingCapacity(); + try writeScreenHead(&s.out, s.gpa, m.pane, m.serial, kind_rows); + var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); + defer s.out = w.toArrayList(); + try screen_state.encodeRows(&s.enc, &tr.track, &state.vt, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); + return try s.gpa.dupe(u8, w.written()); + }, + else => {}, + } + return null; + } +}; + +// ---- a replica ---- + +pub const Stats = struct { + inputs: u32 = 0, + /// Applied at once and clean. + speculated: u32 = 0, + /// Stopped at a host call, a syscall or an emulator: waits one RTT. + stopped: u32 = 0, + /// Not tried: an event of the view's (a resize, the pointer), phase 4's. + unspeculated: u32 = 0, + /// The authority's unit was the pending head, recorded the same. + fast: u32 = 0, + /// The copy put back: the head recorded otherwise (a guess of the clock + /// the authority did not share), or another's unit first. + mispredicted: u32 = 0, + foreign: u32 = 0, + rebases: u32 = 0, + units: u32 = 0, + /// Nanoseconds: one speculative apply, a copy taken, a rollback (the + /// copy put back, the confirmed units replayed, the pending reapplied). + spec_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_ns: std.ArrayListUnmanaged(u64) = .empty, + rollback_ns: std.ArrayListUnmanaged(u64) = .empty, + copy_bytes: u64 = 0, + copies: u32 = 0, + /// Nanoseconds: a terminal's delta applied. + apply_ns: std.ArrayListUnmanaged(u64) = .empty, + /// What stopped them (`Tape.poisoned_by`), a count a name. + stops: [16]Stop = @splat(.{}), + + const Stop = struct { name: []const u8 = "", n: u32 = 0 }; + + fn stop(s: *Stats, name: []const u8) void { + for (&s.stops) |*st| if (st.n == 0 or std.mem.eql(u8, st.name, name)) { + st.name = name; + st.n += 1; + return; + }; + } + + /// `name:n,...` (spaces as dashes), or `-`. + fn writeStops(s: *const Stats, w: *std.Io.Writer) !void { + var any = false; + for (s.stops) |st| if (st.n > 0) { + if (any) try w.writeByte(','); + any = true; + for (st.name) |ch| try w.writeByte(if (ch == ' ') '-' else ch); + try w.print(":{d}", .{st.n}); + }; + if (!any) try w.writeByte('-'); + } + + pub fn deinit(s: *Stats, gpa: Allocator) void { + s.spec_ns.deinit(gpa); + s.copy_ns.deinit(gpa); + s.rollback_ns.deinit(gpa); + s.apply_ns.deinit(gpa); + } +}; + +/// `terminal`: its speculation read a terminal or moved one's view (or a +/// terminal had the keyboard), so what it did depends on the emulator. +const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, guess: ?tape.Guess = null }; + +/// A terminal's view as the authority's was once `after` of the units +/// since the copy were taken (`Replica.since`): a delta's comes after the +/// units it shows, so those before it are not replayed over it. +const View = struct { at: ?usize = null, after: usize = 0 }; + +pub const Replica = struct { + gpa: Allocator, + id: u16, + /// Its own view of the session (Authority.openView's). + view: u8 = 0, + /// The core's allocator; its tapes and the engine's own lists are not. + epoch: rollback.Epoch = .{}, + t: tape.Tape, + copy: rollback.Copy = .{}, + pending: std.ArrayListUnmanaged(Pending) = .empty, + /// pending[0..applied] are applied on top of the confirmed position. + applied: usize = 0, + /// The pending input that waits for the authority (it stopped at a host + /// call, or is the view's), and those after it with it. + blocked: ?u32 = null, + /// Confirmed units fast-pathed since the copy was taken. + since: std.ArrayListUnmanaged([]u8) = .empty, + /// Pending inputs to apply again (after a rollback). + dirty: bool = false, + next_seq: u32 = 1, + stats: Stats = .{}, + diverged: ?[]const u8 = null, + out: std.ArrayListUnmanaged(u8) = .empty, + /// Nanoseconds of the rollback being made, until its reapply. + recovering: ?u64 = null, + /// The digest the last confirmed unit ended on (its last record's + /// bytes): a unit of another's that ends on it again (the session's + /// pump that took this replica's input in, and moved nothing) leaves + /// the speculation on top as it was. + last_digest: std.ArrayListUnmanaged(u8) = .empty, + /// Units of another's taken so, no rollback made. + neutral: u32 = 0, + /// What it shows changed since the host last drew it: a unit replayed + /// on it, a rollback (a unit that confirmed what it applied, or moved + /// nothing, changes nothing to draw). + moved: bool = false, + /// A client of a session elsewhere (`initDump`): the session's clock is + /// guessed, never read here -- its last unit's reading, plus the time + /// since it came, plus a round trip (the input reaches the session that + /// much later). In process, both read one clock. + guess: bool = false, + stamp: struct { real_ns: i64 = 0, mono_ns: i64 = 0, at_ns: u64 = 0 } = .{}, + rtt_ns: u64 = 0, + gmtoff: i32 = 0, + + /// Its terminals' screen state, as the authority's deltas made it. + screens: [pardes.MAX_PANES]?ScreenOf = @splat(null), + /// Each terminal's view (`terminal.viewOf`) as the copy was taken, or + /// as the last delta since set it: the copy holds no emulator, and a + /// speculation may move a view, so a rollback puts these back. + views: [pardes.MAX_PANES]View = @splat(.{}), + screen_out: std.ArrayListUnmanaged(u8) = .empty, + /// Its predicted echo (src/echo.zig), drawn over its core's terminals: + /// fed by its keys, its units and its deltas here, and by `echoFrame`. + echo: pardes.echo.Echo = .{}, + + const ScreenOf = struct { + serial: u32, + rep: screen_state.Replica = .{}, + /// A delta was refused, and a reset asked for: those in flight + /// before it are refused unasked. + refused: bool = false, + /// The rows asked for and not yet answered. + asked: ?[2]u64 = null, + }; + + /// A replica of the session whose first unit is `first`; `screens` + /// as the authority's (its terminals fed by `screen`). + pub fn init(gpa: Allocator, id: u16, first: []const u8, screens: bool) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa) }; + r.t.screen_state = screens; + r.t.core_gpa = r.epoch.allocator(); + r.confirm(first); + if (r.t.core == null) { + r.deinit(); + return error.NoCore; + } + if (screens) r.core().echo = &r.echo; + return r; + } + + /// A replica of a session elsewhere, as its walker dump (src/dump.zig) + /// is: what a client of a socket joins with. `opts` are this process's + /// (a replica's own allocators and names); its core's allocator is the + /// rollback epoch's, but for images and PDFs, whose workers allocate on + /// threads of their own. ponytail: a picture's pixels freed while a copy + /// is held are not put off; a rollback over one shows an image a frame + /// late at worst... or reads freed pixels, which no test session has. + pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica { + const r = try gpa.create(Replica); + r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; + r.t.headless = true; + // Its terminals are the session's deltas (Peers sends them). + r.t.screen_state = true; + errdefer { + tape.release(&r.t); + r.epoch.deinit(); + gpa.destroy(r); + } + const epoch = r.epoch.allocator(); + r.t.core_gpa = epoch; + var o = opts; + o.frame_allocator = epoch; + o.tree_sitter_allocator = epoch; + o.image_allocator = o.image_allocator orelse std.heap.c_allocator; + o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator; + // The session's store and Recent are the authority's. + o.store_path = null; + // Its emulators off the epoch, as `terminal.create` makes a screen + // replica's: deltas write them while a copy is held. + pardes.dump.terminal_gpa = terminal.emulator_gpa; + defer pardes.dump.terminal_gpa = null; + const c = try pardes.dump.load(epoch, o, bytes, null); + // What loading queued (a watch, a theme asked for) is the + // authority's errand, done there already: the session was dumped + // between steps, with nothing queued. + c.effects_len = 0; + c.effects_head = 0; + c.in_len = 0; + c.in_head = 0; + tape.adoptCore(&r.t, c); + // Its keys into a terminal drawn at once (src/echo.zig). + c.echo = &r.echo; + return r; + } + + pub fn deinit(r: *Replica) void { + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); + if (r.t.core) |c| { + const was = Stepping.begin(&r.t, &r.epoch); + if (r.copy.held) r.copy.restore(); + c.deinit(); + was.end(); + } + r.copy.deinit(); + for (r.pending.items) |p| r.freePending(p); + r.pending.deinit(r.gpa); + for (r.since.items) |u| r.gpa.free(u); + r.since.deinit(r.gpa); + r.out.deinit(r.gpa); + r.screen_out.deinit(r.gpa); + r.last_digest.deinit(r.gpa); + r.stats.deinit(r.gpa); + tape.release(&r.t); + if (r.diverged) |why| r.gpa.free(why); + r.epoch.deinit(); + r.gpa.destroy(r); + } + + pub fn core(r: *const Replica) *Pardes { + return r.t.core.?; + } + + fn freePending(r: *Replica, p: Pending) void { + r.gpa.free(p.msg); + if (p.spec) |s| r.gpa.free(s); + } + + /// The replica's own input: applied at once when it can be, and the + /// message for the authority returned (valid until the next call). + pub fn input(r: *Replica, ev: pardes.Event) ![]const u8 { + r.stats.inputs += 1; + const seq = r.next_seq; + r.next_seq += 1; + // Its time, as the session's clocks will read when it steps it (a + // round trip on from the last unit's, the clocks here in process): + // the step's clock reads, here and there (`stampOf`). + const g: tape.Guess = if (r.guess) guess: { + const since: i64 = @intCast(nowNs() -| r.stamp.at_ns + r.rtt_ns); + break :guess .{ .real_ns = r.stamp.real_ns + since, .mono_ns = r.stamp.mono_ns + since, .gmtoff = r.gmtoff }; + } else .{ .real_ns = clockNs(.REALTIME), .mono_ns = clockNs(.MONOTONIC), .gmtoff = gmtoff() }; + const room = try r.gpa.alloc(u8, head_len + stamp_len + wire.clientBound(.{ .event = ev })); + defer r.gpa.free(room); + _ = try wire.encodeClient(room[head_len..][0..stamp_len], .{ .stamp = .{ g.real_ns, g.mono_ns } }); + const n = (try wire.encodeClient(room[head_len + stamp_len ..], .{ .event = ev })).len; + header(room, r.id, seq); + const msg = try r.gpa.dupe(u8, room[0 .. head_len + stamp_len + n]); + try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs(), .guess = g }); + r.dirty = true; + r.settle(); + if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, seq, r.applied == r.pending.items.len); + r.out.clearRetainingCapacity(); + try r.out.appendSlice(r.gpa, msg); + return r.out.items; + } + + /// One unit of the authority's stream, in its order. + pub fn confirm(r: *Replica, msg: []const u8) void { + if (r.diverged != null) return; + const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); + r.stats.units += 1; + if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); + // A copy taken ahead (`ready`), nothing on top: the unit moves the + // state it was of. + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + } + if (r.guess) { + r.stamp = .{ .real_ns = u.real_ns, .mono_ns = u.mono_ns, .at_ns = nowNs() }; + if (u.origin == r.id) for (r.pending.items) |p| if (p.seq == u.seq) { + // Sent to confirmed: what reaching the session takes, and back. + if (p.sent_ns != 0) r.rtt_ns = nowNs() -| p.sent_ns; + break; + }; + } + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and + tape.sameRecords(r.pending.items[0].spec.?, u.tape)) + { + r.stats.fast += 1; + if (r.pending.items[0].terminal and r.applied > 1) { + // Right, but what it did read a terminal: replayed later (a + // rollback's `since`) it would read the deltas that came + // after it. So it is taken now, over the emulator its step + // read, and the ones on top applied again over it. + r.toConfirmed(); + if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why); + if (lastDigest(u.tape)) |d| { + r.last_digest.clearRetainingCapacity(); + r.last_digest.appendSlice(r.gpa, d) catch {}; + } + r.freePending(r.pending.orderedRemove(0)); + r.dirty = true; + return; + } + r.freePending(r.pending.orderedRemove(0)); + r.applied -= 1; + if (r.applied == 0) { + // Nothing speculative on top any more: the state is the + // confirmed one, which its digest says. A step of its own + // that read what the session's did not (a frame's, a + // derived field's) shows here, at the input that did it, + // not at some later unit replayed. + if (lastDigest(u.tape)) |d| { + tape.checkDigest(&r.t, d); + if (r.t.diverged) |why| { + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + return r.fail(why); + } + } + r.copy.release(); + rollback.Epoch.select(null); + r.clearSince(); + return; + } + const kept = r.gpa.dupe(u8, u.tape) catch return r.fail("out of memory"); + r.since.append(r.gpa, kept) catch return r.fail("out of memory"); + if (r.since.items.len >= rebase_after) { + r.stats.rebases += 1; + r.toConfirmed(); + r.dirty = true; + } + return; + } + const ends = lastDigest(u.tape); + defer if (ends) |d| { + r.last_digest.clearRetainingCapacity(); + r.last_digest.appendSlice(r.gpa, d) catch {}; + }; + if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items)) { + // Nothing a digest covers moved: what was applied on top is + // what the session will apply on top of it. Replayed when the + // copy is next put back. + r.neutral += 1; + const kept = r.gpa.dupe(u8, u.tape) catch return r.fail("out of memory"); + r.since.append(r.gpa, kept) catch return r.fail("out of memory"); + if (r.since.items.len >= rebase_after) { + r.stats.rebases += 1; + r.toConfirmed(); + r.dirty = true; + } + return; + } + if (r.applied > 0) { + if (u.origin == r.id) r.stats.mispredicted += 1 else r.stats.foreign += 1; + if (u.origin == r.id and std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }) catch {}; + tape.describe(r.pending.items[0].spec.?, &w.writer); + w.writer.print("and the authority recorded:\n", .{}) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + r.recovering = nowNs(); + r.toConfirmed(); + } + r.moved = true; + if (tape.replayUnit(&r.t, u.tape)) |why| { + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(r.gpa); + defer w.deinit(); + w.writer.print("replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }) catch {}; + tape.describe(u.tape, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + return r.fail(why); + } + if (u.origin == r.id) { + if (r.pending.items.len == 0 or r.pending.items[0].seq != u.seq) return r.fail("the authority confirmed an input this replica did not send next"); + if (r.blocked == u.seq) r.blocked = null; + r.freePending(r.pending.orderedRemove(0)); + } + r.dirty = true; + } + + fn fail(r: *Replica, why: []const u8) void { + if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return; + } + + fn clearSince(r: *Replica) void { + for (r.since.items) |u| r.gpa.free(u); + r.since.clearRetainingCapacity(); + } + + /// Back to the confirmed position: the copy put back, the units fast- + /// pathed since replayed. + fn toConfirmed(r: *Replica) void { + const held = r.copy.held; + if (held) { + r.copy.restore(); + rollback.Epoch.select(null); + // The rows a terminal was read as are no copy's: what the + // speculation built was the epoch's, freed with it, and what + // was there before is of a grid the deltas since moved on + // from (a rollback never brings a terminal's past back). + r.core().shell_rows = .{}; + } + r.applied = 0; + // Its views as the copy was taken, or as a delta set them between + // the units since (which may move them again). + for (r.since.items, 0..) |u, i| { + if (held) r.putViews(i); + if (tape.replayUnit(&r.t, u)) |why| return r.fail(why); + } + if (held) r.putViews(r.since.items.len); + r.clearSince(); + for (r.pending.items) |*p| if (p.spec) |s| { + r.gpa.free(s); + p.spec = null; + }; + } + + /// The pending inputs applied again, as far as they go. + pub fn settle(r: *Replica) void { + if (!r.dirty or r.diverged != null) return; + r.dirty = false; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + while (r.applied < r.pending.items.len and r.blocked != r.pending.items[r.applied].seq) if (!r.speculate()) break; + if (r.recovering) |t0| { + r.stats.rollback_ns.append(r.gpa, nowNs() - t0) catch {}; + r.recovering = null; + } + } + + /// pending[applied] applied on top: whether it was. + fn speculate(r: *Replica) bool { + const p = &r.pending.items[r.applied]; + // Every kind, a view's own (a resize, the pointer) too: each client + // has its own view, and what reaches the authority's (a pty's size, + // the pointer over a sliding pane) stops it as any host call does. + const in = parseInput(p.msg) orelse return false; + const c = r.core(); + if (!r.copy.held) { + const t0 = nowNs(); + // A grid's worth of text the copy would save, and put back stale. + c.shell_rows.reset(c.gpa); + r.copy.take(&r.epoch, c) catch return false; + r.keepViews(); + rollback.Epoch.select(&r.epoch); + r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + var terminals: [pardes.MAX_PANES]u64 = undefined; + if (checked) terminalDigests(c, &terminals); + const t0 = nowNs(); + tape.beginSpeculation(&r.t); + // The time it was sent with, every time it is applied. + r.t.guess = p.guess; + c.toView(r.view); + // Input into a terminal: what reaches its program (a key in tty + // mode, a paste, a mouse report) is a pty write, the authority's, + // and stops here (phase 6 predicts its echo). The rest (pardes's + // own keys over it, the wheel) reads its emulator, here the last + // delta's and there what the pty has said since: it is applied, + // and applied again over each delta while it waits (`screen`), so + // what the authority's unit confirms read the emulator its step + // read (the deltas come before it). + const keyboard = if (c.panes[c.active]) |pane| pane.terminal != null else false; + c.update(in.event); + c.step(); + p.terminal = r.t.screen_state and (keyboard or r.t.touched); + // No digest is taken here: a blob born under the copy and digested + // keeps its word past the epoch's free of it (src/walk.zig), and a + // later blob at that address read as it. + const clean = tape.endSpeculation(&r.t); + const dt = nowNs() - t0; + // Where a blob was written, should one be: the input that did. + if (checked and std.c.getenv("PARDES_REPLICA_TRACE") != null) r.copy.check(); + if (!clean) { + // Undone, and the earlier ones applied again on the confirmed + // position: this one waits for the authority. + r.stats.stopped += 1; + r.stats.stop(r.t.poisoned_by); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call ({s}): it waits\n", .{ r.id, p.seq, in.event, r.t.poisoned_by }); + r.blocked = p.seq; + const upto = r.applied; + r.toConfirmed(); + while (r.applied < upto) if (!r.speculate()) break; + if (r.applied == 0 and r.copy.held) { + r.copy.release(); + rollback.Epoch.select(null); + } + return false; + } + if (checked) { + var after: [pardes.MAX_PANES]u64 = undefined; + terminalDigests(c, &after); + if (!std.mem.eql(u64, &terminals, &after)) { + for (terminals, after, 0..) |x, y, id| if (x != y) std.debug.print("replica {d}: its input {d} ({any}) wrote terminal {d}\n", .{ r.id, p.seq, in.event, id }); + @panic("replica: a speculation wrote a terminal's emulator"); + } + } + r.stats.speculated += 1; + r.stats.spec_ns.append(r.gpa, dt) catch {}; + p.spec = r.gpa.dupe(u8, tape.taken(&r.t)) catch return false; + tape.clearTaken(&r.t); + r.applied += 1; + return true; + } + + /// Its client's, once a frame before it draws: a predicted echo the + /// screen has not shown in time goes, and keys deferred behind one not + /// predicted are predicted once its doing is on the screen. Whether what + /// the overlay draws changed since the last call (by a key, a unit, a + /// delta or this: `echo.changed`). + pub fn echoFrame(r: *Replica) bool { + return r.echo.frame(r.core()); + } + + /// Nothing pending and nothing applied: the replica is the authority's + /// confirmed state, which `digest` says (a copy taken ahead, `ready`, + /// has nothing on top of it). + pub fn quiet(r: *const Replica) bool { + return r.pending.items.len == 0 and r.applied == 0; + } + + /// The copy taken now, while nothing is pending (the host is idle, its + /// frame drawn), so that the next input of its own is applied at once + /// without taking it: a copy is a megabyte saved. Let go when a unit + /// comes first. + pub fn ready(r: *Replica) void { + if (r.copy.held or r.pending.items.len > 0 or r.diverged != null) return; + const c = r.core(); + const t0 = nowNs(); + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + r.copy.take(&r.epoch, c) catch return; + r.keepViews(); + r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {}; + r.stats.copy_bytes += r.copy.savedBytes(); + r.stats.copies += 1; + } + + fn keepViews(r: *Replica) void { + if (!r.t.screen_state) return; + for (r.core().panes, &r.views) |slot, *v| v.* = .{ .at = if (slot) |pane| terminal.viewOf(pane) else null }; + } + + fn putViews(r: *Replica, after: usize) void { + if (!r.t.screen_state) return; + for (r.core().panes, r.views) |slot, v| if (v.after == after) if (slot) |pane| if (pane.terminal != null) terminal.viewTo(pane, v.at); + } + + /// Whether its core loaded from the dump is the session as it was + /// dumped, by the welcome's `digest`. + pub fn loaded(r: *Replica, want: u64) bool { + return r.digest() == want; + } + + /// Its own view drawn and presented through `present`, between steps + /// (views.drawOne): nothing the digest covers moves. The epoch's trees + /// held while a copy is (a render can make or drop a syntax tree). + pub fn draw(r: *Replica, present: pardes.views.Present) void { + rollback.Epoch.select(&r.epoch); + defer rollback.Epoch.select(null); + pardes.views.drawOne(r.core(), r.view, present) catch {}; + } + + /// `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 + /// message for the authority (an acknowledgement, or a request to start + /// afresh), valid until the next call, or null. + pub fn screen(r: *Replica, msg: []const u8) ?[]const u8 { + const m = parseScreen(msg) orelse return null; + const c = r.t.core orelse return null; + const pane = c.panes[m.pane] orelse return null; + if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another + const state = pane.terminal orelse return null; + const slot = &r.screens[m.pane]; + if (slot.* == null or slot.*.?.serial != m.serial) { + if (slot.*) |*old| old.rep.deinit(terminal.emulator_gpa); + slot.* = .{ .serial = m.serial }; + } + const sc = &slot.*.?; + // The emulator is the authority's: on the C heap, outside the + // rollback epoch, so a copy held now never saves or puts back any + // of what this writes (State.gpa). + switch (m.kind) { + kind_delta => { + const t0 = nowNs(); + const applied = sc.rep.apply(state.gpa, terminal.terminalIo(), &state.vt, m.payload) catch |err| { + if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); + if (sc.refused) return null; + sc.refused = true; + return r.screenMessage(m.pane, m.serial, kind_reset, &.{}); + }; + r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; + sc.refused = false; + terminal.takeMarks(state, applied.extra); + r.echo.delta(c, m.pane, sc.rep.top); + // Its rows as the core keeps them read are another grid's. + c.shell_rows.markStale(pane); + // Its view is the authority's now: a rollback puts it back + // here, and what was applied over a terminal goes on top. + if (r.copy.held) r.views[m.pane] = .{ .at = terminal.viewOf(pane), .after = r.since.items.len }; + r.overTerminals(); + var seq: [4]u8 = undefined; + std.mem.writeInt(u32, &seq, applied.seq, .little); + return r.screenMessage(m.pane, m.serial, kind_ack, &seq); + }, + kind_rows => { + sc.asked = null; + sc.rep.applyRows(state.gpa, &state.vt, m.payload) catch r.fail("a terminal's rows did not apply"); + c.shell_rows.markStale(pane); + r.overTerminals(); + return null; + }, + else => return null, + } + } + + /// A terminal was written (a delta, rows): what is applied over the + /// emulator (`Pending.terminal`) read the one before it, and the + /// authority's step reads this one (it comes before that step's unit). + /// Back to the confirmed position, to be applied again over it. + fn overTerminals(r: *Replica) void { + const any = for (r.pending.items[0..r.applied]) |p| { + if (p.terminal) break true; + } else false; + if (!any) return; + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + r.toConfirmed(); + r.dirty = true; + } + + /// A request for the rows a terminal shows here and does not hold (its + /// holes in view, after a scroll back), one a terminal at a time; null + /// when none is owed. Until the answer, a hole shows blank. + pub fn screenRequest(r: *Replica) ?[]const u8 { + const c = r.t.core orelse return null; + for (&r.screens, 0..) |*slot, id| { + const sc: *ScreenOf = if (slot.*) |*x| x else continue; + if (sc.asked != null) continue; + const pane = c.panes[id] orelse continue; + if (pane.serial != sc.serial) continue; + const state = pane.terminal orelse continue; + const shown = sc.rep.shown(&state.vt); + var holes: std.ArrayList([2]u64) = .empty; + defer holes.deinit(r.gpa); + sc.rep.missing(shown[0], shown[1], &holes, r.gpa) catch return null; + if (holes.items.len == 0) continue; + const want: [2]u64 = .{ holes.items[0][0], holes.items[holes.items.len - 1][1] }; + sc.rep.blank(&state.vt, want[0], want[1]); + sc.asked = want; + var range: [16]u8 = undefined; + std.mem.writeInt(u64, range[0..8], want[0], .little); + std.mem.writeInt(u64, range[8..16], want[1], .little); + return r.screenMessage(@intCast(id), sc.serial, kind_rows, &range); + } + return null; + } + + fn screenMessage(r: *Replica, pane: u8, serial: u32, kind: u8, payload: []const u8) ?[]const u8 { + r.screen_out.clearRetainingCapacity(); + writeScreenHead(&r.screen_out, r.gpa, pane, serial, kind) catch return null; + r.screen_out.appendSlice(r.gpa, payload) catch return null; + return r.screen_out.items; + } + + pub fn digest(r: *Replica) u64 { + const was = Stepping.begin(&r.t, &r.epoch); + defer was.end(); + return tape.digestOf(r.core()); + } +}; + +/// The bytes of a unit's last digest record: the state it ended on; null +/// for one that says what the host wrote or which host it is (a replica's +/// core takes those only by replaying the unit). +fn lastDigest(bytes: []const u8) ?[]const u8 { + var r: walk.Reader = .{ .bytes = bytes }; + var last: ?[]const u8 = null; + while (true) { + const head = r.take(5) catch return last; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return last; + if (head[0] == tape.digest_kind) last = payload; + if (head[0] == tape.set_kind) return null; + } +} + +fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void { + for (p.panes, out) |slot, *d| { + d.* = 0; + const pane = slot orelse continue; + const state = pane.terminal orelse continue; + var h = walk.Hasher.init(0); + // Its view aside: a speculation may move it (`views`). + state.digestScreen(&h, false); + d.* = h.final(); + } +} + +// ---- over a socket (docs/next-daemon.md, "Phase 5 part B notes") ---- +// +// The session's half: each client handed over by the daemon (daemon.zig) is +// a Peer, with a view of its own, a thread reading its inputs and one +// writing what it is sent. The recorder is on while any is. At each step +// boundary of the session's loop (`Peers.boundary`, after a pump): +// - the pump's unit goes to every client; +// - a client that came is joined: its view opened, the session dumped +// (src/dump.zig) and sent ahead of every unit after; +// - each input that came is a unit of its own: its view, the input, a +// `step` (what the client's own core applied at once); +// - a client gone takes its view with it. +// A client's link only ever holds bytes queued for it: the session's step +// never waits on one. One that lets `lag_max` pile up is cut off. + +const libc = std.c; +const log = std.log.scoped(.replica); +const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; +/// What a client may leave unread, past its join's dump, before it is cut +/// off: minutes of typing, a screenful of a flood's deltas many times over. +/// PARDES_REPLICA_LAG_KB says another (a test's). +var lag_max: usize = 64 << 20; +const dump_piece = 1 << 20; +/// A blob in a far join's list (wire.zig `blobs`): its place in the state +/// (u64le), its length (u64le) and its BLAKE3 hash. +const blob_entry = 48; + +const Tm = extern struct { sec: c_int, min: c_int, hour: c_int, mday: c_int, mon: c_int, year: c_int, wday: c_int, yday: c_int, isdst: c_int, gmtoff: c_long, zone: ?[*:0]const u8 }; +extern "c" fn localtime_r(t: *const libc.time_t, tm: *Tm) ?*Tm; + +/// This zone's offset from UTC now, seconds. +fn gmtoff() i32 { + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.REALTIME, &ts); + const secs: libc.time_t = ts.sec; + var tm: Tm = undefined; + return if (localtime_r(&secs, &tm) != null) @intCast(tm.gmtoff) else 0; +} + +fn writeAll(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) { + const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) return false; + at += @intCast(n); + } + return true; +} + +/// `bytes` as dump pieces (1 MiB at most each). +fn writePieces(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + const piece = bytes[at..@min(bytes.len, at + dump_piece)]; + const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len); + if (!writeAll(fd, &head) or !writeAll(fd, piece)) return false; + } + return true; +} + +// ---- zstd (libzstd, vendored: build.zig) ---- + +/// A far client's stream from its welcome on (`Peer.farJoin`): its join in +/// one frame at level 6 over an 8 MiB window, since an undo snapshot is a +/// near copy of its file's text, a file's length before or after it (the +/// join's numbers: Phase 6's content cache and zstd notes); what follows in +/// a frame at level 3 over 1 MiB. Each envelope (wire.zig `zstd`) is a +/// flush of at most `piece` of input, so that a reader unpacks one whole. +const zstd = struct { + const CCtx = opaque {}; + const DCtx = opaque {}; + const In = extern struct { src: ?*const anyopaque, size: usize, pos: usize }; + const Out = extern struct { dst: ?*anyopaque, size: usize, pos: usize }; + const Mem = extern struct { + alloc: *const fn (?*anyopaque, usize) callconv(.c) ?*anyopaque, + free: *const fn (?*anyopaque, ?*anyopaque) callconv(.c) void, + ctx: ?*anyopaque = null, + }; + extern fn ZSTD_createCCtx_advanced(mem: Mem) ?*CCtx; + extern fn ZSTD_freeCCtx(c: ?*CCtx) usize; + extern fn ZSTD_CCtx_setParameter(c: *CCtx, param: c_int, value: c_int) usize; + extern fn ZSTD_compressStream2(c: *CCtx, out: *Out, in: *In, op: c_int) usize; + extern fn ZSTD_createDCtx_advanced(mem: Mem) ?*DCtx; + extern fn ZSTD_freeDCtx(d: ?*DCtx) usize; + extern fn ZSTD_decompressStream(d: *DCtx, out: *Out, in: *In) usize; + extern fn ZSTD_isError(code: usize) c_uint; + const level_param = 100; + const window_param = 101; + const Op = enum(c_int) { more = 0, flush = 1, end = 2 }; + const piece = 1 << 20; + + /// What zstd allocates is a Zig allocator's (c_heap): a test's own. + var gpa: Allocator = std.heap.c_allocator; + const heap = c_heap.Heap(&gpa); + fn alloc(_: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + return heap.malloc(n); + } + fn free(_: ?*anyopaque, p: ?*anyopaque) callconv(.c) void { + heap.free(p); + } + const mem: Mem = .{ .alloc = alloc, .free = free }; + + fn compressor(level: c_int, window_log: c_int) ?*CCtx { + const c = ZSTD_createCCtx_advanced(mem) orelse return null; + if (ZSTD_isError(ZSTD_CCtx_setParameter(c, level_param, level)) != 0 or + ZSTD_isError(ZSTD_CCtx_setParameter(c, window_param, window_log)) != 0) + { + _ = ZSTD_freeCCtx(c); + return null; + } + return c; + } +}; + +fn readSome(fd: c_int, buf: []u8) usize { + while (true) { + const n = libc.read(fd, buf.ptr, buf.len); + if (n < 0 and libc.errno(n) == .INTR) continue; + return if (n <= 0) 0 else @intCast(n); + } +} + +/// What a stream's reader holds of it, read and not yet taken: a dump's +/// reader leaves the units read with its last piece to the stream's. +pub const Framer = struct { + buf: std.ArrayListUnmanaged(u8) = .empty, + len: usize = 0, + /// What zstd envelopes (wire.zig `zstd`) unpacked to, its messages not + /// taken yet, and the stream's decompressor: a far client's. + inner: std.ArrayListUnmanaged(u8) = .empty, + z: ?*zstd.DCtx = null, + bad: bool = false, + + pub fn deinit(f: *Framer, gpa: Allocator) void { + f.buf.deinit(gpa); + f.inner.deinit(gpa); + if (f.z) |z| _ = zstd.ZSTD_freeDCtx(z); + f.z = null; + } + + /// Framed messages off `fd` (detached/wire.zig) handed to `take` until + /// the stream ends, `take` says stop, or a message is malformed. An + /// envelope's are handed on as if they had come bare. + fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + if (fr.bad or !fr.drain(ctx, take)) return; + while (true) { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.buf.items[at..fr.len]) catch break) orelse break; + at += f.total; + if (f.tag == @backingInt(wire.ServerTag.zstd)) { + fr.bad = !fr.unpack(gpa, f.payload); + stop = fr.bad or !fr.drain(ctx, take); + } else stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + std.mem.copyForwards(u8, fr.buf.items[0 .. fr.len - at], fr.buf.items[at..fr.len]); + fr.len -= at; + } + if (stop) return; + if (wire.framed(fr.buf.items[0..fr.len])) |_| {} else |_| return; + // Room for what the head says is coming, or 64 KiB more. + const want = if (fr.len >= wire.header_len) wire.header_len + @as(usize, std.mem.readInt(u32, fr.buf.items[1..5], .little)) else 0; + const room = @max(want, fr.len + (64 << 10)); + if (fr.buf.items.len < room) fr.buf.resize(gpa, room) catch return; + const n = readSome(fd, fr.buf.items[fr.len..]); + if (n == 0) return; + fr.len += n; + } + } + + /// An envelope's bytes onto `inner`. Its writer flushed at most + /// `zstd.piece` of input into it, so it unpacks to no more than that. + fn unpack(fr: *Framer, gpa: Allocator, payload: []const u8) bool { + if (fr.z == null) fr.z = zstd.ZSTD_createDCtx_advanced(zstd.mem); + const z = fr.z orelse return false; + var in: zstd.In = .{ .src = payload.ptr, .size = payload.len, .pos = 0 }; + while (true) { + fr.inner.ensureUnusedCapacity(gpa, 256 << 10) catch return false; + const spare = fr.inner.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + if (zstd.ZSTD_isError(zstd.ZSTD_decompressStream(z, &out, &in)) != 0) return false; + fr.inner.items.len += out.pos; + if (in.pos == in.size and out.pos < out.size) return true; + } + } + + /// The whole messages in `inner` handed to `take`; false when it says + /// stop (what is left waits for the next run) or one is malformed. + fn drain(fr: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) bool { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.inner.items[at..]) catch { + fr.bad = true; + return false; + }) orelse break; + at += f.total; + stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + const rest = fr.inner.items.len - at; + std.mem.copyForwards(u8, fr.inner.items[0..rest], fr.inner.items[at..]); + fr.inner.items.len = rest; + } + return !stop; + } +}; + +fn readFramed(gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + var fr: Framer = .{}; + defer fr.deinit(gpa); + fr.run(gpa, fd, ctx, take); +} + +pub const Peer = struct { + peers: *Peers, + fd: c_int, + id: u16, + view: u8, + /// Its bye said it joins again for its view (a divergence's rejoin): + /// kept for it (views.away), not closed. Its reader's, read after its end. + again: bool = false, + /// Its number in the session's `Screens`. + sid: u16 = 0, + /// Inputs read (each one's seq, as the client numbered them). + seq: u32 = 0, + /// The `stamp` frame its next input came with (its reader's). + stamp: [stamp_len]u8 = undefined, + stamped: bool = false, + reader: ?std.Thread = null, + writer: ?std.Thread = null, + lock: std.Io.Mutex = .init, + cond: std.Io.Condition = .init, + /// What it is sent, not written yet, and what the writer writes now. + out: std.ArrayListUnmanaged(u8) = .empty, + sending: std.ArrayListUnmanaged(u8) = .empty, + limit: usize = 64 << 20, + closing: bool = false, + cut: bool = false, + /// Its writer has ended (what it was sent, written). + done: std.atomic.Value(bool) = .init(false), + /// Its join, which its writer writes before what it is sent: the + /// welcome, then the dump with the session's blobs put back where they + /// go, pinned until written (memory.zig `pin`). The core's allocator's. + welcome: [wire.hello_max + 64]u8 = undefined, + welcome_len: usize = 0, + dump: []u8 = &.{}, + blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, + dump_gpa: Allocator = undefined, + /// Its first word after its hello: whether it is far (through ssh), its + /// blobs asked for by hash and its stream in zstd (`farJoin`). + far: ?bool = null, + /// A far client's answer to its join's list: the blobs it lacks. + want: ?[]u8 = null, + /// A far client's stream's compressor, and an envelope's worth of its + /// output; the input since the last envelope. + z: ?*zstd.CCtx = null, + zout: std.ArrayListUnmanaged(u8) = .empty, + zin: usize = 0, + + fn writeJoin(peer: *Peer) bool { + if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const far = peer.far; + peer.lock.unlock(io); + if (far orelse return false) return peer.farJoin(); + var pos: usize = 0; + for (peer.blobs.items) |b| { + if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; + pos = b.at; + } + return writePieces(peer.fd, peer.dump[pos..]); + } + + /// A far client's join, in one zstd frame: the list of the dump's blobs + /// (each one's place in the state, its length and its BLAKE3 hash), the + /// state, then the blobs it says it lacks; what follows the join goes + /// in a cheaper frame. The hashing is this thread's, never the step's. + /// ponytail: a blob is hashed again at every far join, 3.6 GB/s; a + /// session's join after its first would keep them, beside its words. + fn farJoin(peer: *Peer) bool { + const gpa = peer.peers.gpa; + peer.z = zstd.compressor(6, 23) orelse return false; + var list: std.ArrayListUnmanaged(u8) = .empty; + defer list.deinit(gpa); + for (peer.blobs.items) |b| { + // Gone meanwhile: its leave waits for this thread in the + // session's step, so the hashing stops here. + if (peer.letting()) return false; + var entry: [blob_entry]u8 = undefined; + std.mem.writeInt(u64, entry[0..8], b.at, .little); + std.mem.writeInt(u64, entry[8..16], b.bytes.len, .little); + std.crypto.hash.Blake3.hash(b.bytes, entry[16..48], .{}); + list.appendSlice(gpa, &entry) catch return false; + if (list.items.len + blob_entry > dump_piece) { + if (!peer.zMessage(.blobs, list.items)) return false; + list.clearRetainingCapacity(); + } + } + if (list.items.len > 0 and !peer.zMessage(.blobs, list.items)) return false; + if (!peer.zPieces(peer.dump) or !peer.zWrite(&.{}, .flush)) return false; + if (peer.blobs.items.len > 0) { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.want == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const want = peer.want; + peer.lock.unlock(io); + const bits = want orelse return false; + if (bits.len != (peer.blobs.items.len + 7) / 8) return false; + for (peer.blobs.items, 0..) |b, i| if (bits[i / 8] >> @intCast(i % 8) & 1 != 0) { + if (!peer.zPieces(b.bytes)) return false; + }; + } + if (!peer.zWrite(&.{}, .end)) return false; + _ = zstd.ZSTD_freeCCtx(peer.z); + peer.z = zstd.compressor(3, 20); + return peer.z != null; + } + + /// Whether it is being let go (or was cut off). + fn letting(peer: *Peer) bool { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + return peer.closing or peer.cut; + } + + /// One message into its stream's compressor. + fn zMessage(peer: *Peer, tag: wire.ServerTag, payload: []const u8) bool { + const head = wire.header(@backingInt(tag), payload.len); + return peer.zWrite(&head, .more) and peer.zWrite(payload, .more); + } + + /// `bytes` as dump pieces (1 MiB at most each), into the compressor. + fn zPieces(peer: *Peer, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + if (!peer.zMessage(.dump, bytes[at..@min(bytes.len, at + dump_piece)])) return false; + } + return true; + } + + /// `bytes` into its stream's compressor, and what comes out written as + /// envelopes: one at each `zstd.piece` of input, and one now if `op` + /// says flush or end (the frame's). + fn zWrite(peer: *Peer, bytes: []const u8, op: zstd.Op) bool { + const z = peer.z orelse return false; + var rest = bytes; + while (true) { + const n = @min(rest.len, zstd.piece - peer.zin); + const full = peer.zin + n == zstd.piece; + const now: zstd.Op = if (n < rest.len or (full and op == .more)) .flush else op; + var in: zstd.In = .{ .src = rest.ptr, .size = n, .pos = 0 }; + while (true) { + peer.zout.ensureUnusedCapacity(peer.peers.gpa, 128 << 10) catch return false; + const spare = peer.zout.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + const left = zstd.ZSTD_compressStream2(z, &out, &in, @backingInt(now)); + if (zstd.ZSTD_isError(left) != 0) return false; + peer.zout.items.len += out.pos; + if (if (now == .more) in.pos == in.size else left == 0) break; + } + peer.zin += n; + if (now != .more) { + const head = wire.header(@backingInt(wire.ServerTag.zstd), peer.zout.items.len); + if (!writeAll(peer.fd, &head) or !writeAll(peer.fd, peer.zout.items)) return false; + peer.zout.clearRetainingCapacity(); + peer.zin = 0; + } + rest = rest[n..]; + if (rest.len == 0) return true; + } + } + + /// `parts` queued for it, in order; never waits. A client whose queue + /// passes its limit is cut off: its link is shut down, and its reader + /// says it is gone. + fn send(peer: *Peer, parts: []const []const u8) void { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (peer.cut or peer.closing) return; + var n: usize = peer.out.items.len; + for (parts) |p| n += p.len; + if (n > peer.limit) { + log.info("client {d} is {d} MB behind: cut off", .{ peer.id, n >> 20 }); + peer.peers.cut += 1; + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + peer.cond.signal(io); + return; + } + for (parts) |p| peer.out.appendSlice(peer.peers.gpa, p) catch { + peer.cut = true; + _ = libc.shutdown(peer.fd, 2); + break; + }; + peer.cond.signal(io); + } + + fn writeLoop(peer: *Peer) void { + const io = peer.peers.io; + defer peer.done.store(true, .release); + if (!peer.writeJoin()) { + _ = libc.shutdown(peer.fd, 2); + return; + } + // Its pins let go at the next boundary. + peer.peers.post(.{ .joined = peer }); + while (true) { + peer.lock.lockUncancelable(io); + while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + if (peer.out.items.len == 0 or peer.cut) { + peer.lock.unlock(io); + return; + } + std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &peer.sending); + // The limit was for the dump and what is queued behind it. + peer.limit = lag_max; + peer.lock.unlock(io); + const ok = if (peer.z != null) peer.zWrite(peer.sending.items, .flush) else writeAll(peer.fd, peer.sending.items); + peer.sending.clearRetainingCapacity(); + if (!ok) { + _ = libc.shutdown(peer.fd, 2); + return; + } + } + } + + fn take(peer: *Peer, tag: u8, payload: []const u8) bool { + const t = std.enums.fromInt(wire.ClientTag, tag) orelse return false; + switch (t) { + .hello => return false, + .bye => { + peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + return false; + }, + .ping => { + var out: [16]u8 = undefined; + const v = (wire.decodeClient(tag, payload) catch return false).ping; + const pong = wire.encodeServer(&out, .{ .pong = v }) catch return false; + peer.send(&.{pong}); + return true; + }, + // Its word about a terminal (an ack, rows asked, a reset), to + // the session's Screens at the next boundary. + .screen_ack => { + const msg = peer.peers.gpa.dupe(u8, payload) catch return false; + peer.peers.post(.{ .screen = .{ .peer = peer, .msg = msg } }); + return true; + }, + .check => { + peer.peers.post(.{ .check = peer }); + return true; + }, + // What its join's writer waits for (`writeJoin`, `farJoin`). + .far, .want => { + const io = peer.peers.io; + const msg = wire.decodeClient(tag, payload) catch return false; + const bits = if (msg == .want) peer.peers.gpa.dupe(u8, msg.want) catch return false else null; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (bits) |b| { + if (peer.want != null) { + peer.peers.gpa.free(b); + return false; + } + peer.want = b; + } else peer.far = msg.far; + peer.cond.signal(io); + return true; + }, + // Its next input's time, kept for it (this thread's alone). + .stamp => { + const s = (wire.decodeClient(tag, payload) catch return false).stamp; + _ = wire.encodeClient(&peer.stamp, .{ .stamp = s }) catch return false; + peer.stamped = true; + return true; + }, + else => {}, + } + peer.seq += 1; + const stamp: []const u8 = if (peer.stamped) &peer.stamp else &.{}; + peer.stamped = false; + const msg = peer.peers.gpa.alloc(u8, head_len + stamp.len + wire.header_len + payload.len) catch return false; + header(msg, peer.id, peer.seq); + @memcpy(msg[head_len..][0..stamp.len], stamp); + const at = head_len + stamp.len; + msg[at] = tag; + std.mem.writeInt(u32, msg[at + 1 ..][0..4], @intCast(payload.len), .little); + @memcpy(msg[at + wire.header_len ..], payload); + if (parseInput(msg) == null) { + peer.peers.gpa.free(msg); + return false; + } + peer.peers.post(.{ .input = .{ .peer = peer, .msg = msg } }); + return true; + } + + fn readLoop(peer: *Peer) void { + readFramed(peer.peers.gpa, peer.fd, peer, take); + peer.peers.post(.{ .gone = peer }); + } +}; + +/// The session's clients, for `dying`. +var live_peers: ?*Peers = null; + +/// The process is panicking: each client's link shut first, so that it +/// hears the end now, not when the trace is written (seconds, symbolized). +pub fn dying() void { + const ps = live_peers orelse return; + for (ps.list) |slot| if (slot) |peer| { + _ = libc.shutdown(peer.fd, 2); + }; +} + +pub const Peers = struct { + gpa: Allocator = std.heap.c_allocator, + io: std.Io = undefined, + /// This session's name and pid, for a welcome. + name: []const u8 = "", + t: tape.Tape = undefined, + recording: bool = false, + list: [pardes.views.max]?*Peer = @splat(null), + next_id: u16 = 1, + seq: u32 = 0, + lock: std.Io.Mutex = .init, + inbox: std.ArrayListUnmanaged(Note) = .empty, + taken: std.ArrayListUnmanaged(Note) = .empty, + /// A reader posted: the loop's one test a step while nothing is attached. + mail: std.atomic.Value(bool) = .init(false), + /// Wakes the session's loop (its wait for input) from a reader. + wake_ctx: ?*anyopaque = null, + wake: ?*const fn (?*anyopaque) void = null, + /// What the session would be with no client: unattended without a + /// terminal of its own (the host says). + alone_unattended: bool = false, + /// The worst a join cost the session's step, and the last one's dump. + join_ns: u64 = 0, + each_join: std.ArrayListUnmanaged(u64) = .empty, + dump_bytes: usize = 0, + /// ...of it the state the step dumped (the rest, blobs, the writer's). + state_bytes: usize = 0, + /// What each client input cost the session (its step, its unit sent), + /// and each boundary (its pump's unit sent, what came taken in), ns: + /// PARDES_REPLICA_STATS writes them at the session's end. + input_ns: std.ArrayListUnmanaged(u64) = .empty, + boundary_ns: std.ArrayListUnmanaged(u64) = .empty, + cut: u32 = 0, + head: [unit_head]u8 = undefined, + /// Its clients' terminals as screen state (src/screen_state.zig), while + /// it records: what each was sent. + screens: Screens = undefined, + msgs: std.ArrayListUnmanaged([]u8) = .empty, + /// Clients waiting to join while the session's texts are hashed + /// (`joins`), the texts and the thread hashing them. + held: std.ArrayListUnmanaged(Joining) = .empty, + texts: std.ArrayListUnmanaged([]const u8) = .empty, + hasher: ?std.Thread = null, + rounds: u8 = 0, + + pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; + /// A connection to join, and the view it joins again for, if it does. + pub const Joining = struct { fd: c_int, view: ?u8 = null }; + pub const Msg = struct { peer: *Peer, msg: []u8 }; + + pub fn count(ps: *const Peers) usize { + var n: usize = 0; + for (ps.list) |slot| n += @intFromBool(slot != null); + return n; + } + + fn post(ps: *Peers, note: Note) void { + ps.lock.lockUncancelable(ps.io); + ps.inbox.append(ps.gpa, note) catch { + ps.lock.unlock(ps.io); + switch (note) { + .join => |j| _ = libc.close(j.fd), + .input, .screen => |in| ps.gpa.free(in.msg), + .gone, .check, .joined, .hashed => {}, + } + return; + }; + ps.mail.store(true, .release); + ps.lock.unlock(ps.io); + if (ps.wake) |w| w(ps.wake_ctx); + } + + /// A replica's connection the daemon handed over (its hello taken + /// there), and the view its hello says it had: joined at the next step + /// boundary. Any thread. + pub fn handed(ps: *Peers, fd: c_int, view: ?u8) void { + ps.post(.{ .join = .{ .fd = fd, .view = view } }); + } + + /// What was recorded since the last unit, to every client as one, its + /// digest at its end. + fn unit(ps: *Peers, origin: u16, seq: u32) void { + if (tape.taken(&ps.t).len == 0) return; + tape.recordDigest(&ps.t); + const bytes = tape.taken(&ps.t); + unitHeader(&ps.head, origin, seq); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + var w: std.Io.Writer.Allocating = .init(ps.gpa); + defer w.deinit(); + w.writer.print("session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }) catch {}; + tape.describe(bytes, &w.writer); + std.debug.print("{s}", .{w.written()}); + } + const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len); + for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); + tape.clearTaken(&ps.t); + // The sizes its steps gave terminals, now: the next flush says so. + if (ps.t.core) |core| terminal.resizeDeferred(core); + } + + /// After each of the session's pumps: its unit sent, the clients that + /// came joined, their inputs taken in, the ones gone let go. + pub fn boundary(ps: *Peers, core: *Pardes) void { + if (!ps.recording and !ps.mail.load(.acquire)) return; + const t0 = nowNs(); + defer if (ps.recording) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; + if (ps.recording) { + // The rows the pump's steps read, then its unit, then what the + // terminals came to (the next steps read that). + ps.screensOut(core, .fill); + ps.seq += 1; + ps.unit(0, ps.seq); + ps.screensOut(core, .flush); + } + if (!ps.mail.load(.acquire)) return; + ps.lock.lockUncancelable(ps.io); + std.mem.swap(std.ArrayListUnmanaged(Note), &ps.inbox, &ps.taken); + ps.mail.store(false, .release); + ps.lock.unlock(ps.io); + for (ps.taken.items) |note| switch (note) { + .join => |j| { + ps.held.append(ps.gpa, j) catch { + refuse(j.fd, "out of memory"); + continue; + }; + ps.joins(core); + }, + .hashed => { + ps.unhashedDone(); + ps.joins(core); + }, + .input => |in| { + ps.input(core, in.peer, in.msg); + ps.gpa.free(in.msg); + }, + .screen => |in| { + defer ps.gpa.free(in.msg); + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + defer ps.gpa.free(reply); + const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); + in.peer.send(&.{ &head, reply }); + }, + .gone => |peer| ps.leave(core, peer), + .joined => |peer| released(peer), + .check => |peer| { + var out: [16]u8 = undefined; + peer.send(&.{wire.encodeServer(&out, .{ .check = tape.digestOf(core) }) catch unreachable}); + }, + }; + ps.taken.clearRetainingCapacity(); + } + + fn input(ps: *Peers, core: *Pardes, peer: *Peer, msg: []const u8) void { + const in = parseInput(msg) orelse return; + const t0 = nowNs(); + defer ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; + // The terminals as this step reads them, then the step, the rows + // it read, its unit (part B terminal notes' order). + ps.screensOut(core, .flush); + core.toView(peer.view); + ps.t.stamp = stampOf(in); + core.update(in.event); + core.step(); + ps.t.stamp = null; + ps.screensOut(core, .fill); + ps.unit(peer.id, in.seq); + } + + const ScreensOut = enum { flush, fill }; + + /// What each client is owed of the terminals (`Screens.flush`), or the + /// rows a step read that it lacks (`Screens.fill`, then `filled`), each + /// a screen message queued for it. + fn screensOut(ps: *Peers, core: *Pardes, comptime what: ScreensOut) void { + for (ps.list) |slot| if (slot) |peer| ps.screensTo(core, peer, what); + if (what == .fill) ps.screens.filled(core); + } + + fn screensTo(ps: *Peers, core: *Pardes, peer: *Peer, comptime what: ScreensOut) void { + defer { + for (ps.msgs.items) |m| ps.gpa.free(m); + ps.msgs.clearRetainingCapacity(); + } + (switch (what) { + .flush => ps.screens.flush(core, peer.sid, &ps.msgs), + .fill => ps.screens.fill(core, peer.sid, &ps.msgs), + }) catch return; + for (ps.msgs.items) |m| { + const head = wire.header(@backingInt(wire.ServerTag.screen), m.len); + peer.send(&.{ &head, m }); + } + } + + fn refuse(fd: c_int, why: []const u8) void { + var out: [600]u8 = undefined; + if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} + _ = libc.close(fd); + } + + /// The clients waiting to join, joined, once the texts the session + /// never digested have their words (`walk.unhashed`): those are hashed + /// on a thread of their own, pinned meanwhile, and the joins wait a + /// boundary or two for them, the session's step never. A second round + /// joins whatever is left. + fn joins(ps: *Peers, core: *Pardes) void { + if (ps.hasher != null or ps.held.items.len == 0) return; + if (ps.rounds < 2) { + walk.unhashed(Pardes, core, &ps.texts); + if (ps.texts.items.len > 0) { + for (ps.texts.items) |b| pardes.memory.pin(b); + if (std.Thread.spawn(.{}, hashTexts, .{ps})) |t| { + ps.hasher = t; + ps.rounds += 1; + return; + } else |_| ps.unhashedDone(); + } + } + ps.rounds = 0; + for (ps.held.items) |j| ps.join(core, j); + ps.held.clearRetainingCapacity(); + } + + fn hashTexts(ps: *Peers) void { + walk.makeWords(ps.texts.items); + ps.post(.hashed); + } + + /// The hashing thread joined, its texts unpinned. + fn unhashedDone(ps: *Peers) void { + if (ps.hasher) |t| t.join(); + ps.hasher = null; + for (ps.texts.items) |b| pardes.memory.unpin(b); + ps.texts.clearRetainingCapacity(); + } + + fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + const fd = joining.fd; + const t0 = nowNs(); + defer { + ps.join_ns = @max(ps.join_ns, nowNs() - t0); + ps.each_join.append(ps.gpa, nowNs() - t0) catch {}; + } + const slot = for (&ps.list) |*s| { + if (s.* == null) break s; + } else return refuse(fd, "that session has as many clients as it takes"); + live_peers = ps; + if (!ps.recording) { + // A session recording to a file (PARDES_RECORD) has its tape. + if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)"); + ps.t = tape.memory(ps.gpa); + ps.t.unit_digests = true; + // Terminals cross as screen state: a unit carries no pty bytes, + // and its digest is a word (PARDES_REPLICA_TRACE: one a field). + ps.t.screen_state = true; + ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; + ps.screens = .init(ps.gpa); + tape.on = &ps.t; + ps.recording = true; + } + // Someone looks at it now: its messages go by keys, not the clock. + core.unattended = false; + // Its own view again, kept for it since it left (views.away), or one + // new. ponytail: a divergence's redial that came before its old + // link's end (posted by that link's reader, microseconds after the + // bye; the redial is a dial and the daemon's hand-over later) finds + // its view not kept yet, and takes a new one. + const back = if (joining.view) |v| core.rejoinView(v) else false; + const k = (if (back) joining.view else core.openView()) orelse { + core.unattended = ps.count() == 0 and ps.alone_unattended; + if (ps.count() == 0) ps.stop(core); + return refuse(fd, "that session has as many views as it takes"); + }; + // Its own process draws it. + core.undrawn |= @as(u8, 1) << @intCast(k); + ps.seq += 1; + ps.unit(0, ps.seq); + // The session's mutable state, dumped now; its blobs (file texts, + // undo, registers) left out, pinned, and streamed by the client's + // writer thread: the step pays for the state, not for the text. + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const bytes = pardes.dump.dumpJoin(core, &blobs) catch |err| { + blobs.deinit(core.gpa); + core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + core.closeView(k); + ps.unit(0, ps.seq); + if (ps.count() == 0) ps.stop(core); + var why: [96]u8 = undefined; + return refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped"); + }; + var whole = bytes.len; + for (blobs.items) |b| { + pardes.memory.pin(b.bytes); + whole += b.bytes.len; + } + ps.dump_bytes = whole; + ps.state_bytes = bytes.len; + // The joiner's core is the dump's: the host this tape wraps, and + // what the host wrote in the core, are said again at the next input. + tape.resync(&ps.t); + const peer = ps.gpa.create(Peer) catch { + for (blobs.items) |b| pardes.memory.unpin(b.bytes); + blobs.deinit(core.gpa); + core.gpa.free(bytes); + return refuse(fd, "out of memory"); + }; + if (std.c.getenv("PARDES_REPLICA_LAG_KB")) |kb| lag_max = (std.fmt.parseInt(usize, std.mem.span(kb), 10) catch 64 << 10) << 10; + peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa }; + ps.next_id +%= 1; + if (ps.next_id == 0) ps.next_id = 1; + // The session's digest as dumped, for the joiner to check its load by. + const digest = tape.digestOf(core); + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; + peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; + if (peer.writer == null or peer.reader == null) { + ps.letGo(peer); + core.undrawn &= ~(@as(u8, 1) << @intCast(k)); + core.closeView(k); + ps.unit(0, ps.seq); + return; + } + slot.* = peer; + // Its terminals afresh, after its dump and before any unit. + if (ps.screens.addPeer()) |sid| { + peer.sid = sid; + ps.screensTo(core, peer, .flush); + } else |_| _ = libc.shutdown(fd, 2); + log.info("client {d} joined '{s}' as view {d}: a {d} KB dump ({d} KB of state) in {d} us", .{ peer.id, ps.name, k, whole >> 10, bytes.len >> 10, (nowNs() - t0) / 1000 }); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq }); + } + + fn leave(ps: *Peers, core: *Pardes, peer: *Peer) void { + for (&ps.list) |*slot| if (slot.* == peer) { + slot.* = null; + }; + log.info("client {d} left '{s}'", .{ peer.id, ps.name }); + ps.screens.dropPeer(peer.sid); + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + ps.closeViewOf(core, peer, peer.again); + if (ps.count() == 0) core.unattended = ps.alone_unattended; + ps.seq += 1; + ps.unit(0, ps.seq); + ps.letGo(peer); + if (ps.count() == 0) ps.stop(core); + } + + /// A gone client's view closed (its slot let go already), or kept for + /// it (`again`: it joins again for it, views.away): the last one of a + /// session no one else watches leaves the session its layout, for a + /// client that comes after to find. + fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer, again: bool) void { + if (again) core.awayView(peer.view) else if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view); + } + + /// Its threads ended and joined, its link closed. + fn letGo(ps: *Peers, peer: *Peer) void { + const io = ps.io; + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + _ = libc.shutdown(peer.fd, 2); + if (peer.writer) |w| w.join(); + if (peer.reader) |r| r.join(); + released(peer); + _ = libc.close(peer.fd); + peer.out.deinit(ps.gpa); + peer.sending.deinit(ps.gpa); + if (peer.want) |b| ps.gpa.free(b); + if (peer.z) |z| _ = zstd.ZSTD_freeCCtx(z); + peer.zout.deinit(ps.gpa); + // What its reader posted after it was let go (its end) is nobody's. + ps.lock.lockUncancelable(io); + var i: usize = 0; + while (i < ps.inbox.items.len) { + const mine = switch (ps.inbox.items[i]) { + .input, .screen => |in| in.peer == peer, + .gone, .check, .joined => |p| p == peer, + .join, .hashed => false, + }; + if (!mine) { + i += 1; + continue; + } + switch (ps.inbox.items[i]) { + .input, .screen => |in| ps.gpa.free(in.msg), + else => {}, + } + _ = ps.inbox.orderedRemove(i); + } + ps.lock.unlock(io); + ps.gpa.destroy(peer); + } + + /// Every client told `why` and let go, its view with it: an Upgrade's + /// exec is next (tty.zig `upgradeNow`). What each was sent goes first, + /// for up to 200 ms; a client then dials the session again. + pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void { + if (ps.count() == 0) return; + var out: [600]u8 = undefined; + const words = wire.encodeServer(&out, .{ .refused = why }) catch return; + const io = ps.io; + for (ps.list) |slot| if (slot) |peer| { + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + }; + const until = nowNs() + 200 * std.time.ns_per_ms; + while (nowNs() < until) { + const busy = for (ps.list) |slot| { + if (slot) |peer| if (!peer.done.load(.acquire)) break true; + } else false; + if (!busy) break; + const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); + } + + /// A join's dump and pins let go: written, or its client gone. + fn released(peer: *Peer) void { + for (peer.blobs.items) |b| pardes.memory.unpin(b.bytes); + peer.blobs.deinit(peer.dump_gpa); + peer.blobs = .empty; + if (peer.dump.len > 0) peer.dump_gpa.free(peer.dump); + peer.dump = &.{}; + } + + /// Detach typed in client view `k`'s: its link ends (its reader says + /// it is gone, and the next boundary lets it go). + pub fn detach(ps: *Peers, k: u8) bool { + for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { + _ = libc.shutdown(peer.fd, 2); + return true; + }; + return false; + } + + fn stop(ps: *Peers, core: *Pardes) void { + if (!ps.recording) return; + terminal.resizeDeferred(core); + ps.screens.deinit(core); + if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; + tape.on = null; + tape.release(&ps.t); + ps.recording = false; + } + + /// Every client let go (the session's end, a Restore): each sees its + /// link end. + pub fn dropAll(ps: *Peers, core: *Pardes) void { + ps.drop(core, false); + } + + /// ...each view kept for its client if `again` (an Upgrade's). + fn drop(ps: *Peers, core: *Pardes, again: bool) void { + ps.unhashedDone(); + for (ps.held.items) |j| _ = libc.close(j.fd); + ps.held.clearRetainingCapacity(); + ps.rounds = 0; + for (&ps.list) |*slot| if (slot.*) |peer| { + slot.* = null; + core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); + ps.closeViewOf(core, peer, again); + ps.screens.dropPeer(peer.sid); + ps.letGo(peer); + }; + ps.stop(core); + ps.lock.lockUncancelable(ps.io); + for (ps.inbox.items) |note| switch (note) { + .join => |j| _ = libc.close(j.fd), + .input, .screen => |in| ps.gpa.free(in.msg), + .gone, .check, .joined, .hashed => {}, + }; + ps.inbox.clearRetainingCapacity(); + ps.lock.unlock(ps.io); + core.unattended = ps.alone_unattended; + } + + pub fn deinit(ps: *Peers) void { + if (std.c.getenv("PARDES_REPLICA_STATS")) |path| if (ps.input_ns.items.len > 0) ps.writeStats(std.mem.span(path)); + ps.inbox.deinit(ps.gpa); + ps.taken.deinit(ps.gpa); + ps.input_ns.deinit(ps.gpa); + ps.boundary_ns.deinit(ps.gpa); + ps.msgs.deinit(ps.gpa); + ps.held.deinit(ps.gpa); + ps.texts.deinit(std.heap.c_allocator); + ps.each_join.deinit(ps.gpa); + } + + fn writeStats(ps: *Peers, path: []const u8) void { + var buf: [400]u8 = undefined; + const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} state_kb {d} cut {d}\n", .{ + ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000, + ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000, + ps.join_ns / 1000, ps.dump_bytes >> 10, ps.state_bytes >> 10, ps.cut, + }) catch return; + var z: [4096]u8 = undefined; + const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return; + const fd = libc.open(pz, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = libc.write(fd, line.ptr, line.len); + var each: [400]u8 = undefined; + var w: std.Io.Writer = .fixed(&each); + w.writeAll("session joins_us") catch {}; + for (ps.each_join.items) |ns| w.print(" {d}", .{ns / 1000}) catch {}; + w.writeAll("\n") catch {}; + _ = libc.write(fd, w.buffered().ptr, w.buffered().len); + _ = libc.close(fd); + } +}; + +// ---- the client's half ---- + +pub const Joined = struct { + fd: c_int, + pid: u32, + client: u16, + view: u8, + gmtoff: i32, + dump: []u8, + /// The session's digest as dumped (`Replica.loaded`). + digest: u64 = 0, + /// Its build's (wire.semantics): the daemon judged the hello by what it + /// last heard, which an upgrade under way may not have said yet. + semantics: u64 = 0, + /// What was read past the dump: the stream's (`readStream`). + rest: Framer = .{}, + /// A far join's blobs that came over the link, for `Have.store` (the C + /// heap's), and how it went: its blobs, those it held already (from the + /// core it left, from disk) and the bytes of the ones it was sent. + got: []Got = &.{}, + blobs: u32 = 0, + from_memory: u32 = 0, + from_disk: u32 = 0, + sent_bytes: u64 = 0, +}; + +/// A blob a far join was sent: where it is in the dump, and its hash. +pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; + +/// Connected to the replica socket at `path`, its hello said for +/// `session`, welcomed, and the dump read whole: what a client joins with. +/// A far client (`have`) is sent its dump's blobs by hash first, and asks +/// for those it holds nowhere. Errors are said in `why`. `view`: the one +/// it had, joining again for it. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (fd < 0) return said(why, "no socket", .{}); + var addr: libc.sockaddr.un = .{ .path = @splat(0) }; + if (path.len >= addr.path.len) { + _ = libc.close(fd); + return said(why, "the socket's path is too long: {s}", .{path}); + } + @memcpy(addr.path[0..path.len], path); + if (libc.connect(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) { + _ = libc.close(fd); + return said(why, "nothing answers at {s}", .{path}); + } + return joinOn(gpa, fd, session, why, have, view); +} + +/// `dial`'s, on a connection made: the hello, then the join. The +/// connection is closed if the join fails. +fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { + var out: [wire.hello_max + 16]u8 = undefined; + const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { + _ = libc.close(fd); + return said(why, "that session's name is too long", .{}); + }; + // Its first word, in the same write: whether it is far. + const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; + var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; + const Read = struct { + j: *Joined, + gpa: Allocator, + why: []u8, + have: ?*Have, + failed: ?[]const u8 = null, + welcomed: bool = false, + done: bool = false, + /// The join's list of blobs (`blob_entry` each), as it comes. + list: std.ArrayListUnmanaged(u8) = .empty, + /// Where what comes fills the dump, in order (each a place and a + /// length): the state's runs between the blobs, then each blob + /// asked for. A blob listed twice is copied once the dump is whole. + runs: std.ArrayListUnmanaged([2]usize) = .empty, + run: usize = 0, + run_at: usize = 0, + dups: std.ArrayListUnmanaged([3]usize) = .empty, + + fn deinit(r: *@This()) void { + r.list.deinit(r.gpa); + r.runs.deinit(r.gpa); + r.dups.deinit(r.gpa); + } + + fn fail(r: *@This(), words: []const u8) bool { + r.failed = words; + return false; + } + + fn take(r: *@This(), tag: u8, payload: []const u8) bool { + const msg = wire.decodeServer(tag, payload) catch return r.fail("the session's answer was not understood"); + switch (msg) { + .welcome => |w| { + r.j.pid = w.pid; + r.j.client = w.client; + r.j.view = w.view; + r.j.gmtoff = w.gmtoff; + r.j.digest = w.digest; + r.j.semantics = w.semantics; + r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch return r.fail("out of memory for the session's dump"); + r.welcomed = true; + r.j.blobs = if (r.have != null) w.blobs else 0; + // A near client's dump comes whole, and a far one's + // without blobs is its state. + if (r.j.blobs == 0) r.runs.append(r.gpa, .{ 0, r.j.dump.len }) catch return r.fail("out of memory for the session's dump"); + r.done = w.dump_len == 0; + return !r.done; + }, + .refused => |words| { + r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused"; + return false; + }, + .blobs => |entries| { + const whole = @as(usize, r.j.blobs) * blob_entry; + if (r.runs.items.len > 0 or r.list.items.len + entries.len > whole) return r.fail("the session sent more of its blobs' list than it said"); + r.list.appendSlice(r.gpa, entries) catch return r.fail("out of memory for the session's blobs' list"); + if (r.list.items.len == whole) return r.lacking(); + return true; + }, + .dump => |piece| { + if (r.runs.items.len == 0) return r.fail("the session sent its dump before its blobs' list"); + var p = piece; + while (p.len > 0) { + if (r.run == r.runs.items.len) return r.fail("the session sent more dump than it said"); + const run = r.runs.items[r.run]; + const n = @min(p.len, run[1] - r.run_at); + @memcpy(r.j.dump[run[0] + r.run_at ..][0..n], p[0..n]); + r.run_at += n; + p = p[n..]; + if (r.run_at == run[1]) { + r.run += 1; + r.run_at = 0; + } + } + if (r.run < r.runs.items.len) return true; + for (r.dups.items) |d| @memcpy(r.j.dump[d[0]..][0..d[2]], r.j.dump[d[1]..][0..d[2]]); + r.done = true; + return false; + }, + else => return r.fail("the session sent a unit before its dump"), + } + } + + /// The list whole: each blob put in place from what the client + /// holds (`Have.find`), or asked for, and the runs to fill made. + fn lacking(r: *@This()) bool { + const h = r.have.?; + const dump = r.j.dump; + const n: usize = r.j.blobs; + const bits = r.gpa.alloc(u8, (n + 7) / 8) catch return r.fail("out of memory for the session's blobs' list"); + defer r.gpa.free(bits); + @memset(bits, 0); + var got: std.ArrayListUnmanaged(Got) = .empty; + defer got.deinit(std.heap.c_allocator); + var firsts: std.AutoHashMapUnmanaged([32]u8, [2]usize) = .empty; + defer firsts.deinit(r.gpa); + var shift: usize = 0; + var end: usize = 0; + for (0..n) |i| { + const e = r.list.items[i * blob_entry ..][0..blob_entry]; + const len = std.math.cast(usize, std.mem.readInt(u64, e[8..16], .little)) orelse return r.fail("the session's list of blobs is malformed"); + const pos = std.math.add(usize, std.math.cast(usize, std.mem.readInt(u64, e[0..8], .little)) orelse maxInt, shift) catch maxInt; + if (pos < end or pos > dump.len or len > dump.len - pos) return r.fail("the session's list of blobs is malformed"); + shift += len; + if (pos > end) r.runs.append(r.gpa, .{ end, pos - end }) catch return r.fail("out of memory for the session's blobs' list"); + end = pos + len; + const hash = e[16..48]; + const first = firsts.getOrPut(r.gpa, hash.*) catch return r.fail("out of memory for the session's blobs' list"); + if (first.found_existing and first.value_ptr[1] == len) { + r.dups.append(r.gpa, .{ pos, first.value_ptr[0], len }) catch return r.fail("out of memory for the session's blobs' list"); + continue; + } + first.value_ptr.* = .{ pos, len }; + if (h.find(hash, dump[pos..][0..len])) |where| switch (where) { + .memory => r.j.from_memory += 1, + .disk => r.j.from_disk += 1, + } else { + bits[i / 8] |= @as(u8, 1) << @intCast(i % 8); + got.append(std.heap.c_allocator, .{ .pos = pos, .len = len, .hash = hash.* }) catch return r.fail("out of memory for the session's blobs' list"); + r.j.sent_bytes += len; + } + } + if (dump.len > end) r.runs.append(r.gpa, .{ end, dump.len - end }) catch return r.fail("out of memory for the session's blobs' list"); + for (got.items) |g| r.runs.append(r.gpa, .{ g.pos, g.len }) catch return r.fail("out of memory for the session's blobs' list"); + const head = wire.header(@backingInt(wire.ClientTag.want), bits.len); + if (!writeAll(r.j.fd, &head) or !writeAll(r.j.fd, bits)) return r.fail("the session hung up"); + r.j.got = got.toOwnedSlice(std.heap.c_allocator) catch return r.fail("out of memory for the session's blobs' list"); + return true; + } + const maxInt = std.math.maxInt(usize); + }; + if (!writeAll(fd, out[0 .. hello.len + far.len])) { + _ = libc.close(fd); + return said(why, "the session hung up", .{}); + } + var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have }; + defer r.deinit(); + j.rest.run(gpa, fd, &r, Read.take); + if (r.failed != null or !r.done) { + j.rest.deinit(gpa); + if (j.dump.len > 0) gpa.free(j.dump); + std.heap.c_allocator.free(j.got); + _ = libc.close(fd); + const text = r.failed orelse "the session hung up before the dump was whole"; + if (text.ptr == why.ptr) return null; + return said(why, "{s}", .{text}); + } + return j; +} + +// ---- a far client's blobs ---- + +/// What a far client holds of a join's blobs already, by content (`dial`): +/// the cache on disk, and on a rejoin the blobs of the core it leaves. +pub const Have = struct { + io: std.Io, + cache: ?Cache = null, + /// The core it leaves (`leaving`), each blob hashed once when one of its + /// length is looked for. ponytail: a scan a lookup, fine to some + /// thousands of blobs; a map by length would take over past that. + old: std.ArrayListUnmanaged([]const u8) = .empty, + old_hash: std.ArrayListUnmanaged(?[32]u8) = .empty, + /// The thread writing the last join's blobs to disk (`store`). + storing: ?std.Thread = null, + + pub fn init(io: std.Io) Have { + return .{ .io = io, .cache = Cache.open(io) }; + } + + pub fn deinit(h: *Have) void { + h.wait(); + h.left(); + h.old.deinit(std.heap.c_allocator); + h.old_hash.deinit(std.heap.c_allocator); + if (h.cache) |c| c.dir.close(h.io); + } + + fn wait(h: *Have) void { + if (h.storing) |t| t.join(); + h.storing = null; + } + + /// `core`'s blobs, looked at by the next join until `left`: a rejoin + /// takes what it held, by content, before the core goes. + pub fn leaving(h: *Have, core: *const Pardes) void { + h.left(); + walk.blobs(Pardes, core, &h.old); + h.old_hash.appendNTimes(std.heap.c_allocator, null, h.old.items.len) catch h.old.clearRetainingCapacity(); + } + + pub fn left(h: *Have) void { + h.old.clearRetainingCapacity(); + h.old_hash.clearRetainingCapacity(); + } + + pub const Where = enum { memory, disk }; + + /// The blob of `hash` put in `out` (its length), from the core it + /// leaves or the cache; null when it holds it nowhere. + fn find(h: *Have, hash: *const [32]u8, out: []u8) ?Where { + for (h.old.items, h.old_hash.items) |b, *word| { + if (b.len != out.len) continue; + if (word.* == null) { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + word.* = d; + } + if (!std.mem.eql(u8, &word.*.?, hash)) continue; + @memcpy(out, b); + return .memory; + } + if (h.cache) |c| if (c.get(h.io, hash, out)) return .disk; + return null; + } + + /// The blobs a join was sent, written to the cache on a thread of their + /// own, which frees the dump (`gpa`'s) and the list after: the first + /// frame waits for none of it. + pub fn store(h: *Have, gpa: Allocator, j: *Joined) void { + const dump = j.dump; + const got = j.got; + j.dump = &.{}; + j.got = &.{}; + h.wait(); + const Store = struct { + fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got) void { + defer { + a.free(bytes); + std.heap.c_allocator.free(list); + } + var incoming: u64 = 0; + for (list) |g| incoming += g.len; + c.trim(io, @min(incoming, c.cap)); + var put: u64 = 0; + for (list) |g| { + if (put + g.len > c.cap) continue; + put += g.len; + c.put(io, &g.hash, bytes[g.pos..][0..g.len]); + } + } + }; + const c = h.cache orelse { + gpa.free(dump); + std.heap.c_allocator.free(got); + return; + }; + h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got }) catch { + Store.run(c, h.io, gpa, dump, got); + return; + }; + } + + /// `core`'s blobs the cache lacks, written there: what this client made + /// since it joined (an edit's text, its undo), for its next start. + pub fn keep(h: *Have, core: *const Pardes) void { + h.wait(); + const c = h.cache orelse return; + var all: std.ArrayListUnmanaged([]const u8) = .empty; + defer all.deinit(std.heap.c_allocator); + walk.blobs(Pardes, core, &all); + var missing: std.ArrayListUnmanaged(struct { bytes: []const u8, hash: [32]u8 }) = .empty; + defer missing.deinit(std.heap.c_allocator); + var incoming: u64 = 0; + for (all.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + if (c.has(h.io, &d, b.len)) continue; + missing.append(std.heap.c_allocator, .{ .bytes = b, .hash = d }) catch return; + incoming += b.len; + } + if (missing.items.len == 0) return; + c.trim(h.io, @min(incoming, c.cap)); + var put: u64 = 0; + for (missing.items) |m| { + if (put + m.bytes.len > c.cap) continue; + put += m.bytes.len; + c.put(h.io, &m.hash, m.bytes); + } + } +}; + +/// A far client's blobs on disk, a file each named by its BLAKE3 hash in +/// hex, in $XDG_CACHE_HOME/pardes/blobs (else ~/.cache/pardes/blobs): what +/// a client started again, or a second one on this machine, is not sent +/// again. The cache directory and not the runtime one: the blobs are worth +/// keeping across a logout or a reboot and safe to lose, and they can be +/// hundreds of megabytes, which the runtime directory (a tmpfs, often +/// capped at a tenth of memory and emptied at logout) is not for. A file +/// read back is hashed again: one cut short, corrupted or planted is a +/// miss and goes, never wrong content. +pub const Cache = struct { + dir: std.Io.Dir, + /// Bytes it keeps at most: PARDES_BLOB_CACHE_MB, else 1 GiB. + cap: u64, + + pub fn open(io: std.Io) ?Cache { + var buf: [4096]u8 = undefined; + const base = if (std.c.getenv("XDG_CACHE_HOME")) |x| std.mem.span(x) else ""; + const path = if (base.len > 0 and base[0] == '/') + std.fmt.bufPrint(&buf, "{s}/pardes/blobs", .{base}) catch return null + else if (std.c.getenv("HOME")) |home| + std.fmt.bufPrint(&buf, "{s}/.cache/pardes/blobs", .{std.mem.span(home)}) catch return null + else + return null; + const cwd = std.Io.Dir.cwd(); + cwd.createDirPath(io, std.fs.path.dirname(path).?) catch return null; + cwd.createDir(io, path, .fromMode(0o700)) catch |err| if (err != error.PathAlreadyExists) return null; + const mb = if (std.c.getenv("PARDES_BLOB_CACHE_MB")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 1024 else 1024; + return .{ .dir = cwd.openDir(io, path, .{ .iterate = true }) catch return null, .cap = mb << 20 }; + } + + fn name(hash: *const [32]u8) [64]u8 { + return std.fmt.bytesToHex(hash.*, .lower); + } + + /// The blob of `hash` read into `out` (its length) and hashed again. + /// A file of another length or content is a miss, and goes; a hit's + /// time is made now, so the least recently used go first (`trim`). + pub fn get(c: Cache, io: std.Io, hash: *const [32]u8, out: []u8) bool { + const file = name(hash); + var f = c.dir.openFile(io, &file, .{}) catch return false; + const ok = ok: { + defer f.close(io); + const st = f.stat(io) catch break :ok false; + if (st.size != out.len) break :ok false; + var reader = f.reader(io, &.{}); + const n = reader.interface.readSliceShort(out) catch break :ok false; + if (n != out.len) break :ok false; + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(out, &d, .{}); + break :ok std.mem.eql(u8, &d, hash); + }; + if (!ok) { + c.dir.deleteFile(io, &file) catch {}; + return false; + } + c.dir.setTimestampsNow(io, &file, .{}) catch {}; + return true; + } + + /// Whether a file of `len` bytes is there under `hash` (not read). + pub fn has(c: Cache, io: std.Io, hash: *const [32]u8, len: usize) bool { + const file = name(hash); + const st = c.dir.statFile(io, &file, .{}) catch return false; + return st.size == len; + } + + var tmp_serial: std.atomic.Value(u32) = .init(0); + + /// `bytes` kept under `hash`: written aside and renamed into place, so + /// that a reader never meets half a file (and would hash it wrong). + pub fn put(c: Cache, io: std.Io, hash: *const [32]u8, bytes: []const u8) void { + const file = name(hash); + if (c.has(io, hash, bytes.len)) return; + var buf: [96]u8 = undefined; + const tmp = std.fmt.bufPrint(&buf, ".tmp-{d}-{d}-{s}", .{ std.c.getpid(), tmp_serial.fetchAdd(1, .monotonic), file[0..16] }) catch return; + c.dir.writeFile(io, .{ .sub_path = tmp, .data = bytes }) catch { + c.dir.deleteFile(io, tmp) catch {}; + return; + }; + c.dir.rename(tmp, c.dir, &file, io) catch c.dir.deleteFile(io, tmp) catch {}; + } + + /// Room for `incoming` more bytes under the cap, the least recently + /// used files deleted first (a hit makes its file's time now). + /// ponytail: the directory scanned and sorted at each join's store; + /// fine to tens of thousands of blobs, where an index would take over. + pub fn trim(c: Cache, io: std.Io, incoming: u64) void { + const File = struct { name: [80]u8, len: u8, size: u64, mtime: i96 }; + var files: std.ArrayListUnmanaged(File) = .empty; + defer files.deinit(std.heap.c_allocator); + var total: u64 = 0; + var it = c.dir.iterate(); + while (it.next(io) catch null) |entry| { + if (entry.kind != .file or entry.name.len > 80) continue; + const st = c.dir.statFile(io, entry.name, .{}) catch continue; + var f: File = .{ .name = undefined, .len = @intCast(entry.name.len), .size = st.size, .mtime = st.mtime.nanoseconds }; + @memcpy(f.name[0..f.len], entry.name); + files.append(std.heap.c_allocator, f) catch return; + total += st.size; + } + if (total + incoming <= c.cap) return; + std.mem.sort(File, files.items, {}, struct { + fn older(_: void, a: File, b: File) bool { + return a.mtime < b.mtime; + } + }.older); + for (files.items) |f| { + if (total + incoming <= c.cap) break; + c.dir.deleteFile(io, f.name[0..f.len]) catch continue; + total -= f.size; + } + } +}; + +fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined { + _ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {}; + return null; +} + +/// A message `Replica.input` made, to the session: the event's frame (its +/// client and seq are the connection's). +pub fn sendInput(fd: c_int, msg: []const u8) bool { + return writeAll(fd, msg[head_len..]); +} + +/// A message `Replica.screen` or `screenRequest` made, to the session. +pub fn sendScreen(fd: c_int, msg: []const u8) bool { + var buf: [64]u8 = undefined; + if (wire.header_len + msg.len > buf.len) { + const head = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + return writeAll(fd, &head) and writeAll(fd, msg); + } + buf[0..wire.header_len].* = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + @memcpy(buf[wire.header_len..][0..msg.len], msg); + return writeAll(fd, buf[0 .. wire.header_len + msg.len]); +} + +/// Asks the session's digest, answered in stream order. +pub fn askCheck(fd: c_int) bool { + var out: [16]u8 = undefined; + return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); +} + +/// The client's last word: `again`, it joins again for its view at once. +pub fn bye(fd: c_int, again: bool) void { + var out: [16]u8 = undefined; + if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} +} + +/// What follows the dump on the client's connection, each message handed +/// to `take` (a unit, a terminal's delta) until the link ends; `rest` is +/// what the dump's reader read past it. +pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + rest.run(gpa, fd, ctx, take); +} + +/// 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=<file>: 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=<n>: 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.cut(); + 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.cut(); + 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.cut(); + break; + }; + } + + /// A write to the session failed: it hung up (it cut this client off, + /// lagging), as its reader will say. + fn cut(c: *Link) void { + c.quit = true; + c.gone = true; + } + + /// Each pass of the frontend's loop: quiet (nothing of its own + /// pending) 300 ms after its last input and its last check, and + /// changed since, the session's digest is asked once, answered in + /// 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); + if (!c.asked) c.cut(); + 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=<file>: 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: [1024]u8 = undefined; + const e = &c.r.echo.stats; + var w: std.Io.Writer = .fixed(&buf); + w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}", .{ + st.inputs, st.speculated, st.stopped, st.units, st.fast, + c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, + pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, + pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, + c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, + }) catch return; + // What stopped its inputs, last: a word a pair before it. + w.writeAll(" stops ") catch return; + st.writeStops(&w) catch return; + w.writeByte('\n') catch return; + var z: [4096]u8 = undefined; + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, w.buffered()); + } + + /// 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`. + /// `patient`: after an Upgrade's `refused` alone, while the session is + /// away a moment; after a divergence a session that will not answer is + /// gone, and the client says so at once (it froze 20 s, 2026-10-30). + /// `view`: the one it had, which the session kept for it (views.away). + pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined { + var tries: u32 = 0; + while (true) : (tries += 1) { + why.* = @splat(0); + if (dial(gpa, path, session, why, have, view)) |j| { + if (j.semantics == wire.semantics) return j; + bye(j.fd, false); + _ = 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 (!patient or tries > 200) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + } +}; + +test "a client joining again after a divergence is told at once that its session is gone" { + var why: [600]u8 = undefined; + const t0 = nowNs(); + try std.testing.expect(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); + try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); +} + +fn appendTo(path: [:0]const u8, line: []const u8) void { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644)); + if (fd < 0) return; + _ = 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(); + return tape.digestOf(a.core); +} + +const TestHost = struct { + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +/// An edit's file time is the clock's second, which a replica guesses: a +/// second's turn between a key and the authority's take of it is a fair +/// misprediction (11 runs in 40 of a loaded Debug window build's test +/// below). The tests that count fast confirmations start early in a second. +fn freshSecond() void { + while (true) { + var ts: std.c.timespec = undefined; + if (std.c.clock_gettime(.REALTIME, &ts) != 0 or ts.nsec < 50 * std.time.ns_per_ms) return; + const rest: std.c.timespec = .{ .sec = 0, .nsec = std.time.ns_per_s - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } +} + +fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { + const text = [_]u8{@intCast(cp & 0x7f)}; + const msg = try r.input(.{ .key = .{ .cp = cp, .text = if (cp < 0x80 and cp >= 0x20) &text else "" } }); + try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); +} + +test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + freshSecond(); + for ("oabc") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + try std.testing.expectEqual(@as(usize, 5), r.applied); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(@as(u32, 5), r.stats.fast); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "an input's time is its client's: an edit's second confirms on the fast path though the authority takes it in the next" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try key(r, 'o', &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + // Typed 30 ms before a second's turn, taken 30 ms after it: the edit's + // file time is the clock's second, read in the step. + while (true) { + var ts: std.c.timespec = undefined; + _ = std.c.clock_gettime(.REALTIME, &ts); + if (ts.nsec >= 970 * std.time.ns_per_ms) break; + const rest: std.c.timespec = .{ .sec = 0, .nsec = 970 * std.time.ns_per_ms - ts.nsec }; + _ = std.c.nanosleep(&rest, null); + } + try key(r, 'x', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + const late: std.c.timespec = .{ .sec = 0, .nsec = 60 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&late, null); + r.confirm(try a.take(sent.items[1])); + r.settle(); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 2), r.stats.fast); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "another client's input first: a replica rolls back, replays it and applies its own again" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, false); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, false); + defer r2.deinit(); + var s1: std.ArrayListUnmanaged([]u8) = .empty; + var s2: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (s1.items) |m| gpa.free(m); + for (s2.items) |m| gpa.free(m); + s1.deinit(gpa); + s2.deinit(gpa); + } + freshSecond(); + for ("oone") |k| try key(r1, k, &s1); + for ("Otwo") |k| try key(r2, k, &s2); + // The authority hears r2 first, then r1; both hear the units in that + // order. + for ([_]*std.ArrayListUnmanaged([]u8){ &s2, &s1 }) |list| for (list.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + r2.confirm(u); + r2.settle(); + }; + for ([_]*Replica{ r1, r2 }) |r| { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + } + try std.testing.expect(r1.stats.foreign >= 1); + try std.testing.expectEqual(@as(u32, 4), r2.stats.fast); +} + +fn screensTo(a: *Authority, screens: *Screens, peer: u16, r: *Replica) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try screens.flush(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + // Typing goes to the text pane under the shell (the host's write of + // `active`, taped). + a.core.active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.file != null) break id; + } else return error.NoTextPane; + r.confirm(try a.local(.{ .focus = true })); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Typed into the file, at once: a copy is held. + try key(r, 'o', &sent); + try std.testing.expect(r.copy.held); + var line: [64]u8 = undefined; + for (0..3) |round| { + // The shell prints: its unit rolls the replica back and applies the + // key again; then its delta lands, under the new copy. + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); + r.confirm((try a.pending()).?); + r.settle(); + try std.testing.expect(r.copy.held); + try screensTo(a, &screens, peer, r); + // The rows the core keeps, read once under the copy. + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + } + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + gpa.free(m); + } + sent.clearRetainingCapacity(); + try screensTo(a, &screens, peer, r); + // Drawn (its rows read, between steps), then typed into: the copy keeps + // those rows. The shell prints, its delta lands under the copy, and then + // another's unit puts the copy back: the rows it kept are of an older + // grid and must not be read. + r.confirm(try a.local(.{ .focus = false })); + _ = try terminal.shellRows(r.core(), r.core().panes[shell].?); + try key(r, 'x', &sent); + for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try screensTo(a, &screens, peer, r); + r.confirm(try a.local(.{ .focus = true })); + r.settle(); + const n = a.core.panes[shell].?.rows; + const back = try terminal.shellRows(r.core(), r.core().panes[shell].?); + const now = try terminal.shellRows(a.core, a.core.panes[shell].?); + for (now[now.len - n ..], back[back.len - n ..]) |x, y| try std.testing.expectEqualStrings(x, y); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + try screensTo(a, &screens, peer, r); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + var dump_a: std.Io.Writer.Allocating = .init(gpa); + defer dump_a.deinit(); + var dump_r: std.Io.Writer.Allocating = .init(gpa); + defer dump_r.deinit(); + try screen_state.dumpScreen(&a.core.panes[shell].?.terminal.?.vt, &dump_a.writer); + try screen_state.dumpScreen(&r.core().panes[shell].?.terminal.?.vt, &dump_r.writer); + try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); + const rows_a = try terminal.shellRows(a.core, a.core.panes[shell].?); + const rows_r = try terminal.shellRows(r.core(), r.core().panes[shell].?); + // As many rows (the history is the authority's to the row), the screen's + // the same; above it, rows scrolled past more than a screen at once are + // holes until fetched. + try std.testing.expectEqual(rows_a.len, rows_r.len); + const shown = a.core.panes[shell].?.rows; + for (rows_a[rows_a.len - shown ..], rows_r[rows_r.len - shown ..]) |x, y| try std.testing.expectEqualStrings(x, y); +} + +test "a closed terminal brought back on a screen-state replica: its emulator on the C heap, never the rollback epoch's" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + r.confirm(try a.local(.{ .command = "Del" })); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + var line: [32]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}) })); + try std.testing.expect(r.diverged == null); + const back = for (r.core().panes) |slot| { + if (slot) |pane| if (pane.terminal) |state| break state; + } else return error.NotBack; + // Its deltas write it while a copy is held, and its screen replica's + // blobs are freed with the C heap's (`deinit`): the epoch's, a rollback + // freed what they held and the exit freed them behind its back (H21). + try std.testing.expect(r.epoch.owns(@intFromPtr(back)) == null); +} + +test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.deinit(); + a.core.active = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break id; + } else return error.NoShell; + const opened = try a.openView(2); + r2.view = opened.view; + for ([_]*Replica{ r1, r2 }) |r| r.confirm(opened.unit); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // The second client, at another size, types into the terminal: its + // view sizes it. Then it goes (H10, 2026-10-30: the first aborted). + try sent.append(gpa, try gpa.dupe(u8, try r2.input(.{ .resize = .{ .cols = 50, .rows = 20 } }))); + try key(r2, 'x', &sent); + for (sent.items) |m| { + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + r1.confirm(u); + r1.settle(); + } + r1.confirm(try a.closeView(2)); + try std.testing.expectEqual(@as(usize, 0), a.core.effects_len); + try std.testing.expectEqual(@as(usize, 0), r1.core().effects_len); + r1.ready(); + try std.testing.expect(r1.copy.held); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + try key(r1, 'y', &sent); + r1.confirm(try a.take(sent.items[0])); + r1.settle(); + if (r1.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r1.diverged == null and r1.quiet()); + try std.testing.expectEqual(digestOf(a), r1.digest()); +} + +/// What the session sends a peer around a step's unit (Peers.input): the +/// rows the step read that the peer lacks, then the sizes the step gave +/// terminals (`terminal.resizeDeferred`), then the unit. +fn fillTo(a: *Authority, screens: *Screens, peer: u16, r: *Replica) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try screens.fill(a.core, peer, &msgs); + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; + screens.filled(a.core); + terminal.resizeDeferred(a.core); +} + +test "a terminal sized again by a client's key: the step reads the grid it had, on both sides" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 50, .rows = 20, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + // History the replica holds as holes, of lines a narrow grid wraps in + // two, which a wider one unwraps. + var line: [96]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + // Out of terminal mode in the session's view, which sizes it; then a + // client's own view (a copy of it), wider: its first key sizes the + // terminal again, in that key's step (the soak's tty and window at two + // sizes, 2026-10-30: the client's replay read holes the session's step + // had read as text, and the fill came in the new grid's numbers). + r.confirm(try a.local(.{ .key = .{ .cp = 'b', .ctrl = true } })); + for ([_]u21{ 'g', 'g' }) |cp| r.confirm(try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); + const opened = try a.openView(1); + r.view = opened.view; + r.confirm(opened.unit); + try screensTo(a, &screens, peer, r); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + const Step = struct { + fn take(au: *Authority, sc: *Screens, pe: u16, rr: *Replica, msg: []const u8) !void { + try screensTo(au, sc, pe, rr); + const u = try std.testing.allocator.dupe(u8, try au.take(msg)); + defer std.testing.allocator.free(u); + try fillTo(au, sc, pe, rr); + rr.confirm(u); + rr.settle(); + } + }; + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .resize = .{ .cols = 120, .rows = 30 } }))); + try key(r, 'g', &sent); + try key(r, 'g', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + for (sent.items) |m| gpa.free(m); + sent.clearRetainingCapacity(); + // The session's view types into it, which sizes it back (the client's + // copy of its history all holes again, a new numbering); then the + // client's key that reads all of it, and sizes it again. + try screensTo(a, &screens, peer, r); + { + const was = Stepping.begin(&a.t, null); + a.core.toView(0); + a.core.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + a.core.step(); + was.end(); + } + a.seq += 1; + const back = try gpa.dupe(u8, try a.unit(0, a.seq)); + defer gpa.free(back); + try fillTo(a, &screens, peer, r); + r.confirm(back); + r.settle(); + try screensTo(a, &screens, peer, r); + try key(r, 'w', &sent); + try key(r, 'w', &sent); + for (sent.items) |m| try Step.take(a, &screens, peer, r, m); + try screensTo(a, &screens, peer, r); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +fn deliver(a: *Authority, screens: *Screens, peer: u16, r: *Replica, comptime what: enum { flush, fill }) !void { + const gpa = std.testing.allocator; + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + switch (what) { + .flush => try screens.flush(a.core, peer, &msgs), + .fill => { + try screens.fill(a.core, peer, &msgs); + screens.filled(a.core); + }, + } + for (msgs.items) |m| if (r.screen(m)) |reply| { + _ = try screens.take(a.core, peer, reply); + }; +} + +test "pardes's own keys over a terminal's history apply at once: its view moves here, a delta while they wait puts them on top again, the authority's ends the same" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + r.confirm(try a.local(.{ .focus = true })); + var line: [64]u8 = undefined; + for (0..200) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "history line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // Normal mode in the shell, up a few rows, then half a screen and more: + // the cursor leaves the view, and the view follows it back. + const keys = [_]pardes.Key{ + .{ .cp = a.core.opts.tty_toggle, .ctrl = true }, .{ .cp = 'k', .text = "k" }, .{ .cp = 'k', .text = "k" }, + .{ .cp = 'u', .ctrl = true }, .{ .cp = 'u', .ctrl = true }, .{ .cp = 'k', .text = "k" }, + }; + for (keys) |k| try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = k }))); + try std.testing.expectEqual(@as(u32, 0), r.stats.stopped); + try std.testing.expectEqual(sent.items.len, r.applied); + const here = r.core().panes[shell].?; + try std.testing.expect(terminal.viewOf(here) != null); + const scrolled = terminal.viewOf(here); + // The shell prints while they wait: its unit, then its delta, which + // sets the view the authority's (at the bottom still): what was applied + // goes on top of it again. + for (0..5) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "late line {d}\r\n", .{i}) } }); + r.confirm((try a.pending()).?); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + try std.testing.expectEqual(sent.items.len, r.applied); + try std.testing.expect(terminal.viewOf(r.core().panes[shell].?) != null); + // The authority takes them in order, the terminal sent before each and + // the rows its step read after, as the session's loop does. + for (sent.items) |m| { + try deliver(a, &screens, peer, r, .flush); + r.settle(); + const u = try gpa.dupe(u8, try a.take(m)); + defer gpa.free(u); + try deliver(a, &screens, peer, r, .fill); + r.confirm(u); + r.settle(); + } + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expect(terminal.viewOf(a.core.panes[shell].?) != null); + try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); + _ = scrolled; + // A rollback with nothing confirmed on top puts the view back: one more + // key up, and another's unit first. + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'u', .ctrl = true } }))); + const before = terminal.viewOf(a.core.panes[shell].?); + try std.testing.expect(terminal.viewOf(r.core().panes[shell].?).? < before.?); + r.confirm(try a.local(.{ .focus = false })); + r.settle(); + const last = try gpa.dupe(u8, try a.take(sent.items[sent.items.len - 1])); + defer gpa.free(last); + r.toConfirmed(); + try std.testing.expectEqual(before, terminal.viewOf(r.core().panes[shell].?)); + r.dirty = true; + r.settle(); + try deliver(a, &screens, peer, r, .fill); + r.confirm(last); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + r.settle(); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expectEqual(terminal.viewOf(a.core.panes[shell].?), terminal.viewOf(r.core().panes[shell].?)); +} + +/// p50 and p99 of `samples`, sorted in place. +fn spread(samples: []u64) [2]u64 { + std.mem.sort(u64, samples, {}, std.sort.asc(u64)); + return .{ samples[samples.len / 2], samples[@min(samples.len - 1, samples.len * 99 / 100)] }; +} + +test "the authority's units with a big file open: its blobs hashed once (PARDES_DIGEST_BENCH=<MB>)" { + const arg = std.c.getenv("PARDES_DIGEST_BENCH") orelse return error.SkipZigTest; + const mb = std.fmt.parseInt(usize, std.mem.span(arg), 10) catch 100; + const gpa = std.heap.c_allocator; + const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); + var path_buf: [256]u8 = undefined; + const path = try std.fmt.bufPrintSentinel(&path_buf, "{s}/pardes-digest-bench.txt", .{tmp}, 0); + { + const fd = std.c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(std.c.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = std.c.close(fd); + var line: [80]u8 = undefined; + var n: usize = 0; + var i: usize = 0; + while (n < mb << 20) : (i += 1) { + const text = try std.fmt.bufPrint(&line, "line {d:0>9} of a big file, as a log or a dump is\n", .{i}); + if (std.c.write(fd, text.ptr, text.len) != @as(isize, @intCast(text.len))) return error.WriteFailed; + n += text.len; + } + } + defer _ = std.c.unlink(path); + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .file = path }, TestHost.host(), true); + defer a.deinit(); + _ = try a.unit(0, 0); + var unit_ns: [40]u64 = undefined; + var digest_ns: [40]u64 = undefined; + for ([_]bool{ false, true }) |on| { + walk.blob_words_on = on; + defer walk.blob_words_on = true; + walk.forgetAll(); + for (&unit_ns, &digest_ns, 0..) |*u, *d, k| { + // A key into the file: a unit as a replica is sent it. + const t0 = nowNs(); + _ = try a.local(.{ .key = .{ .cp = if (k % 2 == 0) 'j' else 'k', .text = if (k % 2 == 0) "j" else "k" } }); + u.* = nowNs() - t0; + const t1 = nowNs(); + _ = digestOf(a); + d.* = nowNs() - t1; + } + const us = spread(&unit_ns); + const ds = spread(&digest_ns); + std.debug.print("{d} MB file, blobs {s}: a unit p50 {d} us p99 {d} us; a digest p50 {d} us p99 {d} us\n", .{ mb, if (on) "hashed once" else "hashed whole", us[0] / 1000, us[1] / 1000, ds[0] / 1000, ds[1] / 1000 }); + } +} + +test "a replica joined from the session's dump replays the units after it" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .key = .{ .cp = 'j', .text = "j" } }); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + for ("oab") |k| try key(r, k, &sent); + try key(r, pardes.Key.escape, &sent); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { + const gpa = std.testing.allocator; + // Its terminals as screen state, as a session's for its clients are. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), true); + defer a.deinit(); + const was = Stepping.begin(&a.t, null); + const bytes = try pardes.dump.dumpBytes(a.core); + tape.resync(&a.t); + was.end(); + defer a.core.gpa.free(bytes); + const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 }); + defer r.deinit(); + // What tty.zig's and gui.zig's adoptShell and a failed reader do between + // steps: they wrote the core straight, and every client joined again. + const host = Stepping.begin(&a.t, null); + const id = a.core.adoptShell().?; + try std.testing.expect(a.core.panes[id].?.isTerminal() and !a.core.panes[id].?.greet); + a.core.readerFailed(id, error.InputOutput); + a.core.pump(a.host) catch {}; + host.end(); + a.seq += 1; + r.confirm(try a.unit(0, a.seq)); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a join's dump, its blobs put back where they go, is the session's dump" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 20000); + defer gpa.free(text); + @memset(text, 'x'); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + try std.testing.expect(blobs.items.len > 0); + try std.testing.expect(state.len + 20000 <= whole.len); + var joined: std.ArrayListUnmanaged(u8) = .empty; + defer joined.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try joined.appendSlice(gpa, state[pos..b.at]); + try joined.appendSlice(gpa, b.bytes); + pos = b.at; + } + try joined.appendSlice(gpa, state[pos..]); + try std.testing.expectEqualSlices(u8, whole, joined.items); +} + +test "a cached blob is hashed again as it is read: one cut short, corrupted, planted or longer is a miss, and goes" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 1 << 20 }; + var text: [6000]u8 = undefined; + for (&text, 0..) |*b, i| b.* = @truncate(i *% 31 +% i / 7); + var hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&text, &hash, .{}); + const file = Cache.name(&hash); + var out: [6000]u8 = undefined; + try std.testing.expect(!c.get(io, &hash, &out)); + c.put(io, &hash, &text); + try std.testing.expect(c.has(io, &hash, text.len)); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); + var corrupted = text; + corrupted[4000] ^= 0x20; + const planted: [6000]u8 = @splat('x'); + var longer: [6001]u8 = undefined; + @memcpy(longer[0..6000], &text); + longer[6000] = '\n'; + for ([_][]const u8{ text[0..3000], &corrupted, &planted, &longer }) |bad| { + try tmp.dir.writeFile(io, .{ .sub_path = &file, .data = bad }); + @memset(&out, 0); + try std.testing.expect(!c.get(io, &hash, &out)); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, &file, .{})); + } + // ...and put back, it is read again. + c.put(io, &hash, &text); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); +} + +test "the blob cache stays under its cap, the least recently used going first" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 3 * 5000 }; + var blobs: [3][5000]u8 = undefined; + var hashes: [3][32]u8 = undefined; + for (&blobs, &hashes, 0..) |*b, *h, k| { + @memset(b, @intCast('a' + k)); + std.crypto.hash.Blake3.hash(b, h, .{}); + c.put(io, h, b); + // Each older than the next, a thousand seconds apart. + const name = Cache.name(h); + try tmp.dir.setTimestamps(io, &name, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, @intCast(k + 1)) * 1000 * std.time.ns_per_s } } }); + } + // The oldest read: it is the newest now. + var out: [5000]u8 = undefined; + try std.testing.expect(c.get(io, &hashes[0], &out)); + c.trim(io, 5000); + try std.testing.expect(c.has(io, &hashes[0], 5000)); + try std.testing.expect(!c.has(io, &hashes[1], 5000)); + try std.testing.expect(c.has(io, &hashes[2], 5000)); + // A file not a blob's counts, and goes by its time as well. + const junk: [9000]u8 = @splat(0); + try tmp.dir.writeFile(io, .{ .sub_path = ".tmp-1-1-0123456789abcdef", .data = &junk }); + try tmp.dir.setTimestamps(io, ".tmp-1-1-0123456789abcdef", .{ .modify_timestamp = .{ .new = .{ .nanoseconds = 500 * std.time.ns_per_s } } }); + c.trim(io, 5000); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".tmp-1-1-0123456789abcdef", .{})); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Under the cap with room to spare: nothing goes. + c.trim(io, 0); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Room for the whole cap: everything goes, the older first. + c.trim(io, c.cap); + try std.testing.expect(!c.has(io, &hashes[0], 5000) and !c.has(io, &hashes[2], 5000)); +} + +/// A far join over a socket pair, the session's writer and reader threads +/// against the client's `joinOn`, as the daemon hands a connection over: +/// its hello read off first. +fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const state = pardes.dump.dumpJoin(core, &blobs) catch |err| { + _ = libc.close(fds[0]); + _ = libc.close(fds[1]); + return err; + }; + var whole = state.len; + for (blobs.items) |b| whole += b.bytes.len; + const peer = try ps.gpa.create(Peer); + peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa }; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + const Serve = struct { + fn run(p: *Peer) void { + var hello: [wire.header_len + 14]u8 = undefined; + var got: usize = 0; + while (got < hello.len) { + const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); + if (n <= 0) return p.peers.post(.{ .gone = p }); + got += @intCast(n); + } + p.readLoop(); + } + }; + peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer}); + peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); + defer ps.letGo(peer); + var why: [300]u8 = @splat(0); + return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse { + std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); + return error.JoinFailed; + }; +} + +fn endJoin(j: *Joined, have: ?*Have) void { + _ = libc.close(j.fd); + j.rest.deinit(std.testing.allocator); + if (have) |h| { + h.store(std.testing.allocator, j); + h.wait(); + } else { + std.testing.allocator.free(j.dump); + std.heap.c_allocator.free(j.got); + } +} + +test "a far join is sent each blob once: joined again with them on disk or in the core it leaves, the state alone; a cached file gone bad, that blob again" { + const gpa = std.testing.allocator; + const io = std.testing.io; + zstd.gpa = gpa; + defer zstd.gpa = std.heap.c_allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 30000); + defer gpa.free(text); + for (text, 0..) |*b, i| b.* = if (i % 61 == 60) '\n' else @intCast('a' + (i * 7 + i / 13) % 26); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + _ = try a.local(.{ .key = .{ .cp = 'o', .text = "o" } }); + _ = try a.local(.{ .paste = text[0..9000] }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + // What the session's list will say: its blobs, those of one text once. + var listed: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer listed.deinit(a.core.gpa); + a.core.gpa.free(try pardes.dump.dumpJoin(a.core, &listed)); + var distinct: std.AutoHashMapUnmanaged([32]u8, usize) = .empty; + defer distinct.deinit(gpa); + var distinct_bytes: u64 = 0; + var biggest: [32]u8 = undefined; + var biggest_len: usize = 0; + for (listed.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b.bytes, &d, .{}); + if ((try distinct.getOrPut(gpa, d)).found_existing) continue; + distinct_bytes += b.bytes.len; + if (b.bytes.len > biggest_len) { + biggest = d; + biggest_len = b.bytes.len; + } + } + try std.testing.expect(distinct.count() >= 3); + + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + var have: Have = .{ .io = io, .cache = .{ .dir = tmp.dir, .cap = 64 << 20 } }; + defer { + have.cache = null; + have.deinit(); + } + var ps: Peers = .{ .gpa = gpa, .io = io }; + defer ps.deinit(); + + // A near client: the dump whole, as before. + var j = try farJoinOnce(&ps, a.core, null); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(@as(u32, 0), j.blobs); + endJoin(&j, null); + // The first far join: every blob sent, once. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(listed.items.len, j.blobs); + try std.testing.expectEqual(@as(u32, 0), j.from_disk + j.from_memory); + try std.testing.expectEqual(distinct_bytes, j.sent_bytes); + endJoin(&j, &have); + // Again: all of them on disk. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_disk); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); + // The biggest's file corrupted: it alone is sent again. + const name = Cache.name(&biggest); + const junk = try gpa.alloc(u8, biggest_len); + defer gpa.free(junk); + @memset(junk, 'z'); + try tmp.dir.writeFile(io, .{ .sub_path = &name, .data = junk }); + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count() - 1, j.from_disk); + try std.testing.expectEqual(@as(u64, biggest_len), j.sent_bytes); + endJoin(&j, &have); + // A rejoin, no disk: what the core it leaves holds. + const cache = have.cache; + have.cache = null; + have.leaving(a.core); + j = try farJoinOnce(&ps, a.core, &have); + have.left(); + have.cache = cache; + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_memory); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); +} + +/// A replica joined from the session's dump as it is now, between units, +/// as `Peers.join` sends it: its state, its blobs put back where they go. +fn joinedNow(a: *Authority, id: u16, view: u8) !*Replica { + const gpa = std.testing.allocator; + const was = Stepping.begin(&a.t, null); + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer blobs.deinit(a.core.gpa); + const state = try pardes.dump.dumpJoin(a.core, &blobs); + defer a.core.gpa.free(state); + tape.resync(&a.t); + was.end(); + var whole: std.ArrayListUnmanaged(u8) = .empty; + defer whole.deinit(gpa); + var pos: usize = 0; + for (blobs.items) |b| { + try whole.appendSlice(gpa, state[pos..b.at]); + try whole.appendSlice(gpa, b.bytes); + pos = b.at; + } + try whole.appendSlice(gpa, state[pos..]); + return Replica.initDump(gpa, id, view, whole.items, .{ .cols = 80, .rows = 24 }); +} + +fn agrees(a: *Authority, r: *Replica) !void { + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expect(r.quiet()); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +test "a replica joined after a pane closed: the session's jump to it, its Panes and Recent rows, asked by either" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + // A file looked at and closed: a jump to it, Recent's, a closed pane. + var cwd: [std.fs.max_path_bytes]u8 = undefined; + var line: [std.fs.max_path_bytes + 32]u8 = undefined; + const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) }); + _ = try a.local(.{ .command = "Del" }); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + try std.testing.expect(a.core.njumps > 1); + // What another window of the user's wrote to the store, which the + // session read (src/variants_test.zig's case, the variant hunt's). + a.core.recent.opened(a.core.recentGpa(), try std.fmt.bufPrint(&line, "{s}/build.zig", .{dir})); + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + r.confirm(try a.local(.{ .command = "Search Panes" })); + r.confirm(try a.local(.{ .command = "Search Recent" })); + try agrees(a, r); + // The replica's own: it waits for the session's rows. + for ([_][]const u8{ "Search Panes", "Search Recent" }) |asked| { + const msg = try gpa.dupe(u8, try r.input(.{ .command = asked })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + } + try agrees(a, r); + // The closed pane brought back by the replica's Look at it: the + // session's record of it, off the tape. + { + const closed = try std.fmt.bufPrint(&line, "Look @p{d}", .{a.core.store.closed.items[0].serial}); + const msg = try gpa.dupe(u8, try r.input(.{ .command = closed })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(@as(usize, 0), a.core.store.closed.items.len); + try agrees(a, r); + r.confirm(try a.local(.{ .command = "Del" })); + } + // A jump back onto the closed file: opened again, from Recent. + for (0..3) |_| { + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'o', .ctrl = true } })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + } + try agrees(a, r); +} + +test "a replica's Del of a file its session's Recent knows asks the host what the session asked" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var cwd: [std.fs.max_path_bytes]u8 = undefined; + var line: [std.fs.max_path_bytes + 32]u8 = undefined; + const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); + _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/build.zig.zon", .{dir}) }); + // Joined while it is open: in the session's Recent, in no replica's. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + const msg = try gpa.dupe(u8, try r.input(.{ .command = "Del" })); + defer gpa.free(msg); + r.confirm(try a.take(msg)); + r.settle(); + try std.testing.expectEqual(@as(usize, 1), a.core.store.closed.items.len); + try agrees(a, r); +} + +/// A host with ptys whose pump polls for a frame, as a client's does. +const PtyHost = struct { + fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {} + fn poll(_: ?*anyopaque) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 { + const n = (try wire.encodeClient(buf[head_len..], .{ .event = ev })).len; + header(buf, client, seq); + return buf[0 .. head_len + n]; +} + +test "a client leaves, and one joins: what the leave queued (a terminal's resize) is done in its unit, on every side, before the join's dump" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, true); + defer old.deinit(); + // A second client, bigger, types into the terminal: it sizes it. + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .resize = .{ .cols = 120, .rows = 40 } }))); + old.confirm(try a.take(try inputOf(&buf, 2, 2, .{ .key = .{ .cp = 'x', .text = "x" } }))); + // It leaves: the terminal is the first view's size again, its pty's + // resize queued for the next pump; and a client joins at that boundary. + var was = Stepping.begin(&a.t, null); + a.core.closeView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + // The client left idle takes its copy ahead (Replica.ready): of a core + // with nothing queued. + old.ready(); + was = Stepping.begin(&a.t, null); + const k = a.core.openView() orelse return error.NoView; + was.end(); + old.confirm(try a.unit(0, 101)); + const joiner = try joinedNow(a, 3, k); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + // The next pump's unit, every client's. + const u = try gpa.dupe(u8, try a.local(.tick)); + defer gpa.free(u); + old.confirm(u); + joiner.confirm(u); + try agrees(a, old); + try agrees(a, joiner); +} + +test "a client that diverged joins again for its view: kept for it meanwhile, its pane in it again; the other client agrees" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, false); + defer old.deinit(); + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .command = "New" }))); + const made = a.core.active; + // Its bye says it joins again: its view is kept, as every replica keeps it. + var was = Stepping.begin(&a.t, null); + a.core.awayView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + try agrees(a, old); + // Back for it: its own, its pane in it, in the dump it loads. + was = Stepping.begin(&a.t, null); + try std.testing.expect(a.core.rejoinView(opened.view)); + was.end(); + old.confirm(try a.unit(0, 101)); + const back = try joinedNow(a, 3, opened.view); + defer back.deinit(); + try std.testing.expectEqual(digestOf(a), back.digest()); + a.view_of[3] = opened.view; + const u = try gpa.dupe(u8, try a.take(try back.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + defer gpa.free(u); + old.confirm(u); + back.confirm(u); + try std.testing.expectEqual(made, a.core.active); + try std.testing.expect(pardes.layout.findPane(a.core, made) != null); + try agrees(a, old); + try agrees(a, back); +} + +test "a client joined from a dump moves through another view's pane as the session does, its lines not wrapped" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The first client's Wrap off (SPC t w), and its cursor down into the + // file's long lines, near the bottom of its view. + var buf: [256]u8 = undefined; + var seq: u32 = 1; + const before = [_]pardes.Event{.{ .command = "Wrap" }} ++ + @as([10]pardes.Event, @splat(.{ .key = .{ .cp = 'd', .ctrl = true } })) ++ + @as([15]pardes.Event, @splat(.{ .key = .{ .cp = 'j', .text = "j" } })); + for (before) |ev| { + _ = try a.take(try inputOf(&buf, 1, seq, ev)); + seq += 1; + } + // Another client joins, its view not live; the first's keys go on. + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + for (0..3) |_| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, seq, .{ .key = .{ .cp = 'd', .ctrl = true } }))); + defer gpa.free(u); + seq += 1; + joiner.confirm(u); + try agrees(a, joiner); + } +} + +test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + // The session draws: its owner's colours painted for the view (File.refreshHighlights). + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try a.core.render(arena.allocator()); + } + _ = try a.openView(2); + const joiner = try joinedNow(a, 3, 1); + defer joiner.deinit(); + try std.testing.expectEqual(digestOf(a), joiner.digest()); + var buf: [256]u8 = undefined; + for ("/co", 1..) |c, seq| { + const u = try gpa.dupe(u8, try a.take(try inputOf(&buf, 1, @intCast(seq), .{ .key = .{ .cp = c, .text = &.{c} } }))); + defer gpa.free(u); + joiner.confirm(u); + try agrees(a, joiner); + } +} + +/// An input of a replica's in the session's order, as `Peers.input` takes +/// it: the terminals flushed to every peer, the step, the rows it read that +/// each peer holds as holes (`fill`), then its unit to every replica. +fn takeAll(a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, msg: []const u8) !void { + const gpa = std.testing.allocator; + for (rs, ps) |r, peer| try screensTo(a, screens, peer, r); + const u = try gpa.dupe(u8, try a.take(msg)); + defer gpa.free(u); + var rows: std.ArrayListUnmanaged([]u8) = .empty; + defer rows.deinit(gpa); + for (rs, ps) |r, peer| { + rows.clearRetainingCapacity(); + try screens.fill(a.core, peer, &rows); + for (rows.items) |m| { + _ = r.screen(m); + gpa.free(m); + } + } + screens.filled(a.core); + for (rs) |r| { + r.confirm(u); + r.settle(); + } +} + +fn sendAs(r: *Replica, a: *Authority, screens: *Screens, rs: []const *Replica, ps: []const u16, ev: pardes.Event) !void { + const msg = try std.testing.allocator.dupe(u8, try r.input(ev)); + defer std.testing.allocator.free(msg); + try takeAll(a, screens, rs, ps, msg); +} + +test "a step that reads a terminal's rows as the core keeps them sends a new client the ones it holds as holes" { + if (comptime !terminal.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const r1 = try Replica.init(gpa, 1, first, true); + defer r1.deinit(); + const r2 = try Replica.init(gpa, 2, first, true); + defer r2.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + a.core.active = shell; + var line: [64]u8 = undefined; + for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); + const out = try gpa.dupe(u8, (try a.pending()).?); + defer gpa.free(out); + for ([_]*Replica{ r1, r2 }) |r| r.confirm(out); + // The first client alone has the terminal; it reads all of its rows + // (its text, the cursor on the first): the core keeps them. + const p1 = [_]u16{try screens.addPeer()}; + for ([_]pardes.Event{ .{ .key = .{ .cp = 'b', .ctrl = true } }, .{ .key = .{ .cp = 'g', .text = "g" } }, .{ .key = .{ .cp = 'g', .text = "g" } } }) |ev| { + const msg = try gpa.dupe(u8, try r1.input(ev)); + defer gpa.free(msg); + try takeAll(a, &screens, &.{r1}, &p1, msg); + r2.confirm(a.out.items); + } + // A second comes: its history all holes. The first reads the rows the + // core kept, nothing written since. + const ps = [_]u16{ p1[0], try screens.addPeer() }; + try sendAs(r1, a, &screens, &.{ r1, r2 }, &ps, .{ .key = .{ .cp = 'O', .text = "O" } }); + for ([_]*Replica{ r1, r2 }) |r| try agrees(a, r); +} diff --git a/src/rollback.zig b/src/rollback.zig new file mode 100644 index 00000000..cc10d2cc --- /dev/null +++ b/src/rollback.zig @@ -0,0 +1,681 @@ +//! A replica's rollback copy (docs/next-daemon.md, phase 5): the state a +//! speculation can write, saved before it writes, and put back where it was +//! when the authority's order differs. +//! +//! Two halves: +//! - `Epoch`, the replica core's allocator. It knows every allocation, and +//! while a copy is held it frees nothing that was there before (a free +//! is put off) and grows nothing of then in place. What is allocated +//! meanwhile is the epoch's. So whatever the core held when the copy was +//! taken is still where it was when the copy is put back. +//! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a +//! digest covers, so views, buffers, registers, counters and terminal +//! metadata), saving the bytes of every allocation it reaches, the core +//! struct's own included. A file's text, its line index, its undo +//! snapshots and a register's texts are pinned instead: an edit puts a +//! new buffer in their place (File.setContentSpan, Registers.put) and +//! never writes one, so the copy keeps a reference and the put-off free +//! keeps it alive. +//! +//! Putting the copy back writes each saved allocation's bytes over it, so +//! every pointer in them points where it did, at what was there; then the +//! epoch's allocations are freed and its put-off frees forgotten. Letting +//! the copy go (the speculation was right) makes the put-off frees. +//! +//! ponytail: an allocation reached only through a field the walk leaves +//! out (a derived cache's own heap, say) is not saved; that cache is the +//! epoch's as it was written, which is right for one remade from the text +//! it was made of and wrong for one changed in place. The replica harness +//! (test/replicas.zig) checks a digest at every confirmed step. +const std = @import("std"); +const builtin = @import("builtin"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const Allocator = std.mem.Allocator; +const Alignment = std.mem.Alignment; +const Pardes = pardes.Pardes; + +/// Blobs (walk.blob: fields annotated `.blob`) this long or longer are +/// pinned, not copied. Checked unchanged in a safe build when the copy goes. +const blob_min = 4096; +const checked = builtin.mode == .debug or builtin.mode == .safe; + +/// An address as a hash map's key: a multiply, not a Wyhash of its bytes +/// (every allocation of a replica's core is a lookup). +const Addr = struct { + pub fn hash(_: Addr, a: usize) u64 { + return @as(u64, a) *% 0x9e3779b97f4a7c15; + } + pub fn eql(_: Addr, a: usize, b: usize) bool { + return a == b; + } +}; + +pub const Epoch = struct { + child: Allocator = std.heap.c_allocator, + /// Its own bookkeeping, never the epoch's. + meta: Allocator = std.heap.c_allocator, + live: std.HashMapUnmanaged(usize, Entry, Addr, std.hash_map.default_max_load_percentage) = .empty, + on: bool = false, + id: u32 = 0, + born: std.ArrayListUnmanaged(usize) = .empty, + deferred: std.ArrayListUnmanaged(usize) = .empty, + trees: pardes.syntax.HeldTrees = .{ .meta = std.heap.c_allocator }, + + const Entry = struct { len: usize, alignment: Alignment, epoch: u32, freed: bool = false }; + + pub fn allocator(e: *Epoch) Allocator { + return .{ .ptr = e, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + pub fn deinit(e: *Epoch) void { + var it = e.live.iterator(); + while (it.next()) |kv| e.child.rawFree(@as([*]u8, @ptrFromInt(kv.key_ptr.*))[0..kv.value_ptr.len], kv.value_ptr.alignment, @returnAddress()); + e.live.deinit(e.meta); + e.born.deinit(e.meta); + e.deferred.deinit(e.meta); + e.trees.born.deinit(e.meta); + e.trees.freed.deinit(e.meta); + } + + fn mine(e: *const Epoch, entry: Entry) bool { + return e.on and entry.epoch == e.id; + } + + /// The live allocation that starts at `addr`. + pub fn owns(e: *const Epoch, addr: usize) ?Entry { + const entry = e.live.get(addr) orelse return null; + return if (entry.freed) null else entry; + } + + fn note(e: *Epoch, addr: usize, len: usize, alignment: Alignment) bool { + e.live.put(e.meta, addr, .{ .len = len, .alignment = alignment, .epoch = if (e.on) e.id else 0 }) catch return false; + if (e.on) e.born.append(e.meta, addr) catch {}; + return true; + } + + /// Every new allocation filled with this: a core reading memory it never + /// wrote shows as a replica's divergence (test/replicas.zig, a release + /// build's heap is otherwise whatever it was). + pub var fill: ?u8 = null; + + fn alloc(ctx: *anyopaque, len: usize, alignment: Alignment, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const ptr = e.child.rawAlloc(len, alignment, ra) orelse return null; + if (fill) |b| @memset(ptr[0..len], b); + if (!e.note(@intFromPtr(ptr), len, alignment)) { + e.child.rawFree(ptr[0..len], alignment, ra); + return null; + } + return ptr; + } + + fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) bool { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const entry = e.live.getPtr(@intFromPtr(memory.ptr)) orelse return false; + // What the copy saved stays its size: the core moves instead. + if (e.on and !e.mine(entry.*)) return false; + if (!e.child.rawResize(memory, alignment, new_len, ra)) return false; + entry.len = new_len; + return true; + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ra: usize) ?[*]u8 { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const old = @intFromPtr(memory.ptr); + const entry = e.live.get(old) orelse return null; + if (e.on and !e.mine(entry)) return null; + const ptr = e.child.rawRemap(memory, alignment, new_len, ra) orelse return null; + _ = e.live.remove(old); + e.live.put(e.meta, @intFromPtr(ptr), .{ .len = new_len, .alignment = alignment, .epoch = entry.epoch }) catch {}; + if (e.mine(entry) and @intFromPtr(ptr) != old) e.born.append(e.meta, @intFromPtr(ptr)) catch {}; + return ptr; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: Alignment, ra: usize) void { + const e: *Epoch = @ptrCast(@alignCast(ctx)); + const addr = @intFromPtr(memory.ptr); + const entry = e.live.getPtr(addr) orelse return e.child.rawFree(memory, alignment, ra); + if (e.on and !e.mine(entry.*)) { + // Held by the copy until it is put back or let go. + if (!entry.freed) e.deferred.append(e.meta, addr) catch {}; + entry.freed = true; + return; + } + e.child.rawFree(memory.ptr[0..entry.len], entry.alignment, ra); + _ = e.live.remove(addr); + } + + fn reallyFree(e: *Epoch, addr: usize) void { + const entry = e.live.get(addr) orelse return; + e.child.rawFree(@as([*]u8, @ptrFromInt(addr))[0..entry.len], entry.alignment, @returnAddress()); + _ = e.live.remove(addr); + } + + fn begin(e: *Epoch) void { + std.debug.assert(!e.on); + e.id +%= 1; + if (e.id == 0) e.id = 1; + e.on = true; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + pardes.syntax.held_trees = &e.trees; + } + + /// The copy put back: what the epoch made goes, what it freed stays. + fn discard(e: *Epoch) void { + for (e.born.items) |addr| if (e.live.get(addr)) |entry| if (e.mine(entry)) e.reallyFree(addr); + for (e.deferred.items) |addr| if (e.live.getPtr(addr)) |entry| { + entry.freed = false; + }; + for (e.trees.born.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + /// The copy let go: what the epoch freed goes now. + fn keep(e: *Epoch) void { + for (e.deferred.items) |addr| e.reallyFree(addr); + for (e.trees.freed.items) |t| pardes.syntax.destroyTree(t); + e.end(); + } + + fn end(e: *Epoch) void { + e.on = false; + e.born.clearRetainingCapacity(); + e.deferred.clearRetainingCapacity(); + e.trees.born.clearRetainingCapacity(); + e.trees.freed.clearRetainingCapacity(); + if (pardes.syntax.held_trees == &e.trees) pardes.syntax.held_trees = null; + } + + /// The trees of whichever core is stepped next: one in-process harness + /// steps several (syntax.held_trees is process-wide). + pub fn select(e: ?*Epoch) void { + pardes.syntax.held_trees = if (e) |x| (if (x.on) &x.trees else null) else null; + } +}; + +pub const Copy = struct { + meta: Allocator = std.heap.c_allocator, + regions: std.ArrayListUnmanaged(Region) = .empty, + bytes: std.ArrayListUnmanaged(u8) = .empty, + seen: std.HashMapUnmanaged(Seen, void, SeenContext, std.hash_map.default_max_load_percentage) = .empty, + pins: std.ArrayListUnmanaged(Pin) = .empty, + held: bool = false, + epoch: ?*Epoch = null, + /// The field being walked, for a pin's report, and whether it is one + /// of the blobs: a file's text or an undo snapshot's. + where: []const u8 = "", + pinnable: bool = false, + + const Region = struct { addr: usize, off: usize, len: usize }; + const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; + const Seen = struct { addr: usize, ty: usize }; + const SeenContext = struct { + pub fn hash(_: SeenContext, k: Seen) u64 { + return (@as(u64, k.addr) *% 0x9e3779b97f4a7c15) ^ (@as(u64, k.ty) *% 0xc2b2ae3d27d4eb4f); + } + pub fn eql(_: SeenContext, a: Seen, b: Seen) bool { + return a.addr == b.addr and a.ty == b.ty; + } + }; + /// Queued: the core holds effects or inputs not drained yet, which no + /// copy is taken over (the replica's input waits for the authority). + pub const Error = Allocator.Error || error{Queued}; + + pub fn deinit(c: *Copy) void { + c.regions.deinit(c.meta); + c.bytes.deinit(c.meta); + c.seen.deinit(c.meta); + c.pins.deinit(c.meta); + } + + pub fn savedBytes(c: *const Copy) usize { + return c.bytes.items.len; + } + + pub fn pinnedBytes(c: *const Copy) usize { + var n: usize = 0; + for (c.pins.items) |pin| n += pin.len; + return n; + } + + /// The copy of `p`, whose allocator is `e`'s, taken: from now on what + /// it held stays where it is until the copy is put back or let go. + pub fn take(c: *Copy, e: *Epoch, p: *const Pardes) Error!void { + std.debug.assert(!c.held); + // A step drains these before it ends, and a copy is taken between + // steps; an input of a host's without one (a view closed) may leave + // some until it is drained. + if (p.effects_len != 0 or p.in_len != 0) return error.Queued; + c.regions.clearRetainingCapacity(); + c.bytes.clearRetainingCapacity(); + c.seen.clearRetainingCapacity(); + c.pins.clearRetainingCapacity(); + c.epoch = e; + if (c.epoch.?.owns(@intFromPtr(p)) == null) @panic("rollback: the core is not its epoch's"); + try c.object(Pardes, p); + e.begin(); + c.held = true; + } + + /// The core as it was when the copy was taken. + pub fn restore(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + for (c.regions.items) |r| @memcpy(@as([*]u8, @ptrFromInt(r.addr))[0..r.len], c.bytes.items[r.off..][0..r.len]); + c.epoch.?.discard(); + c.held = false; + } + + /// The speculation was right: the copy is not wanted. + pub fn release(c: *Copy) void { + std.debug.assert(c.held); + c.check(); + c.epoch.?.keep(); + c.held = false; + } + + pub fn check(c: *const Copy) void { + if (comptime !checked) return; + for (c.pins.items) |pin| if (std.hash.Wyhash.hash(0, @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]) != pin.hash) { + const now = @as([*]const u8, @ptrFromInt(pin.addr))[0..pin.len]; + const entry = c.epoch.?.live.get(pin.addr); + std.debug.print("rollback: {s}, {d} bytes at {x}, pinned, was written in place: now {any}; entry {any}, epoch {d}\n", .{ pin.where, pin.len, pin.addr, now[0..32], entry, c.epoch.?.id }); + @panic("rollback: a pinned blob was written in place"); + }; + } + + fn save(c: *Copy, addr: usize, len: usize) Error!void { + if (len == 0) return; + const off = c.bytes.items.len; + try c.bytes.appendSlice(c.meta, @as([*]const u8, @ptrFromInt(addr))[0..len]); + try c.regions.append(c.meta, .{ .addr = addr, .off = off, .len = len }); + } + + /// Whether the value at `addr` of type `T` was reached before. + fn reached(c: *Copy, comptime T: type, addr: usize) Error!bool { + const key: Seen = .{ .addr = addr, .ty = @intFromPtr(@typeName(T).ptr) }; + const got = try c.seen.getOrPut(c.meta, key); + return got.found_existing; + } + + /// The allocation at `addr`, saved once, whatever reaches it. + fn allocation(c: *Copy, addr: usize) Error!?usize { + const entry = c.epoch.?.owns(addr) orelse return null; + if (try c.reached(void, addr)) return entry.len; + try c.save(addr, entry.len); + return entry.len; + } + + fn visit(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime !heapIn(T, 0)) return; + switch (@typeInfo(T)) { + .optional => |o| if (v.*) |*payload| try c.visit(o.child, payload), + .array => |a| if (comptime heapIn(a.child, 0)) for (v) |*e| try c.visit(a.child, e), + .pointer => |p| switch (p.size) { + .one => try c.object(p.child, v.*), + .slice => try c.slice(p.child, v.*), + // No length to walk by: its keeper's (a C handle's buffer). + else => {}, + }, + .@"struct" => |s| { + if (comptime walk.machinery(T)) return; + // A terminal: the authority's (its emulator is never written + // by a speculation: terminal.zig's guards). + if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return; + if (s.layout == .@"packed") return; + // std's lists (a safe build's have a third field, a lock). + if (comptime walk.isList(T)) { + if (v.capacity == 0) return; + _ = try c.allocation(@intFromPtr(v.items.ptr)); + const E = @typeInfo(@FieldType(T, "items")).pointer.child; + if (comptime heapIn(E, 0)) for (v.items) |*e| try c.visit(E, e); + return; + } + if (comptime @hasDecl(T, "KV") and @hasDecl(T, "Hash") and @hasField(T, "metadata")) { + const m = v.metadata orelse return; + // std's header sits before the metadata, at the start + // of the one allocation. + const header = std.mem.alignForward(usize, 2 * @sizeOf(usize) + @sizeOf(u32), @alignOf(usize)); + _ = try c.allocation(@intFromPtr(m) - header); + var it = v.iterator(); + while (it.next()) |entry| { + try c.visit(@TypeOf(entry.key_ptr.*), entry.key_ptr); + try c.visit(@TypeOf(entry.value_ptr.*), entry.value_ptr); + } + return; + } + if (comptime @hasField(T, "raw") and s.field_names.len == 1 and @hasDecl(T, "load")) return; + if (comptime @hasField(T, "any_future")) return; + if (comptime @hasField(T, "bit_length") and @hasField(T, "masks") and s.field_names.len == 2) { + if (v.bit_length == 0) return; + _ = try c.allocation(@intFromPtr(v.masks) - @sizeOf(@typeInfo(@FieldType(T, "masks")).pointer.child)); + return; + } + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + // What a digest covers, the caches made from it and this + // process's own (the views not live are among it): a + // safe build's free writes over what it frees, so what + // the speculation freed would come back as that. Not the + // queues a step drains (undefined between steps), nor the + // authority's. + if (attr.@"comptime" or comptime (annotated(T, "authority", name) or ((annotated(T, "local", name) or annotated(T, "derived", name)) and unsaved(name)))) continue; + if (comptime opaqueTo(F)) continue; + c.where = @typeName(T) ++ "." ++ name; + c.pinnable = comptime walk.blob(T, name); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + const len = comptime walk.lengthOf(T, name).?; + if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); + } else try c.visit(F, &@field(v, name)); + } + }, + .@"union" => switch (v.*) { + inline else => |*payload| try c.visit(@TypeOf(payload.*), payload), + }, + .error_union => |e| if (v.*) |*payload| try c.visit(e.payload, payload) else |_| {}, + else => {}, + } + } + + fn object(c: *Copy, comptime P: type, ptr: *const P) Error!void { + if (comptime walk.machinery(P)) return; + const addr = @intFromPtr(ptr); + if (try c.reached(P, addr)) return; + if (comptime @hasDecl(P, "digest") and @TypeOf(P.digest) == fn (*const P, *walk.Hasher) void) return; + if (c.epoch.?.owns(addr) != null) { + _ = try c.reached(void, addr); + try c.fields(P, ptr); + } + try c.visit(P, ptr); + } + + /// A struct's bytes, field by field where it says what its fields are: + /// all of them (this process's own too, which the epoch's frees would + /// leave pointing at nothing), but the queues a step drains and the + /// frame statistics (numbers, megabytes of them), and an array only as + /// far as its length says. + fn fields(c: *Copy, comptime T: type, v: *const T) Error!void { + if (comptime @typeInfo(T) != .@"struct" or !@hasDecl(T, "replica")) return c.save(@intFromPtr(v), @sizeOf(T)); + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime") continue; + if (comptime (annotated(T, "local", name) or annotated(T, "derived", name) or annotated(T, "authority", name)) and unsaved(name)) continue; + const at = @intFromPtr(&@field(v, name)); + if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { + try c.save(at, walk.lengthIn(v, comptime walk.lengthOf(T, name).?) * @sizeOf(@typeInfo(F).array.child)); + } else if (comptime annotatedStruct(F)) { + try c.fields(F, &@field(v, name)); + } else if (comptime @typeInfo(F) == .array and annotatedStruct(@typeInfo(F).array.child)) { + for (&@field(v, name)) |*e| try c.fields(@typeInfo(F).array.child, e); + } else try c.save(at, @sizeOf(F)); + } + } + + fn slice(c: *Copy, comptime E: type, s: []const E) Error!void { + if (s.len == 0) return; + const addr = @intFromPtr(s.ptr); + if (try c.reached([]const E, addr)) return; + if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + // A blob of rows with allocations of their own is pinned whole: + // they are as immutable as it, and kept alive with it. + const bytes = std.mem.sliceAsBytes(s); + try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); + return; + } + _ = try c.allocation(addr); + if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e); + } +}; + +fn annotatedStruct(comptime T: type) bool { + return @typeInfo(T) == .@"struct" and @hasDecl(T, "replica"); +} + +/// The queues every step drains (empty between steps, asserted when a copy +/// is taken: what is in their slots is nothing), the frame statistics +/// (numbers, and a static name), and the rows a terminal was last read as +/// (a grid's worth of text, emptied when a copy is taken and when it is +/// put back: src/replica.zig), and a replica's predicted echo (src/echo.zig: +/// the replica's, never its core's). And the store and Recent, a replica's +/// own: on the host's heap (Pardes.recentGpa), which the epoch never puts a +/// free off for, so their bytes put back would point at what is freed. A +/// step reads what the authority holds of them off the tape +/// (tape.authority), never a replica's. +fn unsaved(comptime name: []const u8) bool { + return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames") or + std.mem.eql(u8, name, "shell_rows") or std.mem.eql(u8, name, "echo") or std.mem.eql(u8, name, "store") or + std.mem.eql(u8, name, "recent"); +} + +fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), kind)) return false; + inline for (@field(T.replica, kind)) |n| if (comptime std.mem.eql(u8, n, name)) return true; + return false; +} + +/// A field no walk sees into (a C handle, a function): its own keeper's. +fn opaqueTo(comptime F: type) bool { + return switch (@typeInfo(F)) { + .optional => |o| opaqueTo(o.child), + .pointer => |p| p.size == .many or p.size == .c or switch (@typeInfo(p.child)) { + .@"opaque", .@"fn" => true, + else => false, + }, + else => false, + }; +} + +/// Whether a `T` can hold a pointer to the heap at all (else a walk of it +/// finds nothing to save). Deep types count as yes. +fn heapIn(comptime T: type, comptime depth: u32) bool { + if (depth > 12) return true; + return switch (@typeInfo(T)) { + .void, .null, .undefined, .bool, .int, .float, .@"enum", .error_set, .comptime_int, .comptime_float, .enum_literal, .type => false, + .optional => |o| heapIn(o.child, depth + 1), + .array => |a| a.len > 0 and heapIn(a.child, depth + 1), + .pointer => true, + .error_union => |e| heapIn(e.payload, depth + 1), + .@"struct" => |s| blk: { + if (s.layout == .@"packed") break :blk false; + for (s.field_types, s.field_attrs) |F, attr| if (!attr.@"comptime" and heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + .@"union" => |u| blk: { + for (u.field_types) |F| if (heapIn(F, depth + 1)) break :blk true; + break :blk false; + }, + else => true, + }; +} + +test "a copy put back is the core as it was; one let go keeps what was done" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const before = walk.digest(Pardes, p); + var c: Copy = .{}; + defer c.deinit(); + const typed = "ox y z"; + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + const edited = walk.digest(Pardes, p); + try std.testing.expect(edited != before); + c.restore(); + try std.testing.expectEqual(before, walk.digest(Pardes, p)); + // The same again, let go this time: what was typed stays. + try c.take(&e, p); + for (typed) |k| p.update(.{ .key = .{ .cp = k, .text = &.{k} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + p.pump(.{}) catch {}; + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + c.release(); + try std.testing.expectEqual(edited, walk.digest(Pardes, p)); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +test "a view's slot while it is live keeps no pointer a rollback could put old bytes back through" { + // The C heap: a freed chunk comes back to the next allocation its size. + var e: Epoch = .{}; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + defer p.deinit(); + p.pump(.{}) catch {}; + _ = p.openView() orelse return error.NoView; + // View 0's column tag has a text of its own; out to view 1 and back. + const text = "the column tag of view 0, its own"; + p.col_tags[0].own = try gpa.dupe(u8, text); + p.toView(1); + p.toView(0); + // The live view drops it, and its memory is another's: something the + // copy has no business saving. + const was = p.col_tags[0].own.?; + gpa.free(was); + p.col_tags[0].own = null; + const other = try gpa.alloc(u8, text.len); + defer gpa.free(other); + if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another + @memset(other, 'a'); + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + // Written in place while the copy is held, then the copy put back. + @memset(other, 'b'); + c.restore(); + try std.testing.expect(std.mem.allEqual(u8, other, 'b')); +} + +test "a register's texts are pinned, not copied, and come back whole" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + // A terminal's whole grid yanked: megabytes, and joined once read. + const grid = try std.testing.allocator.alloc(u8, 2 << 20); + defer std.testing.allocator.free(grid); + @memset(grid, 'y'); + const reg = pardes.Registers.default; + p.registers.put(gpa, reg, grid, 0, 1, true); + _ = p.registers.text(gpa, reg) orelse return error.NoText; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.savedBytes() < 1 << 20); + try std.testing.expect(c.pinnedBytes() >= 4 << 20); + // A speculation yanks over it, and is undone. + p.registers.put(gpa, reg, "a word", 0, 1, true); + c.restore(); + const back = p.registers.value(reg, 0) orelse return error.Lost; + try std.testing.expectEqual(grid.len, back.len); + try std.testing.expect(std.mem.allEqual(u8, back, 'y')); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +test "a file's location rows are pinned whole, with what they own, and come back" { + const locations = @import("locations.zig"); + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const f = for (p.panes) |slot| { + if (slot) |pane| if (pane.file) |*file| break file; + } else return error.NoFile; + // A search's rows: each its own path. + const rows = try gpa.alloc(locations.Row, 500); + for (rows) |*row| row.* = .{ .kind = .match, .path = try gpa.dupe(u8, "src/rollback.zig"), .at = .{}, .location_end = 0, .code_start = 0 }; + f.location_rows = rows; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.pinnedBytes() >= rows.len * @sizeOf(locations.Row)); + // A speculation's new search drops them, and is undone. + locations.freeRows(gpa, f.location_rows); + f.location_rows = &.{}; + c.restore(); + try std.testing.expectEqual(@as(usize, 500), f.location_rows.len); + try std.testing.expectEqualStrings("src/rollback.zig", f.location_rows[499].path); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +/// `Look <name>`, the name a file of the repository's, absolute. +fn lookAt(buf: []u8, name: []const u8) ![]const u8 { + var cwd: [std.fs.max_path_bytes]u8 = undefined; + const dir = std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd; + return std.fmt.bufPrint(buf, "Look {s}/{s}", .{ std.mem.span(@as([*:0]u8, @ptrCast(dir))), name }); +} + +test "a rollback leaves the store and Recent as a speculation left them: they live on the host's heap, which no copy holds" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + var line: [std.fs.max_path_bytes + 16]u8 = undefined; + p.update(.{ .command = try lookAt(&line, "build.zig.zon") }); + p.update(.{ .command = "Del" }); + p.pump(.{}) catch {}; + try std.testing.expectEqual(@as(usize, 1), p.store.closed.items.len); + // Both lists full to what they hold: the next of each moves them. + p.store.closed.shrinkAndFree(p.recentGpa(), p.store.closed.items.len); + p.recent.entries.shrinkAndFree(p.recentGpa(), p.recent.entries.items.len); + const recents = p.recent.entries.items.len; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + // A speculation opens a file and closes it (a Del a replica applies at + // once), and is undone. + p.update(.{ .command = try lookAt(&line, "build.zig") }); + p.update(.{ .command = "Del" }); + p.pump(.{}) catch {}; + c.restore(); + // What the store and Recent hold is where they say it is. + try std.testing.expectEqual(@as(usize, 2), p.store.closed.items.len); + try std.testing.expect(std.mem.endsWith(u8, p.store.closed.items[1].name, "build.zig")); + try std.testing.expectEqual(recents + 1, p.recent.entries.items.len); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +test "a view drawn outside a step (a replica's own, another live) leaves the live view's slot holding nothing a rollback could put old bytes back through" { + var e: Epoch = .{}; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + defer p.deinit(); + p.pump(.{}) catch {}; + _ = p.openView() orelse return error.NoView; + const text = "the column tag of view 0, its own"; + p.col_tags[0].own = try gpa.dupe(u8, text); + // View 1 read (as a replica client's frontend reads its own) while view + // 0 is live: 0 parked and put back. + const Read = struct { + fn read(_: void, _: *Pardes) anyerror!void {} + }; + try pardes.views.within(p, 1, {}, Read.read); + const was = p.col_tags[0].own.?; + gpa.free(was); + p.col_tags[0].own = null; + const other = try gpa.alloc(u8, text.len); + defer gpa.free(other); + if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another + @memset(other, 'a'); + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + @memset(other, 'b'); + c.restore(); + try std.testing.expect(std.mem.allEqual(u8, other, 'b')); +} diff --git a/src/screen_state.zig b/src/screen_state.zig new file mode 100644 index 00000000..002c9da7 --- /dev/null +++ b/src/screen_state.zig @@ -0,0 +1,1478 @@ +//! Screen-state terminals (docs/next-daemon.md, "Terminals cross as +//! authoritative screen state", phase 5). A replica is sent a terminal's +//! state, never its pty's bytes, so a `cat` of a gigabyte costs it screens. +//! +//! The authority encodes a delta for one peer: a header (the cursor, the +//! active area's first row as an absolute row, the history's extent), the +//! content address of the terminal-wide state, the content address of each +//! row of the active area, and the blobs behind the addresses the peer does +//! not hold. Both kinds of blob are ghostty's own snapshot records, so they +//! cross exactly: the terminal-wide state is a TERMINAL record (modes, +//! colours, title, tab stops...), and a row is the PAGE record of a one-row +//! page (its styles, graphemes, hyperlinks, wide cells, wrap and prompt +//! marks). A row's address is a hash of what the row shows, not of its blob, +//! whose style ids are its source page's. An unchanged row costs 8 bytes. +//! +//! Each state the authority sends is a version of its terminal (`Track`), +//! made once whatever the number of peers. A peer's next delta is against +//! the version it was last sent: the stream is ordered and loses nothing, +//! so the peer has that one by the time it applies the next. Several are +//! in flight (`bound`); acknowledgements free their room, and a peer at the +//! bound gets nothing until one comes, then the newest version, the ones +//! between skipped. Peers at the same base are sent the same bytes. A +//! replica holds the blobs of the state it applied last and no others; a +//! delta against another (one lost) is refused, and the peer started afresh. +//! +//! The replica writes each delta into a ghostty Terminal, which a replica's +//! terminal pane draws as a local one (src/terminal.zig reads only that). +//! Rows that scroll off its active area go into its history, made what the +//! authority's are by the delta's tail (their addresses, up to a screen of +//! them). Older ones it never saw, or saw before they last changed, are +//! holes there, fetched by absolute row on demand (`encodeRows`, +//! `applyRows`). +const std = @import("std"); +const vt = @import("ghostty-vt"); +const snapshot = vt.snapshot; +const Terminal = vt.Terminal; +const Page = vt.page.Page; +const Row = vt.page.Row; +const Pin = vt.Pin; +const Screen = vt.Screen; + +// ponytail: 64 bits: two of a million distinct rows collide about once in +// 4*10^7 sessions; phase 6's cache, shared across sessions, wants 128. +pub const Hash = u64; + +/// What a row shows, hashed: its wrap and prompt flags, and each of its +/// first `cols` cells up to the last one that is not blank, with its style, +/// grapheme and hyperlink by value. Not the page's style or hyperlink ids, +/// nor its derived flags. Its width is in it: a blob is a row that wide. +/// (A page can be wider than its terminal: ghostty narrows them lazily.) +pub fn rowHash(page: *Page, row: *Row, cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{rowFlags(row)}); + h.update(std.mem.asBytes(&cols)); + const all = page.getCells(row); + const cells = all[0..@min(cols, all.len)]; + var n = cells.len; + while (n > 0 and @as(u64, @bitCast(cells[n - 1])) == 0) n -= 1; + for (cells[0..n]) |*c| { + var word = c.*; + word.style_id = 0; + h.update(std.mem.asBytes(&@as(u64, @bitCast(word)))); + if (c.style_id != 0) h.update(std.mem.asBytes(&page.styles.get(page.memory, c.style_id).hash())); + if (c.hasGrapheme()) h.update(std.mem.sliceAsBytes(page.lookupGrapheme(c).?)); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| + h.update(std.mem.asBytes(&page.hyperlink_set.get(page.memory, id).hash(page.memory))); + } + return h.final(); +} + +fn rowFlags(row: *const Row) u8 { + return @as(u8, @intFromBool(row.wrap)) | @as(u8, @intFromBool(row.wrap_continuation)) << 1 | + @as(u8, @intFromEnum(row.semantic_prompt)) << 2; +} + +/// The authority's absolute row numbers for one terminal: its primary +/// screen's active area starts at `top`, and a row `d` above it in its +/// history is `top - d`. Kept by a tracked pin at the last active top, so +/// it counts the rows scrolled since. A resize renumbers (`epoch`). +pub const Rows = struct { + pin: ?*Pin = null, + top: u64 = 0, + epoch: u32 = 0, + cols: u16 = 0, + rows: u16 = 0, + + pub fn update(n: *Rows, t: *Terminal) !void { + const pages = &t.screens.get(.primary).?.pages; + const top = pages.getTopLeft(.active); + const y = pages.pointFromPin(.screen, top).?.screen.y; + if (n.pin) |p| { + if (t.cols != n.cols or t.rows != n.rows) n.epoch +%= 1; + // The last top was pruned away: every row the peer has is + // older than the oldest row here. + const was: ?u32 = if (p.garbage) null else if (pages.pointFromPin(.screen, p.*)) |pt| pt.screen.y else null; + n.top += if (was) |w| y -| w else @as(u64, y) + t.rows; + // A reflow (a resize to fewer columns) puts more rows above the + // last top than ever scrolled past it: numbered from here, the + // history's oldest row is still row 0 or later. A resize + // renumbers anyway (`epoch`), so no peer holds the old numbers. + n.top = @max(n.top, y); + p.* = top; + } else { + n.pin = try pages.trackPin(top); + n.top = y; + } + n.cols = t.cols; + n.rows = t.rows; + } + + pub fn deinit(n: *Rows, t: *Terminal) void { + if (n.pin) |p| t.screens.get(.primary).?.pages.untrackPin(p); + n.* = .{}; + } + + /// The pin of absolute row `abs`, if the terminal still has it. + fn pinAt(n: *const Rows, t: *Terminal, abs: u64) ?Pin { + return pinOf(&t.screens.get(.primary).?.pages, n.top, abs); + } +}; + +fn pinOf(pages: anytype, top: u64, abs: u64) ?Pin { + if (abs >= top) return pages.pin(.{ .active = .{ .y = std.math.cast(u32, abs - top) orelse return null } }); + const d = std.math.cast(usize, top - abs) orelse return null; + return pages.getTopLeft(.active).up(d); +} + +/// The authority's scratch: a one-row page each blob is copied into +/// (fresh, so the blob holds that row's styles alone) and encoded from. +pub const Encoder = struct { + gpa: std.mem.Allocator, + scratch: ?Page = null, + blob: std.Io.Writer.Allocating, + /// The rows a delta sends a blob for, and the addresses it lists once. + needed: std.ArrayList(struct { pin: Pin, hash: Hash }) = .empty, + listed: std.AutoHashMapUnmanaged(Hash, void) = .empty, + tail: std.ArrayList(Pin) = .empty, + + pub fn init(gpa: std.mem.Allocator) Encoder { + return .{ .gpa = gpa, .blob = .init(gpa) }; + } + + pub fn deinit(e: *Encoder) void { + if (e.scratch) |*s| s.deinit(); + e.blob.deinit(); + e.needed.deinit(e.gpa); + e.listed.deinit(e.gpa); + e.tail.deinit(e.gpa); + } + + /// The PAGE record of `row` alone, its first `cols` cells. + fn rowBlob(e: *Encoder, page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + var cap: vt.page.Capacity = if (e.scratch) |s| s.capacity else .{ .cols = cols, .rows = 1, .styles = 64 }; + if (cap.cols != cols) cap = .{ .cols = cols, .rows = 1, .styles = 64 }; + for (0..16) |_| { + if (e.scratch) |*s| { + if (std.meta.eql(s.capacity, cap)) s.reinit() else { + s.deinit(); + e.scratch = null; + } + } + if (e.scratch == null) e.scratch = try Page.init(cap); + const s = &e.scratch.?; + s.cloneRowFrom(page, s.getRow(0), row) catch { + // Room for this row: everything doubled until it fits. + cap.styles *|= 2; + cap.grapheme_bytes *|= 2; + cap.hyperlink_bytes *|= 2; + cap.string_bytes *|= 2; + continue; + }; + break; + } else return error.RowTooBig; + var rw: snapshot.record.Writer = .init(e.gpa, w); + defer rw.deinit(); + try snapshot.page.encode(&e.scratch.?, &rw); + } +}; + +/// Deltas a peer may have in flight. At 207 ms and 60 frames a second about +/// 13 are; past the bound a peer gets nothing until an acknowledgement, and +/// then the newest state, the ones between skipped. +pub const bound = 32; + +/// What a peer holds once it has a version: the addresses of its active +/// rows and its meta. A delta against it sends no blob for these. +const Known = struct { + version: u32, + epoch: u32, + top: u64, + addrs: std.AutoHashMapUnmanaged(Hash, void) = .empty, +}; + +/// One terminal as the authority numbers it, for all of its peers: each +/// state it sends is a version, made once whatever the number of peers. +pub const Track = struct { + rows: Rows = .{}, + version: u32 = 0, + /// The emulator's change count the newest version was made at, and its + /// history's rows. + changes: u32 = 0, + history: u64 = 0, + /// The newest version: its active rows (pins, good while the emulator + /// is unchanged) and their addresses, and its meta. + pins: std.ArrayList(Pin) = .empty, + hashes: std.ArrayList(Hash) = .empty, + meta: std.Io.Writer.Allocating, + meta_hash: Hash = 0, + /// The newest versions, oldest first: the bases a peer's next delta may + /// have. A peer whose base has gone starts afresh. + known: std.ArrayList(Known) = .empty, + /// The last delta encoded, for the next peer at the same base. + cache: std.Io.Writer.Allocating, + cache_base: u32 = 0, + cache_version: u32 = 0, + + const kept = bound + 8; + + pub fn init(gpa: std.mem.Allocator) Track { + return .{ .meta = .init(gpa), .cache = .init(gpa) }; + } + + /// `t` is the terminal, or null when it is gone already (with its pins). + pub fn deinit(tr: *Track, gpa: std.mem.Allocator, t: ?*Terminal) void { + if (t) |term| tr.rows.deinit(term); + tr.pins.deinit(gpa); + tr.hashes.deinit(gpa); + tr.meta.deinit(); + for (tr.known.items) |*k| k.addrs.deinit(gpa); + tr.known.deinit(gpa); + tr.cache.deinit(); + } + + fn find(tr: *const Track, version: u32) ?*const Known { + if (version == 0) return null; + for (tr.known.items) |*k| if (k.version == version) return if (k.epoch == tr.rows.epoch) k else null; + return null; + } + + /// A version of `t` as it is now. + fn advance(tr: *Track, gpa: std.mem.Allocator, t: *Terminal, changes: u32) !void { + try tr.rows.update(t); + tr.version +%= 1; + if (tr.version == 0) tr.version = 1; + tr.changes = changes; + const primary = &t.screens.get(.primary).?.pages; + tr.history = primary.total_rows - primary.rows; + tr.meta.clearRetainingCapacity(); + { + var rw: snapshot.record.Writer = .init(gpa, &tr.meta.writer); + defer rw.deinit(); + try snapshot.terminal.encode(t, &rw); + } + tr.meta_hash = std.hash.Wyhash.hash(0, tr.meta.written()); + tr.pins.clearRetainingCapacity(); + tr.hashes.clearRetainingCapacity(); + var it = t.screens.active.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| { + try tr.pins.append(gpa, p); + try tr.hashes.append(gpa, rowHash(p.node.page(), p.rowAndCell().row, t.cols)); + } + if (tr.known.items.len == kept) { + tr.known.items[0].addrs.deinit(gpa); + _ = tr.known.orderedRemove(0); + } + var k: Known = .{ .version = tr.version, .epoch = tr.rows.epoch, .top = tr.rows.top }; + errdefer k.addrs.deinit(gpa); + try k.addrs.ensureTotalCapacity(gpa, @intCast(tr.hashes.items.len + 1)); + for (tr.hashes.items) |h| k.addrs.putAssumeCapacity(h, {}); + k.addrs.putAssumeCapacity(tr.meta_hash, {}); + try tr.known.append(gpa, k); + } +}; + +/// What the authority knows of one peer of one terminal. +pub const Peer = struct { + /// The version last sent, which the next delta is against; 0: none, so + /// the next starts the replica afresh. + sent: u32 = 0, + /// The versions in flight, oldest first. + flight: [bound]u32 = undefined, + n: u8 = 0, + /// The history rows its replica holds as holes, absolute, as the + /// replica keeps them (Replica.apply's rule, from what it was sent): + /// what `fill` sends before a step reads them. + top: u64 = 0, + holes: std.ArrayListUnmanaged([2]u64) = .empty, + + pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { + peer.holes.deinit(gpa); + } + + /// Every version up to `seq` has landed. + pub fn ack(peer: *Peer, seq: u32) void { + var i: u8 = 0; + while (i < peer.n and peer.flight[i] <= seq) i += 1; + std.mem.copyForwards(u32, peer.flight[0 .. peer.n - i], peer.flight[i..peer.n]); + peer.n -= i; + } + + /// Start the peer afresh: it lost its state, or never had one. + pub fn reset(peer: *Peer) void { + var holes = peer.holes; + holes.clearRetainingCapacity(); + peer.* = .{ .holes = holes }; + } + + /// Its replica's holes once it applies a delta to `tr`'s newest version + /// against `base`, as Replica.apply makes them. + fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { + const top = tr.rows.top; + const history = tr.history; + if (base) |b| { + if (top > peer.top) { + const tail = @min(top -| b.top, rows, history); + if (top - peer.top >= @as(u64, rows) + history) { + peer.holes.clearRetainingCapacity(); + if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); + } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); + } + } else { + peer.holes.clearRetainingCapacity(); + if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); + } + peer.top = top; + // Those older than its history's oldest row are gone with it. + var i: usize = 0; + while (i < peer.holes.items.len) { + const hole = &peer.holes.items[i]; + hole[0] = @max(hole[0], top -| history); + if (hole[0] >= hole[1]) _ = peer.holes.orderedRemove(i) else i += 1; + } + } +}; + +/// The delta's fixed header: u32 seq, u32 base (0: none, the replica +/// starts afresh), u8 flags, u32 epoch, u64 meta, u16 cursor x and y, u8 +/// cursor style, u8 pending wrap, u8 the cursor's semantic content, the +/// kitty keyboard flags, u64 top, u64 history rows, u64 the viewport's top +/// as an absolute row (all ones: following the active area), u16 and the +/// caller's extra bytes; then u16 rows and a u64 per active row, u16 and a +/// u64 per row of the tail (those that scrolled off the active area since +/// the base, up to a screen of them, oldest first), and a u16 blob count +/// and each blob as u64 hash, u32 length, bytes. +const flag_reset: u8 = 1; +const KittyFlags = @FieldType(Screen, "kitty_keyboard"); +const following = std.math.maxInt(u64); + +/// The delta that takes `peer` from what it was last sent to `t` as it is, +/// onto `out`; false (nothing written) when it has that already, or has +/// `bound` in flight. `changes` counts the emulator's writes (a new count +/// is a new version); `extra` rides along (the caller's own state of the +/// terminal), the same for every peer of one version. A peer at the same +/// base as the last one encoded is sent the same bytes. +pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, extra: []const u8, out: *std.Io.Writer) !bool { + if (tr.version == 0 or changes != tr.changes) try tr.advance(e.gpa, t, changes); + if (peer.sent == tr.version or peer.n == bound) return false; + const base = tr.find(peer.sent); + const base_version = if (base) |b| b.version else 0; + if (tr.cache_version != tr.version or tr.cache_base != base_version) { + tr.cache.clearRetainingCapacity(); + try write(e, tr, base, t, extra, &tr.cache.writer); + tr.cache_version = tr.version; + tr.cache_base = base_version; + } + try out.writeAll(tr.cache.written()); + try peer.noteSent(e.gpa, tr, base, t.rows); + peer.sent = tr.version; + peer.flight[peer.n] = tr.version; + peer.n += 1; + return true; +} + +fn write(e: *Encoder, tr: *Track, base: ?*const Known, t: *Terminal, extra: []const u8, out: *std.Io.Writer) !void { + const gpa = e.gpa; + const reset = base == null; + const screen = t.screens.active; + const pages = &t.screens.get(.primary).?.pages; + const history = pages.total_rows - pages.rows; + const top = tr.rows.top; + try out.writeInt(u32, tr.version, .little); + try out.writeInt(u32, if (base) |b| b.version else 0, .little); + try out.writeByte(if (reset) flag_reset else 0); + try out.writeInt(u32, tr.rows.epoch, .little); + try out.writeInt(u64, tr.meta_hash, .little); + try out.writeInt(u16, screen.cursor.x, .little); + try out.writeInt(u16, screen.cursor.y, .little); + try out.writeByte(@intFromEnum(screen.cursor.cursor_style)); + try out.writeByte(@intFromBool(screen.cursor.pending_wrap)); + try out.writeByte(@intFromEnum(screen.cursor.semantic_content)); + // ponytail: raw bytes, as the hello refuses a peer of another build. + try out.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try out.writeInt(u64, top, .little); + try out.writeInt(u64, history, .little); + try out.writeInt(u64, if (pages.viewport == .active) following else top - history + pages.scrollbar().offset, .little); + try out.writeInt(u16, @intCast(extra.len), .little); + try out.writeAll(extra); + + // The active rows, then the tail: what scrolled off since the base, + // which may have changed after it was sent. + e.listed.clearRetainingCapacity(); + e.needed.clearRetainingCapacity(); + const tail: u16 = if (base) |b| @intCast(@min(top -| b.top, t.rows, history)) else 0; + e.tail.clearRetainingCapacity(); + for (0..tail) |j| try e.tail.append(gpa, pages.getTopLeft(.active).up(tail - j).?); + try out.writeInt(u16, @intCast(tr.pins.items.len), .little); + for (tr.pins.items, tr.hashes.items) |p, h| { + try out.writeInt(u64, h, .little); + try needBlob(e, base, p, h); + } + try out.writeInt(u16, tail, .little); + for (e.tail.items) |p| { + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try out.writeInt(u64, h, .little); + try needBlob(e, base, p, h); + } + const meta_new = if (base) |b| !b.addrs.contains(tr.meta_hash) else true; + try out.writeInt(u16, @intCast(e.needed.items.len + @intFromBool(meta_new)), .little); + if (meta_new) { + try out.writeInt(u64, tr.meta_hash, .little); + try out.writeInt(u32, @intCast(tr.meta.written().len), .little); + try out.writeAll(tr.meta.written()); + } + for (e.needed.items) |need| { + e.blob.clearRetainingCapacity(); + try e.rowBlob(need.pin.node.page(), need.pin.rowAndCell().row, t.cols, &e.blob.writer); + try out.writeInt(u64, need.hash, .little); + try out.writeInt(u32, @intCast(e.blob.written().len), .little); + try out.writeAll(e.blob.written()); + } +} + +/// A blob goes for an address the base does not hold, once. +fn needBlob(e: *Encoder, base: ?*const Known, p: Pin, h: Hash) !void { + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) return; + if (base) |b| if (b.addrs.contains(h)) return; + try e.needed.append(e.gpa, .{ .pin = p, .hash = h }); +} + +/// The primary screen's rows `[lo, hi)` the authority still has, for a +/// replica that holds them as holes: u32 epoch, u32 blobs, each u64 hash, +/// u32 length, bytes, then u32 rows, each u64 row and u64 hash. A blob goes +/// once a message (a flood's rows are one). +// ponytail: blobs once a message, not once a peer: phase 6's content cache. +pub fn encodeRows(e: *Encoder, tr: *Track, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { + try tr.rows.update(t); + var blobs: std.Io.Writer.Allocating = .init(e.gpa); + defer blobs.deinit(); + var rows: std.Io.Writer.Allocating = .init(e.gpa); + defer rows.deinit(); + e.listed.clearRetainingCapacity(); + var nblobs: u32 = 0; + var nrows: u32 = 0; + var abs = lo; + // The active area is the deltas'. + while (abs < @min(hi, tr.rows.top)) : (abs += 1) { + const p = tr.rows.pinAt(t, abs) orelse continue; + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try rows.writer.writeInt(u64, abs, .little); + try rows.writer.writeInt(u64, h, .little); + nrows += 1; + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) continue; + e.blob.clearRetainingCapacity(); + try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &e.blob.writer); + try blobs.writer.writeInt(u64, h, .little); + try blobs.writer.writeInt(u32, @intCast(e.blob.written().len), .little); + try blobs.writer.writeAll(e.blob.written()); + nblobs += 1; + } + try out.writeInt(u32, tr.rows.epoch, .little); + try out.writeInt(u32, nblobs, .little); + try out.writeAll(blobs.written()); + try out.writeInt(u32, nrows, .little); + try out.writeAll(rows.written()); +} + +/// A replica's terminal: what it was sent, written into a ghostty Terminal. +pub const Replica = struct { + seq: u32 = 0, + epoch: u32 = 0, + meta: Hash = 0, + top: u64 = 0, + /// The address of each active row as the Terminal holds it, a list per + /// screen (primary, alternate). + held: [2]std.ArrayList(Hash) = .{ .empty, .empty }, + /// The blobs of the last state applied: what the authority takes it to + /// hold. Owned copies. + blobs: std.AutoHashMapUnmanaged(Hash, []u8) = .empty, + /// History rows it cannot vouch for (written blank, or seen before + /// they last changed), `[lo, hi)` absolute, oldest first. + holes: std.ArrayList([2]u64) = .empty, + /// The one-row page each row blob is decoded into. + scratch: ?Page = null, + + pub fn deinit(r: *Replica, gpa: std.mem.Allocator) void { + for (&r.held) |*h| h.deinit(gpa); + var it = r.blobs.valueIterator(); + while (it.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.holes.deinit(gpa); + if (r.scratch) |*s| s.deinit(); + } + + pub const Applied = struct { seq: u32, extra: []const u8 }; + + /// Write one delta into `t`: the seq to acknowledge, and the caller's + /// extra bytes (borrowed from `bytes`). error.BaseMismatch: the delta + /// is against a state this replica does not have (one was lost, or it + /// started afresh), so `t` is as it was and it must be sent a reset. + pub fn apply(r: *Replica, gpa: std.mem.Allocator, io: std.Io, t: *Terminal, bytes: []const u8) !Applied { + var in: std.Io.Reader = .fixed(bytes); + const seq = try in.takeInt(u32, .little); + const base = try in.takeInt(u32, .little); + const flags = try in.takeByte(); + const reset = flags & flag_reset != 0; + if (!reset and base != r.seq) return error.BaseMismatch; + const epoch = try in.takeInt(u32, .little); + const meta = try in.takeInt(u64, .little); + const cx = try in.takeInt(u16, .little); + const cy = try in.takeInt(u16, .little); + const style = try in.takeByte(); + const wrap = try in.takeByte(); + const semantic = try in.takeByte(); + var kitty: KittyFlags = undefined; + @memcpy(std.mem.asBytes(&kitty), try in.take(@sizeOf(KittyFlags))); + const top = try in.takeInt(u64, .little); + const history = try in.takeInt(u64, .little); + const viewport = try in.takeInt(u64, .little); + const extra = try in.take(try in.takeInt(u16, .little)); + const nrows = try in.takeInt(u16, .little); + const hashes = try in.take(@as(usize, nrows) * 8); + const tail = try in.takeInt(u16, .little); + const tails = try in.take(@as(usize, tail) * 8); + // The delta's blobs, borrowed from `bytes` while it applies. + var fresh: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer fresh.deinit(gpa); + for (0..try in.takeInt(u16, .little)) |_| { + const h = try in.takeInt(u64, .little); + try fresh.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } + const blobOf = struct { + fn of(rr: *Replica, f: *const std.AutoHashMapUnmanaged(Hash, []const u8), h: Hash) ![]const u8 { + if (f.get(h)) |b| return b; + if (rr.blobs.get(h)) |b| return b; + return error.MissingBlob; + } + }.of; + + if (reset or meta != r.meta) { + var reader: std.Io.Reader = .fixed(try blobOf(r, &fresh, meta)); + var next = try snapshot.terminal.decode(&reader, try snapshot.record.nextHeader(&reader), io, gpa); + if (reset) { + std.mem.swap(Terminal, t, &next); + next.deinit(gpa); + for (&r.held) |*h| h.clearRetainingCapacity(); + r.holes.clearRetainingCapacity(); + r.top = top; + r.epoch = epoch; + // The authority's history, blank, to be fetched. Its own + // limit is the authority's row count (below), not bytes: + // its pages may pack rows otherwise. + const primary = t.screens.get(.primary).?; + primary.pages.setMaxBytes(null); + try scrollUp(primary, history); + if (history > 0) try r.holes.append(gpa, .{ top -| history, top }); + } else { + defer next.deinit(gpa); + try takeMeta(t, &next, io, gpa); + } + r.meta = meta; + } + if (epoch != r.epoch) return error.Renumbered; + + // The primary screen scrolled `top - r.top` rows since the last + // delta: as many go up into the history here (no more than its + // history holds and the screen), each beyond the tail a hole. + const primary = t.screens.get(.primary).?; + if (top > r.top) { + const k = top - r.top; + const held = &r.held[0]; + if (k >= @as(u64, t.rows) + history) { + // A flood: nothing held here is in this state. The rows stay + // where they are, renumbered: the active ones written below, + // the tail too, the rest holes (blanked when they come into + // view, `blank`), not grown and trimmed by the thousand. As + // many more as the history is longer there, grown blank. + const have = primary.pages.total_rows - primary.pages.rows; + if (have < history) try scrollUp(primary, history - have); + held.clearRetainingCapacity(); + try held.appendNTimes(gpa, unknown, t.rows); + r.holes.clearRetainingCapacity(); + if (history > tail) try r.holes.append(gpa, .{ top - history, top - tail }); + } else { + try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); + if (k >= held.items.len) held.clearRetainingCapacity() else { + std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); + held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + } + if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); + } + r.top = top; + } + // The history as long as the authority's (it prunes a page at a + // time, and an ED 3 takes it all): row numbers and the scrollbar + // say the same on both. ponytail: one pruned shorter than that + // (its pages packed otherwise) stays shorter. + const have = primary.pages.total_rows - primary.pages.rows; + if (have > history) primary.pages.eraseHistory(.{ .history = .{ .y = @intCast(have - history - 1) } }); + r.trimHoles(primary); + for (0..tail) |j| { + const p = pinOf(&primary.pages, top, top - tail + j) orelse continue; + const h = std.mem.readInt(u64, tails[j * 8 ..][0..8], .little); + if (rowHash(p.node.page(), p.rowAndCell().row, t.cols) != h) try r.writeRow(gpa, primary, p, try blobOf(r, &fresh, h)); + } + + const screen = t.screens.active; + const held = &r.held[@intFromEnum(t.screens.active_key)]; + const blank_row = blankHash(t.cols); + while (held.items.len < nrows) try held.append(gpa, blank_row); + held.shrinkRetainingCapacity(nrows); + for (0..nrows) |y| { + const h = std.mem.readInt(u64, hashes[y * 8 ..][0..8], .little); + if (h == held.items[y]) continue; + try r.writeRow(gpa, screen, screen.pages.pin(.{ .active = .{ .y = @intCast(y) } }).?, try blobOf(r, &fresh, h)); + held.items[y] = h; + } + screen.cursorReload(); + screen.cursorAbsolute(@min(cx, t.cols - 1), @min(cy, t.rows - 1)); + screen.cursor.cursor_style = @enumFromInt(style); + screen.cursor.pending_wrap = wrap != 0; + screen.cursor.semantic_content = @enumFromInt(semantic); + screen.kitty_keyboard = kitty; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + primary.pages.scroll(if (viewport == following) .active else .{ .row = @intCast(viewport -| oldest) }); + + // Hold the blobs of this state, and no others. + var keep: std.AutoHashMapUnmanaged(Hash, []u8) = .empty; + errdefer keep.deinit(gpa); + try keep.ensureTotalCapacity(gpa, nrows + tail + 1); + for (0..nrows + tail + 1) |i| { + const h = if (i == nrows + tail) meta else if (i < nrows) + std.mem.readInt(u64, hashes[i * 8 ..][0..8], .little) + else + std.mem.readInt(u64, tails[(i - nrows) * 8 ..][0..8], .little); + if (keep.contains(h)) continue; + const b = if (r.blobs.fetchRemove(h)) |kv| kv.value else try gpa.dupe(u8, try blobOf(r, &fresh, h)); + keep.putAssumeCapacity(h, b); + } + var old = r.blobs.valueIterator(); + while (old.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.blobs = keep; + r.seq = seq; + return .{ .seq = seq, .extra = extra }; + } + + /// The holes in `[lo, hi)` blank: until their rows come, a hole shows + /// nothing rather than what that row held before a flood renumbered it. + pub fn blank(r: *const Replica, t: *Terminal, lo: u64, hi: u64) void { + const primary = t.screens.get(.primary).?; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + for (r.holes.items) |hole| { + const a = @max(lo, hole[0], oldest); + const b = @min(hi, hole[1], r.top); + if (a >= b) continue; + primary.clearRows(.{ .screen = .{ .y = @intCast(a - oldest) } }, .{ .screen = .{ .y = @intCast(b - 1 - oldest) } }, false); + } + } + + /// The holes in `[lo, hi)`: the rows to fetch before showing them. + pub fn missing(r: *const Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void { + for (r.holes.items) |hole| { + const a = @max(lo, hole[0]); + const b = @min(hi, hole[1]); + if (a < b) try out.append(gpa, .{ a, b }); + } + } + + /// The absolute row the viewport of `t`'s primary screen starts at, and + /// how many it shows. + pub fn shown(r: *const Replica, t: *Terminal) [2]u64 { + const pages = &t.screens.get(.primary).?.pages; + const sb = pages.scrollbar(); + const oldest = r.top -| (pages.total_rows - pages.rows); + return .{ oldest + sb.offset, oldest + sb.offset + sb.len }; + } + + /// Write fetched history rows (`encodeRows`) into `t`. + pub fn applyRows(r: *Replica, gpa: std.mem.Allocator, t: *Terminal, bytes: []const u8) !void { + var in: std.Io.Reader = .fixed(bytes); + if (try in.takeInt(u32, .little) != r.epoch) return; // numbered before a resize + const primary = t.screens.get(.primary).?; + var blobs: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer blobs.deinit(gpa); + for (0..try in.takeInt(u32, .little)) |_| { + const h = try in.takeInt(u64, .little); + try blobs.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } + for (0..try in.takeInt(u32, .little)) |_| { + const abs = try in.takeInt(u64, .little); + const blob = blobs.get(try in.takeInt(u64, .little)) orelse return error.MissingBlob; + // Only a hole is written: a row the replica holds is the + // authority's already, and newer than what this fetch saw. + if (abs >= r.top or !r.inHole(abs)) continue; + const p = pinOf(&primary.pages, r.top, abs) orelse continue; + try r.writeRow(gpa, primary, p, blob); + try r.fill(gpa, abs); + } + } + + fn inHole(r: *const Replica, abs: u64) bool { + for (r.holes.items) |hole| if (abs >= hole[0] and abs < hole[1]) return true; + return false; + } + + /// Row `abs` is written: out of the holes. + fn fill(r: *Replica, gpa: std.mem.Allocator, abs: u64) !void { + for (r.holes.items, 0..) |*hole, i| { + if (abs < hole[0] or abs >= hole[1]) continue; + if (abs == hole[0]) hole[0] += 1 else if (abs + 1 == hole[1]) hole[1] -= 1 else { + try r.holes.insert(gpa, i + 1, .{ abs + 1, hole[1] }); + r.holes.items[i][1] = abs; + } + if (r.holes.items[i][0] == r.holes.items[i][1]) _ = r.holes.orderedRemove(i); + return; + } + } + + /// Holes older than this history's oldest row are gone with it. + fn trimHoles(r: *Replica, primary: *Screen) void { + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + var i: usize = 0; + while (i < r.holes.items.len) { + const hole = &r.holes.items[i]; + hole[0] = @max(hole[0], oldest); + if (hole[0] >= hole[1]) _ = r.holes.orderedRemove(i) else i += 1; + } + } + + fn writeRow(r: *Replica, gpa: std.mem.Allocator, screen: *Screen, p: Pin, blob: []const u8) !void { + var reader: std.Io.Reader = .fixed(blob); + var dec: snapshot.page.Decoder = undefined; + try dec.init(&reader, try snapshot.record.nextHeader(&reader)); + const cap = dec.capacity(); + if (r.scratch) |*s| { + if (std.meta.eql(s.capacity, cap)) s.reinit() else { + s.deinit(); + r.scratch = null; + } + } + if (r.scratch == null) r.scratch = try Page.init(cap); + const src = &r.scratch.?; + try dec.decode(src, gpa); + if (src.size.cols != screen.pages.cols) return error.BadRow; + _ = screen.clonePartialRowGrowCapacity(p.node, p.y, src, src.getRow(0), 0, src.size.cols); + } +}; + +/// `k` blank rows onto the bottom of a screen's active area, its top `k` +/// going up into the history, as `k` linefeeds at the bottom would. A +/// flood's thousands are grown a row at a time without the cursor (the +/// page list's fast path, a counter), which is put back after. +fn scrollUp(screen: *Screen, k: u64) !void { + if (k == 0) return; + screen.cursorAbsolute(0, screen.pages.rows - 1); + if (k <= screen.pages.rows or screen.no_scrollback or screen.cursor.style_id != 0) { + for (0..@intCast(k)) |_| try screen.cursorDownScroll(); + return; + } + for (0..@intCast(k)) |_| _ = try screen.pages.grow(); + screen.cursorReload(); +} + +/// No row's address: what a row whose content is not known is held as. +const unknown: Hash = std.math.maxInt(Hash); + +/// `rowHash` of a row never written. +fn blankHash(cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{0}); + h.update(std.mem.asBytes(&cols)); + return h.final(); +} + +/// The terminal-wide state of `next` (a decoded TERMINAL record) into `t`, +/// whose screens and pages stay: what `snapshot.terminal.decode` sets. +fn takeMeta(t: *Terminal, next: *Terminal, io: std.Io, gpa: std.mem.Allocator) !void { + if (t.cols != next.cols or t.rows != next.rows) try t.resize(gpa, .{ .cols = next.cols, .rows = next.rows }); + t.width_px = next.width_px; + t.height_px = next.height_px; + t.scrolling_region = next.scrolling_region; + t.status_display = next.status_display; + t.previous_char = next.previous_char; + t.cursor = next.cursor; + t.modes = next.modes; + t.flags.shell_redraws_prompt = next.flags.shell_redraws_prompt; + t.flags.modify_other_keys_2 = next.flags.modify_other_keys_2; + t.flags.mouse_event = next.flags.mouse_event; + t.flags.mouse_format = next.flags.mouse_format; + t.flags.mouse_shift_capture = next.flags.mouse_shift_capture; + t.flags.password_input = next.flags.password_input; + t.mouse_shape = next.mouse_shape; + t.colors = next.colors; + try t.setPwd(next.getPwd() orelse ""); + try t.setTitle(next.getTitle() orelse ""); + std.mem.swap(@TypeOf(t.tabstops), &t.tabstops, &next.tabstops); + const key = next.screens.active_key; + if (key != t.screens.active_key) { + if (t.screens.get(key) == null) _ = try t.screens.getInit(io, gpa, key, .{ .cols = t.cols, .rows = t.rows, .max_scrollback_bytes = 0 }); + t.screens.switchTo(key); + } +} + +// ---- checks: a replica against its authority, cell for cell ---- + +/// What a row shows, written out value by value (not `rowHash`'s walk): +/// its flags, then each cell's word without its page's ids, its style, its +/// grapheme and its hyperlink's URI. +fn dumpRow(page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + try w.print("{d}:", .{rowFlags(row)}); + const all = page.getCells(row); + for (all[0..@min(cols, all.len)]) |*c| { + var word = c.*; + word.style_id = 0; + try w.print(" {x}", .{@as(u64, @bitCast(word))}); + if (c.style_id != 0) try snapshot.style.encode(page.styles.get(page.memory, c.style_id).*, w); + if (c.hasGrapheme()) for (page.lookupGrapheme(c).?) |cp| try w.print("+{x}", .{cp}); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| try w.print("@{s}", .{page.hyperlink_set.get(page.memory, id).uri.slice(page.memory)}); + } + try w.writeByte('\n'); +} + +/// The state a replica must match: the terminal-wide state, the cursor, +/// the history's extent and the viewport, the active screen's rows. +pub fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { + const screen = t.screens.active; + const sb = t.screens.get(.primary).?.pages.scrollbar(); + try w.print("{d}x{d} {s} cursor {d},{d} {s} {} {s} title {s} pwd {s} scrollbar {d} {d} {d}\n", .{ + t.cols, t.rows, @tagName(t.screens.active_key), + screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style), + screen.cursor.pending_wrap, @tagName(screen.cursor.semantic_content), t.getTitle() orelse "", + t.getPwd() orelse "", sb.total, sb.offset, + sb.len, + }); + try w.writeAll(std.mem.asBytes(&t.modes.values)); + try w.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try w.print(" mouse {s} {s} {s}\n", .{ @tagName(t.flags.mouse_event), @tagName(t.flags.mouse_format), @tagName(t.mouse_shape) }); + for (t.colors.palette.current) |c| try w.print("{x}{x}{x}", .{ c.r, c.g, c.b }); + try w.writeByte('\n'); + var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| try dumpRow(p.node.page(), p.rowAndCell().row, t.cols, w); +} + +/// An authority and its replica, fed one stream of pty bytes, the deltas +/// and fetches between them at random: a delta encoded at a random step +/// (so the states between are skipped), several in flight and each landing +/// a random number of steps later, in order, one lost now and then (the +/// replica refuses the next and is started afresh), random resizes of the +/// authority, random fetches of holes. After each delta, the replica's +/// screen is the authority's as it was encoded, and every history row it +/// holds outside its holes is what the authority holds at that absolute +/// row. +pub const Soak = struct { + gpa: std.mem.Allocator, + io: std.Io, + a: *Terminal, + stream: vt.TerminalStream, + track: Track, + enc: Encoder, + peer: Peer = .{}, + /// The authority's writes, as terminal.State counts them. + changes: u32 = 0, + b: *Terminal, + rep: Replica = .{}, + prng: std.Random.DefaultPrng, + /// The deltas in flight, oldest first, each with the authority's dump + /// when it was encoded and the step it lands at. + flight: std.ArrayList(struct { delta: []u8, expect: []u8, at: u64 }) = .empty, + got: std.Io.Writer.Allocating, + now: u64 = 0, + resizes: bool = true, + drops: bool = true, + /// The replica refused a delta and the authority started it afresh: + /// what was in flight before is refused too. + refused: bool = false, + stats: struct { deltas: u64 = 0, bytes: u64 = 0, fetched: u64 = 0, history_checked: u64 = 0, lost: u64 = 0, refused: u64 = 0, most_in_flight: u64 = 0 } = .{}, + + pub fn init(gpa: std.mem.Allocator, io: std.Io, cols: u16, rows: u16, scrollback: usize, seed: u64) !*Soak { + const s = try gpa.create(Soak); + errdefer gpa.destroy(s); + const a = try gpa.create(Terminal); + a.* = try Terminal.init(io, gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = scrollback }); + const b = try gpa.create(Terminal); + // Room to spare: its pages may pack rows otherwise, and it keeps the + // authority's history to the row. + b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 2 * scrollback }); + s.* = .{ + .gpa = gpa, + .io = io, + .a = a, + .stream = a.vtStream(), + .track = .init(gpa), + .enc = .init(gpa), + .b = b, + .prng = .init(seed), + .got = .init(gpa), + }; + return s; + } + + pub fn deinit(s: *Soak) void { + const gpa = s.gpa; + s.stream.deinit(); + s.track.deinit(gpa, s.a); + s.a.deinit(gpa); + gpa.destroy(s.a); + s.b.deinit(gpa); + gpa.destroy(s.b); + s.enc.deinit(); + s.peer.deinit(gpa); + s.rep.deinit(gpa); + for (s.flight.items) |f| { + gpa.free(f.delta); + gpa.free(f.expect); + } + s.flight.deinit(gpa); + s.got.deinit(); + gpa.destroy(s); + } + + /// `bytes` into the authority in random pieces, a step after each. + pub fn feed(s: *Soak, bytes: []const u8) !void { + var at: usize = 0; + const random = s.prng.random(); + while (at < bytes.len) { + const n = @min(bytes.len - at, 1 + random.uintLessThan(usize, 512)); + s.stream.nextSlice(bytes[at..][0..n]); + s.changes +%= 1; + at += n; + try s.step(); + } + } + + pub fn step(s: *Soak) !void { + const random = s.prng.random(); + s.now += 1; + if (s.resizes and random.uintLessThan(u32, 200) == 0) { + try s.a.resize(s.gpa, .{ .cols = 10 + random.uintLessThan(u16, 200), .rows = 3 + random.uintLessThan(u16, 60) }); + s.changes +%= 1; + } + // A frame now and then; the states between are skipped. + if (random.boolean()) try s.send(s.now + random.uintLessThan(u64, 12)); + while (s.flight.items.len > 0 and s.flight.items[0].at <= s.now) try s.land(); + if (random.uintLessThan(u32, 8) == 0) try s.fetch(); + } + + /// A delta, if the peer is owed one, sent to land at step `at`. + fn send(s: *Soak, at: u64) !void { + var d: std.Io.Writer.Allocating = .init(s.gpa); + defer d.deinit(); + if (!try encode(&s.enc, &s.track, &s.peer, s.a, s.changes, "extra", &d.writer)) return; + var expect: std.Io.Writer.Allocating = .init(s.gpa); + defer expect.deinit(); + try dumpScreen(s.a, &expect.writer); + const last = if (s.flight.items.len > 0) s.flight.items[s.flight.items.len - 1].at else 0; + try s.flight.append(s.gpa, .{ .delta = try d.toOwnedSlice(), .expect = try expect.toOwnedSlice(), .at = @max(at, last) }); + s.stats.deltas += 1; + s.stats.bytes += s.flight.items[s.flight.items.len - 1].delta.len; + s.stats.most_in_flight = @max(s.stats.most_in_flight, s.flight.items.len); + } + + /// The oldest delta in flight applied (or lost), acknowledged, checked. + fn land(s: *Soak) !void { + const f = s.flight.orderedRemove(0); + defer s.gpa.free(f.delta); + defer s.gpa.free(f.expect); + if (s.drops and s.prng.random().uintLessThan(u32, 50) == 0) { + s.stats.lost += 1; + return; + } + const applied = s.rep.apply(s.gpa, s.io, s.b, f.delta) catch |err| switch (err) { + error.BaseMismatch => { + // Asked once: the authority starts it afresh. + if (!s.refused) s.peer.reset(); + s.refused = true; + s.stats.refused += 1; + return; + }, + else => return err, + }; + s.refused = false; + if (!std.mem.eql(u8, applied.extra, "extra")) return error.ExtraLost; + s.peer.ack(applied.seq); + s.got.clearRetainingCapacity(); + try dumpScreen(s.b, &s.got.writer); + if (!std.mem.eql(u8, f.expect, s.got.written())) { + reportDiff(f.expect, s.got.written()); + return error.ReplicaDiffers; + } + try s.checkHistory(); + } + + /// Everything in flight landed, then a last delta of the end, until the + /// replica holds it: what the replica shows at quiescence. + fn settle(s: *Soak) !void { + s.drops = false; + for (0..4) |_| { + while (s.flight.items.len > 0) try s.land(); + try s.send(0); + if (s.flight.items.len == 0) return; + } + return error.NeverSettled; + } + + /// Every history row the replica holds, outside its holes and inside + /// what the authority still has, is the authority's (rows in history + /// never change; a resize renumbers and starts the replica afresh). + fn checkHistory(s: *Soak) !void { + // Numbered as of now: the authority may have scrolled on since. + try s.track.rows.update(s.a); + if (s.rep.epoch != s.track.rows.epoch) return; + const pages = &s.b.screens.get(.primary).?.pages; + const have = pages.total_rows - pages.rows; + var ra: std.Io.Writer.Allocating = .init(s.gpa); + defer ra.deinit(); + var rb: std.Io.Writer.Allocating = .init(s.gpa); + defer rb.deinit(); + var d: u64 = 1; + while (d <= have) : (d += 1 + d / 8) { + const abs = s.rep.top - d; + if (s.rep.inHole(abs)) continue; + const pb = pinOf(pages, s.rep.top, abs) orelse continue; + const pa = s.track.rows.pinAt(s.a, abs) orelse continue; + ra.clearRetainingCapacity(); + rb.clearRetainingCapacity(); + try dumpRow(pa.node.page(), pa.rowAndCell().row, s.a.cols, &ra.writer); + try dumpRow(pb.node.page(), pb.rowAndCell().row, s.b.cols, &rb.writer); + if (!std.mem.eql(u8, ra.written(), rb.written())) { + std.debug.print("history row {d} ({d} above the top) differs\n", .{ abs, d }); + reportDiff(ra.written(), rb.written()); + return error.ReplicaHistoryDiffers; + } + s.stats.history_checked += 1; + } + } + + /// Some holes fetched from the authority, as a replica scrolled back. + fn fetch(s: *Soak) !void { + if (s.rep.holes.items.len == 0) return; + const random = s.prng.random(); + const hole = s.rep.holes.items[random.uintLessThan(usize, s.rep.holes.items.len)]; + const lo = hole[0] + random.uintLessThan(u64, hole[1] - hole[0]); + const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); + s.rep.blank(s.b, lo, hi); + var out: std.Io.Writer.Allocating = .init(s.gpa); + defer out.deinit(); + try encodeRows(&s.enc, &s.track, s.a, lo, hi, &out.writer); + try s.rep.applyRows(s.gpa, s.b, out.written()); + s.stats.fetched += hi - lo; + try s.checkHistory(); + } +}; + +fn reportDiff(want: []const u8, got: []const u8) void { + var wl = std.mem.splitScalar(u8, want, '\n'); + var gl = std.mem.splitScalar(u8, got, '\n'); + var line: usize = 0; + while (true) : (line += 1) { + const a = wl.next(); + const b = gl.next(); + if (a == null and b == null) return; + if (a != null and b != null and std.mem.eql(u8, a.?, b.?)) continue; + std.debug.print("line {d} differs:\n authority {s}\n replica {s}\n", .{ line, a orelse "(none)", b orelse "(none)" }); + return; + } +} + +/// Drive one stream through a soak, then settle: the replica's screen at +/// quiescence is the authority's. +pub fn soak(gpa: std.mem.Allocator, io: std.Io, bytes: []const u8, cols: u16, rows: u16, scrollback: usize, seed: u64, resizes: bool) !@FieldType(Soak, "stats") { + const s = try Soak.init(gpa, io, cols, rows, scrollback, seed); + defer s.deinit(); + s.resizes = resizes; + try s.feed(bytes); + try s.settle(); + return s.stats; +} + +/// Every recorded pty stream in `dir` (a `-<cols>x<rows>` in its name sizes +/// it, 80x24 otherwise) through a soak at three seeds, then the encode's +/// cost and the bytes floods put on the wire: the test below, when +/// PARDES_SCREEN_STATE names the directory (numbers from a ReleaseFast +/// build: `zig build core-test -Doptimize=ReleaseFast -Dtest-filter=screen_state`). +fn perf(gpa: std.mem.Allocator, io: std.Io, dir_path: []const u8, w: *std.Io.Writer) !void { + var dir = try std.Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }); + defer dir.close(io); + var it = dir.iterate(); + var files: usize = 0; + var failed: usize = 0; + while (try it.next(io)) |entry| { + if (entry.kind != .file) continue; + const bytes = try dir.readFileAlloc(io, entry.name, gpa, .unlimited); + defer gpa.free(bytes); + var cols: u16 = 80; + var rows: u16 = 24; + if (std.mem.lastIndexOfScalar(u8, entry.name, '-')) |dash| { + const size = std.mem.sliceTo(entry.name[dash + 1 ..], '.'); + if (std.mem.indexOfScalar(u8, size, 'x')) |x| { + cols = std.fmt.parseInt(u16, size[0..x], 10) catch cols; + rows = std.fmt.parseInt(u16, size[x + 1 ..], 10) catch rows; + } + } + files += 1; + for (0..3) |seed| { + const st = soak(gpa, io, bytes, cols, rows, 128 * 1024, seed, seed != 0) catch |err| { + try w.print("screen-state {s} seed {d}: {s}\n", .{ entry.name, seed, @errorName(err) }); + failed += 1; + continue; + }; + if (seed == 0) try w.print("screen-state {s} {d}x{d}: {d} bytes, {d} deltas ({d} lost, {d} refused, {d} in flight at most), {d} wire bytes, {d} history rows checked, {d} fetched\n", .{ entry.name, cols, rows, bytes.len, st.deltas, st.lost, st.refused, st.most_in_flight, st.bytes, st.history_checked, st.fetched }); + } + } + try w.print("screen-state corpus: {d} streams x 3 seeds, {d} failed\n", .{ files, failed }); + try w.flush(); + try bench(gpa, io, w); +} + +fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { + const t = try gpa.create(Terminal); + defer gpa.destroy(t); + t.* = try Terminal.init(io, gpa, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer t.deinit(gpa); + var stream = t.vtStream(); + defer stream.deinit(); + var enc: Encoder = .init(gpa); + defer enc.deinit(); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + var line: [1024]u8 = undefined; + // A full 200x60 screen, every row new to the peer: each a blob. + var full: [200]u64 = undefined; + var same: [200]u64 = undefined; + var full_bytes: usize = 0; + var same_bytes: usize = 0; + var changes: u32 = 0; + for (&full, &same, 0..) |*f, *u, rep| { + for (0..60) |y| { + var lw: std.Io.Writer = .fixed(&line); + try lw.print("\x1b[{d};1H\x1b[0;3{d};4{d}m{d} {d} ", .{ y + 1, rep % 8, y % 8, rep, y }); + while (lw.end < 190) try lw.writeAll("\x1b[1mbold\x1b[22m text "); + stream.nextSlice(lw.buffered()); + } + changes += 1; + var tr: Track = .init(gpa); + defer tr.deinit(gpa, t); + var peer: Peer = .{}; + defer peer.deinit(gpa); + out.clearRetainingCapacity(); + var t0 = std.Io.Clock.awake.now(io).nanoseconds; + _ = try encode(&enc, &tr, &peer, t, changes, "", &out.writer); + f.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); + full_bytes = out.written().len; + // The same screen again as a new version: every row held. + out.clearRetainingCapacity(); + t0 = std.Io.Clock.awake.now(io).nanoseconds; + _ = try encode(&enc, &tr, &peer, t, changes + 1, "", &out.writer); + u.* = @intCast(std.Io.Clock.awake.now(io).nanoseconds - t0); + same_bytes = out.written().len; + } + std.mem.sort(u64, &full, {}, std.sort.asc(u64)); + std.mem.sort(u64, &same, {}, std.sort.asc(u64)); + try w.print("screen-state encode 200x60, every row new: p50 {d} us p99 {d} us, {d} bytes\n", .{ full[100] / 1000, full[198] / 1000, full_bytes }); + try w.print("screen-state encode 200x60, nothing new: p50 {d} us p99 {d} us, {d} bytes\n", .{ same[100] / 1000, same[198] / 1000, same_bytes }); + try w.flush(); + + // Floods: a second of output as fast as ghostty takes it, a frame every + // 16 ms: one version, a delta for each peer (the same bytes for peers + // at the same base), each replica applying its own at once. Under the C + // allocator, as a replica's (the checking one costs an apply 4x). + try floods(gpa, std.heap.c_allocator, io, w); +} + +fn floods(gpa: std.mem.Allocator, ca: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { + var enc: Encoder = .init(ca); + defer enc.deinit(); + var out: std.Io.Writer.Allocating = .init(ca); + defer out.deinit(); + const yes = try yesStream(gpa, 1 << 16); + defer gpa.free(yes); + const shell = try shellStream(gpa); + defer gpa.free(shell); + for ([_]struct { name: []const u8, bytes: []const u8 }{ .{ .name = "yes", .bytes = yes }, .{ .name = "varied text", .bytes = shell } }) |flood| for ([_]usize{ 1, 3 }) |npeers| { + const a = try ca.create(Terminal); + defer ca.destroy(a); + a.* = try Terminal.init(io, ca, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer a.deinit(ca); + var as = a.vtStream(); + defer as.deinit(); + var tr: Track = .init(ca); + defer tr.deinit(ca, a); + var peers: [3]Peer = @splat(.{}); + var reps: [3]Replica = @splat(.{}); + var bs: [3]Terminal = undefined; + for (bs[0..npeers]) |*b| b.* = try Terminal.init(io, ca, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 32 << 20 }); + defer for (bs[0..npeers], reps[0..npeers], peers[0..npeers]) |*b, *r, *peer| { + b.deinit(ca); + r.deinit(ca); + peer.deinit(ca); + }; + var applies: std.ArrayList(u64) = .empty; + defer applies.deinit(gpa); + var frames: std.ArrayList(u64) = .empty; + defer frames.deinit(gpa); + const start = std.Io.Clock.awake.now(io).nanoseconds; + var next = start; + var fed: u64 = 0; + var deltas: u64 = 0; + var wire: u64 = 0; + var version: u32 = 0; + while (true) { + const now = std.Io.Clock.awake.now(io).nanoseconds; + if (now - start >= std.time.ns_per_s) break; + as.nextSlice(flood.bytes); + version += 1; + fed += flood.bytes.len; + if (now < next) continue; + next += 16 * std.time.ns_per_ms; + var frame_ns: u64 = 0; + for (peers[0..npeers], reps[0..npeers], bs[0..npeers]) |*peer, *rep, *b| { + out.clearRetainingCapacity(); + const e0 = std.Io.Clock.awake.now(io).nanoseconds; + if (!try encode(&enc, &tr, peer, a, version, "", &out.writer)) continue; + frame_ns += @intCast(std.Io.Clock.awake.now(io).nanoseconds - e0); + const a0 = std.Io.Clock.awake.now(io).nanoseconds; + peer.ack((try rep.apply(ca, io, b, out.written())).seq); + try applies.append(gpa, @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0)); + deltas += 1; + wire += out.written().len; + } + try frames.append(gpa, frame_ns); + } + std.mem.sort(u64, applies.items, {}, std.sort.asc(u64)); + std.mem.sort(u64, frames.items, {}, std.sort.asc(u64)); + const p = struct { + fn at(s: []const u64, q: usize) u64 { + return if (s.len == 0) 0 else s[@min(s.len - 1, s.len * q / 100)] / 1000; + } + }.at; + try w.print("screen-state flood of {s}, {d} peer(s): {d} MB/s of output, {d} deltas/s a peer, {d} wire bytes/s a peer ({d} a delta); encode a frame, all peers: p50 {d} us p99 {d} us; apply a delta p50 {d} us p99 {d} us\n", .{ + flood.name, npeers, fed / 1_000_000, deltas / npeers, wire / npeers, + wire / @max(1, deltas), p(frames.items, 50), p(frames.items, 99), p(applies.items, 50), + p(applies.items, 99), + }); + }; + try w.flush(); +} + +const testing = std.testing; + +test "recorded streams through replicas, and the encode's cost (PARDES_SCREEN_STATE=<dir>)" { + const dir = std.c.getenv("PARDES_SCREEN_STATE") orelse return error.SkipZigTest; + var buf: [4096]u8 = undefined; + // stderr: a test's stdout is the build runner's. + var out = std.Io.File.stderr().writer(testing.io, &buf); + try perf(testing.allocator, testing.io, std.mem.span(dir), &out.interface); +} + +/// A shell's: prompts marked (OSC 133), colours of the three kinds, wide +/// and combined graphemes, hyperlinks, tabs, a title and a directory, long +/// lines that wrap, and a clear (ED2, ED3) now and then. +pub fn shellStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("\x1b]0;a shell\x07\x1b]7;file://host/home/someone\x07"); + for (0..400) |i| { + try w.print("\x1b]133;A\x07$ \x1b]133;B\x07ls -l {d}\r\n\x1b]133;C\x07", .{i}); + try w.print("\x1b[1;31mred\x1b[0m \x1b[4;38;5;{d}m256\x1b[0m \x1b[38;2;{d};100;200;48;5;17mtrue\x1b[0m 漢字 e\u{301} 👩\u{200d}💻 ", .{ i % 256, i % 256 }); + try w.print("\x1b]8;;https://example.com/{d}\x07link\x1b]8;;\x07\tx\r\n", .{i}); + if (i % 7 == 0) { + for (0..12) |_| try w.writeAll("a long line that wraps "); + try w.writeAll("\r\n"); + } + try w.writeAll("\x1b]133;D;0\x07"); + if (i % 97 == 96) try w.writeAll("\x1b[H\x1b[2J\x1b[3J"); + if (i % 50 == 0) try w.print("\x1b]2;title {d}\x07\x1b]4;1;rgb:{x:0>2}/20/30\x07", .{ i, i % 256 }); + } + return out.toOwnedSlice(); +} + +/// A full-screen program's: the alternate screen and its modes, a status +/// line, a scrolling region scrolled both ways, lines inserted and +/// deleted, cursor shapes, the kitty keyboard pushed and popped. +pub fn tuiStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("before the program\r\n\x1b[?1049h\x1b[?25l\x1b[?1000h\x1b[?1006h\x1b[?2004h\x1b[?1h\x1b[>1u\x1b[4 q\x1b[H\x1b[2J"); + for (0..300) |f| { + try w.print("\x1b[1;1H\x1b[44;97m status {d} \x1b[K\x1b[0m", .{f}); + try w.print("\x1b[3;18r\x1b[18;1H\r\n\x1b[3{d}mline {d}\x1b[0m", .{ f % 8, f }); + if (f % 3 == 0) try w.writeAll("\x1b[3;1H\x1bM\x1b[2mreversed in\x1b[0m"); + if (f % 5 == 0) try w.writeAll("\x1b[6;1H\x1b[2L\x1b[9;1H\x1b[M"); + try w.writeAll("\x1b[r"); + try w.print("\x1b[{d};{d}H\x1b[7m*\x1b[0m\x1b[{d}X", .{ 2 + f % 20, 1 + (f * 7) % 70, f % 5 }); + if (f % 11 == 0) try w.writeAll("\x1b[?25h\x1b[6 q") else if (f % 11 == 5) try w.writeAll("\x1b[?25l\x1b[2 q"); + } + try w.writeAll("\x1b[<u\x1b[?1049l\x1b[?25h\x1b[?1000l\x1b[0 qafter the program\r\n"); + return out.toOwnedSlice(); +} + +pub fn yesStream(gpa: std.mem.Allocator, lines: usize) ![]u8 { + const out = try gpa.alloc(u8, lines * 3); + for (0..lines) |i| @memcpy(out[i * 3 ..][0..3], "y\r\n"); + return out; +} + +test "a replica's screen is its authority's, cell for cell, as states are skipped, resized and fetched" { + const gpa = testing.allocator; + const io = testing.io; + const shell = try shellStream(gpa); + defer gpa.free(shell); + const tui = try tuiStream(gpa); + defer gpa.free(tui); + const yes = try yesStream(gpa, 30_000); + defer gpa.free(yes); + var fetched: u64 = 0; + var history: u64 = 0; + for (0..4) |seed| { + // A small scrollback, so it wraps (pruned at its oldest pages). + for ([_][]const u8{ shell, tui, yes }) |bytes| { + const st = soak(gpa, io, bytes, 80, 24, 64 * 1024, seed, seed != 0) catch |err| { + std.debug.print("soak seed {d} stream {d} bytes: {s}\n", .{ seed, bytes.len, @errorName(err) }); + return err; + }; + try testing.expect(st.deltas > 10); + fetched += st.fetched; + history += st.history_checked; + } + } + // The holes were fetched, and history rows compared, at all. + try testing.expect(fetched > 0 and history > 0); +} + +test "under a flood a delta is a screen of hashes: an unchanged row costs its address alone" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 200, 60, 1 << 20, 1); + defer s.deinit(); + s.resizes = false; + s.drops = false; + const yes = try yesStream(gpa, 10_000); + defer gpa.free(yes); + var last: []u8 = &.{}; + defer gpa.free(last); + for (0..3) |_| { + s.stream.nextSlice(yes); + s.changes += 1; + try s.send(0); + try testing.expectEqual(@as(usize, 1), s.flight.items.len); + gpa.free(last); + last = try gpa.dupe(u8, s.flight.items[0].delta); + try s.land(); + } + // The last: 60 rows and the tail's 60, of hashes the replica holds, no + // blob; the header and counts around them. + const d = last; + const at = 4 + 4 + 1 + 4 + 8 + 2 + 2 + 1 + 1 + 1 + @sizeOf(KittyFlags) + 8 + 8 + 8 + 2 + "extra".len; + const nrows = std.mem.readInt(u16, d[at..][0..2], .little); + const tail = std.mem.readInt(u16, d[at + 2 + nrows * 8 ..][0..2], .little); + const blobs = std.mem.readInt(u16, d[at + 4 + (nrows + tail) * 8 ..][0..2], .little); + if (d.len >= (nrows + tail) * 8 + 128) std.debug.print("delta {d} bytes: {d} rows, {d} tail, {d} blobs\n", .{ d.len, nrows, tail, blobs }); + try testing.expectEqual(@as(u16, 0), blobs); + try testing.expect(d.len < (nrows + tail) * 8 + 128); + try testing.expect(s.rep.holes.items.len > 0); +} + +test "a reflow to fewer columns keeps every history row numbered at or past 0" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 80, 24, 1 << 20, 3); + defer s.deinit(); + s.resizes = false; + s.drops = false; + // Lines of 69 columns scrolled into the history, then the screen + // narrowed so that each wraps in three: more rows above the last top + // than ever scrolled past it (a window client's font, 2026-10-30: the + // session's peer started afresh at `top - history`, which overflowed). + var line: [80]u8 = undefined; + for (0..200) |i| { + s.stream.nextSlice(try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i})); + s.changes += 1; + } + try s.send(0); + try s.land(); + try s.a.resize(gpa, .{ .cols = 30, .rows = 24 }); + s.changes += 1; + try s.send(0); + try s.land(); + try s.settle(); + try testing.expect(s.track.rows.top >= s.track.history); +} + +test "several deltas in flight land in order, and a peer at the bound waits for an acknowledgement" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 80, 24, 1 << 20, 2); + defer s.deinit(); + s.resizes = false; + s.drops = false; + var line: [64]u8 = undefined; + for (0..bound + 5) |i| { + s.stream.nextSlice(try std.fmt.bufPrint(&line, "line {d}\r\n", .{i})); + s.changes += 1; + try s.send(1000); + } + // The bound's worth went; the rest wait, and are skipped. + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.land(); + try s.send(1000); + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.settle(); + try testing.expectEqual(@as(u64, 0), s.stats.refused); +} + +test "a replica's terminal pane draws as its authority's does" { + const pardes = @import("pardes.zig"); + const gpa = testing.allocator; + const a = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer a.deinit(); + const b = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer b.deinit(); + while (a.nextEffect()) |_| {} + while (b.nextEffect()) |_| {} + var fa = std.heap.ArenaAllocator.init(gpa); + defer fa.deinit(); + var fb = std.heap.ArenaAllocator.init(gpa); + defer fb.deinit(); + // Laid out once, so both panes have their size before the first delta. + _ = try a.render(fa.allocator()); + _ = try b.render(fb.allocator()); + const ta = &a.panes[0].?.terminal.?.vt; + const tb = &b.panes[0].?.terminal.?.vt; + var enc: Encoder = .init(gpa); + defer enc.deinit(); + var peer: Peer = .{}; + defer peer.deinit(gpa); + var tr: Track = .init(gpa); + defer tr.deinit(gpa, ta); + var rep: Replica = .{}; + defer rep.deinit(gpa); + var delta: std.Io.Writer.Allocating = .init(gpa); + defer delta.deinit(); + var line: [256]u8 = undefined; + for (0..60) |i| { + const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); + a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + delta.clearRetainingCapacity(); + try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); + peer.ack((try rep.apply(gpa, terminalIoForTest(), tb, delta.written())).seq); + b.needs_frame = true; + _ = fa.reset(.retain_capacity); + _ = fb.reset(.retain_capacity); + const sa = try a.render(fa.allocator()); + const sb = try b.render(fb.allocator()); + const r = a.rects[0]; + // The body: below the pane's tag row. + for (r.y + 1..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { + const ca = sa.at(@intCast(x), @intCast(y)); + const cb = sb.at(@intCast(x), @intCast(y)); + if (!ca.visuallyEqual(cb)) { + std.debug.print("step {d}: cell {d},{d}: '{s}' vs '{s}'\n", .{ i, x, y, ca.grapheme(), cb.grapheme() }); + return error.ReplicaDrawsDifferently; + } + }; + } +} + +fn terminalIoForTest() std.Io { + return std.Io.Threaded.global_single_threaded.io(); +} diff --git a/src/selection_pipe.zig b/src/selection_pipe.zig index 8a43a3af..834e805d 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/syntax.zig b/src/syntax.zig index 985a9888..858ef6a2 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -381,11 +381,35 @@ pub fn parseTree(path: []const u8, content: []const u8) ?*anyopaque { if (!enabled) return null; const selected = (forExt(std.fs.path.extension(path)) catch return null) orelse return null; const tree = selected.parser.parseString(content, null) orelse return null; + if (held_trees) |h| h.born.append(h.meta, @ptrCast(tree)) catch {}; return @ptrCast(tree); } +/// While a replica's rollback copy is held (src/rollback.zig) a tree parsed +/// is the speculation's, and one freed is put off: trees live on the C +/// heap, which the core's allocator cannot see. Null otherwise. +pub var held_trees: ?*HeldTrees = null; +pub const HeldTrees = struct { + meta: std.mem.Allocator, + born: std.ArrayListUnmanaged(*anyopaque) = .empty, + freed: std.ArrayListUnmanaged(*anyopaque) = .empty, +}; + pub fn freeTree(tree: *anyopaque) void { if (!enabled) return; + if (held_trees) |h| { + for (h.born.items, 0..) |t, i| if (t == tree) { + _ = h.born.swapRemove(i); + return destroyTree(tree); + }; + h.freed.append(h.meta, tree) catch destroyTree(tree); + return; + } + destroyTree(tree); +} + +pub fn destroyTree(tree: *anyopaque) void { + if (!enabled) return; const t: *ts.Tree = @ptrCast(@alignCast(tree)); t.destroy(); } diff --git a/src/tagline.zig b/src/tagline.zig index 79c2c09b..7af9ba66 100644 --- a/src/tagline.zig +++ b/src/tagline.zig @@ -10,7 +10,6 @@ const edit = @import("edit.zig"); const exec = @import("exec.zig"); const config = @import("config.zig"); const pdf_enabled = panes.Pdf.enabled; -const dump = @import("dump.zig"); const Layer = @import("Layer.zig"); const Pane = panes.Pane; const Text = panes.Text; @@ -361,6 +360,7 @@ pub fn draftName(p: *Pardes, pane: *Pane, col: usize) void { const name = p.gpa.dupe(u8, f.path) catch return; pane.closePrompt(); pane.prompt = .name; + pane.prompt_view = p.view; pane.input = .{ .what = .input, .own = name, .mode = .insert, .cur_pinned = true, .cur_col = @intCast(@min(name.len, panes.File.rawAtDisplay(name, col))) }; } @@ -423,13 +423,6 @@ pub fn enterHeader(p: *Pardes, column: ?usize) void { t.normal.clear(); } -/// A dump's tag tail, what was typed after the name: a dump from 0.4 on -/// writes it, and an older one's saved tag is not read. -pub fn restoreDumpTail(p: *Pardes, pane: *Pane, src: dump.Pane) void { - const tail = src.tag_tail orelse return; - pane.tag.own = p.gpa.dupe(u8, tail) catch return; -} - /// The span a header's selection covers on line `row` of its text, in /// that line's bytes: up to the caret while typing, and over the /// cursor's own cell too in normal mode, where a cursor is a cell. diff --git a/src/tape.zig b/src/tape.zig new file mode 100644 index 00000000..74576eed --- /dev/null +++ b/src/tape.zig @@ -0,0 +1,1350 @@ +//! 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=<tape> 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, `<prefix><pid>.tape`. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const walk = @import("walk.zig"); +const host_io = @import("host_io.zig"); +const terminal = @import("terminal.zig"); +const Pardes = pardes.Pardes; +const VTable = pardes.Host.VTable; +const libc = std.c; + +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, + step, + 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 }, + /// A dead session's shell taken in (Adopt), and a terminal's reader + /// that failed: what a host writes into the core, made inputs. + adopt_shell, + reader_failed: struct { id: u8, err: anyerror }, + shell_failed: struct { id: u8, err: anyerror }, + restart_failed: struct { id: u8, err: anyerror }, + save_failed: struct { id: u8, path: []const u8, err: anyerror }, + 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, + open_view, + close_view: u8, + leave_view: u8, + away_view: u8, + rejoin_view: u8, + to_view: u8, + /// What a window says of its font and its opacity (gui.zig). + take_font_request, + 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 +/// 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, + /// A search run on the loop (Stream.runHere: a 9P write's Grep or Find). + search, + // Read in place (`value`). + clock, + budget, + wake, + env, + interpreter, + denied, + shell, + mtime, + cwd, + pointer, + /// What a terminal's ingest told the positions pardes keeps over it. + positions, + /// Whether a terminal is at its prompt, as a step reads it after its + /// output (a greeting, a command waiting for the prompt), and what its + /// command printed (a pty/run's answer): a screen-state replica's + /// emulator is the last delta's until the next. + prompt, + /// What the authority alone holds and a step reads (`authority`). + authority, +}; + +/// What the sites return. wire.semantics hashes their shape with the +/// rest of the tape's (`shape`), so two builds whose tapes differ in it +/// refuse each other's replicas in words, and a site returning a type not +/// listed is a compile error (`call`). An error union is listed as +/// `anyerror!` its payload: an error goes by its name (walk.encode). +pub const results = .{ + void, bool, + u32, i64, + [8]u8, ?u64, + []const u8, ?[]const u8, + ?std.Io.File.Kind, ?pardes.Presentation.CellPosition, + ?pardes.filesystem.Resolved, pardes.filesystem.OsReadWrite, + anyerror!pardes.filesystem.OsStat, anyerror![]const u8, + anyerror![]u8, anyerror!void, + terminal.Told, ?pardes.ctlfs.pty.Printed, + @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, + []pardes.Stream.Batch, + // What only the authority holds (`authority`): a closed pane's record, + // Recent's entry for a closed jump, the closed panes' names. + ?@import("Store.zig").Record, ?@import("recent.zig").Entry, + []const @import("Store.zig").Named, +}; + +/// What a host reports to the core as failed (`shell_failed`, `save_failed`, +/// `dump_failed`, `report_error`), as a replica reads it back: an error is +/// taped by its name, and `anyerror` keeps no list of names to look one up +/// in. ponytail: kept by hand, the sets of what a host reports from; one +/// outside them is a Debug build's panic as it is taped (walk.encode) and a +/// release build's divergence (the replica joins again). No host, none. +pub const Failure = if (!pardes.hosted) error{} else ErrorOf(host_io.forkShell) || ErrorOf(pardes.filesystem.write) || + ErrorOf(pardes.filesystem.readRestore) || ErrorOf(pardes.dump.restore) || + @typeInfo(@TypeOf(pardes.config.User.dumpThemes(undefined, undefined, undefined, pardes.themes))).error_union.error_set || + ErrorOf(host_io.Lsp.snapshot) || + ErrorOf(host_io.Inbox.startReader) || std.Io.File.ReadStreamingError || + error{ NoDumpDirectory, ListenFailed, WorkersUnavailable, WorkersBusy }; + +fn ErrorOf(comptime f: anytype) type { + return @typeInfo(@typeInfo(@TypeOf(f)).@"fn".return_type.?).error_union.error_set; +} + +/// Every type the tape carries, as walk.shape says it: wire.semantics's. +pub const shape = blk: { + var types: []const type = &.{ Input, []const u64, struct { u8, u64 } }; + for (results) |R| types = types ++ .{R}; + for (host_fns) |f| types = types ++ .{@typeInfo(@typeInfo(@typeInfo(@FieldType(VTable, f)).optional.child).pointer.child).@"fn".return_type.?}; + for (watched) |name| types = types ++ .{@FieldType(Pardes, name)}; + break :blk walk.shape(types); +}; + +/// Fields a host writes straight into the core between its calls. +const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; + +const Kind = enum(u8) { input, done, host, ret, set, digest }; +/// A digest record's kind byte (replica.zig reads a unit's last). +pub const digest_kind: u8 = @backingInt(Kind.digest); +pub const set_kind: u8 = @backingInt(Kind.set); +const host_fns = @typeInfo(VTable).@"struct".field_names; +/// The `set` that says which Host functions the recording host had. +const set_mask = 0xff; + +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, + /// A replica applying its own input before the authority has ordered it + /// (src/replica.zig): recorded as the authority would record it, so the + /// two can be compared, but nothing is performed. A host call or a + /// syscall stops it (`poisoned`): what the call returns is the + /// authority's to say, and the input waits for it. A read of the clock + /// or the environment (`value`) is made here, a guess the comparison + /// checks. + speculating: bool = false, + poisoned: bool = false, + /// What stopped it: a host function's or a site's name, or a poison's + /// reason (PARDES_REPLICA_STATS counts them). + poisoned_by: []const u8 = "", + /// The authority's last yes or no to each question a replayed unit + /// asked it (`authority`), a speculation's guess. + answers: std.AutoHashMapUnmanaged(u64, bool) = .empty, + /// It read a terminal (its rows) or moved one's view: what it did + /// depends on the emulator, which deltas write (`touchTerminal`). + touched: bool = false, + /// The allocator of the core a replay makes (a replica's rollback epoch, + /// src/rollback.zig), when it is not the tape's own. + core_gpa: ?std.mem.Allocator = null, + /// Where a replay keeps what a core keeps of an input (its options), + /// for a tape that is let go; else the tape's allocator, for good. + kept_arena: ?std.heap.ArenaAllocator = null, + /// No digests recorded (a speculation's: a replica compares what it + /// was given and what it asked, from a state the authority shares). + no_digest: bool = false, + /// PARDES_REPLAY_DUMP (phase 2's gate): at the session's end, its dump + /// loaded must be it, digest for digest, and dump again byte for byte. + dump_check: bool = false, + dumps: usize = 0, + dump_bytes: usize = 0, + /// Screen-state terminals (src/screen_state.zig, docs/next-daemon.md + /// phase 5): an authority recording for replicas, or a replica. A + /// terminal's emulator is the authority's alone: its output is recorded + /// without its bytes (they cross as screen state), its pty writes are + /// not recorded, and no digest covers it. A replica's core never writes + /// one but by `apply`. + screen_state: bool = false, + /// A digest as one word (screen-state replicas: every unit of a pty's + /// output carries one, at the pty's read rate), not one a field; what + /// differs is then not named. + whole_digest: bool = false, + /// A digest only when asked (`recordDigest`): the replica stream's, + /// one a unit, not one after each of its inputs. + unit_digests: bool = false, + /// A replica client's guess of the session's clocks for a speculation + /// (src/replica.zig): its clock reads (a message's stamp, a file's + /// mtime) answer this instead of the clocks here. + guess: ?Guess = null, + /// The same guess, as the client sent it with its input, while the + /// authority steps that input: the step's clock reads are the input's + /// time, so the guess a speculation recorded is what the authority + /// records (a message's millisecond, an edit's second never miss). + stamp: ?Guess = null, + + fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { + t.scratch.clearRetainingCapacity(); + 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, @backingInt(kind)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, std.mem.asBytes(&len)) catch @panic("tape: out of memory"); + t.buf.appendSlice(t.gpa, payload) catch @panic("tape: out of memory"); + if (t.buf.items.len > 1 << 20) t.flush(); + } + + fn flush(t: *Tape) void { + if (comptime !pardes.hosted) return; + // A tape in memory (a replica's, an authority's stream) is taken + // by whoever holds it. + if (t.fd < 0) return; + var at: usize = 0; + while (at < t.buf.items.len) { + const n = libc.write(t.fd, t.buf.items[at..].ptr, t.buf.items.len - at); + 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); + } + // A terminal's output crosses as screen state, not as its bytes. + if (t.screen_state and input.* == .update and input.update == .output) { + var bare = input.*; + bare.update.output.bytes = &.{}; + t.record(.input, Input, &bare); + } else t.record(.input, Input, input); + return .{ .outer = true }; + } + + 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) and !t.no_digest and !t.unit_digests) if (t.core) |core| { + var parts: Parts = undefined; + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); + 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 = @fromBackingInt(@intCast(head[0])), .payload = .{ .bytes = payload } }; + } + + fn peek(t: *const Tape) ?Kind { + if (t.in.bytes.len - t.in.at < 5) return null; + return @fromBackingInt(@intCast(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 whole = [1]u64{digestOf(p)}; + const got: []const u64 = if (want.len == 1) &whole else digestParts(p, &parts); + if (!std.mem.eql(u64, got, want)) t.diverge("digest {d} differs in {s}", .{ t.digests, if (want.len == 1) "the session (a digest of one word: run the authority with whole_digest off to name the field)" else partsDiffer(got, want, t.arena.allocator()) }); + t.digests += 1; + } + + /// The session's dump loaded into a core of its own, which must be the + /// same session (its digest) and dump to the same bytes. The tape is + /// put aside meanwhile: a dump is no input. + fn dumpRoundTrip(t: *Tape, p: *Pardes) void { + on = null; + defer on = t; + const bytes = pardes.dump.dumpBytes(p) catch |err| return t.diverge("dump failed: {t}", .{err}); + defer t.gpa.free(bytes); + var notes: std.ArrayList(u8) = .empty; + defer notes.deinit(t.gpa); + const q = pardes.dump.load(t.gpa, p.opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); + defer q.deinit(); + if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items}); + var want: Parts = undefined; + var got: Parts = undefined; + const a = digestParts(p, &want); + const b = digestParts(q, &got); + if (!std.mem.eql(u64, a, b)) return t.diverge("dump round trip: the loaded digest differs in {s}", .{partsDiffer(b, a, t.arena.allocator())}); + const again = pardes.dump.dumpBytes(q) catch |err| return t.diverge("dump of the loaded failed: {t}", .{err}); + defer t.gpa.free(again); + if (!std.mem.eql(u8, bytes, again)) { + const at = std.mem.indexOfDiff(u8, bytes, again) orelse 0; + return t.diverge("dump is no fixpoint: {d} bytes then {d}, first differing at {d}", .{ bytes.len, again.len, at }); + } + t.dumps += 1; + t.dump_bytes += bytes.len; + } + + 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 keep = if (t.kept_arena) |*k| k.allocator() else t.gpa; + const input = walk.decode(Input, r, if (kept) keep else t.arena.allocator()) catch return t.diverge("a short input", .{}); + const outer = t.applying; + t.applying = switch (input) { + .update => |ev| @tagName(ev), + 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, + }; + const gpa = t.core_gpa orelse t.gpa; + opts.image_allocator = gpa; + opts.pdf_allocator = gpa; + opts.tree_sitter_allocator = gpa; + opts.frame_allocator = gpa; + t.core = switch (input) { + .init => Pardes.init(gpa, opts), + .init_from_dump => |d| pardes.dump.initFromDump(gpa, opts, d.bytes), + else => unreachable, + } catch |err| return t.diverge("init failed: {t}", .{err}); + t.core.?.host = t.hostOf(t.mask); + 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) { + if (t.dump_check) if (t.core) |core| t.dumpRoundTrip(core); + 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 {}, + .step => p.step(), + .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), + .adopt_shell => _ = p.adoptShell(), + .reader_failed => |a| p.readerFailed(a.id, a.err), + .shell_failed => |a| p.shellFailed(a.id, a.err), + .restart_failed => |a| p.restartFailed(a.id, a.err), + .save_failed => |a| p.saveFailed(a.id, a.path, a.err), + .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), + .open_view => _ = p.openView(), + .close_view => |k| p.closeView(k), + .leave_view => |k| p.leaveView(k), + .away_view => |k| p.awayView(k), + .rejoin_view => |k| _ = p.rejoinView(k), + .to_view => |k| p.toView(k), + .take_font_request => _ = p.takeFontRequest(), + .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), + .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(); + } +}; + +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]; + // A terminal's answers and what is typed at it from its output are the + // authority's to write, never a replica's: off the tape on both sides. + if (comptime std.mem.eql(u8, name, "pty_write")) if (t.screen_state and !t.speculating) { + if (!t.replay) @call(.auto, @field(t.real.vtable.*, name).?, .{t.real.ctx} ++ args); + return; + }; + if (t.replay) { + // A replica that never draws is never watched: it asks no one. + if (comptime std.mem.eql(u8, name, "watched")) if (t.headless) return false; + 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 id: u16 = i; + if (t.speculating) { + // Nobody watches a replica's step: its frames are its own, drawn + // outside it (and the authority's host says so too, or the + // comparison fails). + if (comptime std.mem.eql(u8, name, "watched")) { + const r = false; + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + } + t.poisoned = true; + t.poisoned_by = name; + return poisonOf(R); + } + const real = @field(t.real.vtable.*, name).?; + 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); +} + +/// A read of what the authority alone holds (`.authority` state a step +/// makes digested state from: the store's closed panes and Recent, which +/// every window of its user writes; the 9P server's readers and socket), +/// taped as `value` is; a speculation that reaches it waits for the +/// authority's answer, as at a host call. A replica's own is never read. +/// What it returns is the tape's arena's on a replay: `f` returns what +/// lives to the step's end (scratch, or the authority's own), and the +/// caller copies what it keeps. +/// +/// A yes or no (whether a script reads a pane's events, whether the +/// session serves a socket) is guessed by a speculation, mosh's way: the +/// authority's last answer to that question, as a replayed unit gave it, +/// recorded as the answer, so its unit confirms the guess or rolls it back. +/// A Look or a click's command in a pane then shows at once, not a round +/// trip late. ponytail: bools alone; a list (the closed panes, Recent) +/// changes with what the user does, and is asked of the authority. +pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) if (on) |t| { + const key = question(f, args); + if (t.speculating) if (t.answers.get(key)) |was| { + const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority); + t.record(.host, u16, &id); + t.record(.ret, bool, &was); + return was; + }; + if (t.replay) { + const got = value(.authority, f, args); + t.answers.put(t.gpa, key, got) catch {}; + return got; + } + }; + poison("the authority's own state"); + return value(.authority, f, args); +} + +/// What `authority` was asked: `f` and its arguments as they are. +fn question(comptime f: anytype, args: anytype) u64 { + var h = std.hash.Wyhash.init(@intFromPtr(&f)); + inline for (args) |a| h.update(std.mem.asBytes(&a)); + return h.final(); +} + +fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + comptime { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } + // No tape where there is no host to record or replay one. + if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); + const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); + const id: u16 = @as(u16, host_fns.len) + @backingInt(s); + if (t.replay) { + // Diverged (said in `diverged`): what a speculation gets, harmless, + // for the rest of the input. A replica joins again; a replay says so. + if (!t.expectHost(id, @tagName(s))) return poisonOf(R); + var ret = t.last_ret; + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { + t.diverge("the tape's answer to {t} is short", .{s}); + return poisonOf(R); + }; + } + // The host's own (a shell's spawn asking the shell), or one inside a + // site's syscall: the outer call is the record. + if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + // A syscall a speculating replica must not make: its input waits for + // the authority's answer. + if (yields and t.speculating) { + t.poisoned = true; + t.poisoned_by = @tagName(s); + return poisonOf(R); + } + if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |g| { + const r = guessed(R, g); + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + }; + 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; +} + +pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; + +/// A clock read of the session's, as a replica client guesses it: a file's +/// mtime (realtime seconds), a message's clock (monotonic ms), a message's +/// time of day (in the session's zone). +fn guessed(comptime R: type, g: Guess) R { + if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0; + if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms); + if (R == [8]u8) { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400); + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {}; + return clock; + } + @compileError("tape: no guess of a clock read as " ++ @typeName(R)); +} + +/// The session's digest recorded now (a unit's end: the replica stream's +/// one, `unit_digests`). +pub fn recordDigest(t: *Tape) void { + const core = t.core orelse return; + var parts: Parts = undefined; + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); + t.record(.digest, []const u64, &d); +} + +/// The core's digest against a digest record's payload (a unit's last): a +/// difference is `diverged`, its fields named when the record has them. +pub fn checkDigest(t: *Tape, payload: []const u8) void { + var r: walk.Reader = .{ .bytes = payload }; + t.check(&r); +} + +/// A core made outside the tape (a replica client's, from a dump) is the +/// one it replays into. +pub fn adoptCore(t: *Tape, p: *Pardes) void { + t.core = p; + p.host = t.hostOf(t.mask); +} + +/// A replica joins the session recorded on `t` (src/replica.zig): what the +/// host wrote in the core and which host it is are said again at the next +/// input, for a core made from a dump, which has neither. +pub fn resync(t: *Tape) void { + t.seen = @splat(0); + if (t.core) |p| if (p.host.vtable == &t.vtable) { + p.host = t.real; + }; + t.real = .{}; +} + +/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent: +/// what the clock said is taped, so a replica stops its work where this did. +pub fn over(since_ns: u64, budget_ns: u64) bool { + 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 or t.speculating else false; +} + +/// A syscall of the core's that is no site, made by a replica: what it +/// returns is the authority's to say. A speculation waits for it, and a +/// replay has diverged. +pub fn stray() void { + const t = on orelse return; + if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); +} + +/// A replica speculating (src/replica.zig): state only the authority +/// changes (a terminal's emulator) is left alone, and the input waits. +pub fn speculating() bool { + return if (on) |t| t.speculating else false; +} + +/// Screen-state terminals, on either side (`Tape.screen_state`). +pub fn screenState() bool { + return if (on) |t| t.screen_state else false; +} + +/// A replica's core under screen-state terminals: its emulators are +/// written by `apply` alone, never by its own steps. +pub fn screenReplica() bool { + return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; +} + +/// An authority recording for screen-state replicas. +pub fn screenAuthority() bool { + return if (on) |t| t.screen_state and !t.replay and !t.speculating else false; +} + +/// What a speculation reached is the authority's (`why`): the input is +/// undone and waits for the authority's order. +pub fn poison(why: []const u8) void { + if (on) |t| if (t.speculating) { + t.poisoned = true; + t.poisoned_by = why; + }; +} + +/// A speculation read a terminal's rows or moved its view (`Tape.touched`). +pub fn touchTerminal() void { + if (on) |t| if (t.speculating) { + t.touched = true; + }; +} + +/// A harmless stand-in for what a host call or a syscall would have +/// returned to a speculation that is thrown away once it returns: an +/// error where one can be said, else zeroes. +fn poisonOf(comptime R: type) R { + return switch (@typeInfo(R)) { + .error_union => |e| if (@typeInfo(e.error_set).error_set.error_names) |errors| + (if (errors.len > 0) @field(e.error_set, errors[0]) else std.mem.zeroes(R)) + else + error.Unexpected, + .optional => null, + .void => {}, + .@"union" => |u| inline for (u.field_names, u.field_types) |name, F| { + if (comptime zeroable(F)) break @unionInit(R, name, std.mem.zeroes(F)); + } else @compileError("tape: no harmless value of " ++ @typeName(R)), + else => std.mem.zeroes(R), + }; +} + +fn zeroable(comptime T: type) bool { + return switch (@typeInfo(T)) { + .int, .float, .bool, .optional, .void => true, + .pointer => |p| p.size == .slice, + .array => |a| zeroable(a.child), + .@"struct" => |s| blk: { + for (s.field_types) |F| if (!zeroable(F)) break :blk false; + break :blk true; + }, + else => false, + }; +} + +// ---- a replica's tapes in memory (src/replica.zig) ---- + +/// A tape recorded in memory: an authority's stream, taken unit by unit. +pub fn memory(gpa: std.mem.Allocator) Tape { + return .{ .gpa = gpa, .replay = false, .scratch = .init(gpa), .arena = .init(gpa), .kept_arena = .init(gpa) }; +} + +/// What a tape in memory holds, given back (its core is gone already). +pub fn release(t: *Tape) void { + t.answers.deinit(t.gpa); + t.buf.deinit(t.gpa); + t.scratch.deinit(); + t.arena.deinit(); + if (t.kept_arena) |*k| k.deinit(); +} + +/// The session's digest recorded now: a batch of steps recorded without +/// one each (`no_digest`) checked once, at its end. +pub fn digestHere(t: *Tape) void { + const core = t.core orelse return; + var parts: Parts = undefined; + const whole = [1]u64{digestOf(core)}; + const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts); + t.record(.digest, []const u64, &d); +} + +/// What was recorded since the last take; the tape's to reuse after. +pub fn taken(t: *Tape) []const u8 { + return t.buf.items; +} + +pub fn clearTaken(t: *Tape) void { + t.buf.clearRetainingCapacity(); +} + +/// The records of `bytes`, top-level inputs and all, applied to the +/// tape's core as the authority applied them, each digest checked; why +/// they diverged, or null. +pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 { + t.replay = true; + t.speculating = false; + defer t.replay = false; + t.in = .{ .bytes = bytes }; + while (t.diverged == null) { + const kind = t.peek() orelse break; + var rec = t.next().?; + switch (kind) { + .input => { + t.apply(&rec.payload); + _ = t.arena.reset(.retain_capacity); + }, + .set => t.applySet(&rec.payload), + .digest => t.check(&rec.payload), + .host, .ret, .done => t.diverge("a {t} record at the top", .{kind}), + } + } + return t.diverged; +} + +/// Starts recording `t`'s core as a speculation: the host's watched +/// fields as they are now, nothing recorded yet. +pub fn beginSpeculation(t: *Tape) void { + t.replay = false; + t.speculating = true; + t.poisoned = false; + t.poisoned_by = ""; + t.touched = false; + t.no_digest = true; + t.buf.clearRetainingCapacity(); + t.seeFields(); +} + +/// Whether it stayed clean (no host call, no syscall, no authority state). +pub fn endSpeculation(t: *Tape) bool { + t.speculating = false; + t.no_digest = false; + return !t.poisoned; +} + +/// Whether two tapes record the same inputs, host calls and answers, +/// their digests aside. +pub fn sameRecords(a: []const u8, b: []const u8) bool { + var x: walk.Reader = .{ .bytes = a }; + var y: walk.Reader = .{ .bytes = b }; + while (true) { + const ra = nextRecord(&x); + const rb = nextRecord(&y); + if (ra == null or rb == null) return ra == null and rb == null; + if (ra.?.kind != rb.?.kind or !std.mem.eql(u8, ra.?.payload, rb.?.payload)) return false; + } +} + +fn nextRecord(r: *walk.Reader) ?struct { kind: Kind, payload: []const u8 } { + while (true) { + const head = r.take(5) catch return null; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return null; + const kind: Kind = @fromBackingInt(@intCast(head[0])); + if (kind != .digest) return .{ .kind = kind, .payload = payload }; + } +} + +/// A tape's records, one a line (kind; an input's or a host call's name), +/// for a divergence's report. +pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { + var r: walk.Reader = .{ .bytes = bytes }; + var depth: usize = 0; + while (true) { + const head = r.take(5) catch return; + const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return; + const kind: Kind = @fromBackingInt(@intCast(head[0])); + if (kind == .done or kind == .ret) depth -|= 1; + w.splatByteAll(' ', depth * 2) catch {}; + switch (kind) { + .input => { + const tag = std.mem.bytesToValue(std.meta.Tag(Input), payload[0..@sizeOf(std.meta.Tag(Input))]); + w.print("input {t}", .{tag}) catch {}; + if (tag == .update) w.print(" {t}", .{std.mem.bytesToValue(std.meta.Tag(pardes.Event), payload[@sizeOf(std.meta.Tag(Input))..][0..@sizeOf(std.meta.Tag(pardes.Event))])}) catch {}; + // A key's or a pointer's own words: which one a divergence + // starts at. + var room: [4096]u8 = undefined; + var fixed: std.heap.FixedBufferAllocator = .init(&room); + var in: walk.Reader = .{ .bytes = payload }; + if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update) { + .key => |k| w.print(" {any}", .{k}) catch {}, + .mouse => |m| w.print(" {t} {t} {d},{d} body {any} tag {any}", .{ m.button, m.kind, m.col, m.row, m.body_hit != null, m.tag_hit != null }) catch {}, + else => {}, + } else |_| {}; + }, + .host => { + const id = std.mem.bytesToValue(u16, payload[0..2]); + w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {}; + }, + .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {}, + else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {}, + } + w.writeByte('\n') catch {}; + if (kind == .input or kind == .host) depth += 1; + } +} + +/// The digest a replica and its authority must agree on. +pub fn digestOf(p: *const Pardes) u64 { + return walk.digest(Pardes, p); +} + +/// PARDES_RECORD set: the tape is written from the session's first call. +pub fn startFromEnv() void { + if (comptime !pardes.hosted) return; + 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=<tape>`: 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) }; + t.dump_check = std.mem.eql(u8, run.name, "first") and libc.getenv("PARDES_REPLAY_DUMP") != null; + 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; + }; + // One killed (as the harnesses end theirs) has its end state here. + if (t.dump_check and t.diverged == null) if (t.core) |core| t.dumpRoundTrip(core); + 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 }); + if (t.dump_check and t.diverged == null) std.debug.print("replay {s}: {d} dumps loaded to their digests and dumped to their bytes again ({d} bytes)\n", .{ path, t.dumps, t.dump_bytes }); + } + return if (failed) 1 else 0; +} + +test "a syscall that is no site, made replaying, diverges the replay and poisons a speculation" { + if (comptime !pardes.hosted) return; + const gpa = std.testing.allocator; + var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; + defer t.scratch.deinit(); + defer t.arena.deinit(); + defer if (t.diverged) |why| gpa.free(why); + on = &t; + defer on = null; + pardes.turn.yield(); + pardes.turn.back(); + try std.testing.expect(t.diverged != null); + t.replay = false; + t.speculating = true; + pardes.turn.yield(); + pardes.turn.back(); + try std.testing.expect(t.poisoned); +} + +test "a replay that finds no answer where its core reads one diverges, and the read goes on harmless" { + if (comptime !pardes.hosted) return; + const gpa = std.testing.allocator; + var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; + defer t.scratch.deinit(); + defer t.arena.deinit(); + defer if (t.diverged) |why| gpa.free(why); + // The input's end where the core reads a value of the host's (a + // window client's step that took a turn the session's had not, H9 of + // 2026-10-30: it aborted the client). + const bytes = [5]u8{ @backingInt(Kind.done), 0, 0, 0, 0 }; + t.in = .{ .bytes = &bytes }; + t.host_side = false; + on = &t; + defer on = null; + const read = struct { + fn f() bool { + return true; + } + }.f; + try std.testing.expect(!value(.denied, read, .{})); + try std.testing.expect(t.diverged != null); + try std.testing.expect(!value(.denied, read, .{})); +} diff --git a/src/terminal.zig b/src/terminal.zig index ce41bd95..86eec32d 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -8,7 +8,6 @@ const Pardes = pardes.Pardes; const Surface = pardes.Surface; const modal = @import("modal.zig"); const tracy = @import("tracy.zig"); -const dump = @import("dump.zig"); const tagline = @import("tagline.zig"); const Key = pardes.Key; const Pane = panes.Pane; @@ -21,6 +20,8 @@ const ghostty_vt = if (enabled) @import("ghostty-vt") else struct {}; pub const history_max = if (enabled) 64 else 8; pub const VtSlot = if (enabled) ghostty_vt.Terminal else void; +/// A cell of an emulator's pages, as `getCell` reads it (src/echo.zig). +pub const PageCell = if (enabled) ghostty_vt.PageList.Cell else void; /// Kitty graphics are drawn where pixel geometry is (pdf_enabled, as for /// image panes' native placements). pub const images_enabled = enabled and pardes.pdf_enabled; @@ -144,6 +145,11 @@ pub const MarkedHandler = if (enabled) struct { } else void; pub const State = struct { + /// What the emulator and this struct are allocated with: the core's, + /// but on a screen-state replica the C heap (`emulator_gpa`), outside + /// its rollback epoch: written by deltas while a copy is held, no part + /// of it is ever a copy's to save or put back. + gpa: std.mem.Allocator, vt: VtSlot, stream: StreamSlot, /// The emulator's answers to its program (DSR, DA, kitty graphics, ...) @@ -159,6 +165,176 @@ pub const State = struct { mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {}, /// The kitty graphics its program placed, as drawn (TerminalImages.zig). images: TerminalImages = if (images_enabled) .{} else {}, + /// Counts the emulator's writes (output, a resize, its view scrolled): + /// a new count is a new state for its replicas (screen_state.Track). + changes: u32 = 0, + /// A step read its rows (its history too): a replica replaying that + /// step must hold them, so its authority sends the ones it does not + /// before the step's unit (replica.Screens.fill). + read: bool = false, + /// The size a step gave it, on an authority with screen-state replicas: + /// made after the step's unit (`resizeDeferred`), so that the rest of + /// the step reads the grid it had, as its replicas' replays do, and the + /// rows sent them for it (Screens.fill) are of that grid's numbering. + resize_to: ?struct { cols: u16, rows: u16 } = null, + + /// The emulator as its program has made it, for the replay digest + /// (src/walk.zig): the active screen's text, how much history is above + /// 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; + // Under screen-state terminals the emulator is the authority's, and + // a replica's is what the last delta made it: compared as screens + // (test/replicas.zig), not step by step. + if (pardes.tape.screenState()) return; + s.digestScreen(h, true); + } + + /// `view`: where its view is too (a replica's speculation may move it). + pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher, view: bool) void { + if (comptime !enabled) return; + const screen = s.vt.screens.active; + const sb = screen.pages.scrollbar(); + const cur = screen.cursor; + const offset = if (view) sb.offset else 0; + const facts = .{ sb.total, offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; + inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); + h.update(s.reply[0..s.reply_len]); + var buf: [4096]u8 = undefined; + 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; + } + + /// What the dump keeps of a terminal beside its emulator, by name as + /// every dump is (src/walk.zig). + const Parts = struct { + reply: []const u8 = "", + reply_dropped: u32 = 0, + mouse_cell: ?ghostty_vt.Coordinate = null, + petscii: bool = false, + /// The view's top row, counted from the top of the history. + view: usize = 0, + phase: @FieldType(MarkedHandler, "phase") = .none, + started: u32 = 0, + prompts: u32 = 0, + declined: u32 = 0, + continuation: bool = false, + lost: bool = false, + finished: @FieldType(MarkedHandler, "finished") = null, + out_start: ?ghostty_vt.Coordinate = null, + out_end: ?ghostty_vt.Coordinate = null, + }; + + fn pinAt(s: *const State, slot: ?*ghostty_vt.Pin) ?ghostty_vt.Coordinate { + const pin = slot orelse return null; + const screen = s.vt.screens.get(.primary) orelse return null; + return (screen.pages.pointFromPin(.screen, pin.*) orelse return null).screen; + } + + /// The dump's: ghostty's snapshot of the emulator (both screens, their + /// history, styles, links and kitty images), then `Parts`. A sequence + /// its program had begun and not ended is not kept: the snapshot is at + /// the ground state. + pub fn dumpWrite(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer) !void { + if (comptime !enabled) return; + const h = &s.stream.handler; + const parts: Parts = .{ + .reply = s.reply[0..s.reply_len], + .reply_dropped = s.reply_dropped, + .mouse_cell = s.mouse_cell, + .petscii = if (comptime images_enabled) s.images.petscii else false, + .view = s.vt.screens.active.pages.scrollbar().offset, + .phase = h.phase, + .started = h.started, + .prompts = h.prompts, + .declined = h.declined, + .continuation = h.continuation, + .lost = h.lost, + .finished = h.finished, + .out_start = s.pinAt(h.out_start), + .out_end = s.pinAt(h.out_end), + }; + const bytes = try pardes.walk.save(Parts, gpa, &parts); + defer gpa.free(bytes); + try w.writeAll(std.mem.asBytes(&@as(u64, bytes.len))); + try w.writeAll(bytes); + // A join's (dump.zig `dumpJoin`): its size alone, an emulator made + // afresh there, which its first delta replaces. + if (pardes.dump.slim_terminals) return w.writeAll(std.mem.asBytes(&[2]u16{ s.vt.cols, s.vt.rows })); + try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = .ground }); + } + + /// What `dumpRead` made, freed: a load that failed after it. + pub fn dumpFree(s: *State, gpa: std.mem.Allocator) void { + if (comptime !enabled) return; + if (comptime images_enabled) s.images.deinit(gpa); + s.stream.deinit(); + s.vt.deinit(gpa); + } + + /// `dumpWrite`'s back into `out`, a new State, as `create` makes one. + pub fn dumpRead(out: *State, gpa: std.mem.Allocator, bytes: []const u8) !void { + if (comptime !enabled) { + out.* = .{ .vt = {}, .stream = {} }; + return; + } + if (bytes.len < 8) return error.EndOfDump; + const n = std.math.cast(usize, std.mem.bytesToValue(u64, bytes[0..8])) orelse return error.EndOfDump; + if (bytes.len - 8 < n) return error.EndOfDump; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const parts = try pardes.walk.load(Parts, arena.allocator(), bytes[8..][0..n], null); + var reader: std.Io.Reader = .fixed(bytes[8 + n ..]); + if (reader.buffer.len == 4) { + const size = std.mem.bytesToValue([2]u16, bytes[8 + n ..][0..4]); + out.* = .{ .gpa = gpa, .vt = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .stream = undefined }; + } else { + var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ + .max_continuation_bytes = 0, + .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, + }); + defer decoded.deinit(gpa); + out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; + } + if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { + if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); + }; + out.stream = .init(.{ .handler = .{ .inner = out.vt.vtHandler() }, .allocator = gpa }); + out.stream.handler.inner.effects.write_pty = ptyReport; + out.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; + out.stream.handler.inner.effects.size = ptySize; + const reply = parts.reply[0..@min(parts.reply.len, out.reply.len)]; + @memcpy(out.reply[0..reply.len], reply); + out.reply_len = @intCast(reply.len); + out.reply_dropped = parts.reply_dropped; + out.mouse_cell = parts.mouse_cell; + out.images = if (images_enabled) .{ .petscii = parts.petscii } else {}; + if (comptime images_enabled) TerminalImages.installPngDecoder(); + const h = &out.stream.handler; + h.phase = parts.phase; + h.started = parts.started; + h.prompts = parts.prompts; + h.declined = parts.declined; + h.continuation = parts.continuation; + h.lost = parts.lost; + h.finished = parts.finished; + if (out.vt.screens.get(.primary)) |screen| { + inline for (.{ .{ "out_start", parts.out_start }, .{ "out_end", parts.out_end } }) |pair| if (pair[1]) |at| { + if (screen.pages.pin(.{ .screen = at })) |pin| @field(h, pair[0]) = screen.pages.trackPin(pin) catch null; + }; + } + const screen = out.vt.screens.active; + const bottom = screen.pages.scrollbar().offset; + screen.scroll(.{ .delta_row = @as(isize, @intCast(parts.view)) - @as(isize, @intCast(bottom)) }); + } }; const GColor = ghostty_vt.color; @@ -302,7 +478,7 @@ pub fn shellSpawned(p: *Pardes, id: usize, prompt_marks: bool) void { pub fn releasePendingCommandIfReady(p: *Pardes, id: usize, pane: *Pane) void { if (pane.pending_command.wait == .input) - releasePendingCommand(p, id, pane, promptInputReady(pane)); + releasePendingCommand(p, id, pane, pardes.tape.value(.prompt, promptInputReady, .{pane})); } fn releasePendingCommand(p: *Pardes, id: usize, pane: *Pane, ready: bool) void { @@ -371,15 +547,118 @@ pub fn visibleCursor(pane: *const Pane) ?GridCursor { const cur = state.vt.screens.active.cursor; const sb = scrollbar(pane); const row = sb.total - sb.len + cur.y; - if (row < sb.offset or row - sb.offset >= sb.len) return null; - return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset) }; + if (row < sb.offset + pane.pan or row - sb.offset >= sb.len) return null; + return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset - pane.pan) }; +} + +/// The rows terminal `pane`'s top is passed over by in a view whose rect +/// `r` is shorter than it, one that does not size it (Pardes.sizes): as +/// many as keep its cursor's row in sight, as tmux pans a bigger window in +/// a smaller client. 0 in the view that sizes it, and in one tall enough. +pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { + if (comptime !enabled) return 0; + if (pane.sized_by == p.view or pane.collapsed or !pane.isTerminal()) return 0; + const shown = r.h -| pane.tag_rows; + if (shown == 0 or shown >= pane.rows) return 0; + const state = pane.terminal orelse return 0; + const sb = scrollbar(pane); + // The cursor's row in the viewport; past its end (scrolled back), its + // last row. + const row = sb.total - sb.len + state.vt.screens.active.cursor.y; + const at: usize = if (row < sb.offset) 0 else @min(row - sb.offset, pane.rows - 1); + return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); +} + +/// What pardes made of a terminal's stream that its emulator does not hold +/// (the OSC 133 cycle, MarkedHandler), for a replica's delta: phase u8, +/// started, prompts, declined u32, continuation and lost u8, a finished +/// command's seq u32 and status i32 (all ones: none; i32 min: no status). +// ponytail: the output pins stay the authority's (its 9P pty/run reads them). +pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4; + +pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 { + if (comptime !enabled) return &.{}; + const m = &state.stream.handler; + var w: std.Io.Writer = .fixed(buf); + w.writeByte(@intFromEnum(m.phase)) catch unreachable; + inline for (.{ m.started, m.prompts, m.declined }) |n| w.writeInt(u32, n, .little) catch unreachable; + w.writeAll(&.{ @intFromBool(m.continuation), @intFromBool(m.lost) }) catch unreachable; + w.writeInt(u32, if (m.finished) |f| f.seq else std.math.maxInt(u32), .little) catch unreachable; + w.writeInt(i32, if (m.finished) |f| f.status orelse std.math.minInt(i32) else 0, .little) catch unreachable; + return w.buffered(); +} + +/// A replica's terminal takes its authority's marks (`marksOf`). +pub fn takeMarks(state: *State, bytes: []const u8) void { + if (comptime !enabled) return; + if (bytes.len != marks_len) return; + const m = &state.stream.handler; + m.phase = @enumFromInt(bytes[0]); + m.started = std.mem.readInt(u32, bytes[1..5], .little); + m.prompts = std.mem.readInt(u32, bytes[5..9], .little); + m.declined = std.mem.readInt(u32, bytes[9..13], .little); + m.continuation = bytes[13] != 0; + m.lost = bytes[14] != 0; + const seq = std.mem.readInt(u32, bytes[15..19], .little); + const status = std.mem.readInt(i32, bytes[19..23], .little); + m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; +} + +/// A screen-state replica's emulators' allocator (`State.gpa`). +pub const emulator_gpa = std.heap.c_allocator; + +/// What a terminal's emulator is made with, by `create` or a pane's load +/// (dump.loadPane, a closed terminal brought back): `gpa`, but on a screen- +/// state replica `emulator_gpa`. A hosted build's: the web has no C heap. +pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator { + return if (comptime pardes.hosted) (if (pardes.tape.screenReplica()) emulator_gpa else gpa) else gpa; +} + +/// A replica's speculation leaves an emulator be: it is the authority's +/// (docs/next-daemon.md, phase 5), so the input waits for its order. +fn authorityOnly(comptime why: []const u8) bool { + if (!pardes.tape.speculating()) return false; + pardes.tape.poison(why); + return true; +} + +/// A speculation that moves a terminal's view: a screen-state replica's +/// may (its rollback puts the view back, replica.zig `views`, and a delta +/// sets it), any other stops (`authorityOnly`). +fn viewOnly(comptime why: []const u8) bool { + if (!pardes.tape.speculating()) return false; + if (pardes.tape.screenState()) { + pardes.tape.touchTerminal(); + return false; + } + pardes.tape.poison(why); + return true; +} + +/// Where a terminal's view is: its top row counted from the top of its +/// history, or null following its output (a replica's rollback copy, +/// which holds no emulator, keeps it so). +pub fn viewOf(pane: *const Pane) ?usize { + if (comptime !enabled) return null; + const state = pane.terminal orelse return null; + const pages = &state.vt.screens.active.pages; + if (pages.viewport == .active) return null; + return pages.scrollbar().offset; +} + +pub fn viewTo(pane: *Pane, at: ?usize) void { + if (comptime !enabled) return; + const state = pane.terminal orelse return; + state.vt.screens.active.scroll(if (at) |row| .{ .row = row } else .active); } /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). pub fn scrollGrid(pane: *Pane, delta: i32) void { if (comptime !enabled) return; + if (viewOnly("scroll a terminal")) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); + state.changes +%= 1; } /// Is the viewport already showing the last row of output? @@ -392,20 +671,47 @@ pub fn atBottom(pane: *const Pane) bool { /// Snap the viewport back onto live output. pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; + if (viewOnly("follow a terminal")) return; const state = pane.terminal orelse return; + const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); + state.changes +%= @intFromBool(moved); } /// Reflow the grid. A failed reflow keeps the grid it had rather than dropping /// a scrollback; the next resize retries with the same numbers. pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) void { if (comptime !enabled) return; + if (authorityOnly("resize a terminal")) return; const state = pane.terminal orelse return; if (cols != pane.cols) for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); pane.sel[button].state = .none; }; - state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {}; + // A replica's emulator is sized by its authority: the next delta says. + if (pardes.tape.screenReplica()) return; + // ...and the authority's once the step's unit is out (a client typing + // at another size than the last typist's read history rows its replay + // held as holes, and the rows came renumbered, 2026-10-30). + if (pardes.tape.screenAuthority()) { + state.resize_to = .{ .cols = cols, .rows = rows }; + return; + } + state.changes +%= 1; + _ = gpa; + state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; +} + +/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an +/// authority with screen-state replicas (replica.Peers.unit). +pub fn resizeDeferred(p: *Pardes) void { + if (comptime !enabled) return; + for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| { + state.resize_to = null; + state.changes +%= 1; + p.shell_rows.markStale(pane); + state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; + }; } /// DECSET 2004: the program wants its pastes bracketed. @@ -601,23 +907,21 @@ pub fn screenTextLen(pane: *Pane) usize { /// Release the emulator's heap. The Pane allocation itself is the core's. pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { if (comptime !enabled) return; + // A pane with none (a file's, a picker's, a preview's) closes in a + // speculation as anywhere. const state = pane.terminal orelse return; + if (authorityOnly("close a terminal")) return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); - state.vt.deinit(gpa); - gpa.destroy(state); + const own = state.gpa; + state.vt.deinit(own); + own.destroy(state); pane.terminal = null; } -/// Allocate the live emulator half of a terminal pane. Slot ownership, serial -/// assignment, and spawn effects remain core lifecycle invariants. -pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - const pane = try createDoc(gpa, cols, rows); - errdefer gpa.destroy(pane); - if (comptime !enabled) return pane; - const state = try gpa.create(State); - errdefer gpa.destroy(state); - state.vt = try ghostty_vt.Terminal.init(terminalIo(), gpa, .{ +/// A terminal pane's emulator, `cols` by `rows`, as `create` makes one. +fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { + return ghostty_vt.Terminal.init(terminalIo(), gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = 16 * 1024 * 1024, @@ -628,10 +932,30 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { // defaults to direct alone, which `kitten icat` and timg detect and // fall back from, base64 through the pty. What the program can read // it could send itself: it runs as the user, under pardes. - .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else .direct, + .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, }); - state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); +} + +/// Allocate the live emulator half of a terminal pane. Slot ownership, serial +/// assignment, and spawn effects remain core lifecycle invariants. +pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { + if (authorityOnly("open a terminal")) return error.OutOfMemory; + const pane = try createDoc(gpa, cols, rows); + errdefer gpa.destroy(pane); + if (comptime !enabled) return pane; + const own = emulatorGpa(gpa); + const state = try own.create(State); + errdefer own.destroy(state); + state.gpa = own; + state.vt = try emulator(own, cols, rows); + state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = own }); state.reply_len = 0; + state.changes = 0; + state.read = false; + state.resize_to = null; + // Every field its default (create leaves them what the heap had, which + // a digest then reads: two replicas must agree, src/replica.zig). + state.reply_dropped = 0; state.mouse_cell = null; state.images = if (images_enabled) .{} else {}; if (comptime images_enabled) TerminalImages.installPngDecoder(); @@ -656,48 +980,35 @@ pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { /// Where a restored terminal's history ends and its new shell begins. pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history \u{2500}\u{2500}\x1b[0m\r\n"; -/// Rebuild a dump's terminal emulator from its recorded output. `live`: a -/// shell will be spawned into it, so the history is marked off from what -/// that shell prints and the pane waits for it like any new terminal. -/// Registration and tag/cwd policy stay with the core; raw VT replay and -/// viewport restoration belong here. -pub fn restore(p: *Pardes, src: dump.Pane, live: bool) !*Pane { - const terminal = src.terminal.?; - if (comptime !enabled) { - const pane = try create(p.gpa, @max(1, src.cols), @max(1, src.rows)); - errdefer p.gpa.destroy(pane); - if (terminal.stream.len > 0) - pane.ovl = .{ .row = 0, .rows = 1, .text = try p.gpa.dupe(u8, terminal.stream) }; - return pane; - } - const bytes = if (terminal.stream_b64.len > 0) - try dump.decodeBytes(p.scratch.allocator(), terminal.stream_b64) - else - &.{}; - const pane = try create(p.gpa, @max(1, src.cols), @max(1, src.rows)); - if (live) { - if (bytes.len > 0) { - ingest(pane, bytes); - ingest(pane, restored_banner); - } - followOutput(pane); - // A view left scrolled back stays on what it was showing: the - // banner's line breaks scrolled a full screen by that many more. - if (src.scroll > 0) scrollGrid(pane, -@as(i32, @intCast(src.scroll + comptime std.mem.count(u8, restored_banner, "\n")))); - armShellSpawn(pane); - } else if (bytes.len > 0) { - ingest(pane, bytes); - followOutput(pane); - if (src.scroll > 0) - scrollGrid(pane, -@as(i32, @intCast(src.scroll))); - } - return pane; +/// A terminal loaded from a dump, its shell not there, made ready for a new +/// one: its program's screen modes reset (the old program is gone), its +/// history marked off from what the new shell prints, and the pane waiting +/// for it like any new terminal (a line queued for the old one goes to it). +pub fn reviveShell(pane: *Pane) void { + if (comptime !enabled) return; + if (pane.terminal == null) return; + // The primary screen, keys, cursor and mouse as a shell starts with + // them, the cursor where it was (a soft reset, or leaving a primary + // screen for itself, would take it home). + if (pane.terminal.?.vt.screens.active_key == .alternate) ingest(pane, "\x1b[?1049l"); + ingest(pane, "\x1b[?1l\x1b>\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[<u"); + ingest(pane, restored_banner); + // What the old shell was, ran and said is gone with it: the REPL it + // ran, the Tty+ word naming it. + pane.shell_spoke = false; + pane.had_shell = false; + pane.shell_failed = false; + pane.shell_tail_len = 0; + pane.repl = null; + pane.sent_command = null; + pane.pending_command.wait = .spawn; } /// Feed the emulator. Live output and restoration share this byte path, /// then apply different views. fn ingest(pane: *Pane, bytes: []const u8) void { pane.terminal.?.stream.nextSlice(bytes); + pane.terminal.?.changes +%= 1; } test "replay ownership is terminal-only and construction rolls back on allocation failure" { @@ -714,6 +1025,40 @@ test "replay ownership is terminal-only and construction rolls back on allocatio try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{true}); } +test "a terminal made fresh has no size waiting: one never resized keeps its grid when a unit is cut" { + if (comptime !enabled) return error.SkipZigTest; + // Fresh memory as a Debug build's allocator hands it out: 0xAA. A field + // `create` left unset read a size of 43690x43690 (fixer 2's, 2026-10-30). + const Filling = struct { + child: std.mem.Allocator, + fn alloc(ctx: *anyopaque, len: usize, a: std.mem.Alignment, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + const ptr = self.child.rawAlloc(len, a, ra) orelse return null; + @memset(ptr[0..len], 0xAA); + return ptr; + } + fn resize(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawResize(buf, a, n, ra); + } + fn remap(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawRemap(buf, a, n, ra); + } + fn free(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, ra: usize) void { + const self: *@This() = @ptrCast(@alignCast(ctx)); + self.child.rawFree(buf, a, ra); + } + const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free }; + }; + var filling: Filling = .{ .child = std.testing.allocator }; + const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; + const pane = try create(gpa, 40, 12); + defer gpa.destroy(pane); + defer deinitEmulator(pane, gpa); + try std.testing.expect(pane.terminal.?.resize_to == null); +} + test "document construction allocates only the pane and has inert terminal defaults" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1 }); const allocator = failing.allocator(); @@ -764,32 +1109,32 @@ test "terminal state keeps parser fragments and sends emulator replies through i try std.testing.expectEqual(@as(u16, 0), state.reply_len); } -test "a terminal stream restores the cells it drew" { +test "a terminal's dump restores the cells it drew, its history and where its view was" { if (comptime !enabled) return error.SkipZigTest; const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); while (p.nextEffect()) |_| {} const pane = p.panes[0].?; - const drawn = "\x1b[31mred\x1b[0m\r\ncafé\r\n"; - feedOutput(p, pane, drawn); - const encoded = try dump.encodeBytes(gpa, drawn); - defer gpa.free(encoded); - const restored = try restore(p, .{ - .kind = .terminal, - .tag = "", - .body = "", - .cols = pane.cols, - .rows = pane.rows, - .terminal = .{ .stream_b64 = encoded }, - }, false); - defer gpa.destroy(restored); - defer deinitEmulator(restored, gpa); - const before = try screenTextAlloc(pane, gpa); - defer gpa.free(before); - const after = try screenTextAlloc(restored, gpa); - defer gpa.free(after); - try std.testing.expectEqualStrings(before, after); + feedOutput(p, pane, "\x1b[31mred\x1b[0m\r\ncafé\r\n"); + for (0..30) |i| { + var line: [16]u8 = undefined; + feedOutput(p, pane, std.fmt.bufPrint(&line, "line {d}\r\n", .{i}) catch unreachable); + } + scrollGrid(pane, -5); + const bytes = try pardes.walk.save(State, gpa, pane.terminal.?); + defer gpa.free(bytes); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const state = try gpa.create(State); + defer gpa.destroy(state); + state.* = try pardes.walk.load(State, gpa, bytes, null); + defer { + state.stream.deinit(); + state.vt.deinit(gpa); + } + try std.testing.expectEqual(pardes.walk.digest(State, pane.terminal.?), pardes.walk.digest(State, state)); + try std.testing.expectEqual(pane.terminal.?.vt.screens.active.pages.scrollbar(), state.vt.screens.active.pages.scrollbar()); } test "a restored command pane comes back finished, its command not run again" { @@ -802,8 +1147,8 @@ test "a restored command pane comes back finished, its command not run again" { _ = try p.newCommand(cmd, "/tmp", "make test"); feedOutput(p, p.panes[cmd].?, "ok\r\n"); p.update(.{ .exited = .{ .pane = @intCast(cmd), .status = 2 } }); - try dump.dumpState(p); - const restored = try dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, p.dump_out.?); + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, p.dump_out.?); defer restored.deinit(); var spawns: usize = 0; while (restored.nextEffect()) |effect| switch (effect) { @@ -832,8 +1177,8 @@ test "a restored terminal keeps its history above a marker and gets a shell wher feedOutput(p, pane, "old output\r\n$ ls\r\n"); try pane.setOwnedCwd("/tmp"); pane.shell = try gpa.dupe(u8, "bash"); // `Tty bash`: it comes back as bash - try dump.dumpState(p); - const restored = try dump.initFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, p.dump_out.?); + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, p.dump_out.?); defer restored.deinit(); var spawned = false; while (restored.nextEffect()) |effect| switch (effect) { @@ -858,9 +1203,16 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. if (comptime !enabled) return; if (pane.terminal == null) return; + if (authorityOnly("a terminal's output")) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; + // A replica's emulator is what the authority's deltas make it; what + // pardes keeps over it moves as the authority's ingest told it. + if (pardes.tape.screenReplica()) { + if (has_positions) ingestWithPositions(pane, bytes); + return; + } // Sticky bottom, as every other terminal does it: output drags the // viewport down only when the viewport was already on the last row, so // a reader scrolled back into the scrollback stays where they scrolled. @@ -879,7 +1231,9 @@ const PositionPin = struct { edit_row: ?i32 = null, }; -fn trackRow(pages: *ghostty_vt.PageList, row: i32, failed: *bool) RowPin { +fn trackRow(maybe_pages: ?*ghostty_vt.PageList, row: i32, failed: *bool) RowPin { + // A screen-state replica's: told by the tape, not its emulator. + const pages = maybe_pages orelse return .{ .offset = row }; if (row < 0) return .{ .offset = row }; const bounded: usize = @min(@as(usize, @intCast(row)), pages.total_rows - 1); var left = pages.total_rows - 1 - bounded; @@ -909,7 +1263,7 @@ fn pinnedRow(pages: ?*ghostty_vt.PageList, row: RowPin) i32 { return 0; } -fn trackPosition(pages: *ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { +fn trackPosition(pages: ?*ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { var grid = target.*; if (overlay) |edit| { const lines: i32 = @intCast(modal.lineCount(edit.text)); @@ -928,11 +1282,24 @@ fn pointerRowCount(pane: *const Pane) usize { return count; } +/// What an ingest told the positions over it (`ingestWithPositions`): each +/// tracked row where it is after it (the spans' starts and ends, the +/// positions, the pointer rows, in that order) and whether the program +/// erased it, whether the screen was cleared, whether its pages lived and +/// whether a pin could not be made. Taped: a screen-state replica, which +/// ingests nothing, is told the authority's. +pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; + +fn told(t: Told) Told { + return t; +} + fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { const screens = &pane.terminal.?.vt.screens; const key = screens.active_key; const generation = screens.generation(key); - const pages = &screens.active.pages; + const replica = pardes.tape.screenReplica(); + const pages: ?*ghostty_vt.PageList = if (replica) null else &screens.active.pages; const Group = struct { overlay: ?*EditBuffer, cursor: *i32, selection: *panes.Text.CharSel }; var groups: [history_max * 2 + 1]Group = undefined; groups[0] = .{ @@ -1014,31 +1381,61 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { } }; } else failed = true; - // Whether the program cleared the screen (ED 2, a switch of screens); - // pardes reads the flag nowhere else. - pane.terminal.?.vt.flags.dirty.clear = false; - ingest(pane, bytes); - const cleared = pane.terminal.?.vt.flags.dirty.clear; - // RIS can destroy the alternate screen and every pin it owned. - const alive = if (screens.generation(key) == generation) pages else null; - var moved = false; - var pointer_rows_lost = alive == null or pointer_positions == null; - if (pointer_positions) |pins| for (pins) |position| { - if (alive != null) if (position.row.pin) |pin| { - if (pin.garbage) pointer_rows_lost = true; + var cleared = false; + var alive: ?*ghostty_vt.PageList = null; + if (!replica) { + // Whether the program cleared the screen (ED 2, a switch of + // screens); pardes reads the flag nowhere else. + pane.terminal.?.vt.flags.dirty.clear = false; + ingest(pane, bytes); + cleared = pane.terminal.?.vt.flags.dirty.clear; + // RIS can destroy the alternate screen and every pin it owned. + alive = if (screens.generation(key) == generation) pages else null; + } + const pointers: []PositionPin = pointer_positions orelse &.{}; + const n = 2 * nspans + npositions + pointers.len; + // On the stack, as the local path never allocated here but for the + // pointer's rows. + var rows_buf: [512]i32 = undefined; + var erased_buf: [512]bool = undefined; + const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return; + defer if (n > rows_buf.len) pane.gpa.free(rows_after); + const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return; + defer if (n > erased_buf.len) pane.gpa.free(erased_after); + @memset(rows_after, 0); + @memset(erased_after, false); + if (!replica) { + var k: usize = 0; + for (spans[0..nspans]) |span| for ([_]RowPin{ span.start, span.end }) |rp| { + rows_after[k] = pinnedRow(alive, rp); + erased_after[k] = alive != null and rp.pin != null and rp.pin.?.garbage; + k += 1; + }; + for ([_][]PositionPin{ positions[0..npositions], pointers }) |batch| for (batch) |position| { + rows_after[k] = pinnedRow(alive, position.row); + erased_after[k] = alive != null and position.row.pin != null and position.row.pin.?.garbage; + k += 1; }; + } + const after = pardes.tape.value(.positions, told, .{Told{ .rows = rows_after, .erased = erased_after, .cleared = cleared, .alive = alive != null, .failed = failed }}); + if (after.rows.len != n or after.erased.len != n) return; // a replica its authority's states apart (diverged) + failed = after.failed; + const at_position = 2 * nspans; + const at_pointer = at_position + npositions; + var moved = false; + var pointer_rows_lost = !after.alive or pointer_positions == null; + for (0..pointers.len) |i| if (after.erased[at_pointer + i]) { + pointer_rows_lost = true; }; // The program cleared the screen, or the rows the live copy stood on are // gone (pruned, a reset): the copy goes with them, as what the program // drew next. Kept, ghostty would move its rows to the top of what is // left, over the program's new text. - var copy_lost = cleared or alive == null; - for (spans[0..nspans]) |span| { - if (span.overlay == overlay) if (span.start.pin) |pin| if (alive != null and pin.garbage) { - copy_lost = true; - }; - const start = pinnedRow(alive, span.start); - const rows = @max(1, pinnedRow(alive, span.end) -| start +| 1); + var copy_lost = after.cleared or !after.alive; + for (spans[0..nspans], 0..) |span, i| { + if (span.overlay == overlay and after.erased[2 * i]) copy_lost = true; + const start = after.rows[2 * i]; + const rows = @max(1, after.rows[2 * i + 1] -| start +| 1); moved = moved or start != span.overlay.row or rows != span.overlay.rows; span.overlay.row = start; span.overlay.rows = rows; @@ -1050,23 +1447,16 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) void { // The rule above for the copy holds for the selections: one whose row // is gone (ED 3 erased its history row, which ghostty flags `garbage`) // goes too, rather than being clamped to the top. - const erased = struct { - fn at(owner: ?*ghostty_vt.PageList, all: []const PositionPin, index: usize) bool { - if (owner == null) return false; - const pin = all[index].row.pin orelse return false; - return pin.garbage; - } - }.at; + const erased = after.erased[at_position..]; const vsel_lost = if (vsel_at) |i| - erased(alive, positions[0..npositions], i) or (copy_lost and positions[i].edit_row != null) + erased[i] or (copy_lost and positions[i].edit_row != null) else false; - const msel_lost = if (msel_at) |i| erased(alive, positions[0..npositions], i) or erased(alive, positions[0..npositions], i + 1) else false; + const msel_lost = if (msel_at) |i| erased[i] or erased[i + 1] else false; var sels_keep: [panes.Text.max_selections]bool = undefined; - for (0..pane.body.nsel) |i| sels_keep[i] = !(erased(alive, positions[0..npositions], sels_at + 2 * i) or - erased(alive, positions[0..npositions], sels_at + 2 * i + 1)); - for ([_][]PositionPin{ positions[0..npositions], pointer_positions orelse &.{} }) |batch| for (batch) |position| { - var row = pinnedRow(alive, position.row); + for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]); + for ([_][]PositionPin{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |position, k| { + var row = after.rows[k]; if (position.overlay) |edit| { if (position.edit_row) |relative| { row = edit.row +| relative; @@ -1508,8 +1898,14 @@ fn dumpedBytes(pin: ghostty_vt.Pin, cell: *const ghostty_vt.Cell) usize { pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { if (comptime !enabled) return &empty_grid; if (pane.terminal == null) return &empty_grid; + pardes.tape.touchTerminal(); const c = &p.shell_rows; - if (!c.stale and c.pane == pane) return c.rows; + if (!c.stale and c.pane == pane) { + // A step's read whether the rows are kept or made: a client that + // came since holds them as holes, and is sent them (Screens.fill). + if (!drawing) pane.terminal.?.read = true; + return c.rows; + } // Another pane's rows, or a dropped pane's still waiting for the step's // sweep (dropPane keeps them, as they may still be borrowed): this // step's rows are scratch, and nothing in the slot is written over. @@ -1670,105 +2066,12 @@ pub fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void { overlay.text = new; } -/// The shell's screen and scrollback (the primary screen, even under a -/// full-screen program) as VT that replays to what was shown: styled -/// text, soft-wrapped lines rejoined so they reflow at another width. -fn screenVt(pane: *const Pane, arena: std.mem.Allocator) ![]const u8 { - const screen = pane.terminal.?.vt.screens.get(.primary) orelse return &.{}; - var out: std.Io.Writer.Allocating = .init(arena); - try ghostty_vt.formatter.ScreenFormatter.init(screen, .{ .emit = .vt, .unwrap = true }).format(&out.writer); - return out.written(); -} -/// Serialize terminal-only state; the core supplies shared pane metadata. -pub fn dumpPane( - pane: *Pane, - arena: std.mem.Allocator, - tag: []const u8, - body: []const u8, - scroll: usize, -) !dump.Pane { - if (!enabled or pane.terminal == null) { - const text = if (pane.ovl) |overlay| overlay.text else ""; - return .{ - .kind = .terminal, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .terminal = .{ - .cwd = try arena.dupe(u8, pane.cwdSlice()), - .shell = try arena.dupe(u8, pane.shell orelse ""), - .command = try arena.dupe(u8, pane.command orelse ""), - .status = pane.command_status, - .stream = try arena.dupe(u8, text), - .stream_b64 = &.{}, - .cursor = .{ .col = 0, .row = 0 }, - }, - }; - } - const full = try pane.terminal.?.vt.screens.active.dumpStringAlloc(arena, .{ .screen = .{} }); - const extra = if (pane.ovl) |overlay| overlay.text.len else 0; - const stream = try arena.alloc(u8, try std.math.add(usize, full.len, extra)); - var len: usize = 0; - var lines = std.mem.splitAny(u8, full, "\n"); - var prompts = pane.terminal.?.vt.screens.active.pages.rowIterator(.right_down, .{ .screen = .{} }, null); - var row: i32 = 0; - var skip: i32 = 0; - while (lines.next()) |raw| : (row += 1) { - // Hidden overlay rows still consume prompt pins to keep them aligned. - const prompt = if (pane.body.mode != .tty) prompts.next() else null; - if (skip > 0) { - skip -= 1; - continue; - } - if (row > 0) { - stream[len] = '\n'; - len += 1; - } - if (pane.body.mode != .tty) if (pane.ovl) |overlay| if (row == overlay.row) { - @memcpy(stream[len..][0..overlay.text.len], overlay.text); - len += overlay.text.len; - skip = overlay.rows - 1; - continue; - }; - const shown = if (prompt) |pin| - if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, raw) else raw - else - raw; - @memcpy(stream[len..][0..shown.len], shown); - len += shown.len; - } - return .{ - .kind = .terminal, - .tag = tag, - .body = body, - .scroll = scroll, - .cols = pane.cols, - .rows = pane.rows, - .vweight = pane.vweight, - .collapsed = pane.collapsed, - .terminal = .{ - .cwd = try arena.dupe(u8, pane.cwdSlice()), - .shell = try arena.dupe(u8, pane.shell orelse ""), - .command = try arena.dupe(u8, pane.command orelse ""), - .status = pane.command_status, - .stream = stream[0..len], - .stream_b64 = try dump.encodeBytes(arena, try screenVt(pane, arena)), - .stream_is_screen = true, - .petscii = petscii(pane) orelse false, - .cursor = .{ - .col = pane.terminal.?.vt.screens.active.cursor.x, - .row = pane.terminal.?.vt.screens.active.cursor.y, - }, - }, - }; -} +/// Set while a frame is drawn: what it reads is no step's. +pub var drawing = false; fn buildRows(alloc: std.mem.Allocator, p: *Pardes, pane: *Pane) !Rows { + if (!drawing) pane.terminal.?.read = true; const full = try pane.terminal.?.vt.screens.active.dumpStringAlloc(p.scratch.allocator(), .{ .screen = .{} }); var pit = pane.terminal.?.vt.screens.active.pages.rowIterator(.right_down, .{ .screen = .{} }, null); // split yields one more item than delimiters; the extra final slot is the @@ -1862,7 +2165,7 @@ const BodyWalk = struct { return .{ .pane = pane, .goff = goff, - .g = if (pane.body.mode == .tty) goff else pane.gridRow(off), + .g = if (pane.body.mode == .tty) goff + pane.pan else pane.gridRow(off), .skip = if (pane.ovl) |o| @intCast(@max(0, off - pane.surfRow(o.row))) else 0, }; } @@ -2116,7 +2419,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 crossed = true; const upto = anchor.shell_len - @min(anchor.suffix, anchor.shell_len); while (sb < upto) { - const ci = pages.getCell(.{ .viewport = .{ .x = gc, .y = @intCast(vi) } }) orelse break; + const ci = cellAt(pages, row_pin, gc) orelse break; sb += dumpedBytes(row_pin, ci.cell); gc = std.math.add(u16, gc, 1) catch break; } @@ -2127,7 +2430,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 var covered: usize = 0; var style: ?pardes.CellStyle = null; while (covered < want) { - const ci = pages.getCell(.{ .viewport = .{ .x = gc, .y = @intCast(vi) } }) orelse break; + const ci = cellAt(pages, row_pin, gc) orelse break; if (style == null and ci.cell.wide != .spacer_tail and ci.cell.wide != .spacer_head) style = cellStyle(p, ci, filtered, scnm); covered += dumpedBytes(row_pin, ci.cell); @@ -2136,7 +2439,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 // spin on a row that ends in one. if (covered == 0 and gc >= pane.cols) break; } - while (pages.getCell(.{ .viewport = .{ .x = gc, .y = @intCast(vi) } })) |t| { + while (cellAt(pages, row_pin, gc)) |t| { if (t.cell.wide != .spacer_tail) break; gc = std.math.add(u16, gc, 1) catch break; } @@ -2153,7 +2456,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 var tail: ?pardes.CellStyle = null; if (anchor.whole() or anchor.suffix > 0) { while (sc < tw) { - const ci = pages.getCell(.{ .viewport = .{ .x = gc, .y = @intCast(vi) } }) orelse break; + const ci = cellAt(pages, row_pin, gc) orelse break; gc = std.math.add(u16, gc, 1) catch break; if (ci.cell.wide == .spacer_tail) continue; const cell = s.at(tx + sc, body_y + vr); @@ -2174,6 +2477,16 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 } } +/// The cell at column `x` of the row `row` pins, as `pages.getCell` reads +/// it, without walking from the viewport's top down to the row again: that +/// walk, once a cell, was most of a terminal's paint (once getCell was no +/// longer inlined, a frame of a 120x40 shell took 75 µs more). +fn cellAt(pages: *const ghostty_vt.PageList, row: ghostty_vt.Pin, x: u16) ?ghostty_vt.PageList.Cell { + if (x >= pages.cols or x >= row.node.cols()) return null; + const rac = row.node.page().getRowAndCell(x, row.y); + return .{ .node = row.node, .row = rac.row, .cell = rac.cell, .row_idx = row.y, .col_idx = x }; +} + fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColors, scnm: bool) pardes.CellStyle { const style = ci.style(); var cs: pardes.CellStyle = .{ diff --git a/src/tty/tty.zig b/src/tty/tty.zig index a358051e..4c054021 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -14,24 +14,55 @@ const file_watch = @import("../file_watch.zig"); const selection_pipe = @import("../selection_pipe.zig"); const ninep_io = @import("../9p_io.zig"); const panel_compositor = @import("panel_compositor.zig"); -const detached_client = @import("../detached/client.zig"); -const detached_server = @import("../detached/server.zig"); -const wire = @import("../detached/wire.zig"); +const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); +const upgrade = @import("../upgrade.zig"); +const replica = @import("../replica.zig"); +const wire = @import("../detached/wire.zig"); extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) posix.T.IOCSWINSZ else 0x80087467)); -/// The terminal answered the shared-memory probe (`kittyShmProbe`): it can -/// read a raster from a POSIX shm object, so a page costs a name on the wire -/// instead of its whole base64 body. Set by the input reader, which sees the -/// reply before vaxis' parser swallows every graphics APC alike. -var kitty_shm = std.atomic.Value(bool).init(false); -/// Only a reply that arrives while the probe is out counts. The id is one -/// vaxis' own image numbering (1, 2, ...) never reaches, because a terminal -/// answers every transmission `i=<id>;OK` and one of those must not pass. -var kitty_shm_probing = std.atomic.Value(bool).init(false); +/// What a terminal's input reader keeps, one a terminal (views.zig): its +/// graphemes, the frame it waits on the answer to, the shm probe, and the +/// view its input is. +const Feed = struct { + view: u8 = 0, + cache: vaxis.GraphemeCache = .{}, + ack: FrameAck = .{}, + /// The terminal answered the shared-memory probe (`kittyShmProbe`): it + /// can read a raster from a POSIX shm object, so a page costs a name on + /// the wire instead of its whole base64 body. Set by the input reader, + /// which sees the reply before vaxis' parser swallows every graphics APC + /// alike. + shm: std.atomic.Value(bool) = .init(false), + /// Only a reply that arrives while the probe is out counts. The id is + /// one vaxis' own image numbering (1, 2, ...) never reaches, because a + /// terminal answers every transmission `i=<id>;OK` and one of those must + /// not pass. + shm_probing: std.atomic.Value(bool) = .init(false), +}; + +/// A terminal's input onto the loop's queue, as its view's: a `from` goes +/// ahead of it whenever another terminal posted last. With one terminal +/// none ever does. +const Poster = struct { + loop: *Loop, + view: u8, + var lock: std.Io.Mutex = .init; + var last: u8 = 0; + + pub fn postEvent(p: Poster, ev: @TypeOf(Command.value)) !void { + lock.lockUncancelable(p.loop.io); + defer lock.unlock(p.loop.io); + if (last != p.view) { + try p.loop.postEvent(.{ .from = p.view }); + last = p.view; + } + try p.loop.postEvent(ev); + } +}; const kitty_shm_probe_id = 0x7fff_fff1; var kitty_shm_serial: u32 = 0; /// Transmissions an unread shm segment outlives before it is removed. @@ -41,6 +72,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)); @@ -64,13 +105,13 @@ fn kittyShmWrite(name: [:0]const u8, bytes: []const u8) bool { /// Ask with a 1x1 RGB image in shm whether the terminal reads that medium. A /// remote terminal cannot see our shm and answers an error (or nothing). -fn kittyShmProbe(w: *std.Io.Writer) void { +fn kittyShmProbe(w: *std.Io.Writer, feed: *Feed) void { var buf: [64]u8 = undefined; const name = kittyShmName(&buf, 0) catch return; if (!kittyShmWrite(name, &.{ 0, 0, 0 })) return; var b64: [64]u8 = undefined; const enc = std.base64.standard.Encoder.encode(&b64, name); - kitty_shm_probing.store(true, .release); + feed.shm_probing.store(true, .release); w.print("\x1b_Gi={d},s=1,v=1,a=q,t=s,f=24;{s}\x1b\\", .{ kitty_shm_probe_id, enc }) catch return; w.flush() catch {}; } @@ -78,9 +119,22 @@ fn kittyShmProbe(w: *std.Io.Writer) void { pub const Command = struct { pub var value: union(enum) { nop, + /// The terminal's input ended: its end of file, a hangup. A session + /// a daemon keeps carries on detached; any other ends. quit, + /// A terminal for this detached session, and its stub's pipe + /// (daemon.Note.attach). + attach: struct { tty: posix.fd_t, stub: posix.fd_t }, + /// A shell a dead session left, its pty's master (daemon.Note.adopted). + adopted: struct { pid: posix.pid_t, fd: posix.fd_t }, /// A timed wait ran out: the core's next wake has come. wake, + /// The input after this is the terminal of this view's (Poster). + from: u8, + /// A SIGWINCH: a terminal changed size (Shell.winched). + winched, + /// A SIGHUP: the session's own terminal hung up. + hangup, key_press: vaxis.Key, pty_read: struct { id: usize, gen: u32, bytes: []u8 }, pty_eof: struct { id: usize, gen: u32 }, @@ -98,38 +152,47 @@ pub const Command = struct { search_rows: pardes.Stream.Batch, files_changed, fs_ready, + /// A replica's connection, handed over by the daemon (Peers). + replica: daemon.Joining, + /// A replica's reader posted: the step boundary takes it in. + replica_mail, + /// A replica client's: what the session streamed (replica.Link). + said: replica.Said, + link_gone, } = .nop; }; const Loop = vaxis.Loop(@TypeOf(Command.value)); -fn startInput(loop: *Loop, cache: *vaxis.GraphemeCache) !void { +fn startInput(loop: *Loop, sc: *Screen) !void { if (comptime builtin.os.tag == .windows) return loop.start(); - if (loop.thread != null) return; - loop.thread = try loop.io.concurrent(inputThread, .{ loop, cache }); + if (sc.thread != null) return; + sc.thread = try loop.io.concurrent(inputThread, .{ loop, sc }); } -fn stopInput(loop: *Loop) void { - if (comptime builtin.os.tag == .windows) return loop.stop(); - if (loop.thread) |*thread| { - thread.cancel(loop.io); - loop.thread = null; +fn stopInput(io: std.Io, thread: *?std.Io.Future(void)) void { + if (thread.*) |*t| { + t.cancel(io); + thread.* = null; } } -fn inputThread(loop: *Loop, cache: *vaxis.GraphemeCache) void { - inputReader(loop, loop.tty, cache); +fn inputThread(loop: *Loop, sc: *Screen) void { + // vaxis's test terminal reads nothing off Linux: it has no read at all. + if (comptime !@hasDecl(vaxis.Tty, "read")) return; + inputReader(loop, &sc.tty, &sc.vx, &sc.feed); } -fn inputReader(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) void { - readInput(loop, tty, cache) catch |err| { +fn inputReader(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) void { + readInput(loop, tty, vx, feed) catch |err| { if (err == error.Canceled) return; std.log.err("terminal input: {s}", .{@errorName(err)}); }; - loop.postEvent(.quit) catch {}; + (Poster{ .loop = loop, .view = feed.view }).postEvent(.quit) catch {}; } -fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { - try loop.postEvent(.{ .winsize = try tty.getWinsize() }); +fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { + const post: Poster = .{ .loop = loop, .view = feed.view }; + try post.postEvent(.{ .winsize = try tty.getWinsize() }); var parser: vaxis.Parser = .{}; var buf: [1024]u8 = undefined; var carried: usize = 0; @@ -140,14 +203,14 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { // The terminal's answers to frames (FrameAck) come out first, // wherever they sit: never events, and never glued to a key (an // Escape pressed just before one would read as Alt on it). - const end = FrameAck.take(buf[0 .. carried + received], loop); + const end = feed.ack.take(buf[0 .. carried + received], loop); if (end == 0) { carried = 0; continue; } - if (kitty_shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| { - kitty_shm_probing.store(false, .release); - kitty_shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release); + if (feed.shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| { + feed.shm_probing.store(false, .release); + feed.shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release); // Answered either way: a terminal that could not read it left it. var name_buf: [64]u8 = undefined; if (kittyShmName(&name_buf, 0)) |name| _ = libc.shm_unlink(name) else |_| {} @@ -180,9 +243,9 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { std.mem.startsWith(u8, FrameAck.answer, rest)) break; if (!waited and parse_end == end and rest[0] == 0x1b and (rest.len == 1 or (rest.len == 2 and rest[1] == 0x1b))) { waited = true; - if (try inputFollows(tty, loop.io, if (loop.vaxis.caps.kitty_keyboard) 1000 else 50)) break; + if (try inputFollows(tty, loop.io, if (vx.caps.kitty_keyboard) 1000 else 50)) break; } - const result = try parser.parse(buf[consumed..parse_end], loop.vaxis.opts.system_clipboard_allocator); + const result = try parser.parse(buf[consumed..parse_end], vx.opts.system_clipboard_allocator); if (result.n == 0) break; consumed += result.n; // A modifier pressed on its own, which the kitty keyboard @@ -190,8 +253,8 @@ fn readInput(loop: *Loop, tty: anytype, cache: *vaxis.GraphemeCache) !void { // chord, and a shell pane was typed its private-use code. if (result.event) |event| if (event == .key_press and event.key_press.isModifier()) continue; if (result.event) |event| - vaxis.loop.handleEventGeneric(loop, loop.vaxis, cache, @TypeOf(Command.value), event, loop.vaxis.opts.system_clipboard_allocator) catch |err| { - if (event == .paste) if (loop.vaxis.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); + vaxis.loop.handleEventGeneric(post, vx, &feed.cache, @TypeOf(Command.value), event, vx.opts.system_clipboard_allocator) catch |err| { + if (event == .paste) if (vx.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); return err; }; // The last answer to the startup queries is no event of its @@ -219,14 +282,14 @@ const FrameAck = struct { const request = "\x1b[5n"; const answer = "\x1b[0n"; const timeout_ns: u64 = std.time.ns_per_s; - var waiting = std.atomic.Value(bool).init(false); - var off = std.atomic.Value(bool).init(false); + waiting: std.atomic.Value(bool) = .init(false), + off: std.atomic.Value(bool) = .init(false), /// When the request went out (the loop's thread only). - var sent_ns: u64 = 0; + sent_ns: u64 = 0, /// Take every answer out of `bytes`, closing up the rest; the new /// length. The loop is woken for the frame that waited on one. - fn take(bytes: []u8, loop: *Loop) usize { + fn take(a: *FrameAck, bytes: []u8, loop: *Loop) usize { var len = bytes.len; var found = false; while (std.mem.indexOf(u8, bytes[0..len], answer)) |at| { @@ -234,18 +297,18 @@ const FrameAck = struct { len -= answer.len; found = true; } - if (found and waiting.swap(false, .acq_rel)) loop.postEvent(.nop) catch {}; + if (found and a.waiting.swap(false, .acq_rel)) loop.postEvent(.nop) catch {}; return len; } /// Whether a new frame must wait (and the loop has a wake at the /// timeout); past the timeout the terminal is taken to never answer. - fn holds(now_ns: u64, io: std.Io) bool { - if (off.load(.acquire) or !waiting.load(.acquire)) return false; - const waited_ns = now_ns -| sent_ns; + fn holds(a: *FrameAck, now_ns: u64, io: std.Io) bool { + if (a.off.load(.acquire) or !a.waiting.load(.acquire)) return false; + const waited_ns = now_ns -| a.sent_ns; if (waited_ns >= timeout_ns) { - off.store(true, .release); - waiting.store(false, .release); + a.off.store(true, .release); + a.waiting.store(false, .release); return false; } requestWake(io, @intCast((timeout_ns - waited_ns + std.time.ns_per_ms - 1) / std.time.ns_per_ms)); @@ -253,14 +316,14 @@ const FrameAck = struct { } /// After a frame that wrote something: ask for the answer. - fn ask(w: *std.Io.Writer, now_ns: u64) void { - if (off.load(.acquire)) return; + fn ask(a: *FrameAck, w: *std.Io.Writer, now_ns: u64) void { + if (a.off.load(.acquire)) return; // Waiting before the request goes: a local terminal can answer // before the write returns, and an answer nobody waited for is lost. - sent_ns = now_ns; - waiting.store(true, .release); - w.writeAll(request) catch return waiting.store(false, .release); - w.flush() catch return waiting.store(false, .release); + a.sent_ns = now_ns; + a.waiting.store(true, .release); + w.writeAll(request) catch return a.waiting.store(false, .release); + w.flush() catch return a.waiting.store(false, .release); } }; @@ -292,7 +355,7 @@ test "terminal input preserves fragmented keys queries paste and text after EOF" vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -314,7 +377,7 @@ test "terminal input preserves fragmented keys queries paste and text after EOF" "plain \x1b[?62;", "4c\x1b[", "A\x1b[200", "~caf\xc3", "\xa9 \xe7", "\x95", "\x8c \xf0\x9f", "\x98\x80", "\x1b[201", "~\x1b]52;c;Y2", "xpcA==\x07tail", "\x1b", } }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(vx.queries_done.load(.unordered)); var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); @@ -377,7 +440,7 @@ test "terminal replies and malformed sequences never become typed text" { vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -420,7 +483,7 @@ test "terminal replies and malformed sequences never become typed text" { "c", }, }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(vx.queries_done.load(.unordered)); try std.testing.expect(vx.caps.kitty_graphics); var text: std.ArrayList(u8) = .empty; @@ -459,7 +522,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; // Two reads with `gap_ms` between them, as an ssh link delivers a // sequence cut across two packets. const Reader = struct { @@ -524,7 +587,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { const all = try std.mem.concat(gpa, u8, &.{ "x", case.bytes, "y" }); defer gpa.free(all); var one: Reader = .{ .parts = .{ all, "" }, .gap_ms = 0 }; - try readInput(&loop, &one, &cache); + try readInput(&loop, &one, &vx, &feed); try Reader.events(&loop, &whole); for (1..all.len) |cut| { // A cut just after an ESC is the one that has to wait, and a @@ -532,7 +595,7 @@ test "terminal input split at every byte with a gap reads as it does whole" { // anywhere else is carried however long the gap. const after_esc = all[cut - 1] == 0x1b; var two: Reader = .{ .parts = .{ all[0..cut], all[cut..] }, .gap_ms = if (after_esc and !case.kitty) 40 else 60 }; - try readInput(&loop, &two, &cache); + try readInput(&loop, &two, &vx, &feed); try Reader.events(&loop, &split); std.testing.expectEqualStrings(whole.items, split.items) catch |err| { std.debug.print("case {f} cut at {d}\n", .{ std.ascii.hexEscape(case.bytes, .lower), cut }); @@ -555,7 +618,7 @@ test "an ESC read by itself is the Escape key at once" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; // More always follows at once: a reader that waited on the ESC would // take `j` for the rest of its sequence (Alt-j). const Reader = struct { @@ -580,7 +643,7 @@ test "an ESC read by itself is the Escape key at once" { for ([_]bool{ false, true }) |kitty| { vx.caps.kitty_keyboard = kitty; var reader: Reader = .{ .parts = &.{ "\x1b", "j" } }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); try std.testing.expect(!reader.asked); try std.testing.expectEqual(.winsize, std.meta.activeTag((try loop.tryEvent()).?)); const escape = (try loop.tryEvent()).?.key_press; @@ -606,7 +669,7 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { defer vx.deinit(gpa, &output.writer); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: [2][]const u8, next: usize = 0, @@ -628,12 +691,12 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { } }; const all = "x" ++ FrameAck.answer ++ "y"; - defer FrameAck.waiting.store(false, .release); + defer feed.ack.waiting.store(false, .release); for (0..all.len) |cut| { - FrameAck.waiting.store(true, .release); + feed.ack.waiting.store(true, .release); var reader: Reader = .{ .parts = .{ all[0..cut], all[cut..] } }; - try readInput(&loop, &reader, &cache); - try std.testing.expect(!FrameAck.waiting.load(.acquire)); + try readInput(&loop, &reader, &vx, &feed); + try std.testing.expect(!feed.ack.waiting.load(.acquire)); var typed: std.ArrayList(u8) = .empty; defer typed.deinit(gpa); var woke = false; @@ -651,9 +714,9 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { } // An Escape pressed just before an answer, in the same read, is the // Escape key, not Alt on the answer. - FrameAck.waiting.store(true, .release); + feed.ack.waiting.store(true, .release); var glued: Reader = .{ .parts = .{ "\x1b" ++ FrameAck.answer, "j" } }; - try readInput(&loop, &glued, &cache); + try readInput(&loop, &glued, &vx, &feed); try std.testing.expectEqual(.winsize, std.meta.activeTag((try loop.tryEvent()).?)); try std.testing.expectEqual(.nop, std.meta.activeTag((try loop.tryEvent()).?)); const escape = (try loop.tryEvent()).?.key_press; @@ -665,25 +728,21 @@ test "a frame's answer is taken wherever the reads cut it, and never typed" { test "a terminal that never answers a frame stops being asked after one timeout" { const io = std.testing.io; - defer { - FrameAck.off.store(false, .release); - FrameAck.waiting.store(false, .release); - FrameAck.sent_ns = 0; - } + var ack: FrameAck = .{}; var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); const t0: u64 = 5 * std.time.ns_per_s; - FrameAck.ask(&out.writer, t0); + ack.ask(&out.writer, t0); try std.testing.expectEqualStrings(FrameAck.request, out.written()); // Unanswered: the next frame waits, until the timeout. - try std.testing.expect(FrameAck.holds(t0 + 10 * std.time.ns_per_ms, io)); - try std.testing.expect(FrameAck.holds(t0 + 999 * std.time.ns_per_ms, io)); - try std.testing.expect(!FrameAck.holds(t0 + FrameAck.timeout_ns, io)); + try std.testing.expect(ack.holds(t0 + 10 * std.time.ns_per_ms, io)); + try std.testing.expect(ack.holds(t0 + 999 * std.time.ns_per_ms, io)); + try std.testing.expect(!ack.holds(t0 + FrameAck.timeout_ns, io)); // Then never again: no request, no wait. out.clearRetainingCapacity(); - FrameAck.ask(&out.writer, t0 + 2 * std.time.ns_per_s); + ack.ask(&out.writer, t0 + 2 * std.time.ns_per_s); try std.testing.expectEqual(@as(usize, 0), out.written().len); - try std.testing.expect(!FrameAck.holds(t0 + 2 * std.time.ns_per_s, io)); + try std.testing.expect(!ack.holds(t0 + 2 * std.time.ns_per_s, io)); } test "terminal input cancellation joins blocked reads and queued EOF" { @@ -717,8 +776,8 @@ test "terminal input cancellation joins blocked reads and queued EOF" { } return self.file.readStreaming(std.testing.io, &.{buf}); } - fn run(loop: *Loop, reader: *@This(), cache: *vaxis.GraphemeCache) void { - inputReader(loop, reader, cache); + fn run(loop: *Loop, reader: *@This(), vx_: *vaxis.Vaxis, feed: *Feed) void { + inputReader(loop, reader, vx_, feed); } }; const Case = enum { blocked_read, cancel_eof, deliver_eof }; @@ -731,9 +790,9 @@ test "terminal input cancellation joins blocked reads and queued EOF" { var reader: Reader = .{ .file = .{ .handle = fds[0], .flags = .{ .nonblocking = false } }, .eof = eof }; var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; - loop.thread = try io.concurrent(Reader.run, .{ &loop, &reader, &cache }); - defer stopInput(&loop); + var feed: Feed = .{}; + var thread: ?std.Io.Future(void) = try io.concurrent(Reader.run, .{ &loop, &reader, &vx, &feed }); + defer stopInput(io, &thread); try std.testing.expectEqual(.winsize, std.meta.activeTag(try loop.nextEvent())); try reader.entered.wait(io); if (eof) { @@ -743,10 +802,10 @@ test "terminal input cancellation joins blocked reads and queued EOF" { } if (case == .deliver_eof) { try std.testing.expectEqual(.nop, std.meta.activeTag(try loop.nextEvent())); - loop.thread.?.await(io); + thread.?.await(io); } - stopInput(&loop); - try std.testing.expect(loop.thread == null); + stopInput(io, &thread); + try std.testing.expect(thread == null); var count: usize = 0; while (try loop.tryEvent()) |event| { const expected: std.meta.Tag(@TypeOf(Command.value)) = if (case == .deliver_eof and count == 511) .quit else .nop; @@ -788,14 +847,18 @@ fn terminalCellPixels(total_pixels: u16, cells: u16, fallback: u32) u32 { } fn updateCoreTerminalSize(core: *pardes.Pardes, cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) void { - core.update(.{ .resize = .{ + core.update(.{ .resize = terminalSize(cols, rows, pixel_w, pixel_h) }); +} + +fn terminalSize(cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) @FieldType(pardes.Event, "resize") { + return .{ .cols = cols, .rows = rows, .cell_pixels = .{ .w = @intCast(terminalCellPixels(pixel_w, cols, 8)), .h = @intCast(terminalCellPixels(pixel_h, rows, 16)), }, - } }); + }; } /// A terminal pane's image fragment (`native.exact`): its `src` into its @@ -1166,111 +1229,414 @@ fn nativePdfWheelTarget(core: *const pardes.Pardes, mouse: vaxis.Mouse) ?PdfWhee return null; } -pub fn run(init: std.process.Init, opts: pardes.Options, attach: ?[]const u8) !void { +/// 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`). +/// 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; const gpa = init.gpa; - var attach_end: AttachEnd = .none; - defer attach_end.report(io); - - var name_buf: [detached_server.path_max]u8 = undefined; - var attach_name: []const u8 = &.{}; - if (attach) |requested| switch (detached_client.resolve(&name_buf, requested)) { - .name => |resolved| attach_name = resolved, - .none => { - attach_end = if (requested.len != 0) .{ .no_session = requested } else .nothing_detached; - return; - }, - .ambiguous => |found| { - attach_end = .{ .ambiguous = found }; - return; - }, - }; - var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); + // Kept by a daemon, a hangup is the terminal's end, not the session's: + // winchWatch takes it, and the session carries on detached. + if (daemon.link >= 0) posix.sigaddset(&winch_set, posix.SIG.HUP); + // FreeBSD drops a signal whose action is to be ignored (WINCH's + // default) as it is sent, sigwait or not: a handler keeps it. + if (comptime builtin.os.tag == .freebsd) + posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); var tty_buf: [0x10000]u8 = undefined; - var tty = try vaxis.Tty.init(io, &tty_buf); - defer tty.deinit(); // restore cooked termios LAST, after vx flushed its resets - var vx = try vaxis.init(io, gpa, init.environ_map, .{ .system_clipboard_allocator = gpa }); - defer vx.deinit(gpa, tty.writer()); - try vx.enterAltScreen(tty.writer()); - defer vx.exitAltScreen(tty.writer()) catch {}; - try vx.setMouseMode(tty.writer(), true); - try vx.setBracketedPaste(tty.writer(), true); - Killed.arm(tty.fd.handle, tty.termios); - defer Killed.disarm(); + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + defer screen.kitty.deinit(); + switch (term) { + .open => try screen.enter(try vaxis.Tty.init(io, &tty_buf)), + .fd => |t| try screen.enter(try Screen.ttyOn(io, &tty_buf, t.fd, t.termios)), + .none => {}, + } + defer screen.leave(); - if (attach != null) { - attach_end = attachSession(init, attach_name, &tty, &vx); - return; + var paste_buf: std.Io.Writer.Allocating = .init(gpa); + defer paste_buf.deinit(); + var loop: Loop = .init(io, &screen.tty, &screen.vx); + + try localSession(init, opts, &screen, &loop, &paste_buf); +} + +/// A replica client's link to its session (`remote`), drawn here. +const Remote = struct { + 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 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; + r.draw(.{ .ctx = s, .f = Shell.present }); + core.frames.mark_render = core.frames.mark_update; + 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.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 Post = struct { + fn post(l: *Loop, said: replica.Said) bool { + l.postEvent(.{ .said = said }) catch return false; + return true; + } + }; + replica.readLink(gpa, fd, rest, loop, Post.post); + loop.postEvent(.link_gone) catch {}; +} + +/// `pardes --attach=<socket>:<session>` (and `<host>:<session>`, through +/// ssh, main.zig): this terminal a replica client of a session elsewhere +/// (docs/next-daemon.md, "Phase 5 part B notes"). Its own core, loaded +/// from the session's dump, applies its keys at once (src/replica.zig); +/// the session's units are the truth it rolls back to. Its own view of the +/// session is drawn here, as the session draws its terminals'. +pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far_link: bool) !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 said: [800]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&said, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + var why: [600]u8 = @splat(0); + // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash, + // those it holds kept from the link, and its stream in zstd. A client + // on this machine's own socket is sent the dump whole, as before: a + // hash and a compression there cost more than the bytes. + const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else far_link; + var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; + defer have_store.deinit(); + const have: ?*replica.Have = if (far) &have_store else null; + var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + var joined = replica.dial(gpa, path, session, &why, have, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); - var kitty_handles = std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image).init(gpa); + const allocs = pardes.memory.init(gpa); + defer pardes.memory.deinit(); + pardes.image.start(io, allocs.image); + if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf); + pardes.syntax.start(allocs.tree_sitter); defer { - clearNativeImages(&kitty_handles, &vx, &tty); - kitty_handles.deinit(); + pardes.image.stop(); + if (comptime pardes.pdf_enabled) pardes.pdf.stop(); + pardes.syntax.stop(); } + var o = opts; + o.image_allocator = allocs.image; + o.pdf_allocator = allocs.pdf; + 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)); + + var winch_set = posix.sigemptyset(); + posix.sigaddset(&winch_set, posix.SIG.WINCH); + // A hangup ends it the normal way (`quit`), what it made kept on disk + // (`Have.keep`) and its ssh forward's socket unlinked (main.zig). + posix.sigaddset(&winch_set, posix.SIG.HUP); + hangup_quits = true; + if (comptime builtin.os.tag == .freebsd) + posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); + posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); + var tty_buf: [0x10000]u8 = undefined; + var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true }; + defer screen.kitty.deinit(); + try screen.enter(try vaxis.Tty.init(io, &tty_buf)); + defer screen.leave(); + defer screen.glide.deinit(gpa); var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); - var loop: Loop = .init(io, &tty, &vx); - var input_cache: vaxis.GraphemeCache = .{}; - - var attached: ?detached_client.Client = null; - defer if (attached != null) { - stopInput(&loop); - drainAttachedQueue(&loop, gpa); + var loop: Loop = .init(io, &screen.tty, &screen.vx); + var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame); + defer frame_arena.deinit(); + 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, + .lsp_gpa = allocs.lsp, + .core = r.core(), + .prompt_rcs = undefined, + .loop = &loop, + .frame = &frame_arena, + .paste_buf = &paste_buf, + .inotify_fd = -1, + .from = joined.view, + .remote = &remote_link, }; - try localSession(init, opts, &tty, &vx, &loop, &input_cache, &kitty_handles, &paste_buf, &attached); - if (attached) |*client| { - defer client.detach(); - var a: Attach = .{ - .gpa = gpa, - .client = client, - .loop = &loop, - .vx = &vx, - .tty = &tty, - .paste_buf = &paste_buf, + + try startInput(&loop, &screen); + defer stopInput(io, &screen.thread); + (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach(); + (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); + try screen.vx.queryTerminalSend(screen.tty.writer()); + // A join a pass: the first, and one after the replica diverged or the + // session let it go to upgrade. The terminal stays as it is. + while (true) { + r.gmtoff = joined.gmtoff; + sh.core = r.core(); + sh.from = joined.view; + sh.terms = @splat(null); + sh.terms[joined.view] = &screen; + screen.feed.view = joined.view; + link.reset(r, joined.fd); + 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 }); + // 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) 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(); + break :idle loop.nextEvent() catch break; + }; + var ev = first; + var batch: usize = 1; + while (true) { + _ = sh.apply(ev); + if (link.quit or (!sh.in_paste and batch >= 64)) break; + ev = (loop.tryEvent() catch null) orelse break; + batch += 1; + } + r.settle(); + if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { + screen.caps_pending = false; + screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; + screen.vx.queueRefresh(); + link.dirty = true; + } + if (link.owed 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.quietCheck()) |ms| requestWake(io, ms); + 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, r.diverged != null); + // The link's reader ends with the link, posting as it goes: what it + // and the terminal posted is let go until it has. Its end is read + // before the drain, not after: its last post (`link_gone`) comes + // just before it, and left queued it ended the next link at once. + _ = libc.shutdown(joined.fd, 2); + while (true) { + const ended = reader_done.load(.acquire); + while (loop.tryEvent() catch null) |ev| switch (ev) { + .said => |said| switch (said) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .checked => {}, + }, + .paste => |b| gpa.free(@constCast(b)), + else => {}, + }; + if (ended) break; + std.Thread.yield() catch {}; + } + reader.join(); + _ = libc.close(joined.fd); + joined.rest.deinit(gpa); + // Why it joins again, in a line over its frame until a key. + if (!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) { + screen.leave(); + Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); + } + return; + } + // The session again, the core it leaves held meanwhile: its blobs + // are not sent again. + if (have) |h| h.leaving(r.core()); + dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; + joined = replica.Link.redial(gpa, path, session, &why, have, r.diverged == null, joined.view) orelse { + screen.leave(); + Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); }; - attach_end = attachLoop(&a); + if (have) |h| h.left(); + r.deinit(); + r = replica.Link.load(gpa, &joined, o, &why, have) orelse { + screen.leave(); + Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + }; + dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); } } -/// SIGTERM or SIGHUP: the terminal put back as a clean exit leaves it -- + +/// A terminal a session draws on, while it has one: entered (raw, the +/// alternate screen, mouse and paste reports) and left (all of it put back, +/// the terminal closed) at the session's start and end, and at a detach and +/// a reattach in between (daemon.zig). One a view (views.zig): the session's +/// own (`primary`, its stub `daemon.stub`), and one more for each terminal +/// `pardes --attach`ed while it is attached, which keeps its own. +const Screen = struct { + io: std.Io, + gpa: std.mem.Allocator, + env: *std.process.Environ.Map, + buf: []u8, + tty: vaxis.Tty = undefined, + vx: vaxis.Vaxis = undefined, + on: bool = false, + primary: bool = false, + /// The pipe to its stub, an attached one's (the primary's is daemon.stub). + stub: posix.fd_t = -1, + /// Its slot in Killed. + killed: ?usize = null, + feed: Feed = .{}, + thread: ?std.Io.Future(void) = null, + kitty: std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + caps_pending: bool = true, + /// Its size as last taken (`Shell.resized`). + size: ?vaxis.Winsize = null, + tracks: []const pardes.animation.Track = &.{}, + /// T2: the cursor's jump trail (JumpTrail). + trail: JumpTrail = .{}, + /// The view's glide, the GUI's motion drawn at whole rows (anim 12). + glide: pardes.glide.Glide = .{}, + /// T7: when each pane's view last moved (ThumbFlash). + flash: ThumbFlash = .{}, + + /// `fd` made raw, as vaxis.Tty.init makes /dev/tty: the terminal of a + /// forked session is no controlling terminal of its, which /dev/tty is. + /// 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 = 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) }; + } + + fn enter(sc: *Screen, tty: vaxis.Tty) !void { + sc.tty = tty; + errdefer sc.tty.deinit(); + sc.vx = try vaxis.init(sc.io, sc.gpa, sc.env, .{ .system_clipboard_allocator = sc.gpa }); + errdefer sc.vx.deinit(sc.gpa, sc.tty.writer()); + try sc.vx.enterAltScreen(sc.tty.writer()); + try sc.vx.setMouseMode(sc.tty.writer(), true); + try sc.vx.setBracketedPaste(sc.tty.writer(), true); + if (comptime !builtin.is_test) { + sc.killed = Killed.arm(sc.tty.fd.handle, sc.tty.termios); + // What a panic puts back (vaxis.recover). + if (sc.primary) vaxis.tty.global_tty = sc.tty; + } + const view = sc.feed.view; + sc.feed = .{ .view = view }; + sc.caps_pending = true; + sc.size = null; + sc.on = true; + } + + fn leave(sc: *Screen) void { + if (!sc.on) return; + sc.on = false; + if (sc.killed) |slot| Killed.disarm(slot); + sc.killed = null; + clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); + sc.vx.exitAltScreen(sc.tty.writer()) catch {}; + sc.vx.deinit(sc.gpa, sc.tty.writer()); + if (comptime !builtin.is_test) if (sc.primary) { + vaxis.tty.global_tty = null; + }; + sc.tty.deinit(); // restore cooked termios LAST, after vx flushed its resets + } + + /// Its stub told the terminal is detached (it ends, and its shell has + /// the terminal back). + fn sayDetached(sc: *Screen) void { + if (sc.primary) return daemon.tellStubDetached(); + daemon.tellDetached(sc.stub); + sc.stub = -1; + } + + /// Its terminal hung up, or the stub that waits on it is gone. + fn hungUp(sc: *const Screen) bool { + const stub = if (sc.primary) daemon.stub else sc.stub; + const handle = if (@TypeOf(sc.tty.fd) == std.Io.File) sc.tty.fd.handle else sc.tty.fd; + var fds = [_]posix.pollfd{ + .{ .fd = handle, .events = 0, .revents = 0 }, + .{ .fd = stub, .events = 0, .revents = 0 }, + }; + _ = posix.poll(fds[0..if (stub >= 0) 2 else 1], 0) catch return false; + const gone = posix.POLL.HUP | posix.POLL.ERR; + return fds[0].revents & gone != 0 or (stub >= 0 and fds[1].revents & gone != 0); + } + + fn setStub(sc: *Screen, fd: posix.fd_t) void { + if (sc.primary) daemon.stub = fd else sc.stub = fd; + } +}; + +/// SIGTERM or SIGHUP: each terminal put back as a clean exit leaves it -- /// the main screen, no mouse, paste or keyboard modes, the cursor shown, /// cooked termios -- and then the signal's own death. A nested editor /// killed in a pane must not leave that pane's terminal wedged. /// Async-signal-safe: write, tcsetattr, sigaction, raise. const Killed = struct { - var fd: posix.fd_t = -1; - var cooked: posix.termios = undefined; + var fds: [pardes.views.max]posix.fd_t = @splat(-1); + var cooked: [pardes.views.max]posix.termios = undefined; const reset = "\x1b[<u\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1004l\x1b[?2004l\x1b[0m\x1b[?25h\x1b[?1049l"; - const signals = [_]posix.SIG{ .TERM, .HUP }; - - fn arm(tty_fd: posix.fd_t, termios: posix.termios) void { - cooked = termios; - fd = tty_fd; + fn arm(tty_fd: posix.fd_t, termios: posix.termios) ?usize { + const slot = std.mem.findScalar(posix.fd_t, &fds, -1) orelse return null; + cooked[slot] = termios; + fds[slot] = tty_fd; const on: posix.Sigaction = .{ .handler = .{ .handler = handle }, .mask = posix.sigemptyset(), .flags = posix.SA.RESETHAND }; - for (signals) |sig| posix.sigaction(sig, &on, null); + posix.sigaction(.TERM, &on, null); + // A hangup a daemon keeps the session through is winchWatch's. + if (daemon.link < 0) posix.sigaction(.HUP, &on, null); + return slot; } - fn disarm() void { - fd = -1; - const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 }; - for (signals) |sig| posix.sigaction(sig, &default, null); + /// The terminal gone (put back, or detached): a TERM still takes the + /// socket, the prompt files and the shells with it, and leaves the + /// terminal alone. + fn disarm(slot: usize) void { + fds[slot] = -1; } fn handle(sig: posix.SIG) callconv(.c) void { - if (fd >= 0) { + for (fds, &cooked) |fd, *termios| if (fd >= 0) { _ = std.c.write(fd, reset, reset.len); - _ = std.c.tcsetattr(fd, .FLUSH, &cooked); - } + _ = std.c.tcsetattr(fd, .FLUSH, termios); + }; // The 9P socket and private prompt files go too: a killed editor - // leaves none behind. + // leaves none behind. Its shells too, the daemon's dups let go. ninep_io.unlinkOwnSocket(); host_io.Shell.unlinkOwnedRcs(); + daemon.hangUp(); // SA_RESETHAND put the default back: the same signal, now fatal. _ = std.c.raise(sig); } @@ -1279,13 +1645,9 @@ const Killed = struct { fn localSession( init: std.process.Init, opts: pardes.Options, - tty: *vaxis.Tty, - vx: *vaxis.Vaxis, + screen: *Screen, loop: *Loop, - input_cache: *vaxis.GraphemeCache, - kitty_handles: *std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), paste_buf: *std.Io.Writer.Allocating, - attached: *?detached_client.Client, ) !void { const io = init.io; const gpa = init.gpa; @@ -1310,7 +1672,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); @@ -1333,30 +1697,36 @@ fn localSession( .core = core, .prompt_rcs = &prompt_rcs, .loop = loop, - .vx = vx, - .tty = tty, - .kitty = kitty_handles, .frame = &frame_arena, .paste_buf = paste_buf, .inotify_fd = file_watch.init(false), .fs = fs, .test_clock = host_io.testClock(), }; + sh.terms[0] = screen; + sh.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake_ctx = loop, .wake = wakeForReplica }; + defer sh.peers.deinit(); + // Before the core goes: each client sees its link end. + defer sh.peers.dropAll(sh.core); pardes.lsp.setStatusSink(&sh, lspStatusSink); host_io.setExitWake(loop, wakeForExit); - defer sh.glide.deinit(gpa); + defer screen.glide.deinit(gpa); + // The terminals of the other views end with the session: each put back, + // its stub told. + defer for (&sh.terms) |*slot| if (slot.*) |sc| if (!sc.primary) { + stopInput(io, &sc.thread); + sh.dropScreen(sc, .exit); + slot.* = null; + }; defer { host_io.setExitWake(null, null); pardes.lsp.setStatusSink(null, null); for (&sh.ptys) |*slot| if (slot.*) |*pt| { pt.reader.cancel(io) catch {}; _ = libc.close(pt.file.handle); - if (attached.* != null) { - _ = libc.kill(pt.pid, posix.SIG.KILL); - _ = libc.waitpid(pt.pid, null, 0); - } slot.* = null; }; + for (sh.adoptions[0..sh.adoptions_len]) |a| _ = libc.close(a.fd); if (sh.lsp_task) |*t| { t.future.cancel(io); sh.lsp_task = null; @@ -1374,7 +1744,10 @@ fn localSession( sh.inotify_fd = -1; } while (loop.tryEvent() catch null) |ev| switch (ev) { - .pty_read => |pr| gpa.free(pr.bytes), + .pty_read => |pr| { + tookRead(io, pr.id); + gpa.free(pr.bytes); + }, .paste => |b| gpa.free(@constCast(b)), .lsp_done => |d| if (d.rows) |rows| allocs.lsp.free(rows), .lsp_status => |text| allocs.lsp.free(text), @@ -1383,20 +1756,49 @@ fn localSession( response.deinit(gpa); }, .search_rows => |b| b.deinit(gpa), + .attach => |a| { + _ = libc.close(a.tty); + if (a.stub >= 0) _ = libc.close(a.stub); + }, + .adopted => |a| _ = libc.close(a.fd), else => {}, }; } 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). + sh.setAttended(sh.attached()); while (core.nextEffect()) |effect| core.perform(effect); + // ...and its shells, once a fresh session's own has a directory to + // adopt them in. + if (upgrade.resumed) |*rec| { + sh.resumePtys(rec); + while (core.nextEffect()) |effect| core.perform(effect); + } - try startInput(loop, input_cache); - defer if (attached.* == null) stopInput(loop); - (try std.Thread.spawn(.{}, winchWatch, .{ loop, vx, tty })).detach(); + if (screen.on) try startInput(loop, screen); + defer stopInput(io, &screen.thread); + (try std.Thread.spawn(.{}, winchWatch, .{loop})).detach(); (try std.Thread.spawn(.{}, wakeWatch, .{ io, loop })).detach(); if (fs) |f| try f.watch(loop, wakeFs); - try vx.queryTerminalSend(tty.writer()); + if (screen.on) try screen.vx.queryTerminalSend(screen.tty.writer()); + // Detached from the start, nothing else may wake the first wait. + if (!screen.on) daemon.watch(loop, deliverNote); + // PARDES_TEST_VIEWS: the terminals at these paths are views of the + // session too, from its start, as `pardes --attach`ed ones are (the + // snapshot harness's `start ... views=N`). + if (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, null); + } + }; sh.threads_ok = true; for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { @@ -1404,19 +1806,23 @@ 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 |_| {} - } - } - frames: while (!core.quit) { + defer unlinkKittyShm(); + while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Its unit to each replica client, and theirs taken in. + sh.peers.boundary(core); + upgrade.locate(); + // Asked in the step (the word), done between steps: the session whole. + if (sh.upgrade_asked) |pane| sh.upgradeNow(pane); + // 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. + daemon.watch(loop, deliverNote); if (core.takeRestore()) |rp| blk: { + // ponytail: a Restore replaces the core its clients replicate; + // they are let go, and attach again. + sh.peers.dropAll(core); const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| { core.reportError(core.active, "Restore", err); break :blk; @@ -1435,6 +1841,7 @@ fn localSession( for (&sh.ptys) |*slot| if (slot.*) |*pt| { pt.reader.cancel(io) catch {}; _ = libc.close(pt.file.handle); + daemon.closed(pt.pid); host_io.retireShell(pt.pid); slot.* = null; }; @@ -1447,28 +1854,31 @@ fn localSession( .{ .text = 0 }, ); _ = file_watch.applyThemeEffect(core, gpa, sh.inotify_fd, &sh.watches, 0, false, false); - clearNativeImages(kitty_handles, vx, tty); - nc.native_images = vx.caps.kitty_graphics; + // ponytail: a restored session is one view, the live one's + // terminal: the others are let go, as a detach of each. + const live = sh.term().?; + for (&sh.terms) |*slot| if (slot.*) |sc| if (sc != live) { + stopInput(io, &sc.thread); + sh.dropScreen(sc, .detach); + slot.* = null; + }; + sh.terms = @splat(null); + sh.terms[0] = live; + stopInput(io, &live.thread); + live.feed.view = 0; + sh.from = 0; + if (live.on) startInput(loop, live) catch {}; + if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty); + nc.native_images = live.on and live.vx.caps.kitty_graphics; + nc.unattended = !live.on; + sh.peers.alone_unattended = !live.on; if (fs) |f| f.reset(nc); core.deinit(); core = nc; sh.core = nc; - if (comptime pardes.pdf_enabled) { - updateCoreTerminalSize(core, vx.screen.width, vx.screen.height, vx.screen.width_pix, vx.screen.height_pix); - } - } - if (core.takeAttach()) |req| { - var attempt = detached_client.attempt(gpa, req.name, core.screen_w, core.screen_h); - switch (attempt) { - .greeted => |client| { - attached.* = client; - break :frames; - }, - else => { - var mbuf: [256]u8 = undefined; - core.setMessage(req.pane, attemptEnd(&attempt, req.name).row(&mbuf)); - }, - } + if (comptime pardes.pdf_enabled) if (live.on) { + updateCoreTerminalSize(core, live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix); + }; } } if (fs) |f| { @@ -1488,11 +1898,37 @@ fn clearNativeImages( kitty_handles.clearRetainingCapacity(); } -fn drainAttachedQueue(loop: *Loop, gpa: std.mem.Allocator) void { - while (loop.tryEvent() catch null) |ev| switch (ev) { - .paste => |b| gpa.free(@constCast(b)), - else => {}, - }; +const Adoption = struct { pid: posix.pid_t, fd: posix.fd_t, pane: ?u8 }; + +/// The daemon's thread: what it says, onto the loop's queue. +fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { + const loop: *Loop = @ptrCast(@alignCast(ctx)); + switch (note) { + .attach => |a| if (a.tty < 0) { + if (a.stub >= 0) daemon.refuseStub(a.stub, "a terminal session takes a terminal"); + } else loop.postEvent(.{ .attach = .{ .tty = a.tty, .stub = a.stub } }) catch { + _ = libc.close(a.tty); + _ = libc.close(a.stub); + }, + .adopted => |a| { + for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| loop.postEvent(.{ .adopted = .{ .pid = pid, .fd = fd } }) catch { + _ = libc.close(fd); + }; + loop.postEvent(.{ .adopted = .{ .pid = 0, .fd = -1 } }) catch {}; + }, + .replica => |j| loop.postEvent(.{ .replica = j }) catch { + _ = libc.close(j.fd); + }, + } +} + +/// A replica reader's post: the session's wait ends, and its step +/// boundary takes the post in. +fn wakeForReplica(ctx: ?*anyopaque) void { + const loop: *Loop = @ptrCast(@alignCast(ctx.?)); + // Never waits: a full queue wakes the loop anyway, and a reader the + // loop is joining (a client let go) must not wait on it. + _ = loop.tryPostEvent(.replica_mail) catch {}; } const Shell = struct { @@ -1502,12 +1938,19 @@ const Shell = struct { core: *pardes.Pardes, prompt_rcs: *const host_io.Shell.PromptFiles, loop: *Loop, - vx: *vaxis.Vaxis, - tty: *vaxis.Tty, - kitty: *std.AutoHashMap(pardes.ImageCacheKey, vaxis.Image), + /// Each view's terminal, by view (views.zig): the session's own first; + /// none in tests. + terms: [pardes.views.max]?*Screen = @splat(null), + /// The view the input taken now is from (`Command.value.from`). + from: u8 = 0, frame: *std.heap.ArenaAllocator, paste_buf: *std.Io.Writer.Allocating, inotify_fd: c_int, + /// Shells a dead session left (Adopt), each waiting for the spawn of + /// the terminal pane made for it. + adoptions: [pardes.MAX_PANES]Adoption = undefined, + adoptions_len: usize = 0, + adopted: usize = 0, fs: ?*ninep_io.Listener = null, ptys: [pardes.MAX_PANES]?Pty = @splat(null), gens: [pardes.MAX_PANES]u32 = @splat(0), @@ -1516,16 +1959,10 @@ const Shell = struct { lsp_task: ?host_io.Lsp.Task = null, pipe_tasks: PipeTasks = .{}, threads_ok: bool = false, - caps_pending: bool = true, check_files: bool = false, + // ponytail: one bracketed paste at a time for every terminal: keys + // another terminal sends while one pastes join its paste. in_paste: bool = false, - tracks: []const pardes.animation.Track = &.{}, - /// T2: the cursor's jump trail (JumpTrail). - trail: JumpTrail = .{}, - /// The view's glide, the GUI's motion drawn at whole rows (anim 12). - glide: pardes.glide.Glide = .{}, - /// T7: when each pane's view last moved (ThumbFlash). - flash: ThumbFlash = .{}, /// PARDES_TEST_CLOCK's virtual time (host_io.testClock), and where the /// running-out wait moves it: the core's next wake. test_clock: ?u64 = null, @@ -1534,6 +1971,54 @@ const Shell = struct { pace_ns: u64 = 0, input_seen: bool = false, wake_to: u64 = 0, + /// waitInput's pacing of output: whether this wait read a pane's output, + /// and when the last frame was written, the last input was read and the + /// focused pane's output last was. + output_seen: bool = false, + 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, + /// The session's replica clients (src/replica.zig), on sockets. + peers: replica.Peers = .{}, + /// This wait took only a replica's post (its input, its join): the + /// pump after it reads no new time, so it moves nothing the replicas' + /// speculations stand on (a replica takes such a unit as is). + mail_only: bool = false, + waited: bool = false, + /// This wait's steps so far, and the input held for the next (`split`). + took_input: bool = false, + took_output: bool = false, + held: ?@TypeOf(Command.value) = null, + /// This process a replica client of a session elsewhere (`remote`): + /// its inputs go to the replica, never straight to the core. + remote: ?*Remote = null, + + const Leaving = enum { detach, exit }; + + /// A terminal's input, to the core, or to a replica client's engine + /// (applied at once there, and sent to the session). + fn feed(s: *Shell, ev: pardes.Event) void { + const c = s.remote orelse return s.core.update(ev); + c.link.input(ev); + } + + /// Whether a terminal of this process's shows the session, or + /// nothing (a test) can. + fn local(s: *const Shell) bool { + for (s.terms) |slot| if (slot != null) return true; + return false; + } + + /// Whether anyone looks at the session: a terminal here, or a replica + /// client (Peers keeps it attended while one is there). + fn setAttended(s: *Shell, on: bool) void { + s.peers.alone_unattended = !on; + s.core.unattended = !on and s.peers.count() == 0; + } fn of(ctx: ?*anyopaque) *Shell { return @ptrCast(@alignCast(ctx.?)); @@ -1557,6 +2042,11 @@ const Shell = struct { .present = present, .post_present = postPresent, .poll_frame = pollFrame, + .watched = watched, + .detach = detach, + .adopt = adopt, + .upgrade = upgradeAsked, + .newer_build = newerBuild, .spawn = spawn, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -1580,9 +2070,354 @@ const Shell = struct { fn clockNow(ctx: ?*anyopaque) u64 { const s = of(ctx); + if (s.waited) { + s.waited = false; + if (s.mail_only and s.peers.count() > 0 and s.core.clock_started) return s.core.now_ns; + } return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); } + /// The live view's terminal (null in tests). + fn term(s: *const Shell) ?*Screen { + return s.terms[s.core.view]; + } + + fn attached(s: *const Shell) bool { + const screen = s.term() orelse return true; + return screen.on; + } + + /// Drawn only while a terminal shows it: a replica client's view live + /// (its input the last), whichever terminal here shows another. + fn watched(ctx: ?*anyopaque) bool { + const s = of(ctx); + if (s.term() == null and s.local()) { + for (s.terms) |slot| if (slot) |sc| if (sc.on) return true; + return false; + } + return s.attached(); + } + + /// Detach: the terminal put back and let go, the stub told (it ends, + /// and the shell has its terminal again), the session left running. + fn detach(ctx: ?*anyopaque) void { + const s = of(ctx); + // A replica client's: its link ends. + if (s.peers.detach(s.core.view)) return; + if (!s.attached()) return s.core.reportFailure(s.core.active, "Detach: no terminal is attached"); + // One of several: its view goes, and the session stays on the rest. + if (pardes.views.count(s.core) > 1) return s.dropView(s.core.view, .detach); + if (!daemon.kept()) return s.core.reportFailure(s.core.active, "Detach: no pardes daemon keeps this session (it is its terminal's job)"); + s.leaveTerminal(); + } + + /// View `k`'s terminal let go (detached, or gone) while others stay: the + /// view goes with it. + fn dropView(s: *Shell, k: u8, how: Leaving) void { + const sc = s.terms[k] orelse return; + stopInput(s.io, &sc.thread); + s.terms[k] = null; + s.core.closeView(k); + s.dropScreen(sc, how); + } + + /// A terminal put back and its stub told; an attached one's Screen freed + /// (the session's own stays, a view of none). + fn dropScreen(s: *Shell, sc: *Screen, how: Leaving) void { + if (sc.primary) { + sc.leave(); + return sc.sayDetached(); + } + sc.leave(); + switch (how) { + .detach => sc.sayDetached(), + .exit => { + daemon.tellExit(sc.stub, 0); + sc.stub = -1; + }, + } + sc.glide.deinit(s.gpa); + sc.kitty.deinit(); + s.gpa.free(sc.buf); + s.gpa.destroy(sc); + } + + fn leaveTerminal(s: *Shell) void { + const screen = s.term() orelse return; + stopInput(s.io, &screen.thread); + screen.leave(); + s.core.native_images = false; + s.setAttended(false); + // The stub's stdin, stdout and stderr were its terminal too. + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (null_fd >= 0) { + for (0..3) |i| _ = libc.dup2(null_fd, @intCast(i)); + if (null_fd > 2) _ = libc.close(null_fd); + } + screen.sayDetached(); + daemon.setState(true); + } + + /// `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, cooked: ?posix.termios) void { + // A replica client's view live (its input the last) has no terminal + // here: the session's own is the one a terminal takes, or joins. + const live = s.term() orelse for (s.terms) |slot| { + if (slot) |sc| if (sc.primary) break sc; + } else { + _ = libc.close(fd); + return refuse(stub, "that session has no terminal of its own to attach to"); + }; + const screen = if (!live.on) live else extra: { + const sc = s.gpa.create(Screen) catch break :extra null; + const buf = s.gpa.alloc(u8, 0x10000) catch { + s.gpa.destroy(sc); + break :extra null; + }; + sc.* = .{ .io = s.io, .gpa = s.gpa, .env = live.env, .buf = buf, .kitty = .init(s.gpa) }; + break :extra sc; + } orelse { + _ = libc.close(fd); + return refuse(stub, "that session has no room for another terminal"); + }; + const extra = screen != live; + 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"); + }; + screen.enter(tty) catch { + if (extra) s.freeScreen(screen) else daemon.setState(true); + return refuse(stub, "that terminal would not take the screen"); + }; + if (extra) { + const k = s.core.openView() orelse { + screen.leave(); + s.freeScreen(screen); + return refuse(stub, "that session has as many terminals as it takes"); + }; + screen.feed.view = k; + s.terms[k] = screen; + } + screen.setStub(stub); + s.setAttended(true); + startInput(s.loop, screen) catch {}; + screen.vx.queryTerminalSend(screen.tty.writer()) catch {}; + s.core.needs_frame = true; + daemon.setState(false); + } + + fn freeScreen(s: *Shell, sc: *Screen) void { + sc.kitty.deinit(); + s.gpa.free(sc.buf); + s.gpa.destroy(sc); + } + + /// A terminal refused: its stub told why (a test's has none). + fn refuse(stub: posix.fd_t, why: []const u8) void { + if (stub >= 0) daemon.refuseStub(stub, why); + } + + /// Adopt: the daemon is asked for the shells of sessions that died + /// holding them; each comes back (`adopted`) as a terminal pane here. + fn adopt(ctx: ?*anyopaque, pane: u8) void { + const s = of(ctx); + if (!daemon.kept()) return s.core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session"); + s.adopted = 0; + daemon.adopt(); + } + + /// A client's own build is not the session's to say (its core's host + /// is the session's, through the tape). + fn newerBuild(ctx: ?*anyopaque) bool { + return of(ctx).remote == null and upgrade.newer(); + } + + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + const s = of(ctx); + if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); + 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(); + // Its replica clients told, their views gone before the dump: each + // dials the session again once it is back. + s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); + var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; + for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); + for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ + .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[<u") catch {}; + sc.vx.state.kitty_keyboard = false; + sc.tty.writer().flush() catch {}; + }; + popped = true; + unlinkKittyShm(); + 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, 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, but a replica client's, kept for its rejoin (views.away). + fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { + if (comptime builtin.is_test) unreachable; + const core = s.core; + 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) { + // The live view alone (never one kept for a rejoin) is the + // session's own. + for (0..pardes.views.max) |k| if (k != core.view and pardes.views.isOpen(core, @intCast(k)) and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); + s.terms[core.view] = primary; + primary.feed.view = core.view; + return; + } + for (rec.ttys[0..rec.nttys]) |t| { + 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 and !pardes.views.isAway(core, @intCast(k))) 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; + if (pid == 0) { + var buf: [96]u8 = undefined; + return core.setMessage(core.active, if (s.adopted == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ s.adopted, if (s.adopted == 1) "" else "s" }) catch "Adopt: shells taken in"); + } + if (s.adoptions_len < s.adoptions.len) { + // A terminal pane, as the Tty word makes one; its spawn takes + // this pty instead of forking a shell (`spawn`). + s.adoptions[s.adoptions_len] = .{ .pid = pid, .fd = fd, .pane = null }; + s.adoptions_len += 1; + if (core.adoptShell()) |id| { + s.adoptions[s.adoptions_len - 1].pane = id; + s.adopted += 1; + return; + } + s.adoptions_len -= 1; + } + // No room for it: its master goes, and the shell with this session. + _ = libc.close(fd); + var buf: [96]u8 = undefined; + core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell"); + } + fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { const s = of(ctx); // The wait is the 9P connections' turn with the core. A timeout (the @@ -1593,6 +2428,14 @@ const Shell = struct { // ends the wait at once and its frame is drawn then; the ticks after // it run from it. var wait = timeout_ms; + // Detached, nobody's key dismisses a message: they go on the clock + // (the pump's, after this wait), which wakes it. + if (!s.attached()) { + for (s.core.panes) |slot| if (slot) |pane| if (pane.msg_len > 0) { + wait = if (wait == 0) 100 else @min(wait, 100); + break; + }; + } const paced = s.test_clock == null and s.core.continuous(); var tick: u64 = 0; if (paced) { @@ -1604,6 +2447,11 @@ const Shell = struct { wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms)); } s.input_seen = false; + s.output_seen = false; + s.took_input = false; + s.took_output = false; + s.mail_only = true; + defer s.waited = true; // Paced, what is not an input (a frame's ack, a pty's output) is // taken in and the wait goes on to the tick: the acks alone ran the // loop at the terminal's round trip. @@ -1615,29 +2463,51 @@ const Shell = struct { wait = @intCast((tick - now) / std.time.ns_per_ms); } if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + // Output alone is drawn a frame a tick at most, what came in till + // then in one: a flood (`yes`, 4 KB a read) was drawn a read at a + // time, nearly all of its frames written over before they were + // seen. Never while what someone waits on is undrawn: an input + // (its frame held for the terminal's answer, FrameAck), or the + // focused pane's output soon after one, its echo (a remote shell's + // an RTT later). + while (!paced and s.output_seen and !s.input_seen and !s.core.quit and s.test_clock == null) { + if (s.input_ns > s.drawn_ns) break; + if (s.focused_ns > s.drawn_ns and s.focused_ns -| s.input_ns < std.time.ns_per_s) break; + const due = s.drawn_ns + pardes.animation.frame_ns; + const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (now + std.time.ns_per_ms > due) break; + s.waitOnce(@intCast((due - now) / std.time.ns_per_ms)); + } } fn waitOnce(s: *Shell, wait: u32) void { var batch: usize = 0; - if (wait != 0) { - // A virtual clock moves a frame a wake while anything moves - // continuously (the glide), as the real one would. - s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; - requestWake(s.io, wait); - } - pardes.turn.rest(); - const first = s.loop.nextEvent() catch { + const first = if (s.held) |held| held: { + s.held = null; + break :held held; + } else next: { + if (wait != 0) { + // A virtual clock moves a frame a wake while anything moves + // continuously (the glide), as the real one would. + s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; + requestWake(s.io, wait); + } + pardes.turn.rest(); + const got = s.loop.nextEvent() catch { + pardes.turn.wake(); + s.core.quit = true; + return s.reloadWatched(); + }; pardes.turn.wake(); - s.core.quit = true; - return s.reloadWatched(); + break :next got; }; - pardes.turn.wake(); batch = 1; // A run of queued motions is one move to its last cell: each would // otherwise redo hover and selection work the next one undoes. var motion: ?@TypeOf(Command.value) = null; var ev = first; while (true) { + if (s.split(ev)) break; if (ev == .mouse and (ev.mouse.type == .motion or ev.mouse.type == .drag)) { if (motion) |m| if (m.mouse.button != ev.mouse.button or m.mouse.type != ev.mouse.type or @as(u3, @bitCast(m.mouse.mods)) != @as(u3, @bitCast(ev.mouse.mods))) @@ -1661,6 +2531,31 @@ const Shell = struct { s.reloadWatched(); } + /// With replica clients, a wait (one pump, one unit) takes one step of + /// a person's input, and none after a pty's output: a step reads the + /// terminals as the clients' deltas bring them, sent after each unit + /// (docs/next-daemon.md, part B terminal notes). One that must wait is + /// held for the next. Whether `ev` is. + fn split(s: *Shell, ev: @TypeOf(Command.value)) bool { + if (!s.peers.recording) return false; + switch (ev) { + .pty_read => { + s.took_output = true; + return false; + }, + .key_press => if (s.in_paste) return false, + .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {}, + else => return false, + } + if (s.took_output or s.took_input) { + s.held = ev; + s.input_seen = true; + return true; + } + s.took_input = true; + return false; + } + fn apply(s: *Shell, event: @TypeOf(Command.value)) bool { const core = s.core; const tz_event = tracy.zone(@src(), "event"); @@ -1679,28 +2574,85 @@ const Shell = struct { .paste, .paste_start, .winsize => s.input_seen = true, else => {}, } + if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (event != .replica_mail) s.mail_only = false; + // A terminal's input goes into its view; a closed one's last is + // dropped. + switch (event) { + .key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => { + if (s.terms[s.from] == null and s.local()) { + if (event == .paste) s.gpa.free(@constCast(event.paste)); + return false; + } + if (s.local() and s.remote == null) core.toView(s.from); + }, + else => {}, + } switch (event) { .nop => {}, + .from => |k| s.from = k, + .winched => s.winched(), + // A terminal hung up (its stub passes the hangup on) or its stub + // died (a forked session's death signal): that one's input has + // ended. A HUP says not whose: each is asked. + .hangup => for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on and sc.hungUp()) { + s.from = @intCast(k); + if (s.apply(.quit)) return true; + }, .wake => if (s.test_clock) |*virtual| { virtual.* = @max(virtual.*, s.wake_to); }, .quit => { + if (s.remote) |c| { + c.link.quit = true; + return true; + } + // One of several: its view goes with it. + if (pardes.views.count(core) > 1) { + s.dropView(core.view, .detach); + return true; + } + // The terminal went: a session the daemon keeps carries on + // without it, to be attached again. Gone before the first + // frame, the reader that takes the daemon's `ok` (kept + // waits on it) is not started yet: started here. + daemon.watch(s.loop, deliverNote); + if (daemon.kept()) { + if (s.attached()) s.leaveTerminal(); + return true; + } core.quit = true; return true; }, - .focus_in => core.update(.{ .focus = true }), - .focus_out => { - core.update(.pointer_leave); - core.update(.{ .focus = false }); + .attach => |a| s.attachTerminal(a.tty, a.stub, null), + .adopted => |a| s.adoptShell(a.pid, a.fd), + .replica => |j| { + s.peers.handed(j.fd, j.view); + s.input_seen = true; }, - .winsize => |ws| { - s.vx.resize(s.gpa, s.tty.writer(), ws) catch {}; - if (comptime pardes.pdf_enabled) - updateCoreTerminalSize(core, ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) - else - core.update(.{ .resize = .{ .cols = ws.cols, .rows = ws.rows } }); + .replica_mail => s.input_seen = true, + .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 => {}, }, + .link_gone => if (s.remote) |c| { + c.link.quit = true; + c.link.gone = true; + return true; + }, + .focus_in => s.feed(.{ .focus = true }), + .focus_out => { + s.feed(.pointer_leave); + s.feed(.{ .focus = false }); + }, + .winsize => |ws| if (s.attached()) s.resized(s.terms[s.from], ws), .pty_read => |pr| { + tookRead(s.io, pr.id); + s.output_seen = true; + if (pr.id == core.active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); if (s.gens[pr.id] == pr.gen) core.update(.{ .output = .{ .pane = @intCast(pr.id), .bytes = pr.bytes } }); s.gpa.free(pr.bytes); @@ -1708,7 +2660,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: @@ -1733,10 +2685,10 @@ const Shell = struct { const bytes = pasteBytes(key); const room = max_paste_bytes -| s.paste_buf.written().len; s.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {}; - } else core.update(vaxis_input.keyEvent(key)), + } else s.feed(vaxis_input.keyEvent(key)), .mouse => |m| { const pdf_before = nativePdfWheelTarget(core, m); - if (mouseEvent(m)) |ev| core.update(ev); + if (mouseEvent(m)) |ev| s.feed(ev); if (pdf_before) |before| { if (core.panes[before.pane]) |pane| { if (pane.pdfPage()) |page| { @@ -1746,7 +2698,7 @@ const Shell = struct { } }, .paste => |bytes| { - core.update(.{ .paste = bytes }); + s.feed(.{ .paste = bytes }); s.gpa.free(@constCast(bytes)); }, .paste_start => { @@ -1756,7 +2708,7 @@ const Shell = struct { .paste_end => { s.in_paste = false; const pasted = s.paste_buf.written(); - if (pasted.len > 0) core.update(.{ .paste = pasted }); + if (pasted.len > 0) s.feed(.{ .paste = pasted }); s.paste_buf.clearRetainingCapacity(); }, .files_changed => s.check_files = true, @@ -1793,6 +2745,28 @@ const Shell = struct { return false; } + /// Terminal `screen` (the live view's) is `ws` big. + fn resized(s: *Shell, screen: ?*Screen, ws: vaxis.Winsize) void { + if (screen) |sc| { + sc.size = ws; + sc.vx.resize(s.gpa, sc.tty.writer(), ws) catch {}; + } + s.feed(.{ .resize = if (comptime pardes.pdf_enabled) terminalSize(ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) else .{ .cols = ws.cols, .rows = ws.rows } }); + } + + /// A SIGWINCH: each terminal whose size it changed is resized, in its + /// view (a terminal reporting in band says so as input). + fn winched(s: *Shell) void { + for (s.terms, 0..) |slot, k| { + const sc = slot orelse continue; + if (!sc.on or sc.vx.state.in_band_resize) continue; + const ws = sc.tty.getWinsize() catch continue; + if (sc.size) |was| if (std.meta.eql(was, ws)) continue; + if (s.remote == null) s.core.toView(@intCast(k)); + s.resized(sc, ws); + } + } + fn reloadWatched(s: *Shell) void { if (!s.check_files) return; s.check_files = false; @@ -1805,13 +2779,12 @@ const Shell = struct { /// notches are); `wheeled` from the mouse event if it should glide. fn stepGlide(ctx: ?*anyopaque) void { const s = of(ctx); - s.glide.step(s.core, s.gpa, null, null); + if (s.term()) |sc| sc.glide.step(s.core, s.gpa, null, null); } fn pollFrame(ctx: ?*anyopaque) void { const s = of(ctx); stepGlide(ctx); - s.core.native_image_patches = kitty_shm.load(.acquire); if (s.fs) |f| if (f.tick().pending) { _ = s.loop.tryPostEvent(.fs_ready) catch {}; }; @@ -1820,23 +2793,26 @@ const Shell = struct { if (host_io.ttyCwd(pt.pid, pt.file.handle, s.core.settings.shell.effective.get(), &lbuf)) |cwd| s.core.setCwd(id, cwd); }; - if (s.caps_pending and s.vx.queries_done.load(.unordered)) { - s.caps_pending = false; - s.vx.enableDetectedFeatures(s.tty.writer()) catch {}; - s.core.native_images = s.vx.caps.kitty_graphics; - if (s.vx.caps.kitty_graphics) kittyShmProbe(s.tty.writer()); - s.vx.queueRefresh(); + const sc = s.term() orelse return; + s.core.native_image_patches = sc.feed.shm.load(.acquire); + if (sc.caps_pending and sc.on and sc.vx.queries_done.load(.unordered)) { + sc.caps_pending = false; + sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; + s.core.native_images = sc.vx.caps.kitty_graphics; + if (sc.vx.caps.kitty_graphics) kittyShmProbe(sc.tty.writer(), &sc.feed); + sc.vx.queueRefresh(); s.core.needs_frame = true; } } fn present(ctx: ?*anyopaque, canonical: *const pardes.Surface) void { const s = of(ctx); - const vx = s.vx; - s.tracks = canonical.panelTracks(); + const sc = s.term() orelse return; + const vx = &sc.vx; + sc.tracks = canonical.panelTracks(); // The last frame is not answered yet: this one stays owed, and the // answer's wake draws the state as it is then. - if (FrameAck.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { + if (sc.feed.ack.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { s.core.present_skipped = true; return; } @@ -1844,22 +2820,23 @@ const Shell = struct { const surface = panel_compositor.compose( s.frame.allocator(), @constCast(canonical), - s.tracks, + sc.tracks, s.core.theme().bg, ) catch return; const tz_cells = tracy.zone(@src(), "surface->vaxis"); const win = vx.window(); const truecolor = vx.caps.rgb or colortermTruecolor(); // Each of the shell's own motions below asks for frames while it runs. - if (s.glide.pending) s.glide.measure(s.gpa, canonical); - if (s.glide.moving()) s.glide.advance(s.core.now_ns, s.glide.motion); - s.core.shell_continuous = s.glide.moving(); - s.core.frames.scroll = @round(s.glide.follow.offset); - const glided = pardes.glide.shifted(&s.glide, s.frame.allocator(), surface) catch surface; + if (sc.glide.pending) sc.glide.measure(s.gpa, canonical); + if (sc.glide.moving()) sc.glide.advance(s.core.now_ns, sc.glide.motion); + s.core.shell_continuous = sc.glide.moving(); + s.core.frames.scroll = @round(sc.glide.follow.offset); + const glided = pardes.glide.shifted(&sc.glide, s.frame.allocator(), surface) catch surface; const shadowed = chipShadows(s.frame.allocator(), glided, s.core, truecolor) catch glided; - const flashed = s.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; - const trailed = s.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; + const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; + const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; paintCells(win, trailed.cells, trailed.cols, trailed.rows); + 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; @@ -1868,7 +2845,7 @@ const Shell = struct { if (!kittyImageRepresentable(place)) continue; } const key = place.cacheKey(); - if (!s.kitty.contains(key) and vx.caps.kitty_graphics and kitty_shm.load(.acquire)) shm: { + if (!sc.kitty.contains(key) and vx.caps.kitty_graphics and sc.feed.shm.load(.acquire)) shm: { var buf: [64]u8 = undefined; kitty_shm_serial +%= 1; // Bounded: a segment this many transmissions old was never @@ -1880,21 +2857,21 @@ const Shell = struct { } const name = kittyShmName(&buf, kitty_shm_serial) catch break :shm; if (!kittyShmWrite(name, place.rgba)) break :shm; - if (vx.transmitLocalImagePath(s.gpa, s.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| - s.kitty.put(key, handle) catch vx.freeImage(s.tty.writer(), handle.id) + if (vx.transmitLocalImagePath(s.gpa, sc.tty.writer(), name, @intCast(place.iw), @intCast(place.ih), .shared_mem, .rgba) catch null) |handle| + sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id) else _ = libc.shm_unlink(name); } - if (!s.kitty.contains(key) and vx.caps.kitty_graphics) { + if (!sc.kitty.contains(key) and vx.caps.kitty_graphics) { const enc = std.base64.standard.Encoder; if (s.gpa.alloc(u8, enc.calcSize(place.rgba.len))) |b64| { defer s.gpa.free(b64); _ = enc.encode(b64, place.rgba); - if (vx.transmitPreEncodedImage(s.tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null) |handle| - s.kitty.put(key, handle) catch vx.freeImage(s.tty.writer(), handle.id); + if (vx.transmitPreEncodedImage(sc.tty.writer(), b64, @intCast(place.iw), @intCast(place.ih), .rgba) catch null) |handle| + sc.kitty.put(key, handle) catch vx.freeImage(sc.tty.writer(), handle.id); } else |_| {} } - if (s.kitty.get(key)) |cached| { + if (sc.kitty.get(key)) |cached| { if (comptime !pardes.pdf_enabled) { const child = win.child(.{ .x_off = place.x, .y_off = place.y, .width = place.w, .height = place.h }); cached.draw(child, .{ .scale = .contain }) catch {}; @@ -1935,36 +2912,42 @@ const Shell = struct { while (true) { var stale: [pardes.MAX_PANES]pardes.ImageCacheKey = undefined; var stale_len: usize = 0; - var image_iterator = s.kitty.iterator(); + var image_iterator = sc.kitty.iterator(); while (image_iterator.next()) |entry| { if (surfaceHasKittyKey(surface, entry.key_ptr.*)) continue; stale[stale_len] = entry.key_ptr.*; stale_len += 1; if (stale_len == stale.len) break; } - for (stale[0..stale_len]) |key| if (s.kitty.fetchRemove(key)) |removed| - vx.freeImage(s.tty.writer(), removed.value.id); + for (stale[0..stale_len]) |key| if (sc.kitty.fetchRemove(key)) |removed| + vx.freeImage(sc.tty.writer(), removed.value.id); if (stale_len < stale.len) break; } if (glided.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar); const tz_render = tracy.zone(@src(), "vx.render"); - const before = s.tty.tty_writer.pos + s.tty.writer().end; - vx.render(s.tty.writer()) catch {}; + const before = sc.tty.tty_writer.pos + sc.tty.writer().end; + vx.render(sc.tty.writer()) catch {}; // The frame's cost to the terminal (FrameStats.Frame.bytes): what // vaxis's diff of the cells wrote. - s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), s.tty.tty_writer.pos + s.tty.writer().end -| before)); - if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); + s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), sc.tty.tty_writer.pos + sc.tty.writer().end -| before)); + if (sc.tty.tty_writer.pos + sc.tty.writer().end != before) sc.feed.ack.ask(sc.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); + s.drawn_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); tz_render.end(); } fn postPresent(ctx: ?*anyopaque) void { const s = of(ctx); - s.core.acknowledgePanelPresentation(s.tracks); + if (s.term()) |sc| s.core.acknowledgePanelPresentation(sc.tracks); tracy.frameMark(); } fn spawn(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { const s = of(ctx); + for (s.adoptions[0..s.adoptions_len], 0..) |a, i| if (a.pane == pane) { + std.mem.copyForwards(Adoption, s.adoptions[i .. s.adoptions_len - 1], s.adoptions[i + 1 .. s.adoptions_len]); + s.adoptions_len -= 1; + return s.takeShell(pane, a); + }; // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = s.ptys[pane] != null; @@ -1988,6 +2971,25 @@ const Shell = struct { } } + /// An adopted shell, running already, as pane `pane`'s: told the size + /// twice (a size it never had, then the pane's), which makes it redraw. + fn takeShell(s: *Shell, pane: u8, a: Adoption) void { + closePty(s, pane); + s.gens[pane] +%= 1; + const file: std.Io.File = .{ .handle = a.fd, .flags = .{ .nonblocking = false } }; + s.ptys[pane] = .{ .file = file, .pid = a.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{} }; + var exe_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + s.core.acknowledgeShell(pane, host_io.processExe(a.pid, &exe_buf) orelse "sh", host_io.marksPrompts(a.pid)); + var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(pane, wd); + const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; + _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); + if (s.core.panes[pane]) |pn| ptyResize(s, pane, @max(pn.cols, 1), @max(pn.rows, 1)); + if (s.threads_ok) if (s.ptys[pane]) |*pt| { + pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, @as(usize, pane), s.gens[pane], s.loop }) catch pt.reader; + }; + } + fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { const s = of(ctx); if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); @@ -2018,6 +3020,7 @@ const Shell = struct { s.gens[pane] +%= 1; pt.reader.cancel(s.io) catch {}; _ = libc.close(pt.file.handle); + daemon.closed(pt.pid); host_io.retireShell(pt.pid); } @@ -2078,12 +3081,17 @@ const Shell = struct { fn setClipboard(ctx: ?*anyopaque, text: []const u8) void { const s = of(ctx); - copyToClipboard(s.vx, s.tty, s.gpa, text); + // Detached, the session keeps it (a 9P read, a paste here). + const sc = s.term() orelse return s.core.fallback.setClipboard(text); + if (!sc.on) return s.core.fallback.setClipboard(text); + copyToClipboard(&sc.vx, &sc.tty, s.gpa, text); } fn readClipboard(ctx: ?*anyopaque) void { const s = of(ctx); - requestClipboard(s.vx, s.tty); + const sc = s.term() orelse return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); + if (!sc.on) return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); + requestClipboard(&sc.vx, &sc.tty); } fn openLink(_: ?*anyopaque, url: []const u8) void { @@ -2147,9 +3155,6 @@ test "TTY queued results reject old PTY generations and LSP request IDs" { .core = core, .prompt_rcs = undefined, .loop = undefined, - .vx = undefined, - .tty = undefined, - .kitty = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, @@ -2195,9 +3200,6 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .core = core, .prompt_rcs = undefined, .loop = undefined, - .vx = undefined, - .tty = undefined, - .kitty = undefined, .frame = undefined, .paste_buf = undefined, .inotify_fd = -1, @@ -2246,7 +3248,7 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { } fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { - if (comptime builtin.os.tag != .linux) { + if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) { while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break; return; } @@ -2261,23 +3263,22 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { } } -fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { - winchWatchUntil(loop, vx, tty, null); -} +fn keepSignal(_: posix.SIG) callconv(.c) void {} -/// A SIGWINCH watcher that ends: `stop` set, then a SIGWINCH sent to its -/// thread, wakes its sigwait and it returns, so whoever owns `loop` can join -/// it before the loop goes (attachSession's loop is on its stack). -fn winchWatchUntil(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty, stop: ?*const std.atomic.Value(bool)) void { +/// A replica client's (`remote`): a hangup is its end, whoever sent it. +var hangup_quits = false; + +fn winchWatch(loop: *Loop) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); + if (daemon.link >= 0 or hangup_quits) posix.sigaddset(&set, posix.SIG.HUP); while (true) { var sig: c_int = 0; if (libc.sigwait(&set, &sig) != 0) continue; - if (stop) |flag| if (flag.load(.acquire)) return; - if (vx.state.in_band_resize) continue; // terminal reports via CSI 48 - const ws = tty.getWinsize() catch continue; - loop.postEvent(.{ .winsize = ws }) catch {}; + // The terminals are the loop's: it asks each its size, and takes a + // hangup as the end of the one that hung up. + const ev: @TypeOf(Command.value) = if (sig != @backingInt(posix.SIG.HUP)) .winched else if (hangup_quits) .quit else .hangup; + loop.postEvent(ev) catch {}; } } @@ -2356,15 +3357,37 @@ fn wakeFs(ctx: ?*anyopaque) void { _ = loop.tryPostEvent(.fs_ready) catch {}; } +/// Each pane's reads posted and not yet taken in. Two at most: a flood the +/// loop cannot keep up with waits in its pty, not in the queue ahead of a +/// key and its echo (hundreds of 4 KB reads queued were half a second). +var reads_queued: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0)); + +/// A read of pane `id` taken in (or dropped): its reader may read on. Never +/// below zero, for an event no reader posted (a test's). +fn tookRead(io: std.Io, id: usize) void { + var queued = reads_queued[id].load(.acquire); + while (queued > 0) queued = reads_queued[id].cmpxchgWeak(queued, queued - 1, .acq_rel, .acquire) orelse { + if (queued == 2) io.futexWake(u32, &reads_queued[id].raw, 1); + return; + }; +} + fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) anyerror!void { // One buffer, read into directly: a Reader around it only added a second // (and in a safe build, filled both as `undefined` on every start). var buf: [0x10000]u8 = undefined; - while (true) { + read: while (true) { + while (true) { + const queued = reads_queued[id].load(.acquire); + if (queued < 2) break; + io.futexWait(u32, &reads_queued[id].raw, queued) catch break :read; + } const n = pty.readStreaming(io, &.{&buf}) catch break; if (n == 0) break; const bytes = try gpa.dupe(u8, buf[0..n]); + _ = reads_queued[id].fetchAdd(1, .acq_rel); loop.postEvent(.{ .pty_read = .{ .id = id, .gen = gen, .bytes = bytes } }) catch { + tookRead(io, id); gpa.free(bytes); break; }; @@ -2396,7 +3419,7 @@ test "a terminal pane gets function keys as their sequences and lone modifiers n vx.queries_done.store(false, .unordered); var tty: vaxis.Tty = undefined; var loop: Loop = .init(io, &tty, &vx); - var cache: vaxis.GraphemeCache = .{}; + var feed: Feed = .{}; const Reader = struct { parts: []const []const u8, next: usize = 0, @@ -2429,7 +3452,7 @@ test "a terminal pane gets function keys as their sequences and lone modifiers n "\x1b[104;;104u", }, }; - try readInput(&loop, &reader, &cache); + try readInput(&loop, &reader, &vx, &feed); const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); core.panes[0].?.body.mode = .tty; @@ -2572,6 +3595,17 @@ test "a mouse's side buttons, xterm's 8 and 9, are Back and Forward, as SDL's X1 } } +/// A replica client's own line over its frame's last row (`Remote.notify`), +/// reversed. `text` is printable ASCII and outlives the render (vaxis keeps +/// the slices). +fn noticeLine(win: vaxis.Window, text: []const u8, cols: u16, rows: u16) void { + if (rows == 0) return; + for (0..cols) |x| win.writeCell(@intCast(x), rows - 1, .{ + .char = .{ .grapheme = if (x < text.len) text[x..][0..1] else " " }, + .style = .{ .reverse = true }, + }); +} + /// A blank whose ink alone changed is not sent: vaxis's render skips it /// (pardes's patch to Vaxis.zig in build.zig), since a blank's ink shows /// nowhere. Deciding that here cost every cell of every frame a look at the @@ -2869,243 +3903,6 @@ fn vaxisColor(c: pardes.Color) vaxis.Color { }; } -const AttachEnd = union(enum) { - none, - no_session: []const u8, - nothing_detached, - ambiguous: usize, - refused: wire.Refusal, - lost: anyerror, - rejected, - silent, - detached: []const u8, - - fn report(e: AttachEnd, io: std.Io) void { - var buf: [512]u8 = undefined; - var ok = false; - const text: []const u8 = switch (e) { - .none => return, - .detached => |name| blk: { - ok = true; - break :blk if (name.len != 0) std.fmt.bufPrint( - &buf, - "pardes: detached from '{s}', which is still running — come back with `pardes --attach={s}`\n", - .{ name, name }, - ) catch "pardes: detached; that session is still running\n" else "pardes: detached; that session is still running — come back with `pardes --attach`\n"; - }, - .no_session => |name| std.fmt.bufPrint( - &buf, - "pardes: no detached session called '{s}' (start one with `pardes --detach={s}`)\n", - .{ name, name }, - ) catch "pardes: no detached session under that name\n", - .nothing_detached => "pardes: no detached session is running (start one with `pardes --detach`)\n", - .ambiguous => |n| std.fmt.bufPrint( - &buf, - "pardes: {d} detached sessions are running; say which with --attach=<name>\n", - .{n}, - ) catch "pardes: several detached sessions are running; say which with --attach=<name>\n", - .refused => |why| switch (why) { - .version => "pardes: that session speaks a different wire version — it is another build of pardes\n", - .full => "pardes: that session already has every frontend slot taken\n", - .quitting => "pardes: that session is ending\n", - }, - .lost => |err| std.fmt.bufPrint(&buf, "pardes: detached session lost: {t}\n", .{err}) catch - "pardes: detached session lost\n", - .rejected => "pardes: that session hung up on the connect — every frontend slot is taken (32), or it is shutting down. `PARDES_LOG=1` on the session names which\n", - .silent => "pardes: that session accepted the connection and then never greeted it — it is wedged inside its own loop, or something else is listening at that path. `PARDES_LOG=1` on the session says which\n", - }; - const out = if (ok) std.Io.File.stdout() else std.Io.File.stderr(); - out.writeStreamingAll(io, text) catch {}; - if (!ok) std.process.exit(1); - } - - fn row(e: AttachEnd, buf: []u8) []const u8 { - return switch (e) { - .none => "attach: that session ended", - .no_session => |name| std.fmt.bufPrint(buf, "attach: no session '{s}'", .{name}) catch - "attach: no session under that name", - .nothing_detached => "attach: no detached session is running", - .ambiguous => |n| std.fmt.bufPrint(buf, "attach: {d} sessions running, name one", .{n}) catch - "attach: several sessions running, name one", - .refused => |why| switch (why) { - .version => "attach: that session is another build of pardes", - .full => "attach: that session has every frontend slot taken", - .quitting => "attach: that session is ending", - }, - .lost => |err| std.fmt.bufPrint(buf, "attach: {t}", .{err}) catch "attach: link lost", - .rejected => "attach: that session hung up on the connect", - .silent => "attach: that session accepted and never greeted", - .detached => "attach: detached from that session", - }; - } -}; - -const Attach = struct { - gpa: std.mem.Allocator, - client: *detached_client.Client, - loop: *Loop, - vx: *vaxis.Vaxis, - tty: *vaxis.Tty, - paste_buf: *std.Io.Writer.Allocating, - session: []const u8 = &.{}, - caps_pending: bool = true, - in_paste: bool = false, - dirty: bool = false, - - fn send(a: *Attach, ev: pardes.Event) ?AttachEnd { - a.client.send(.{ .event = ev }) catch |err| switch (err) { - error.Overlong, error.WriteFailed => return null, - else => return .{ .lost = err }, - }; - return null; - } - - fn handle(a: *Attach, msg: wire.ServerMsg) ?AttachEnd { - switch (msg) { - .welcome => {}, - .refuse => |why| return .{ .refused = why }, - .frame => a.dirty = true, - .quit => return .none, - .detach => return .{ .detached = a.session }, - .set_clipboard => |text| copyToClipboard(a.vx, a.tty, a.gpa, text), - .read_clipboard => requestClipboard(a.vx, a.tty), - .open_link => |url| look.openLink(url), - // A terminal has no post chain to draw through. - .post => {}, - } - return null; - } - - fn apply(a: *Attach, event: @TypeOf(Command.value)) ?AttachEnd { - switch (event) { - .nop, .wake, .fs_ready, .lsp_done, .lsp_status, .pipe_done, .search_rows, .pty_read, .pty_eof, .files_changed => {}, - .quit => return .none, - .focus_in => return a.send(.{ .focus = true }), - .focus_out => { - if (a.send(.pointer_leave)) |end| return end; - return a.send(.{ .focus = false }); - }, - .winsize => |ws| { - a.vx.resize(a.gpa, a.tty.writer(), ws) catch {}; - a.dirty = true; - a.client.resize(ws.cols, ws.rows) catch |err| return .{ .lost = err }; - }, - .key_press => |key| if (a.in_paste) { - const bytes = pasteBytes(key); - const room = max_paste_bytes -| a.paste_buf.written().len; - a.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {}; - } else return a.send(vaxis_input.keyEvent(key)), - .mouse => |m| if (mouseEvent(m)) |ev| return a.send(ev), - .paste => |bytes| { - defer a.gpa.free(@constCast(bytes)); - return a.send(.{ .paste = bytes }); - }, - .paste_start => { - a.in_paste = true; - a.paste_buf.clearRetainingCapacity(); - }, - .paste_end => { - a.in_paste = false; - defer a.paste_buf.clearRetainingCapacity(); - const pasted = a.paste_buf.written(); - if (pasted.len > 0) return a.send(.{ .paste = pasted }); - }, - } - return null; - } - - fn enableCaps(a: *Attach) void { - if (!a.caps_pending or !a.vx.queries_done.load(.unordered)) return; - a.caps_pending = false; - a.vx.enableDetectedFeatures(a.tty.writer()) catch {}; - a.vx.queueRefresh(); - a.dirty = true; - } - - fn paint(a: *Attach) void { - a.dirty = false; - const win = a.vx.window(); - paintCells(win, a.client.grid.items, a.client.cols, a.client.rows); - if (a.client.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar); - a.vx.render(a.tty.writer()) catch {}; - } -}; - -fn attemptEnd(a: *const detached_client.Attempt, requested: []const u8) AttachEnd { - return switch (a.*) { - .greeted => .none, - .no_session => if (requested.len != 0) .{ .no_session = requested } else .nothing_detached, - .ambiguous => |found| .{ .ambiguous = found }, - .refused => |why| .{ .refused = why }, - .silent => .silent, - .lost => |err| if (err == error.Closed) .rejected else .{ .lost = err }, - }; -} - -fn attachLoop(a: *Attach) AttachEnd { - while (true) { - const link = a.client.wait(detached_client.poll_ms); - while (true) { - const msg = (a.client.next() catch |err| return .{ .lost = err }) orelse break; - if (a.handle(msg)) |end| return end; - } - link catch |err| return .{ .lost = err }; - var batch: usize = 0; - while (a.in_paste or batch < 64) { - const ev = (a.loop.tryEvent() catch |err| return .{ .lost = err }) orelse break; - batch += 1; - if (a.apply(ev)) |end| return end; - } - a.enableCaps(); - if (a.dirty) a.paint(); - } -} - -fn attachSession(init: std.process.Init, name: []const u8, tty: *vaxis.Tty, vx: *vaxis.Vaxis) AttachEnd { - const io = init.io; - const gpa = init.gpa; - - const ws = tty.getWinsize() catch |err| return .{ .lost = err }; - if (ws.cols == 0 or ws.rows == 0) return .{ .lost = error.NoWinsize }; - vx.resize(gpa, tty.writer(), ws) catch |err| return .{ .lost = err }; - - var attempt = detached_client.attempt(gpa, name, ws.cols, ws.rows); - var client = switch (attempt) { - .greeted => |c| c, - else => return attemptEnd(&attempt, name), - }; - defer client.detach(); - - var loop: Loop = .init(io, tty, vx); - var input_cache: vaxis.GraphemeCache = .{}; - var paste_buf: std.Io.Writer.Allocating = .init(gpa); - defer paste_buf.deinit(); - - var a: Attach = .{ - .gpa = gpa, - .client = &client, - .loop = &loop, - .vx = vx, - .tty = tty, - .paste_buf = &paste_buf, - .session = name, - }; - defer drainAttachedQueue(&loop, gpa); - - startInput(&loop, &input_cache) catch |err| return .{ .lost = err }; - defer stopInput(&loop); - var winch_stop: std.atomic.Value(bool) = .init(false); - const winch = std.Thread.spawn(.{}, winchWatchUntil, .{ &loop, vx, tty, &winch_stop }) catch |err| return .{ .lost = err }; - defer { - winch_stop.store(true, .release); - _ = libc.pthread_kill(winch.getHandle(), posix.SIG.WINCH); - winch.join(); - } - vx.queryTerminalSend(tty.writer()) catch {}; - - return attachLoop(&a); -} - test "a jump leaves a trail of a few cells behind the cursor that fades in 120 ms" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); diff --git a/src/tutor.txt b/src/tutor.txt index 3f153ebd..5cfb245b 100644 --- a/src/tutor.txt +++ b/src/tutor.txt @@ -144,7 +144,7 @@ run at start. 2. In a shell, pardes --detach=work starts a session with no screen; pardes --attach=work shows it; `Detach` (SPC s D) leaves it - running. + running, as a terminal that closes does. -> Guide: Sessions, Config @@ -292,7 +292,7 @@ typed SPC y yank to the clipboard SPC p paste from it SPC w window mode: h/j/k/l, Enter; d Del, c Collapse SPC j j `Last` SPC j o / SPC j i back / forward - SPC s a `Attach` SPC s D `Detach` + SPC s D `Detach` SPC t ... the toggles SPC l ... the language SPC h t this tutor diff --git a/src/upgrade.zig b/src/upgrade.zig new file mode 100644 index 00000000..37c79991 --- /dev/null +++ b/src/upgrade.zig @@ -0,0 +1,432 @@ +//! 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=<record>` 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 = &.{}; +/// The file at that path when it started, and the newer one last said. +var started_inode: ?u64 = null; +var told_inode: ?u64 = null; + +var located = false; + +/// Its arguments kept, at its start; an upgraded process's path is the one +/// 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; + }; +} + +/// Its path asked of the system (a readlink) and the file there now (a +/// stat), once: after the first frame, off a startup's way (the hosts' +/// loops; nothing is reinstalled within a frame of a start). +pub fn locate() void { + if (located) return; + located = true; + if (exe_len == 0) { + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; + exe_buf[n] = 0; + exe_len = n; + } + if (exe()) |path| started_inode = inodeOf(path); +} + +fn inodeOf(path: [:0]const u8) ?u64 { + const st = std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), path, .{}) catch return null; + return @intCast(st.inode); +} + +/// A newer build installed at that path since this process started, said +/// in a line, once a build: asked at a focus-in, where one comes back from +/// rebuilding (a stat). ponytail: explicit only, no watch and no automatic +/// upgrade. +pub fn noticeNewer(core: *pardes.Pardes) void { + if (newer()) core.setMessage(core.active, pardes.newer_build_words); +} + +/// Whether to say a newer build is installed now (`noticeNewer`): the +/// tty's Host answers the core's focus step with it, so its replica +/// clients take the line from the tape. +pub fn newer() bool { + const was = started_inode orelse return false; + const now = inodeOf(exe() orelse return false) orelse return false; + if (now == was or told_inode == now) return false; + told_inode = now; + return true; +} + +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 <version>`), 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); + // The binary it replaced, let go: held, its file (deleted) would + // keep its blocks for as long as this session runs. + if (rec.old >= 0) _ = libc.close(rec.old); + rec.old = -1; + 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/src/vaxis_input.zig b/src/vaxis_input.zig index 0e25dea1..59846d0f 100644 --- a/src/vaxis_input.zig +++ b/src/vaxis_input.zig @@ -75,3 +75,34 @@ test "F-keys, Insert and the keypad map to the core's keys" { try std.testing.expectEqual(pardes.Key.enter, keyEvent(.{ .codepoint = vaxis.Key.kp_enter }).key.cp); try std.testing.expectEqual(@as(u21, 'a'), mapKey('a')); } + +test "an event queue emptied in one batch lets every writer it held go on (build.zig's patch)" { + const io = std.testing.io; + const Q = vaxis.Queue(u8, 2); + var q: Q = .init(io); + try q.push(1); + try q.push(2); + const Writer = struct { + fn run(queue: *Q, byte: u8) !void { + try queue.push(byte); + } + }; + // Two writers waiting on the full queue, as a pane's pty reader and + // the frame ack's wake do under `yes`. + var a = try io.concurrent(Writer.run, .{ &q, 3 }); + defer a.cancel(io) catch {}; + var b = try io.concurrent(Writer.run, .{ &q, 4 }); + defer b.cancel(io) catch {}; + try io.sleep(.fromMilliseconds(20), .awake); + // The loop takes both at once: one pop from full, then one more. + try q.lock(); + _ = q.drain().?; + _ = q.drain().?; + q.unlock(); + var got: usize = 0; + var waited: usize = 0; + while (got < 2 and waited < 200) : (waited += 1) { + if (try q.tryPop() != null) got += 1 else try io.sleep(.fromMilliseconds(5), .awake); + } + try std.testing.expectEqual(@as(usize, 2), got); +} diff --git a/src/views.zig b/src/views.zig new file mode 100644 index 00000000..b1786f83 --- /dev/null +++ b/src/views.zig @@ -0,0 +1,1048 @@ +//! Views: several clients on one session, kakoune's way (docs/next-daemon.md, +//! "Phase 4 notes"). Each view has its own layout, focus, selections and +//! scroll over the panes every view shares: their text, terminals, tags, +//! registers and messages. +//! +//! The core still sees one view, the live one, in the fields it always had: +//! a view's share of the session (`fields`) and of each pane (`PaneView`) is +//! copied out when another view goes live (`park`) and back when it is live +//! again (`unpark`). No line of the core reads a view through an +//! indirection, and one view never parks: today's single client runs +//! exactly as before. +//! +//! A parked view is in no digest (Pardes.replica's `parked`, Pane's `pv`): +//! a slot is written whenever its view goes out, so the live view's slot is +//! stale by design, and a parked view that diverged shows when it is live +//! again. The pump draws the other views by going into each and back +//! (`drawOthers`), which leaves every digested field as it was; a replica +//! that draws nothing skips it. +const std = @import("std"); +const pardes = @import("pardes.zig"); +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const Text = pardes.panes.Text; +const File = pardes.panes.File; +const layout = pardes.layout; +const MAX_PANES = pardes.MAX_PANES; + +/// Views at once: a window or terminal each. +pub const max = 4; + +/// The session's fields that are each view's own: its screen and layout, +/// its focus, its pointer and gestures, its jumps, its key prefixes, and +/// what drawing it keeps (animations, the presentation of its panels). +pub const fields = [_][:0]const u8{ + "screen_w", "screen_h", "cell_pixels", "row_metrics", + "ncol", "col_weight", "col_panes", "col_n", + "col_tags", "col_serial", "finder", "rects", + "col_x", "col_w", "presentation", "active", + "jumps", "njumps", "jcur", "jump_force", + "active_column", "lifts", "lift_serials", "cursor_glide", + "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous", + "input_ns", "native_images", "native_image_patches", "windowed", + "drag", "mouse_grab", "window_focused", "focus_told", + "hover_col", "hover_row", "pointer_raw_col", "pointer_raw_row", + "pointer_body_hit", "pointer_tag_hit", "raw_hover_intent", "pointer_inside", + "last_click", "click_as", "chord_hold", "wheel_guard", + "ctrl_w_pending", "register", "leader_on", "window_pick", + "leader_keys", "leader_n", "header_focus", "header_column", + "header_drag", "header_sweep", "header_scroll", "header_top", + "ov_pinch_scale", "ov_touch_scroll_delta", "chord_arg", "clip_pending", +}; + +/// A pane's fields that are each view's own: where it is in the view's +/// layout, which of its texts has the keyboard, how far it is scrolled, and +/// what the view's step measured of it (its tag's rows, its bands). +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "attached_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan" }; + +/// The body's: its cursor, selections and mode. Its undo stays the pane's. +const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "msel", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; + +pub const View = Part(Pardes, &fields); + +/// Named field by field, not taken from Pane's type: a pane holds its own. +pub const PaneView = struct { + focus: Pane.Focus = .body, + vweight: f32 = 1, + collapsed: bool = false, + attached_to: ?u32 = null, + hscroll: i32 = 0, + tag_scroll: u16 = 0, + tag_top: u16 = 0, + tag_rows: u16 = 1, + view: Pane.SessionView = .{}, + notices: Pane.Notices = .{}, + pan: u16 = 0, + body: Part(Text, &body_fields) = .{}, + /// A document's scroll and size; a terminal's size is its pty's, which + /// every view shows (sized by its last typist, `Pardes.sync`). + scroll: usize = 0, + cols: u16 = 1, + rows: u16 = 1, + + /// In a dump (`Dumped`): what a pane's step works out again. Its `view` + /// is dumped: the step after `unpark` reads it before it syncs. + pub const replica = .{ .derived = .{ "notices", "pan" } }; +}; + +/// A view not live: its share of the session, and the serial of each pane +/// slot when it went out, by which the panes closed since leave its layout. +pub const Parked = struct { + view: View, + serials: [MAX_PANES]u32 = @splat(0), +}; + +/// What a session's dump keeps of a view's share: all of it but what +/// drawing keeps (Pardes.replica's local fields: its animations, its +/// panels as presented). +const kept = blk: { + @setEvalBranchQuota(100_000); + var names: []const [:0]const u8 = &.{}; + for (fields) |name| { + const local = for (Pardes.replica.local) |l| { + if (std.mem.eql(u8, l, name)) break true; + } else false; + if (!local) names = names ++ &[_][:0]const u8{name}; + } + const out: [names.len][:0]const u8 = names[0..names.len].*; + break :blk out; +}; + +/// A view not live as a session's dump keeps it (dump.zig): its share of +/// the session, its slots' serials, and its share of each pane. The live +/// view is the core's own fields; its slot is stale, and no part of it. +pub const Dumped = struct { + k: u8, + view: Part(Pardes, &kept), + serials: [MAX_PANES]u32 = @splat(0), + panes: []const Share = &.{}, + + pub const Share = struct { id: u16, pv: PaneView }; +}; + +fn bit(k: usize) u8 { + return @as(u8, 1) << @intCast(k); +} + +/// The views not live, as a dump keeps them: the slices `gpa`'s, what +/// they hold the session's. +pub fn dumped(p: *const Pardes, gpa: std.mem.Allocator) ![]Dumped { + var out: std.ArrayList(Dumped) = .empty; + errdefer { + for (out.items) |d| gpa.free(d.panes); + out.deinit(gpa); + } + for (0..max) |k| { + if (k == p.view or !isOpen(p, @intCast(k))) continue; + const slot = p.parked[k] orelse continue; + var d: Dumped = .{ .k = @intCast(k), .view = undefined, .serials = slot.serials }; + copy(&kept, &d.view, &slot.view); + var shares: std.ArrayList(Dumped.Share) = .empty; + errdefer shares.deinit(gpa); + // A view not live has a share of every pane: written when it went + // out, made with one made since (`installed`). `pv_known` says + // which shares own their selections, not which there are. + for (p.panes, 0..) |s, id| if (s) |pane| { + try shares.append(gpa, .{ .id = @intCast(id), .pv = pane.pv[k] }); + }; + d.panes = try shares.toOwnedSlice(gpa); + try out.append(gpa, d); + } + return out.toOwnedSlice(gpa); +} + +/// The views a dump kept (`dumped`) parked again in `p`, a session just +/// loaded with its live view: each its slot, each pane its share in it +/// (one a view had none of as the live view has it). What they hold is the +/// session's then, the slices the caller's; a failure moves nothing. A +/// view the core says open that the dump has none of is not. +pub fn undumped(p: *Pardes, others: []const Dumped) !void { + var take: u8 = 0; + for (others) |d| if (d.k < max and d.k != p.view and isOpen(p, d.k) and take & bit(d.k) == 0) { + take |= bit(d.k); + }; + var made: u8 = 0; + errdefer for (0..max) |k| if (made & bit(k) != 0) { + p.gpa.destroy(p.parked[k].?); + p.parked[k] = null; + }; + if (take != 0) for (0..max) |k| if ((take | bit(p.view)) & bit(k) != 0 and p.parked[k] == null) { + const slot = try p.gpa.create(Parked); + slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; + p.parked[k] = slot; + made |= bit(k); + }; + // Nothing fails from here. + p.views_open &= take | bit(p.view); + p.views_away &= p.views_open; + // The live view's slot owning nothing, as `installed` makes it. + for (p.panes) |s| if (s) |pane| { + if (take != 0) pane.pv[p.view] = fresh(pane); + pane.pv_known |= bit(p.view); + }; + var left = take; + for (others) |d| { + var view = d.view; + if (left & bit(d.k) == 0) { + // Refused (another build's, or twice): what it holds goes. + for (&view.col_tags) |*t| t.deinit(p.gpa); + if (view.chord_arg) |arg| p.gpa.free(arg); + for (d.panes) |sh| if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); + continue; + } + left &= ~bit(d.k); + const slot = p.parked[d.k].?; + slot.serials = d.serials; + copy(&kept, &slot.view, &view); + for (d.panes) |sh| { + const pane = (if (sh.id < MAX_PANES) p.panes[sh.id] else null) orelse { + if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); + continue; + }; + if (pane.pv_known & bit(d.k) != 0) { + if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); + continue; + } + pane.pv[d.k] = sh.pv; + pane.pv_known |= bit(d.k); + } + for (p.panes) |s| if (s) |pane| if (pane.pv_known & bit(d.k) == 0) { + pane.pv[d.k] = fresh(pane); + pane.pv_known |= bit(d.k); + }; + } +} + +/// The live view alone, as view 0: every other closed. A session loaded +/// for one client (a Restore, `-l`) is one view. +pub fn only(p: *Pardes) void { + p.views_away = 0; + for (0..max) |k| if (k != p.view) close(p, @intCast(k)); + emptied(p, p.view); + const k = p.view; + if (k == 0) return; + std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]); + for (p.panes) |s| if (s) |pane| { + // The live slot is stale: as `installed` makes it, owning nothing. + pane.pv[0] = fresh(pane); + pane.pv_known = if (pane.pv_known & bit(k) != 0) 1 else 0; + if (pane.sized_by == k) pane.sized_by = 0; + if (pane.prompt_view == k) pane.prompt_view = 0; + }; + p.view = 0; + p.views_open = 1; +} + +/// The fields of `T` named, as a struct of their own, each of its type and +/// default. +fn Part(comptime T: type, comptime names: []const [:0]const u8) type { + @setEvalBranchQuota(100_000); + const s = @typeInfo(T).@"struct"; + var types: [names.len]type = undefined; + var attrs: [names.len]std.lang.Type.Struct.FieldAttributes = undefined; + for (names, 0..) |name, i| { + const at = for (s.field_names, 0..) |n, j| { + if (std.mem.eql(u8, n, name)) break j; + } else @compileError("views: " ++ @typeName(T) ++ " has no field " ++ name); + types[i] = s.field_types[at]; + attrs[i] = .{ .default_value_ptr = s.field_attrs[at].default_value_ptr }; + } + return @Struct(.auto, null, names, &types, &attrs); +} + +fn copy(comptime names: []const [:0]const u8, dst: anytype, src: anytype) void { + inline for (names) |name| @field(dst, name) = @field(src, name); +} + +pub fn isOpen(p: *const Pardes, k: u8) bool { + return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; +} + +/// Kept for its client's rejoin (`away`). +pub fn isAway(p: *const Pardes, k: u8) bool { + return k < max and p.views_away & (@as(u8, 1) << @intCast(k)) != 0; +} + +/// The views someone looks at: open, none kept for a rejoin. +pub fn count(p: *const Pardes) usize { + return @popCount(p.views_open & ~p.views_away); +} + +/// An open view but `k` someone looks at (`count`), if there is one. +fn looked(p: *const Pardes, k: u8) ?u8 { + for (0..max) |i| if (i != k and isOpen(p, @intCast(i)) and !isAway(p, @intCast(i))) return @intCast(i); + return null; +} + +/// More than one view: the one test the single client's path pays. +pub fn several(p: *const Pardes) bool { + return p.views_open & (p.views_open -% 1) != 0; +} + +/// Pane `pane`'s share of the live view. +fn extract(pane: *const Pane) PaneView { + var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; + copy(&pane_fields, &v, pane); + copy(&body_fields, &v.body, &pane.body); + if (pane.file) |f| v.scroll = f.scroll; + return v; +} + +/// Its share of view `v` put back. +fn apply(pane: *Pane, v: *const PaneView) void { + copy(&pane_fields, pane, v); + copy(&body_fields, &pane.body, &v.body); + if (!pane.isTerminal()) { + pane.cols = v.cols; + pane.rows = v.rows; + } + const f = if (pane.file) |*file| file else return; + // The text may have changed while the view was out: what it held is + // brought back inside it. + const lines = File.nlines(pane.gpa, f); + f.scroll = @min(v.scroll, lines -| 1); + if (pane.body.cur_row >= lines) { + pane.body.vsel.active = false; + pane.body.msel.active = false; + pane.body.nsel = 0; + } + _ = pardes.edit.clampFileCursor(&pane.body); +} + +/// A copy of the live view's share for another view, owning nothing of the +/// live one's (its extra selections stay behind). +fn fresh(pane: *const Pane) PaneView { + var v = extract(pane); + v.body.sels = &.{}; + v.body.nsel = 0; + return v; +} + +/// A new pane is in each other view as it was made, there to be shown. +pub fn installed(p: *Pardes, pane: *Pane) void { + pane.sized_by = p.view; + if (!several(p)) return; + // The live view's slot too, owning nothing: a pane closed before its + // view next parks is torn down with that slot never written, and + // `tornDown` frees what the slots of the views not live hold. + for (0..max) |k| if (isOpen(p, @intCast(k))) { + pane.pv[k] = fresh(pane); + }; + pane.pv_known = p.views_open; +} + +/// A pane going: the other views' selections of it go too. +pub fn tornDown(p: *Pardes, pane: *Pane) void { + for (0..max) |k| if (k != p.view and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { + const sels = pane.pv[k].body.sels; + if (sels.len > 0) p.gpa.free(sels); + pane.pv[k].body.sels = &.{}; + }; +} + +fn park(p: *Pardes) void { + const slot = p.parked[p.view].?; + copy(&fields, &slot.view, p); + for (p.panes, 0..) |s, id| { + const pane = s orelse { + slot.serials[id] = 0; + continue; + }; + slot.serials[id] = pane.serial; + pane.pv[p.view] = extract(pane); + } +} + +fn unpark(p: *Pardes, k: u8) void { + const slot = p.parked[k].?; + copy(&fields, p, &slot.view); + p.view = k; + for (p.panes) |s| if (s) |pane| { + apply(pane, &pane.pv[k]); + // The live body owns the selections now; the slot, stale while its + // view is live, holds none (a pane closed before the view parks + // again would otherwise free them twice). + pane.pv[k].body.sels = &.{}; + pane.pv[k].body.nsel = 0; + }; + // The panes closed while it was out (a slot empty or another pane's + // now) leave its layout, and its keyboard leaves them. + for (0..p.ncol) |c| { + var n: usize = 0; + for (p.col_panes[c][0..p.col_n[c]]) |id| { + const pane = p.panes[id] orelse continue; + if (pane.serial != slot.serials[id]) continue; + p.col_panes[c][n] = id; + n += 1; + } + p.col_n[c] = n; + } + if (layout.findPane(p, p.active) == null) { + for (0..p.ncol) |c| if (p.col_n[c] > 0) { + p.active = p.col_panes[c][0]; + break; + }; + } + layout.compute(p); + // Its terminals' cursors moved while it was out: each pan follows. + for (p.panes, 0..) |s, id| if (s) |pane| { + pane.pan = pardes.panes.terminal.panFor(p, pane, p.rects[id]); + }; +} + +/// The pane with the keyboard is one the live view shows: focused where its +/// layout has it not (a Look at a file another view opened, a jump, a 9P +/// focus), it goes into the view's active column, as kakoune shows a buffer +/// in the window that asked for it. +pub fn show(p: *Pardes) void { + if (p.panes[p.active] == null or layout.findPane(p, p.active) != null) return; + const c = layout.firstColumn(p) orelse layout.columnBySerial(p, p.active_column) orelse p.ncol - 1; + layout.acmePlace(p, c, null, p.active); +} + +/// Into view `k`: the live one parked, `k`'s put in its place. +pub fn to(p: *Pardes, k: u8) void { + std.debug.assert(isOpen(p, k)); + if (k == p.view) return; + park(p); + unpark(p, k); + emptied(p, k); +} + +/// View `k` is live: its slot, stale until it parks again (written whole +/// then), keeps no pointer to what it handed the live fields. A rollback +/// copy walks every slot (src/rollback.zig): one left there, freed by the +/// live view and the memory another's since, would be saved and its old +/// bytes put back over the other's. (A pane's share holds no pointer but +/// its selections, which `unpark` hands over.) +fn emptied(p: *Pardes, k: u8) void { + const slot = p.parked[k] orelse return; + slot.view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h }; + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); +} + +/// A new view, as the live one is laid out and focused, with its panes +/// where they are scrolled and its cursors where they are; the rest of it +/// (column tags, gestures, animations) as a session starts. Null when there +/// are `max` already. +pub fn open(p: *Pardes) ?u8 { + const k: u8 = for (0..max) |i| { + if (!isOpen(p, @intCast(i))) break @intCast(i); + } else return null; + // Each live view parks too: its slot is made with the first other. + for ([_]u8{ p.view, k }) |i| if (p.parked[i] == null) { + const slot = p.gpa.create(Parked) catch return null; + slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; + p.parked[i] = slot; + }; + const slot = p.parked[k].?; + slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } }; + inline for (sized ++ laid_out ++ .{ "windowed", "native_images", "native_image_patches" }) |name| + @field(slot.view, name) = @field(p, name); + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); + for (p.panes, 0..) |s, id| { + const pane = s orelse continue; + slot.serials[id] = pane.serial; + pane.pv[k] = fresh(pane); + pane.pv_known |= @as(u8, 1) << @intCast(k); + } + p.views_open |= @as(u8, 1) << @intCast(k); + return k; +} + +/// How a view is laid out and focused: what a new one takes of the live +/// one (`open`), and what one going leaves the view that stays (`closeKeeping`). +const laid_out = .{ "ncol", "col_weight", "col_panes", "col_n", "col_serial", "finder", "active", "active_column", "jumps", "njumps", "jcur" }; +const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; + +/// View `k` gone, and the view that stays live laid out as `k` was: its +/// columns, its focus, its panes where they were scrolled and its cursors +/// where they were. For a session's last client while no one else watches +/// it (replica.Peers.leave): the client's rejoin (an Upgrade's, a diverged +/// one's) copies the live view (`open`), so it finds its own again. +pub fn closeKeeping(p: *Pardes, k: u8) void { + if (!isOpen(p, k) or count(p) == 1) return; + to(p, k); + // Never into one kept for a rejoin (`away`): that one is its client's. + const other = looked(p, k).?; + const slot = p.parked[other].?; + // Its size too: the terminals it sized keep theirs, no shell redrawn for + // a size it is given back at the rejoin. + inline for (sized ++ laid_out) |name| @field(slot.view, name) = @field(p, name); + for (p.panes, 0..) |s, id| { + const pane = s orelse { + slot.serials[id] = 0; + continue; + }; + if (pane.pv_known & (@as(u8, 1) << @intCast(other)) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); + slot.serials[id] = pane.serial; + pane.pv[other] = fresh(pane); + pane.pv_known |= @as(u8, 1) << @intCast(other); + } + close(p, k); +} + +/// View `k` gone, and all it held; the live one moves to another first. +/// The last view someone looks at stays: a session without a client keeps +/// its own. One kept for a rejoin (`away`) goes whenever it is closed. +pub fn close(p: *Pardes, k: u8) void { + if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; + if (k == p.view) { + const other: u8 = looked(p, k) orelse for (0..max) |i| { + if (i != k and isOpen(p, @intCast(i))) break @intCast(i); + } else unreachable; + park(p); + unpark(p, other); + emptied(p, other); + } + const v = &p.parked[k].?.view; + for (&v.col_tags) |*t| t.deinit(p.gpa); + if (v.chord_arg) |a| p.gpa.free(a); + v.presentation.deinit(p.gpa); + for (p.panes) |s| if (s) |pane| { + const sels = pane.pv[k].body.sels; + if (sels.len > 0) p.gpa.free(sels); + pane.pv[k].body.sels = &.{}; + pane.pv_known &= ~(@as(u8, 1) << @intCast(k)); + // A name it was drafting goes with it. + if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); + }; + p.views_open &= ~(@as(u8, 1) << @intCast(k)); + p.views_away &= ~(@as(u8, 1) << @intCast(k)); +} + +/// View `k`'s client gone a moment, to join again for it (an Upgrade's +/// rejoin, a divergence's): the view kept as it is, layout, focus and the +/// panes opened in it, until its client is back (Pardes.rejoinView) or a +/// join wants its place (Pardes.openView). Dumped with the rest, so an +/// Upgrade's new build has it too. The live view is never one: the session +/// goes into another someone looks at, or into a copy of this one of its own. +pub fn away(p: *Pardes, k: u8) void { + if (!isOpen(p, k)) return; + if (k == p.view) if (looked(p, k) orelse open(p)) |other| to(p, other); + p.views_away |= bit(k); +} + +/// The session's end: what each view not live holds. +pub fn deinit(p: *Pardes) void { + for (0..max) |i| { + const k: u8 = @intCast(i); + const slot = p.parked[k] orelse continue; + if (k != p.view and isOpen(p, k)) { + const v = &slot.view; + for (&v.col_tags) |*t| t.deinit(p.gpa); + if (v.chord_arg) |a| p.gpa.free(a); + v.presentation.deinit(p.gpa); + } + p.gpa.destroy(slot); + } +} + +/// What drawing a view keeps of its own (its animations, its panels as +/// presented), kept from a frame drawn while it was out. +const drawn_fields = [_][:0]const u8{ "presentation", "lifts", "lift_serials", "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "shell_continuous" }; + +/// Each view but the live one drawn and presented, the live one put back +/// after exactly as it was: no digested field moves, so a replica that +/// draws nothing (and skips this) stays the same. A view drawn keeps only +/// what its drawing made (`drawn_fields`); whatever its unpark brought +/// inside a changed text is not kept, and is done again when it is next +/// live. Before the live one's frame: the surface after the pump is its. +pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { + const live = p.view; + const v = h.vtable; + park(p); + defer { + copy(&fields, p, &p.parked[live].?.view); + emptied(p, live); + p.view = live; + for (p.panes) |s| if (s) |pane| { + const pv = &pane.pv[live]; + copy(&pane_fields, pane, pv); + copy(&body_fields, &pane.body, &pv.body); + if (!pane.isTerminal()) { + pane.cols = pv.cols; + pane.rows = pv.rows; + } + if (pane.file) |*f| f.scroll = pv.scroll; + }; + } + for (0..max) |i| { + const k: u8 = @intCast(i); + if (k == live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(k) != 0) continue; + // No post_present: what a shell acknowledges there moves the + // session's pointer and hover, which are the live view's to move; + // a view's panels are acknowledged when it is live and drawn. + try drawParked(p, k, .{ .ctx = h.ctx, .f = v.present }); + } +} + +pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) void }; + +/// Parked view `k` unparked, drawn and presented. `unpark` hands each +/// slot's selections to its body; drawn, the view is out again, and they +/// are the slot's again (else a frame drawn would take them from a view, +/// and a replica that draws nothing would keep them). +fn drawParked(p: *Pardes, k: u8, present: Present) !void { + 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 }; + }; + unpark(p, k); + defer for (p.panes, 0..) |s, id| if (s) |pane| { + pane.pv[k].body.sels = sels[id][0]; + pane.pv[k].body.nsel = sels[id][1]; + }; + try f(ctx, p); +} + +/// View `k` drawn and presented outside any step, by a replica client +/// (src/replica.zig) whose own it is: as `drawOthers` draws one, the live +/// view put back after exactly as it was, so the replica's digest is the +/// authority's still. No post_present, as there. +pub fn drawOne(p: *Pardes, k: u8, present: Present) !void { + if (k == p.view or !isOpen(p, k)) 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 { + copy(&fields, p, &p.parked[live].?.view); + // Its slot and its panes' shares, stale while it is live, keep no + // pointer to what they handed back, as `unpark` leaves them: a + // replica's rollback copy walks them (`emptied`). + emptied(p, live); + p.view = live; + for (p.panes) |s| if (s) |pane| { + const pv = &pane.pv[live]; + copy(&pane_fields, pane, pv); + copy(&body_fields, &pane.body, &pv.body); + if (!pane.isTerminal()) { + pane.cols = pv.cols; + pane.rows = pv.rows; + } + if (pane.file) |*file| file.scroll = pv.scroll; + pv.body.sels = &.{}; + pv.body.nsel = 0; + }; + } + try f(ctx, p, k); +} + +// ---- tests ---- + +const testing = std.testing; +const Key = pardes.Key; + +fn typeKeys(p: *Pardes, text: []const u8) void { + for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); +} + +fn escape(p: *Pardes) void { + p.update(.{ .key = .{ .cp = Key.escape } }); +} + +fn numbered(gpa: std.mem.Allocator, n: usize) ![]u8 { + var out: std.ArrayList(u8) = .empty; + for (1..n + 1) |i| try out.print(gpa, "line {d}\n", .{i}); + return out.toOwnedSlice(gpa); +} + +/// The cursor put on `row`, as a jump puts it, and the step after. +fn cursorTo(p: *Pardes, pane: *Pane, row: i32) void { + pane.body.vsel.active = false; + pane.body.nsel = 0; + pane.body.cur_row = row; + pane.body.cur_col = 0; + pane.body.cur_pinned = true; + pane.ensureCursorVisible(); + p.sync(); +} + +test "each view has its own cursor and scroll, and an edit in one is in the other" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 200); + defer gpa.free(text); + const pane = try p.setTestFile(text); + try testing.expectEqual(@as(?u8, 1), p.openView()); + cursorTo(p, pane, 119); + const scrolled = pane.file.?.scroll; + try testing.expect(scrolled > 0); + // The other view is where the session was when it came. + p.toView(1); + try testing.expectEqual(@as(i32, 0), pane.body.cur_row); + try testing.expectEqual(@as(usize, 0), pane.file.?.scroll); + typeKeys(p, "ix"); + escape(p); + try testing.expectEqual(Text.Mode.normal, pane.body.mode); + p.toView(0); + try testing.expectEqual(@as(i32, 119), pane.body.cur_row); + try testing.expectEqual(scrolled, pane.file.?.scroll); + try testing.expect(std.mem.startsWith(u8, pane.file.?.content, "xline 1\n")); + // Its mode is its own: insert here leaves the other in normal. + typeKeys(p, "i"); + try testing.expectEqual(Text.Mode.insert, pane.body.mode); + p.toView(1); + try testing.expectEqual(Text.Mode.normal, pane.body.mode); +} + +test "each view has its own layout and focus over the same panes" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + const k = p.openView().?; + const ncol = p.ncol; + p.toView(k); + p.update(.{ .command = "Newcol" }); + p.update(.{ .command = "New" }); + const made = p.active; + try testing.expect(made != 0); + try testing.expectEqual(ncol + 1, p.ncol); + p.toView(0); + try testing.expectEqual(ncol, p.ncol); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expect(layout.findPane(p, made) == null); + try testing.expectEqual(pardes.Rect{}, p.rects[made]); + // The pane is the session's: focused here, this view shows it too. + p.active = made; + p.sync(); + try testing.expect(layout.findPane(p, made) != null); + try testing.expect(p.rects[made].h > 0); + // Closed here, it leaves the other view's layout, and its keyboard. + try p.removePane(made, null); + p.sync(); + p.toView(k); + try testing.expect(layout.findPane(p, made) == null); + try testing.expect(p.panes[p.active] != null); + try testing.expect(layout.findPane(p, p.active) != null); +} + +test "registers are the session's: yanked in one view, put in another" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha\nbeta\n"); + const k = p.openView().?; + typeKeys(p, "xy"); + p.toView(k); + cursorTo(p, pane, 1); + typeKeys(p, "p"); + try testing.expectEqualStrings("alpha\nbeta\nalpha\n", pane.file.?.content); +} + +test "a terminal shown at two sizes takes the size of the view that typed into it last" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const term = p.panes[p.active].?; + try testing.expect(term.isTerminal()); + const small = .{ term.cols, term.rows }; + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); + // Shown bigger here, it keeps the size of the view that typed last. + try testing.expect(p.rects[p.active].w > 80); + try testing.expectEqual(small, .{ term.cols, term.rows }); + p.update(.{ .key = .{ .cp = Key.right } }); + try testing.expect(term.cols > small[0] and term.rows > small[1]); + const big = .{ term.cols, term.rows }; + p.toView(0); + p.sync(); + try testing.expectEqual(big, .{ term.cols, term.rows }); + p.update(.{ .key = .{ .cp = Key.right } }); + try testing.expectEqual(small, .{ term.cols, term.rows }); + // That view gone, the one left sizes it. + p.toView(k); + p.closeView(0); + try testing.expectEqual(k, p.view); + try testing.expectEqual(big, .{ term.cols, term.rows }); +} + +test "a view closing leaves the others as they were" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + const text = try numbered(gpa, 100); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const a = p.openView().?; + const b = p.openView().?; + try testing.expectEqual(@as(usize, 3), count(p)); + p.toView(b); + p.update(.{ .command = "Newcol" }); + cursorTo(p, pane, 40); + typeKeys(p, "xy"); + p.toView(a); + cursorTo(p, pane, 70); + p.toView(0); + cursorTo(p, pane, 10); + const ncol = p.ncol; + // The live one closing: the session goes into another. + p.toView(b); + p.closeView(b); + try testing.expect(p.view != b); + try testing.expectEqual(@as(usize, 2), count(p)); + p.toView(a); + try testing.expectEqual(@as(i32, 70), pane.body.cur_row); + p.closeView(0); + try testing.expectEqual(a, p.view); + try testing.expectEqual(ncol, p.ncol); + // The last stays. + p.closeView(a); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expectEqual(@as(i32, 70), pane.body.cur_row); +} + +test "the last client's view going leaves the session its layout, which a rejoin copies" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + const text = try numbered(gpa, 100); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); + p.update(.{ .command = "Newcol" }); + p.update(.{ .command = "New" }); + const made = p.active; + const ncol = p.ncol; + p.active = 0; + p.sync(); + cursorTo(p, pane, 60); + p.leaveView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); + try testing.expectEqual(@as(u16, 120), p.screen_w); + // Its client back (a view copied from the live one): all of it again. + const again = p.openView().?; + p.toView(again); + try testing.expectEqual(ncol, p.ncol); + try testing.expect(layout.findPane(p, made) != null); + try testing.expectEqual(@as(usize, 0), p.active); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); +} + +test "a client's view kept for its rejoin: through an Upgrade's dump, each client back in its own; a join with no room lets one go" { + const gpa = testing.allocator; + const opts: pardes.Options = .{ .tty_only = true, .cols = 100, .rows = 30 }; + const p = try Pardes.init(gpa, opts); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + // Two clients, each a pane of its own in its own view. + const ks: [2]u8 = .{ p.openView().?, p.openView().? }; + var made: [2]usize = undefined; + for (ks, &made) |k, *m| { + p.toView(k); + p.update(.{ .command = "New" }); + m.* = p.active; + } + // Both let go to upgrade, the live view one of theirs: theirs are kept, + // and the session's own is live. + for (ks) |k| p.awayView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expect(!isAway(p, p.view)); + // The new build loads the dump; each client is back in its own view. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const q = try pardes.dump.load(gpa, opts, bytes, null); + defer q.deinit(); + for (ks, made, 0..) |k, m, i| { + try testing.expect(isAway(q, k)); + try testing.expect(q.rejoinView(k)); + q.toView(k); + try testing.expectEqual(m, q.active); + try testing.expect(layout.findPane(q, m) != null); + try testing.expect(layout.findPane(q, made[1 - i]) == null); + } + try testing.expect(!q.rejoinView(ks[0])); + // A client that never comes back: its view goes when a join has no room. + p.toView(0); + try testing.expectEqual(@as(?u8, 3), p.openView()); + try testing.expectEqual(ks[0], p.openView()); + try testing.expect(!isAway(p, ks[0]) and isAway(p, ks[1])); +} + +test "drawing the other views leaves the live one as it was, to the digest" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 100); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + cursorTo(p, pane, 60); + typeKeys(p, "dd"); + p.toView(0); + cursorTo(p, pane, 95); + const before = pardes.walk.digest(Pardes, p); + try drawOthers(p, .{}); + try testing.expectEqual(@as(u8, 0), p.view); + try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); + p.toView(k); + try testing.expectEqual(@as(i32, 60), pane.body.cur_row); +} + +test "a view drawn while another is live keeps its own cursors" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile("ab\ncd\nef\n"); + const k = p.openView().?; + p.toView(k); + p.update(.{ .key = .{ .cp = 'C', .text = "C" } }); + try testing.expectEqual(@as(u16, 1), pane.body.nsel); + p.toView(0); + const before = pardes.walk.digest(Pardes, p); + try drawOthers(p, .{}); + try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); + p.toView(k); + try testing.expectEqual(@as(u16, 1), pane.body.nsel); +} + +test "a view not live keeps its place in a text another view edits" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 200); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const k = p.openView().?; + p.toView(k); + cursorTo(p, pane, 100); + const scrolled = pane.file.?.scroll; + p.toView(0); + // Three lines put above it, one taken below it. + typeKeys(p, "xyppp"); + cursorTo(p, pane, 150); + typeKeys(p, "xd"); + p.toView(k); + try testing.expectEqual(@as(i32, 103), pane.body.cur_row); + try testing.expectEqual(scrolled + 3, pane.file.?.scroll); + try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101")); +} + +test "a terminal shown in a view shorter than it keeps its cursor in sight" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 20 }); + defer p.deinit(); + const id = p.active; + const term = p.panes[id].?; + const k = p.openView().?; + p.toView(k); + p.update(.{ .resize = .{ .cols = 100, .rows = 50 } }); + p.update(.{ .key = .{ .cp = Key.right } }); + const rows = term.rows; + try testing.expect(rows > 30); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); + for (1..rows) |i| try out.print(gpa, "row-{d}\r\n", .{i}); + try out.appendSlice(gpa, "LAST$ "); + p.update(.{ .output = .{ .pane = @intCast(id), .bytes = out.items } }); + // Sized for the tall view, it fits there whole: nothing is passed over. + try testing.expectEqual(@as(u16, 0), term.pan); + p.toView(0); + p.sync(); + try testing.expectEqual(rows, term.rows); + try testing.expect(term.pan > 0); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const r = p.rects[id]; + // The cursor's row, the last, is on screen in the short view, inside + // the pane, and so is the line it is on. + const cursor = s.cursor.?; + try testing.expect(cursor.y >= r.y and cursor.y < r.y + r.h); + var row_text: std.ArrayList(u8) = .empty; + defer row_text.deinit(gpa); + for (0..s.cols) |x| try row_text.appendSlice(gpa, s.cells[@as(usize, cursor.y) * s.cols + x].grapheme()); + try testing.expect(std.mem.indexOf(u8, row_text.items, "LAST$") != null); + // Back in the tall view, its top is where it was. + p.toView(k); + try testing.expectEqual(@as(u16, 0), term.pan); +} + +test "a session's dump keeps its views: loaded, each where it was; revived for one client, the live one alone" { + const gpa = testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = try numbered(gpa, 200); + defer gpa.free(text); + const pane = try p.setTestFile(text); + const id = for (p.panes, 0..) |s, i| { + if (s == pane) break i; + } else unreachable; + const k = p.openView().?; + cursorTo(p, pane, 119); + p.toView(k); + p.update(.{ .command = "Newcol" }); + p.toView(0); + p.toView(k); + cursorTo(p, pane, 30); + const ncol = p.ncol; + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); + defer loaded.deinit(); + try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); + const again = try pardes.dump.dumpBytes(loaded); + defer gpa.free(again); + try testing.expectEqualSlices(u8, bytes, again); + const lp = loaded.panes[id].?; + loaded.toView(0); + try testing.expectEqual(@as(i32, 119), lp.body.cur_row); + try testing.expectEqual(ncol - 1, loaded.ncol); + loaded.toView(k); + try testing.expectEqual(@as(i32, 30), lp.body.cur_row); + try testing.expectEqual(ncol, loaded.ncol); + // For one client: the view live when dumped, as view 0. + const one = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes); + defer one.deinit(); + try testing.expectEqual(@as(usize, 1), count(one)); + try testing.expectEqual(@as(u8, 0), one.view); + try testing.expectEqual(ncol, one.ncol); + try testing.expectEqual(@as(i32, 30), one.panes[id].?.body.cur_row); + try testing.expectEqual(@as(?u8, 1), one.openView()); +} diff --git a/src/walk.zig b/src/walk.zig new file mode 100644 index 00000000..14e1929e --- /dev/null +++ b/src/walk.zig @@ -0,0 +1,1975 @@ +//! The reflective walk over the core's state: one comptime `@typeInfo` walk +//! that hashes a session into a digest (the replay harness compares two), +//! encodes values for the tape, and dumps a session for another build to +//! load by name (`Dump`, `Load`: Restore, -l, the store, a handover, and +//! the join of phase 5). The rollback copy is to walk the same way +//! (docs/next-daemon.md). +//! +//! Values, never addresses: a pointer is followed to what it points at, a +//! slice is its elements, an ArrayList its items, a hash map its entries in +//! no order. A type the walk cannot see into (an opaque handle, a function, +//! an untagged union, a many-pointer) is a compile error naming it, until +//! the struct that holds it says what the field is: +//! +//! pub const replica = .{ +//! // This process's own: presentation, timing, the host's handles +//! // and caches. A replica has its own and never compares them. +//! .local = .{ "frames", "surface" }, +//! // Recomputed from the rest whenever it is wanted. +//! .derived = .{ "wrap_line" }, +//! // Held by the session that performs effects alone. +//! .authority = .{ "host" }, +//! // A field written only as far as another says: hashed that far. +//! .len = .{ .{ "jumps", "njumps" } }, +//! // What a field holds is made with another allocator on a load. +//! .alloc = .{ .{ "image", "image" } }, +//! // Immutable blobs: replaced whole, never written, freed raw +//! // (memory.freeRaw, freeUnfilled). A digest hashes each once and +//! // folds that word in; a replica's rollback copy pins them. +//! .blob = .{ "content" }, +//! }; +//! +//! None of the three kinds is in a digest or a dump. A type with a `pub fn +//! digest(*const T, *Hasher) void` of its own hashes itself; one with +//! `dumpWrite`, `dumpRead` (and `dumpFree`) dumps itself as bytes. +const std = @import("std"); + +pub const Hasher = std.hash.Wyhash; +const builtin = @import("builtin"); +/// Only a Debug build hashes a blob's bytes at every digest and checks its +/// kept word; a release one, ReleaseSafe the user's own, takes the word. +const rehash = builtin.mode == .debug; + +/// Whether `T.name` holds immutable blobs (`.blob`): a file's text, an undo +/// snapshot, a register's text, a file's location rows. +pub fn blob(comptime T: type, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "blob")) return false; + inline for (T.replica.blob) |n| if (comptime std.mem.eql(u8, n, name)) return true; + return false; +} + +// ---- a blob's hash, made once ---- +// +// A digest walks the whole core at every unit a replica is sent (and every +// unit a replica replays): a 100 MB file was hashed in full each time. A +// blob of `blob_min` bytes or more is hashed once instead, its word kept by +// its address, and the digest folds the word in. The word goes when the +// blob is freed (`forget`, from memory.freeRaw and freeUnfilled: every +// such blob is freed raw, for the rollback copy's pins), so an address +// used again is hashed again. A Debug build hashes the bytes all the same, +// folds those in, and stops where a kept word is not their hash: a blob +// written in place (which the pins forbid too), or freed past `forget`. A +// release build never stops there: a stale word shows as a divergence. + +const blob_min = 4096; +var blob_words: std.AutoHashMapUnmanaged(usize, struct { len: usize, word: u64 }) = .empty; +var blob_lock: std.atomic.Value(bool) = .init(false); +var blob_count: std.atomic.Value(usize) = .init(0); +threadlocal var in_blob: bool = false; +/// Off: every blob hashed whole at every digest, as before (the bench's). +pub var blob_words_on = true; + +fn lockBlobs() void { + while (blob_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); +} + +fn unlockBlobs() void { + blob_lock.store(false, .release); +} + +/// The blob at `addr` is freed: its word, if it has one, goes with it. +pub fn forget(addr: usize) void { + if (blob_count.load(.acquire) == 0) return; + lockBlobs(); + defer unlockBlobs(); + if (blob_words.remove(addr)) blob_count.store(blob_words.count(), .release); +} + +/// The word kept for the blob at `addr` (a test's). +pub fn wordOf(addr: usize) ?u64 { + lockBlobs(); + defer unlockBlobs(); + return if (blob_words.get(addr)) |b| b.word else null; +} + +/// Every word forgotten (the bench's cold digests). +pub fn forgetAll() void { + lockBlobs(); + defer unlockBlobs(); + blob_words.clearRetainingCapacity(); + blob_count.store(0, .release); +} + +/// A blob as a digest takes it: its length and its word. +fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { + const addr = @intFromPtr(items.ptr); + const word: u64 = word: { + const kept: ?u64 = if (blob_words_on) kept: { + lockBlobs(); + defer unlockBlobs(); + const b = blob_words.get(addr) orelse break :kept null; + break :kept if (b.len == items.len) b.word else null; + } else null; + // The lists are the C heap's (a replica client's, a session's join), + // which a build with no libc (the web) has neither of. + if (comptime builtin.link_libc) if (blobs_out) |list| { + list.append(std.heap.c_allocator, std.mem.sliceAsBytes(items)) catch {}; + break :word 0; + }; + if (!rehash) if (kept) |w| break :word w; + if (comptime builtin.link_libc) if (E == u8) if (unhashed_out) |list| if (kept == null) { + list.append(std.heap.c_allocator, items) catch {}; + break :word 0; + }; + in_blob = false; + defer in_blob = true; + var one = Hasher.init(0); + hashSlice(E, items, &one); + const w = one.final(); + if (kept) |k| if (k != w) std.debug.panic("walk: the {d}-byte blob at 0x{x} is not what its kept word was: written in place, or freed without memory.freeRaw", .{ items.len, addr }); + if (blob_words_on and kept == null) { + lockBlobs(); + defer unlockBlobs(); + blob_words.put(std.heap.page_allocator, addr, .{ .len = items.len, .word = w }) catch {}; + blob_count.store(blob_words.count(), .release); + } + break :word w; + }; + h.update(std.mem.asBytes(&items.len)); + h.update(std.mem.asBytes(&word)); +} + +/// Where `unhashed` notes the texts a digest would hash for want of a word. +threadlocal var unhashed_out: ?*std.ArrayListUnmanaged([]const u8) = null; +/// Where `blobs` notes every blob, hashing none. +threadlocal var blobs_out: ?*std.ArrayListUnmanaged([]const u8) = null; + +/// Every blob of `v` a digest would take as one (`blob_min` bytes and up), +/// as its bytes, onto `out` (the C heap's), hashed by none: a replica +/// client's, which knows its blobs by content (src/replica.zig `Have`). +pub fn blobs(comptime T: type, v: *const T, out: *std.ArrayListUnmanaged([]const u8)) void { + blobs_out = out; + defer blobs_out = null; + _ = digest(T, v); +} + +/// The blob texts a digest of `v` would hash now, having no word yet, onto +/// `out` (the C heap's): a walk with no blob hashed, which `makeWords` then +/// makes on another thread. A replica's join (src/replica.zig) does that +/// first: the session's step never hashes a text it never digested before. +pub fn unhashed(comptime T: type, v: *const T, out: *std.ArrayListUnmanaged([]const u8)) void { + unhashed_out = out; + defer unhashed_out = null; + _ = digest(T, v); +} + +/// The words of `texts` (`unhashed`'s), made on any thread: they must stay +/// as they are meanwhile (pinned, memory.zig), and the next digest takes +/// them. +pub fn makeWords(texts: []const []const u8) void { + for (texts) |items| { + // As hashBlob makes it (hashSlice's length, then the bytes). + var one = Hasher.init(0); + one.update(std.mem.asBytes(&items.len)); + one.update(items); + const w = one.final(); + lockBlobs(); + defer unlockBlobs(); + const got = blob_words.getOrPut(std.heap.page_allocator, @intFromPtr(items.ptr)) catch continue; + if (!got.found_existing) got.value_ptr.* = .{ .len = items.len, .word = w }; + blob_count.store(blob_words.count(), .release); + } +} + +pub fn digest(comptime T: type, v: *const T) u64 { + var h = Hasher.init(0); + hash(T, v, &h); + return h.final(); +} + +/// A digest of each field `hash` would walk, in order (what a divergence +/// is narrowed down by); `names` are theirs. +pub fn fieldDigests(comptime T: type, v: *const T, out: []u64) []u64 { + var n: usize = 0; + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or comptime excluded(T, name)) continue; + var h = Hasher.init(0); + const was = in_blob; + in_blob = comptime blob(T, name); + defer in_blob = was; + if (comptime lengthOf(T, name)) |len| { + hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], &h); + } else hash(F, &@field(v, name), &h); + out[n] = h.final(); + n += 1; + } + return out[0..n]; +} + +pub fn fieldNames(comptime T: type) []const []const u8 { + comptime { + var names: []const []const u8 = &.{}; + const s = @typeInfo(T).@"struct"; + for (s.field_names, s.field_attrs) |name, attr| { + if (attr.@"comptime" or excluded(T, name)) continue; + names = names ++ .{name}; + } + return names; + } +} + +/// Types that are machinery, never state: an allocator, the Io, a lock. +pub fn machinery(comptime T: type) bool { + inline for (.{ std.mem.Allocator, std.Io, std.Io.Mutex, std.Io.Condition, std.Thread, std.heap.ArenaAllocator }) |M| if (T == M) return true; + return false; +} + +/// Bytes that are the value whole: no padding, no pointer. +fn flat(comptime T: type) bool { + return switch (@typeInfo(T)) { + .int => @sizeOf(T) * 8 == @bitSizeOf(T), + .bool => true, + .@"enum" => |e| @sizeOf(T) * 8 == @bitSizeOf(e.tag_type), + else => false, + }; +} + +/// A field the walk cannot follow: a pointer to an opaque thing or a +/// function, or a many-pointer, bare or optional (the struct holding it is +/// named in the error, not the walk's depth). +fn opaqueTo(comptime F: type) bool { + return switch (@typeInfo(F)) { + .optional => |o| opaqueTo(o.child), + .pointer => |p| p.size == .many or p.size == .c or switch (@typeInfo(p.child)) { + .@"opaque", .@"fn" => true, + else => false, + }, + else => false, + }; +} + +pub fn excluded(comptime T: type, comptime name: []const u8) bool { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica")) return false; + inline for (.{ "local", "derived", "authority" }) |kind| { + if (@hasField(@TypeOf(T.replica), kind)) inline for (@field(T.replica, kind)) |n| { + if (comptime std.mem.eql(u8, n, name)) return true; + }; + } + return false; +} + +/// How far the field `len` names says its array is written (null: none). +/// What a `len`-cut field holds: a fixed array's, or a slice's room (a +/// selection's ranges, made once and used `nsel` deep). +fn Element(comptime F: type) type { + return switch (@typeInfo(F)) { + .array => |a| a.child, + .pointer => |p| p.child, + else => @compileError("walk: a `len` cuts an array or a slice, not " ++ @typeName(F)), + }; +} + +pub fn lengthIn(v: anytype, comptime len: []const u8) usize { + const n = @field(v, len); + return if (@typeInfo(@TypeOf(n)) == .optional) (if (n) |k| k else 0) else n; +} + +pub fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 { + @setEvalBranchQuota(100_000); + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "len")) return null; + inline for (T.replica.len) |pair| if (comptime std.mem.eql(u8, pair[0], name)) return pair[1]; + return null; +} + +/// Every name an annotation gives is a field: a rename cannot quietly put +/// a field back in the digest. +fn checkAnnotations(comptime T: type) void { + if (!@hasDecl(T, "replica")) return; + inline for (@typeInfo(@TypeOf(T.replica)).@"struct".field_names) |kind| { + inline for (@field(T.replica, kind)) |entry| { + const name = if (comptime std.mem.eql(u8, kind, "len") or std.mem.eql(u8, kind, "alloc")) entry[0] else entry; + if (!@hasField(T, name)) @compileError("walk: " ++ @typeName(T) ++ " annotates no field " ++ name); + } + } +} + +pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { + switch (@typeInfo(T)) { + .void, .null, .undefined => {}, + .bool => h.update(&.{@intFromBool(v.*)}), + .int => hashInt(T, v.*, h), + .float => h.update(std.mem.asBytes(v)), + .@"enum" => hashEnum(T, v.*, h), + .optional => |o| if (v.*) |*payload| { + h.update(&.{1}); + hash(o.child, payload, h); + } else h.update(&.{0}), + .array => |a| if (comptime flatHash(a.child)) h.update(std.mem.sliceAsBytes(v)) else for (v) |*e| hash(a.child, e, h), + .pointer => |p| switch (p.size) { + .one => hash(p.child, v.*, h), + .slice => hashSlice(p.child, v.*, h), + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| { + if (comptime machinery(T)) return; + if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *Hasher) void) return v.digest(h); + if (s.layout == .@"packed") return hashInt(s.backing_integer.?, @bitCast(v.*), h); + if (comptime isList(T)) + return hashSlice(@typeInfo(@FieldType(T, "items")).pointer.child, v.items, h); + if (comptime @hasDecl(T, "KV") and @hasDecl(T, "Hash") and @hasField(T, "metadata")) return hashMap(T, v, h); + // std.atomic.Value: what it holds. + if (comptime @hasField(T, "raw") and s.field_names.len == 1 and @hasDecl(T, "load")) return hash(@FieldType(T, "raw"), &v.raw, h); + // A worker's future is a handle of the host's, never state. + if (comptime @hasField(T, "any_future")) return; + // std.DynamicBitSetUnmanaged: its bits. + if (comptime @hasField(T, "bit_length") and @hasField(T, "masks") and s.field_names.len == 2) { + h.update(std.mem.asBytes(&v.bit_length)); + return h.update(std.mem.sliceAsBytes(v.masks[0 .. (v.bit_length + @bitSizeOf(usize) - 1) / @bitSizeOf(usize)])); + } + comptime checkAnnotations(T); + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or comptime excluded(T, name)) continue; + if (comptime opaqueTo(F)) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is " ++ @typeName(F) ++ ", which no walk sees into: say what it is (`replica`)"); + const was = in_blob; + in_blob = comptime blob(T, name); + defer in_blob = was; + if (comptime lengthOf(T, name)) |len| { + hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], h); + } else hash(F, &@field(v, name), h); + } + }, + .@"union" => |u| { + if (u.tag_type == null) @compileError("walk: an untagged union: " ++ @typeName(T)); + const tag: u.tag_type.? = v.*; + hash(u.tag_type.?, &tag, h); + switch (v.*) { + inline else => |*payload| hash(@TypeOf(payload.*), payload, h), + } + }, + .error_set => h.update(@errorName(v.*)), + .error_union => |e| if (v.*) |*payload| { + h.update(&.{1}); + hash(e.payload, payload, h); + } else |err| { + h.update(&.{0}); + h.update(@errorName(err)); + }, + else => @compileError("walk: cannot hash " ++ @typeName(T)), + } +} + +/// Bytes a digest takes whole: an enum's are its build's number, not it. +fn flatHash(comptime T: type) bool { + return flat(T) and @typeInfo(T) != .@"enum"; +} + +/// An enum by its name, as a dump reads it, not by its number: an enum +/// made at compile time numbers its words as its build has them +/// (`registry.Builtin` has fewer in a FreeBSD build than a Linux one, which +/// has Tty9p), so a window client here of a session on the box loaded a +/// +LocationsConfig to another digest. A name's word (its hash, fixed +/// width, so that two names in a row are not one), looked up by number +/// where the words are numbered 0 up; one numbered by hand, or a value no +/// word has, by its number. +fn hashEnum(comptime T: type, v: T, h: *Hasher) void { + const e = @typeInfo(T).@"enum"; + const words: ?[e.field_names.len]u64 = comptime words: { + @setEvalBranchQuota(100_000); + var out: [e.field_names.len]u64 = undefined; + for (e.field_names, e.field_values, 0..) |name, n, i| { + if (n != i) break :words null; + out[i] = std.hash.Wyhash.hash(0, name); + } + break :words out; + }; + const n = @backingInt(v); + if (words) |table| if (n >= 0 and n < table.len) return h.update(std.mem.asBytes(&table[@intCast(n)])); + hashInt(e.tag_type, n, h); +} + +/// An integer by its value: one narrower than its bytes (a u21, an i2) has +/// bits in memory that are no part of it. +fn hashInt(comptime I: type, n: I, h: *Hasher) void { + const W = @Int(@typeInfo(I).int.signedness, @sizeOf(I) * 8); + const wide: W = n; + h.update(std.mem.asBytes(&wide)); +} + +fn hashSlice(comptime E: type, items: []const E, h: *Hasher) void { + if (in_blob and items.len * @sizeOf(E) >= blob_min) return hashBlob(E, items, h); + h.update(std.mem.asBytes(&items.len)); + if (comptime flatHash(E)) return h.update(std.mem.sliceAsBytes(items)); + for (items) |*e| hash(E, e, h); +} + +/// A hash map's entries in no order: each one's own digest, summed. +fn hashMap(comptime T: type, v: *const T, h: *Hasher) void { + var sum: u64 = 0; + var it = v.iterator(); + while (it.next()) |entry| { + var one = Hasher.init(0); + hash(@TypeOf(entry.key_ptr.*), entry.key_ptr, &one); + hash(@TypeOf(entry.value_ptr.*), entry.value_ptr, &one); + sum +%= one.final(); + } + h.update(std.mem.asBytes(&v.count())); + h.update(std.mem.asBytes(&sum)); +} + +/// The value as bytes, the same walk: what the tape carries (src/tape.zig), +/// to a replica of another build too (a window of a terminal's session, a +/// Linux client of a FreeBSD one). Native byte order, which every machine +/// pardes runs on shares, and an error by its name: its number is its +/// build's own (a Linux terminal's FileNotFound is a window's +/// NoDefaultValue). What a field is laid out as (a std.Io.File.Stat's +/// `nlink` is another width on FreeBSD) is the tape's shape, which +/// wire.semantics hashes (`shape`). +pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Error!void { + switch (@typeInfo(T)) { + .void, .null, .undefined => {}, + .bool, .int, .float, .@"enum" => try w.writeAll(std.mem.asBytes(v)), + .optional => |o| if (comptime machinery(o.child)) try w.writeByte(0) else if (v.*) |*payload| { + try w.writeByte(1); + try encode(o.child, payload, w); + } else try w.writeByte(0), + .array => |a| if (comptime flat(a.child)) try w.writeAll(std.mem.sliceAsBytes(v)) else for (v) |*e| try encode(a.child, e, w), + .pointer => |p| switch (p.size) { + .one => try encode(p.child, v.*, w), + .slice => { + try w.writeAll(std.mem.asBytes(&v.len)); + if (comptime flat(p.child)) try w.writeAll(std.mem.sliceAsBytes(v.*)) else for (v.*) |*e| try encode(p.child, e, w); + }, + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| { + if (comptime machinery(T)) return; + if (s.layout == .@"packed") return w.writeAll(std.mem.asBytes(v)); + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or comptime excluded(T, name)) continue; + try encode(F, &@field(v, name), w); + } + }, + .@"union" => |u| { + if (u.tag_type == null) @compileError("walk: an untagged union: " ++ @typeName(T)); + const tag: u.tag_type.? = v.*; + try encode(u.tag_type.?, &tag, w); + switch (v.*) { + inline else => |*payload| try encode(@TypeOf(payload.*), payload, w), + } + }, + .error_set => try encodeError(T, v.*, w), + .error_union => |e| if (v.*) |*payload| { + try w.writeByte(1); + try encode(e.payload, payload, w); + } else |err| { + try w.writeByte(0); + try encodeError(e.error_set, err, w); + }, + else => @compileError("walk: cannot encode " ++ @typeName(T)), + } +} + +/// What `encode` lays each of `types` out as, in words: each field, element +/// and tag where it goes, an enum's names and numbers, an error as an +/// error (it goes by its name). wire.semantics hashes the tape's: builds +/// whose tapes differ in shape, as a FreeBSD std.Io.File.Stat differs from +/// a Linux one, refuse each other's replicas in words, not read each other +/// out of step. +pub fn shape(comptime types: []const type) []const u8 { + comptime { + @setEvalBranchQuota(10_000_000); + var out: []const u8 = ""; + for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n"; + return out; + } +} + +fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { + comptime { + if (depth > 32) @compileError("walk: the tape's shape nests past 32 deep at " ++ @typeName(T) ++ ": a type that holds itself?"); + return switch (@typeInfo(T)) { + .void, .null, .undefined => "v", + .bool => "b", + .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }), + .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), + .@"enum" => |e| blk: { + var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; + for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v }); + break :blk out ++ "}"; + }, + .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), + .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1), + .pointer => |p| switch (p.size) { + .one => shapeOf(p.child, depth + 1), + .slice => "[]" ++ shapeOf(p.child, depth + 1), + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| blk: { + if (machinery(T)) break :blk ""; + var out: []const u8 = if (s.layout == .@"packed") "p{" else "{"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or excluded(T, name)) continue; + out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + } + break :blk out ++ "}"; + }, + .@"union" => |u| blk: { + var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "("; + for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + break :blk out ++ ")"; + }, + .error_set => "error", + .error_union => |e| "!" ++ shapeOf(e.payload, depth + 1), + else => @compileError("walk: cannot encode " ++ @typeName(T)), + }; + } +} + +/// An error by its name. An `anyerror` must be one `decode` can read back +/// (`errorNamed`): a Debug build stops at one that is not, as it is taped. +fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void { + if (comptime E == anyerror and builtin.mode == .debug) if (errorNamed(E, @errorName(err)) == null) + std.debug.panic("walk: error.{s} is taped as an anyerror, which a replica reads back only as one of tape.Failure's: add the set it comes from", .{@errorName(err)}); + try writeName(w, @errorName(err)); +} + +/// The error of `E` called `name`. `anyerror` keeps no list of its names +/// to look one up in: one is read back as one of tape.Failure's, what a +/// host reports as failed (the tape's only anyerrors). +fn errorNamed(comptime E: type, name: []const u8) ?E { + const Named = if (E == anyerror) @import("tape.zig").Failure else E; + inline for (@typeInfo(Named).error_set.error_names.?) |n| if (std.mem.eql(u8, name, n)) return @field(Named, n); + return null; +} + +pub const Reader = struct { + bytes: []const u8, + at: usize = 0, + + pub fn take(r: *Reader, n: usize) error{EndOfTape}![]const u8 { + if (r.bytes.len - r.at < n) return error.EndOfTape; + defer r.at += n; + return r.bytes[r.at..][0..n]; + } + + fn scalar(r: *Reader, comptime T: type) error{EndOfTape}!T { + return std.mem.bytesToValue(T, try r.take(@sizeOf(T))); + } +}; + +/// What an error read back is when this build has none of that name (an +/// OS's own, a newer build's): the tape is another build's, and diverges. +pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError }; + +/// The inverse of `encode`; slices (and what a pointer points at) are made +/// in `a`. A field `encode` left out is its default. +pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T { + switch (@typeInfo(T)) { + .void, .null, .undefined => return {}, + .bool => return (try r.take(1))[0] != 0, + .int, .float, .@"enum" => return r.scalar(T), + .optional => |o| { + if ((try r.take(1))[0] == 0) return null; + if (comptime machinery(o.child)) unreachable; + return try decode(o.child, r, a); + }, + .array => |a_info| { + var out: T = undefined; + if (comptime flat(a_info.child)) { + @memcpy(std.mem.sliceAsBytes(&out), try r.take(@sizeOf(T))); + } else for (&out) |*e| { + e.* = try decode(a_info.child, r, a); + } + return out; + }, + .pointer => |p| switch (p.size) { + .one => { + const out = try a.create(p.child); + out.* = try decode(p.child, r, a); + return out; + }, + .slice => { + const n = try r.scalar(usize); + const out = try a.alloc(p.child, n); + if (comptime flat(p.child)) { + @memcpy(std.mem.sliceAsBytes(out), try r.take(n * @sizeOf(p.child))); + } else for (out) |*e| { + e.* = try decode(p.child, r, a); + } + return out; + }, + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => |s| { + if (s.layout == .@"packed") return r.scalar(T); + var out: T = undefined; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime") continue; + if (comptime excluded(T, name)) { + const default = attr.default_value_ptr orelse @compileError("walk: " ++ name ++ " has no default to decode to"); + @field(out, name) = @as(*const F, @ptrCast(@alignCast(default))).*; + } else if (comptime @typeInfo(F) == .error_set) { + // No `!anyerror` to return it through: read in place. + try decodeError(F, r, &@field(out, name)); + } else @field(out, name) = try decode(F, r, a); + } + return out; + }, + .@"union" => |u| { + const tag = try decode(u.tag_type.?, r, a); + inline for (u.field_names, u.field_types) |name, F| if (tag == @field(u.tag_type.?, name)) { + return @unionInit(T, name, try decode(F, r, a)); + }; + unreachable; + }, + .error_union => |e| { + if ((try r.take(1))[0] != 0) return try decode(e.payload, r, a); + // The value's error, not decode's own. + var err: e.error_set = undefined; + try decodeError(e.error_set, r, &err); + return @as(T, err); + }, + else => @compileError("walk: cannot decode " ++ @typeName(T)), + } +} + +/// An error by its name, as `encode` wrote it, into `out`: no error union +/// holds an error set. +fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void { + const len = try r.scalar(u16); + out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError; +} + +test "a value comes back from its bytes" { + const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 }; + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const values = [_]E{ .{ .key = .{ .cp = 'x', .text = "x" } }, .tick, .{ .failed = error.FileNotFound }, .{ .failed = 9 } }; + for (&values) |*v| try encode(E, v, &w); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + for (&values) |*v| try std.testing.expectEqual(digest(E, v), digest(E, &try decode(E, &r, arena.allocator()))); + try std.testing.expectEqual(r.bytes.len, r.at); +} + +test "an error is taped by its name, which another build reads back as its own" { + // The build's number for an error is its own: 451 of 505 names were + // numbered otherwise in a Linux window's build than a terminal's, so a + // window replica read a terminal session's FileNotFound as another. + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir; + try encode(@TypeOf(failed), &failed, &w); + try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered()); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + var r: Reader = .{ .bytes = w.buffered() }; + try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator())); + // A name this build's set lacks (an OS's own): the tape is another + // build's, and the replay diverges rather than read some other error. + r = .{ .bytes = "\x00\x0c\x00FileTooLarge" }; + try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator())); + // An anyerror (a host's report) is read back as one of tape.Failure's. + const Report = struct { err: anyerror }; + w = .fixed(&buf); + try encode(Report, &.{ .err = error.FileNotFound }, &w); + r = .{ .bytes = w.buffered() }; + try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err); +} + +test "an enum is digested by its name, which another build may number otherwise" { + // registry.Builtin as a Linux build makes it (Tty9p in it) and as a + // FreeBSD one does: LocationsConfig is another number in each. + const Linux = enum { Look, Tty9p, LocationsConfig }; + const FreeBSD = enum { Look, LocationsConfig }; + const S = struct { from: Linux, also: [2]Linux }; + const T = struct { from: FreeBSD, also: [2]FreeBSD }; + try std.testing.expectEqual(digest(S, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } }), digest(T, &.{ .from = .LocationsConfig, .also = .{ .Look, .LocationsConfig } })); + try std.testing.expect(digest(S, &.{ .from = .Look, .also = .{ .Look, .Look } }) != digest(S, &.{ .from = .Tty9p, .also = .{ .Look, .Look } })); +} + +test "a digest is of values: the same text at two addresses is one digest" { + const S = struct { name: []const u8, n: u32, next: ?*const u32 }; + const a = "same text"; + var b: [9]u8 = undefined; + @memcpy(&b, a); + const x: u32 = 7; + const y: u32 = 7; + try std.testing.expectEqual(digest(S, &.{ .name = a, .n = 1, .next = &x }), digest(S, &.{ .name = &b, .n = 1, .next = &y })); + try std.testing.expect(digest(S, &.{ .name = a, .n = 1, .next = &x }) != digest(S, &.{ .name = a, .n = 2, .next = &x })); +} + +test "an annotated field is in no digest, and a length cuts its array" { + const S = struct { + kept: u32, + cache: u32, + ring: [4]u8, + ring_len: u8, + pub const replica = .{ .derived = .{"cache"}, .len = .{.{ "ring", "ring_len" }} }; + }; + try std.testing.expectEqual( + digest(S, &.{ .kept = 1, .cache = 2, .ring = .{ 1, 2, 9, 9 }, .ring_len = 2 }), + digest(S, &.{ .kept = 1, .cache = 3, .ring = .{ 1, 2, 0, 0 }, .ring_len = 2 }), + ); +} + +// ---- the dump: the same walk, by name, for another build ---- +// +// A dump (docs/next-daemon.md, phase 2) is `dump_magic`, then a table of +// every type its roots' walk reaches -- each struct's and union's fields by +// name, each enum's names, each number's width -- then each root's value, +// laid out as `encode` lays it out but for three things: an enum or a +// union's tag is its place among the table's names, a pointer or a slice met +// again is a reference to the first (what two fields share is shared again), +// and a type with a `dumpWrite` of its own is its bytes, counted. +// +// A build that is not the writer's matches by name: a field added since +// takes its default, one removed is passed over, one whose kind changed +// takes its default and says so (`Decoder.notes`). With no default to take, +// the value it is part of is refused (`error.Incompatible`), and an element +// of an array of optional pointers (a session's panes) is then left out, +// nothing above it. A dump whose table is this build's own is read field +// after field, no name asked. +// +// Native byte order: the machines pardes runs on are all little-endian. + +pub const dump_magic = "pardes-dump\x00\x02\n"; + +const Kind = enum(u8) { skip, void, bool, int, float, @"enum", optional, one, slice, array, prefix, @"struct", @"union", err, error_union, custom, bitset }; + +/// std's lists: their items, not the capacity behind them (nor, in a safe +/// build, the lock that checks no pointer into them moves). +pub fn isList(comptime T: type) bool { + if (@typeInfo(T) != .@"struct" or !@hasField(T, "items") or !@hasField(T, "capacity")) return false; + const n = @typeInfo(T).@"struct".field_names.len; + return n == 2 or (n == 3 and @hasField(T, "pointer_stability")); +} +fn isAtomic(comptime T: type) bool { + return @typeInfo(T) == .@"struct" and @hasField(T, "raw") and @typeInfo(T).@"struct".field_names.len == 1 and @hasDecl(T, "load"); +} +fn isBitset(comptime T: type) bool { + return @typeInfo(T) == .@"struct" and @hasField(T, "bit_length") and @hasField(T, "masks") and @typeInfo(T).@"struct".field_names.len == 2; +} + +/// A field written only as far as its `len` says: a count, then that many. +fn Prefix(comptime E: type) type { + return struct { + pub const element = E; + }; +} + +/// What the dump walks `T` as: an atomic as what it holds, a list as its +/// items. +fn Canon(comptime T: type) type { + if (comptime isAtomic(T)) return Canon(@FieldType(T, "raw")); + if (comptime isList(T)) return []@typeInfo(@FieldType(T, "items")).pointer.child; + return T; +} + +fn kindOf(comptime T: type) Kind { + return switch (@typeInfo(T)) { + .void, .null, .undefined => .void, + .bool => .bool, + .int => .int, + .float => .float, + .@"enum" => .@"enum", + .optional => .optional, + .array => .array, + .pointer => |p| switch (p.size) { + .one => .one, + .slice => .slice, + else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)), + }, + .@"struct" => if (machinery(T) or @hasField(T, "any_future")) + .skip + else if (@hasDecl(T, "dumpWrite")) + .custom + else if (@hasDecl(T, "element") and T == Prefix(T.element)) + .prefix + else if (isBitset(T)) + .bitset + else + .@"struct", + .@"union" => |u| if (u.tag_type == null) @compileError("walk: an untagged union: " ++ @typeName(T)) else .@"union", + .error_set => .err, + .error_union => .error_union, + else => @compileError("walk: cannot dump " ++ @typeName(T)), + }; +} + +/// The fields of struct `T` a dump holds, in order. +fn dumpFields(comptime T: type) []const [:0]const u8 { + comptime { + @setEvalBranchQuota(1_000_000); + var names: []const [:0]const u8 = &.{}; + const s = @typeInfo(T).@"struct"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or excluded(T, name) or kindOf(Canon(F)) == .skip) continue; + if (opaqueTo(F)) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is " ++ @typeName(F) ++ ", which no walk sees into: say what it is (`replica`)"); + names = names ++ .{name}; + } + return names; + } +} + +/// The type a dump walks field `name` of `T` as. +fn FieldDump(comptime T: type, comptime name: []const u8) type { + const F = @FieldType(T, name); + if (comptime cutArray(T, name)) return Prefix(Canon(@typeInfo(F).array.child)); + return Canon(F); +} + +/// A fixed array a `len` cuts, which a dump writes as far as it says. A +/// slice it cuts (a text's room for its ranges, made whole) is written +/// whole, room and all, which only plain numbers may be: a load gives it +/// the room it had. +fn cutArray(comptime T: type, comptime name: []const u8) bool { + const F = @FieldType(T, name); + if (comptime lengthOf(T, name) == null) return false; + if (@typeInfo(F) == .array) return true; + if (!plain(Element(F))) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is a slice a `len` cuts, of what a dump cannot write past the cut"); + return false; +} + +/// Every bit pattern one of its values: what a dump may write where +/// nothing was ever written. +fn plain(comptime T: type) bool { + return switch (@typeInfo(T)) { + .int, .float => true, + .array => |a| plain(a.child), + .@"struct" => |st| st.layout != .@"packed" and blk: { + for (st.field_types) |F| if (!plain(F)) break :blk false; + break :blk true; + }, + else => false, + }; +} + +fn children(comptime T: type) []const type { + comptime { + return switch (kindOf(T)) { + .optional => &.{Canon(@typeInfo(T).optional.child)}, + .one, .slice => &.{Canon(@typeInfo(T).pointer.child)}, + .array => &.{Canon(@typeInfo(T).array.child)}, + .prefix => &.{T.element}, + .error_union => &.{Canon(@typeInfo(T).error_union.payload)}, + .@"struct" => blk: { + var out: []const type = &.{}; + for (dumpFields(T)) |name| out = out ++ .{FieldDump(T, name)}; + break :blk out; + }, + .@"union" => blk: { + var out: []const type = &.{}; + for (@typeInfo(T).@"union".field_types) |F| out = out ++ .{Canon(F)}; + break :blk out; + }, + else => &.{}, + }; + } +} + +fn collect(comptime T: type, comptime list: []const type) []const type { + comptime { + for (list) |U| if (U == T) return list; + var out: []const type = list ++ .{T}; + if (kindOf(T) == .@"struct") checkAnnotations(T); + for (children(T)) |C| out = collect(C, out); + return out; + } +} + +/// Every type the walk of `roots` reaches, each once. +fn graph(comptime roots: []const type) []const type { + comptime { + @setEvalBranchQuota(100_000_000); + var list: []const type = &.{}; + for (roots) |R| list = collect(Canon(R), list); + return list; + } +} + +fn indexIn(comptime types: []const type, comptime T: type) u32 { + comptime { + @setEvalBranchQuota(100_000_000); + for (types, 0..) |U, i| if (U == T) return i; + @compileError("walk: not in the dump's table: " ++ @typeName(T)); + } +} + +/// How many bytes an integer of `bits` is written in: its own size. +fn intBytes(bits: u16) usize { + return if (bits == 0) 0 else if (bits <= 8) 1 else if (bits <= 16) 2 else if (bits <= 32) 4 else if (bits <= 64) 8 else 16; +} + +fn writeName(w: *std.Io.Writer, name: []const u8) !void { + try w.writeAll(std.mem.asBytes(&@as(u16, @intCast(name.len)))); + try w.writeAll(name); +} + +fn writeU32(w: *std.Io.Writer, n: u32) !void { + try w.writeAll(std.mem.asBytes(&n)); +} + +fn writeTable(comptime types: []const type, w: *std.Io.Writer) !void { + try writeU32(w, types.len); + inline for (types) |T| { + const k = comptime kindOf(T); + try w.writeByte(@intFromEnum(k)); + switch (k) { + .int => { + try w.writeByte(@intFromBool(@typeInfo(T).int.signedness == .signed)); + try w.writeAll(std.mem.asBytes(&@as(u16, @typeInfo(T).int.bits))); + }, + .float => try w.writeAll(std.mem.asBytes(&@as(u16, @typeInfo(T).float.bits))), + .@"enum" => { + const e = @typeInfo(T).@"enum"; + const tag = @typeInfo(e.tag_type).int; + try w.writeByte(@intFromBool(tag.signedness == .signed)); + try w.writeAll(std.mem.asBytes(&@as(u16, tag.bits))); + try writeU32(w, e.field_names.len); + inline for (e.field_names, e.field_values) |name, value| { + try writeName(w, name); + try w.writeAll(std.mem.asBytes(&@as(i128, value))); + } + }, + .array => { + try w.writeAll(std.mem.asBytes(&@as(u64, @typeInfo(T).array.len))); + try writeU32(w, comptime indexIn(types, children(T)[0])); + }, + .optional, .one, .slice, .prefix, .error_union => try writeU32(w, comptime indexIn(types, children(T)[0])), + .@"struct" => { + const names = comptime dumpFields(T); + try writeU32(w, names.len); + inline for (names) |name| { + try writeName(w, name); + try writeU32(w, comptime indexIn(types, FieldDump(T, name))); + } + }, + .@"union" => { + const u = @typeInfo(T).@"union"; + try writeU32(w, u.field_names.len); + inline for (u.field_names, u.field_types) |name, F| { + try writeName(w, name); + try writeU32(w, comptime indexIn(types, Canon(F))); + } + }, + .custom => try writeName(w, @typeName(T)), + .skip, .void, .bool, .err, .bitset => {}, + } + } +} + +/// Integers by value, in the bytes `intBytes` gives them: a `u21`'s +/// padding is no part of it. +fn putInt(comptime I: type, n: I, w: *std.Io.Writer) !void { + const info = @typeInfo(I).int; + const W = @Int(info.signedness, intBytes(info.bits) * 8); + const wide: W = n; + try w.writeAll(std.mem.asBytes(&wide)); +} + +/// Bytes the walk may copy whole between builds when both sides agree on +/// them: integers and bools (an enum's numbers may not be the other's). +fn isSigned(comptime T: type) bool { + return @typeInfo(T) == .int and @typeInfo(T).int.signedness == .signed; +} + +fn plainBytes(comptime T: type) bool { + return switch (@typeInfo(T)) { + .int => @sizeOf(T) * 8 == @bitSizeOf(T), + .bool => true, + else => false, + }; +} + +/// The table of `types` as bytes, made once a process (it is the build's): +/// a store record or a dump writes it, a load compares it. +fn tableOf(comptime types: []const type) []const u8 { + const Once = struct { + // Of these types alone: a container that named none of its + // function's parameters would be one for every table. + const of = types; + /// 0 none, 1 being made, 2 made. + var state: std.atomic.Value(u8) = .init(0); + var bytes: []const u8 = &.{}; + }; + if (Once.state.load(.acquire) == 2) return Once.bytes; + if (Once.state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) { + var out: std.Io.Writer.Allocating = .init(std.heap.page_allocator); + writeTable(types, &out.writer) catch @panic("walk: out of memory for a dump's table"); + Once.bytes = out.toOwnedSlice() catch @panic("walk: out of memory for a dump's table"); + Once.state.store(2, .release); + } else while (Once.state.load(.acquire) != 2) std.atomic.spinLoopHint(); + return Once.bytes; +} + +/// A blob's bytes a dump left out (`Dump.deferred`): they go at `at`. +pub const Deferred = struct { at: usize, bytes: []const u8 }; + +/// Writes a dump of `roots`' values, one `put` each in order. +pub fn Dump(comptime roots: []const type) type { + return struct { + const Self = @This(); + pub const types = graph(roots); + + gpa: std.mem.Allocator, + out: std.Io.Writer.Allocating, + memo: std.AutoHashMapUnmanaged(Key, u32) = .empty, + shared: u32 = 0, + /// Set: a `.blob`'s bytes (of `blob_min` and up) are not written but + /// noted, where they go and what they are; the dump with them put + /// back in place is the dump. A replica's join streams them from + /// its writer thread (src/replica.zig), the session's step paying + /// for the rest alone. + deferred: ?*std.ArrayListUnmanaged(Deferred) = null, + in_blob: bool = false, + + const Key = struct { addr: usize, len: usize, ty: u32 }; + + pub fn init(gpa: std.mem.Allocator) !Self { + var d: Self = .{ .gpa = gpa, .out = .init(gpa) }; + errdefer d.out.deinit(); + try d.out.writer.writeAll(dump_magic); + try d.out.writer.writeAll(tableOf(types)); + return d; + } + + pub fn deinit(d: *Self) void { + d.memo.deinit(d.gpa); + d.out.deinit(); + } + + /// The dump, the caller's (gpa). + pub fn finish(d: *Self) ![]u8 { + d.memo.deinit(d.gpa); + d.memo = .empty; + return d.out.toOwnedSlice(); + } + + pub fn put(d: *Self, comptime T: type, v: *const T) !void { + try writeU32(&d.out.writer, comptime indexIn(types, Canon(T))); + try d.value(T, v); + } + + /// Whether the pointer or slice at `addr` was met before: its + /// reference written if so, else 0 and it is noted. + fn seen(d: *Self, comptime T: type, addr: usize, len: usize) Error!bool { + const w = &d.out.writer; + if (len == 0) { + try writeU32(w, 0); + return false; + } + const got = try d.memo.getOrPut(d.gpa, .{ .addr = addr, .len = len, .ty = comptime indexIn(types, Canon(T)) }); + if (got.found_existing) { + try writeU32(w, got.value_ptr.*); + return true; + } + d.shared += 1; + got.value_ptr.* = d.shared; + try writeU32(w, 0); + return false; + } + + const Error = std.Io.Writer.Error || std.mem.Allocator.Error; + + fn items(d: *Self, comptime E: type, slice: []const E) Error!void { + const w = &d.out.writer; + try w.writeAll(std.mem.asBytes(&@as(u64, slice.len))); + if (comptime flat(E)) return w.writeAll(std.mem.sliceAsBytes(slice)); + for (slice) |*e| try d.value(E, e); + } + + fn value(d: *Self, comptime T: type, v: *const T) Error!void { + if (comptime isAtomic(T)) return d.value(@FieldType(T, "raw"), &v.raw); + const w = &d.out.writer; + if (comptime isList(T)) { + const E = @typeInfo(@FieldType(T, "items")).pointer.child; + if (try d.seen([]E, @intFromPtr(v.items.ptr), v.items.len)) return; + return d.items(E, v.items); + } + switch (comptime kindOf(T)) { + .skip, .void => {}, + .bool => try w.writeByte(@intFromBool(v.*)), + .int => try putInt(T, v.*, w), + .float => try w.writeAll(std.mem.asBytes(v)), + .@"enum" => try putInt(@typeInfo(T).@"enum".tag_type, @intFromEnum(v.*), w), + .optional => if (v.*) |*payload| { + try w.writeByte(1); + try d.value(@typeInfo(T).optional.child, payload); + } else try w.writeByte(0), + .one => { + const C = @typeInfo(T).pointer.child; + if (try d.seen(T, @intFromPtr(v.*), @max(1, @sizeOf(C)))) return; + try d.value(C, v.*); + }, + .slice => { + const E = @typeInfo(T).pointer.child; + if (try d.seen(T, @intFromPtr(v.ptr), v.len)) return; + if (comptime flat(E)) if (d.in_blob) if (d.deferred) |later| { + const bytes = std.mem.sliceAsBytes(v.*); + if (bytes.len >= blob_min) { + try w.writeAll(std.mem.asBytes(&@as(u64, v.len))); + return later.append(d.gpa, .{ .at = d.out.writer.end, .bytes = bytes }); + } + }; + try d.items(E, v.*); + }, + .array => { + const E = @typeInfo(T).array.child; + if (comptime flat(E)) return w.writeAll(std.mem.sliceAsBytes(v)); + for (v) |*e| try d.value(E, e); + }, + .prefix => unreachable, + .@"struct" => { + const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed"; + inline for (comptime dumpFields(T)) |name| { + const F = @FieldType(T, name); + const was = d.in_blob; + d.in_blob = was or comptime blob(T, name); + defer d.in_blob = was; + if (comptime cutArray(T, name)) { + const all = &@field(v, name); + try d.items(@typeInfo(F).array.child, all[0..@min(all.len, lengthIn(v, comptime lengthOf(T, name).?))]); + } else if (packed_struct) { + const copy: F = @field(v, name); + try d.value(F, ©); + } else try d.value(F, &@field(v, name)); + } + }, + .@"union" => { + const tag = std.meta.activeTag(v.*); + const ordinal: u16 = inline for (@typeInfo(T).@"union".field_names, 0..) |name, i| { + if (tag == @field(std.meta.Tag(T), name)) break i; + } else unreachable; + try w.writeAll(std.mem.asBytes(&ordinal)); + switch (v.*) { + inline else => |*payload| try d.value(@TypeOf(payload.*), payload), + } + }, + .err => try writeName(w, @errorName(v.*)), + .error_union => if (v.*) |*payload| { + try w.writeByte(1); + try d.value(@typeInfo(T).error_union.payload, payload); + } else |err| { + try w.writeByte(0); + try writeName(w, @errorName(err)); + }, + .custom => { + const at = d.out.writer.end; + try w.writeAll(&std.mem.toBytes(@as(u64, 0))); + T.dumpWrite(v, d.gpa, w) catch return error.WriteFailed; + const n: u64 = d.out.writer.end - at - 8; + @memcpy(d.out.writer.buffer[at..][0..8], std.mem.asBytes(&n)); + }, + .bitset => { + try w.writeAll(std.mem.asBytes(&@as(u64, v.bit_length))); + const words = (v.bit_length + @bitSizeOf(usize) - 1) / @bitSizeOf(usize); + if (words > 0) try w.writeAll(std.mem.sliceAsBytes(v.masks[0..words])); + }, + } + } + }; +} + +/// One value `T` as a dump of its own (gpa): a closed pane, a terminal's +/// parts that are not its emulator. +pub fn save(comptime T: type, gpa: std.mem.Allocator, v: *const T) ![]u8 { + var d = try Dump(&.{T}).init(gpa); + errdefer d.deinit(); + try d.put(T, v); + return d.finish(); +} + +pub const DecodeError = error{ EndOfDump, BadDump, Incompatible, OutOfMemory }; + +/// The allocators a `replica.alloc` annotation may name, beside the +/// loader's own: `.alloc = .{ .{ "image", "image" } }` makes what field +/// `image` holds with `allocs.image`. +pub const Allocators = struct { + image: ?std.mem.Allocator = null, + pdf: ?std.mem.Allocator = null, + terminal: ?std.mem.Allocator = null, +}; + +fn allocOf(comptime T: type, comptime name: []const u8) ?[]const u8 { + if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "alloc")) return null; + inline for (T.replica.alloc) |pair| if (comptime std.mem.eql(u8, pair[0], name)) return pair[1]; + return null; +} + +fn countOf(n: u64) DecodeError!usize { + return std.math.cast(usize, n) orelse error.BadDump; +} + +/// An allocation a load made: what a reference to it finds, and what a +/// failed load frees. +const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; + +/// A type as the dump's writer had it. +const SrcType = struct { + kind: Kind, + signed: bool = false, + bits: u16 = 0, + child: u32 = 0, + len: u64 = 0, + /// A struct's or union's fields, an enum's names (and `value`s). + fields: []const SrcField = &.{}, +}; +const SrcField = struct { name: []const u8, ty: u32 = 0, value: i128 = 0 }; + +/// Reads a dump of `roots`, one `into` or `get` each in order. +pub fn Load(comptime roots: []const type) type { + return struct { + const Self = @This(); + const types = graph(roots); + + /// Where what is read is made (a `replica.alloc` field's own + /// allocator while it is read). + gpa: std.mem.Allocator, + /// The loader's own lists'. + own: std.mem.Allocator, + bytes: []const u8, + at: usize = 0, + arena: std.heap.ArenaAllocator, + src: []SrcType = &.{}, + /// Its table is this build's: no name is asked. + same: bool = false, + /// Each pointer and slice made so far: what a reference finds, and + /// what `rollback` frees. + shared: std.ArrayList(Made) = .empty, + /// What a type's `dumpRead` made, freed by its `dumpFree` too. + customs: std.ArrayList(struct { ptr: *anyopaque, gpa: std.mem.Allocator, free: *const fn (*anyopaque, std.mem.Allocator) void }) = .empty, + /// What was let go, for the person whose session it is. + notes: std.ArrayList(u8) = .empty, + /// The field being read, for a note. + field: []const u8 = "", + /// Where a field's annotation (`replica.alloc`) says its values are + /// made instead of `gpa`. + allocs: Allocators = .{}, + + pub fn init(gpa: std.mem.Allocator, bytes: []const u8) DecodeError!Self { + var l: Self = .{ .gpa = gpa, .own = gpa, .bytes = bytes, .arena = .init(gpa) }; + errdefer l.arena.deinit(); + if (!std.mem.startsWith(u8, bytes, dump_magic)) return error.BadDump; + l.at = dump_magic.len; + const table_at = l.at; + try l.readTable(); + l.same = std.mem.eql(u8, tableOf(types), bytes[table_at..l.at]); + return l; + } + + pub fn deinit(l: *Self) void { + l.shared.deinit(l.own); + l.customs.deinit(l.own); + l.notes.deinit(l.own); + l.arena.deinit(); + } + + /// Everything read so far freed: a load that fails leaves nothing. + /// What it was read into must not be used after (nor freed). + pub fn rollback(l: *Self) void { + for (l.customs.items) |c| c.free(c.ptr, c.gpa); + for (l.shared.items) |m| if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress()); + l.customs.clearRetainingCapacity(); + l.shared.clearRetainingCapacity(); + } + + fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize { + try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); + return l.shared.items.len - 1; + } + + fn take(l: *Self, n: usize) DecodeError![]const u8 { + if (l.bytes.len - l.at < n) return error.EndOfDump; + defer l.at += n; + return l.bytes[l.at..][0..n]; + } + + fn scalar(l: *Self, comptime T: type) DecodeError!T { + return std.mem.bytesToValue(T, try l.take(@sizeOf(T))); + } + + /// A count, as this machine counts (a wasm32 one has 32 bits). + fn count(l: *Self) DecodeError!usize { + return countOf(try l.scalar(u64)); + } + + fn name(l: *Self) DecodeError![]const u8 { + return l.take(try l.scalar(u16)); + } + + fn readTable(l: *Self) DecodeError!void { + const a = l.arena.allocator(); + const n = try l.scalar(u32); + if (n > l.bytes.len) return error.BadDump; + l.src = try a.alloc(SrcType, n); + for (l.src) |*t| { + const k = std.enums.fromInt(Kind, (try l.take(1))[0]) orelse return error.BadDump; + t.* = .{ .kind = k }; + switch (k) { + .int => { + t.signed = (try l.take(1))[0] != 0; + t.bits = try l.scalar(u16); + }, + .float => t.bits = try l.scalar(u16), + .@"enum" => { + t.signed = (try l.take(1))[0] != 0; + t.bits = try l.scalar(u16); + const list = try a.alloc(SrcField, try l.scalar(u32)); + for (list) |*f| f.* = .{ .name = try l.name(), .value = try l.scalar(i128) }; + t.fields = list; + }, + .array => { + t.len = try l.scalar(u64); + t.child = try l.scalar(u32); + }, + .optional, .one, .slice, .prefix, .error_union => t.child = try l.scalar(u32), + .@"struct", .@"union" => { + const nfields = try l.scalar(u32); + if (nfields > l.bytes.len) return error.BadDump; + const list = try a.alloc(SrcField, nfields); + for (list) |*f| f.* = .{ .name = try l.name(), .ty = try l.scalar(u32) }; + t.fields = list; + }, + .custom => _ = try l.name(), + .skip, .void, .bool, .err, .bitset => {}, + } + } + for (l.src) |t| { + if (t.child >= n) return error.BadDump; + for (t.fields) |f| if (f.ty >= n) return error.BadDump; + } + } + + pub fn note(l: *Self, comptime fmt: []const u8, args: anytype) void { + if (l.notes.items.len > 4096) return; + l.notes.print(l.own, fmt ++ "\n", args) catch {}; + } + + fn root(l: *Self) DecodeError!u32 { + const ty = try l.scalar(u32); + if (ty >= l.src.len) return error.BadDump; + return ty; + } + + /// The next root, made: what it points at is `gpa`'s. + pub fn get(l: *Self, comptime T: type) DecodeError!T { + var out: T = undefined; + try l.fresh(T, &out); + try l.value(T, try l.root(), &out); + return out; + } + + /// The next root read into `out` as it stands: a field the dump + /// holds is replaced (what it held is not freed), every other one + /// is left as it is. A field another build cannot give is left too, + /// and noted. + pub fn into(l: *Self, comptime T: type, out: *T) DecodeError!void { + const ty = try l.root(); + const s = l.src[ty]; + if (s.kind != .@"struct") return error.BadDump; + try l.fields(T, s, out, true); + } + + /// `out` as a new `T` is before its fields are read: each field's + /// default, an allocator the dump's. + fn fresh(l: *Self, comptime T: type, out: *T) DecodeError!void { + if (comptime @typeInfo(T) != .@"struct" or isList(T) or isAtomic(T) or isBitset(T) or kindOf(T) == .custom) return; + if (comptime kindOf(T) == .skip) return; + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |field, F, attr| { + if (attr.@"comptime") continue; + if (comptime attr.default_value_ptr) |dp| { + @field(out, field) = @as(*const F, @ptrCast(@alignCast(dp))).*; + } else if (comptime F == std.mem.Allocator) { + @field(out, field) = l.gpa; + } else if (comptime !walkedField(T, field)) { + @compileError("walk: " ++ @typeName(T) ++ "." ++ field ++ " has no default to load to"); + } + } + } + + fn walkedField(comptime T: type, comptime field: []const u8) bool { + inline for (comptime dumpFields(T)) |n| if (comptime std.mem.eql(u8, n, field)) return true; + return false; + } + + /// Whether the source's `ty` and `T` are of one kind, else the + /// source's value passed over. + fn kindMatches(l: *Self, comptime T: type, ty: u32) DecodeError!bool { + if (l.same) return true; + const want = comptime kindOf(Canon(T)); + const got = l.src[ty].kind; + if (got == want) return true; + // A field that came to hold a count and so many, or stopped. + if ((want == .array and got == .prefix) or (want == .prefix and got == .array)) return true; + try l.skip(ty); + return false; + } + + fn readInt(l: *Self, comptime I: type, s: SrcType) DecodeError!I { + if (l.same) { + const info = @typeInfo(I).int; + const W = @Int(info.signedness, intBytes(info.bits) * 8); + return @intCast(try l.scalar(W)); + } + const n = intBytes(s.bits); + const raw = try l.take(n); + var wide: u128 = 0; + for (raw, 0..) |b, i| wide |= @as(u128, b) << @intCast(i * 8); + if (s.signed and n > 0) { + const shift: u7 = @intCast(128 - n * 8); + const signed: i128 = @as(i128, @bitCast(wide << shift)) >> shift; + return std.math.cast(I, signed) orelse error.Incompatible; + } + return std.math.cast(I, wide) orelse error.Incompatible; + } + + /// `T` read from the source's type `ty` into `out`, which `fresh` + /// has made ready. + fn value(l: *Self, comptime T: type, ty: u32, out: *T) DecodeError!void { + if (comptime isAtomic(T)) { + const R = @FieldType(T, "raw"); + var raw: R = undefined; + try l.fresh(R, &raw); + try l.value(R, ty, &raw); + out.* = .{ .raw = raw }; + return; + } + // A field this build compiles out (a PDF slot where there is no + // MuPDF): passed over, said only when it held something. + if (comptime kindOf(Canon(T)) == .void) if (!l.same and l.src[ty].kind != .void) { + const held = l.src[ty].kind != .optional or (l.at < l.bytes.len and l.bytes[l.at] != 0); + try l.skip(ty); + if (held) l.note("{s}: not in this build", .{l.field}); + return; + }; + if (!try l.kindMatches(T, ty)) return error.Incompatible; + const s = l.src[ty]; + if (comptime isList(T)) { + const E = @typeInfo(@FieldType(T, "items")).pointer.child; + const got = try l.slice(E, s); + out.* = .empty; + out.items = got; + out.capacity = got.len; + return; + } + switch (comptime kindOf(T)) { + .skip, .void => {}, + .bool => out.* = (try l.take(1))[0] != 0, + .int => out.* = try l.readInt(T, s), + .float => { + if (l.same or s.bits == @typeInfo(T).float.bits) { + out.* = try l.scalar(T); + } else if (s.bits == 32) { + out.* = @floatCast(try l.scalar(f32)); + } else if (s.bits == 64) { + out.* = @floatCast(try l.scalar(f64)); + } else return error.Incompatible; + }, + .@"enum" => { + const e = @typeInfo(T).@"enum"; + if (l.same) { + out.* = @enumFromInt(try l.readInt(e.tag_type, s)); + return; + } + const n = try l.readInt(i128, s); + const was = for (s.fields) |f| { + if (f.value == n) break f.name; + } else null; + if (was) |w| inline for (e.field_names) |field| if (std.mem.eql(u8, w, field)) { + out.* = @field(T, field); + return; + }; + if (e.mode == .nonexhaustive and was == null) + if (std.math.cast(e.tag_type, n)) |tag| { + out.* = @enumFromInt(tag); + return; + }; + l.note("{s}: no {s} here", .{ l.field, was orelse "such value" }); + return error.Incompatible; + }, + .optional => { + const C = @typeInfo(T).optional.child; + if ((try l.take(1))[0] == 0) { + out.* = null; + return; + } + if (comptime kindOf(Canon(C)) == .skip) { + out.* = if (comptime C == std.mem.Allocator) l.gpa else null; + return; + } + // A slice's or a pointer's null is its address: set to + // an undefined payload, the optional itself is undefined, + // and ReleaseSafe's optimizer read it as null (a pane's + // `?[]u8` command one byte long, every field after it out + // of step). Read through a local and set it from that. + if (comptime @typeInfo(C) == .pointer) { + var payload: C = undefined; + try l.value(C, s.child, &payload); + out.* = payload; + return; + } + out.* = @as(C, undefined); + try l.fresh(C, &out.*.?); + l.value(C, s.child, &out.*.?) catch |err| { + out.* = null; + return err; + }; + }, + .one => { + const C = @typeInfo(T).pointer.child; + if (try l.ref()) |again| { + out.* = @ptrCast(@alignCast(again.ptr)); + return; + } + const one = try l.gpa.create(C); + const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| { + l.gpa.destroy(one); + return err; + }; + try l.fresh(C, one); + l.value(C, s.child, one) catch |err| { + // Left half made: no later reference may find it. + l.shared.items[at].dead = true; + return err; + }; + out.* = one; + }, + .slice => { + const E = @typeInfo(T).pointer.child; + const got = try l.slice(E, s); + if (comptime @typeInfo(T).pointer.sentinel_ptr != null) @compileError("walk: a sentinel slice: " ++ @typeName(T)); + out.* = got; + }, + .array => { + const E = @typeInfo(T).array.child; + const n: u64 = if (s.kind == .prefix) try l.scalar(u64) else s.len; + try l.elements(E, s.child, n, out); + }, + .prefix => unreachable, + .@"struct" => try l.fields(T, s, out, false), + .@"union" => { + const u = @typeInfo(T).@"union"; + const ordinal = try l.scalar(u16); + if (ordinal >= s.fields.len) return error.BadDump; + const was = s.fields[ordinal]; + inline for (u.field_names, u.field_types, 0..) |field, F, i| { + if (if (l.same) ordinal == i else std.mem.eql(u8, was.name, field)) { + out.* = @unionInit(T, field, undefined); + try l.fresh(F, &@field(out.*, field)); + return l.value(F, was.ty, &@field(out.*, field)); + } + } + try l.skip(was.ty); + l.note("{s}: no {s} here", .{ l.field, was.name }); + return error.Incompatible; + }, + .err => out.* = try l.errorNamed(T), + .error_union => { + const P = @typeInfo(T).error_union.payload; + if ((try l.take(1))[0] != 0) { + var payload: P = undefined; + try l.fresh(P, &payload); + try l.value(P, s.child, &payload); + out.* = payload; + } else out.* = try l.errorNamed(@typeInfo(T).error_union.error_set); + }, + .custom => { + const bytes = try l.take(try l.count()); + T.dumpRead(out, l.gpa, bytes) catch |err| { + if (err == error.OutOfMemory) return error.OutOfMemory; + l.note("{s}: {t}", .{ l.field, err }); + return error.Incompatible; + }; + if (comptime @hasDecl(T, "dumpFree")) try l.customs.append(l.own, .{ .ptr = out, .gpa = l.gpa, .free = @ptrCast(&T.dumpFree) }); + }, + .bitset => { + const bits = try l.count(); + out.* = try .initEmpty(l.gpa, bits); + const words = (bits + @bitSizeOf(usize) - 1) / @bitSizeOf(usize); + if (words > 0) @memcpy(std.mem.sliceAsBytes(out.masks[0..words]), try l.take(words * @sizeOf(usize))); + }, + } + } + + fn errorNamed(l: *Self, comptime E: type) DecodeError!E { + const was = try l.name(); + const names = @typeInfo(E).error_set.error_names orelse @compileError("walk: anyerror cannot be loaded by name: " ++ @typeName(E)); + inline for (names) |field| if (std.mem.eql(u8, was, field)) return @field(E, field); + return error.Incompatible; + } + + /// A reference to a pointer or slice made before, or null for a + /// new one (its length then read). + fn ref(l: *Self) DecodeError!?[]u8 { + const n = try l.scalar(u32); + if (n == 0) return null; + if (n > l.shared.items.len) return error.BadDump; + const got = l.shared.items[n - 1]; + if (got.dead) return error.Incompatible; + return got.bytes; + } + + fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E { + if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; + const n = try l.count(); + if (n == 0) return &.{}; + if (n > l.bytes.len) return error.BadDump; + const many = try l.gpa.alloc(E, n); + const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| { + l.gpa.free(many); + return err; + }; + l.elements(E, s.child, n, many) catch |err| { + l.shared.items[at].dead = true; + return err; + }; + return many; + } + + /// `n` elements of the source's type `ty` into `out` (an array or + /// a slice), as many as it holds, the rest passed over. + fn elements(l: *Self, comptime E: type, ty: u32, n: u64, out: anytype) DecodeError!void { + const keep: usize = @intCast(@min(n, out.len)); + const s = l.src[ty]; + const bulk = if (comptime flat(E)) l.same or (comptime plainBytes(E)) and s.kind == (comptime kindOf(E)) and + (s.kind == .bool or (s.bits == @bitSizeOf(E) and s.signed == isSigned(E))) else false; + if (bulk) { + if (n > l.bytes.len) return error.EndOfDump; + @memcpy(std.mem.sliceAsBytes(out[0..keep]), try l.take(keep * @sizeOf(E))); + _ = try l.take(@intCast((n - keep) * @sizeOf(E))); + return; + } + // An array of optional pointers (a session's panes): one that + // cannot be read is left out, not the array. + const droppable = comptime @typeInfo(E) == .optional and @typeInfo(@typeInfo(E).optional.child) == .pointer; + var failed = false; + for (0..try countOf(n)) |i| { + if (i >= keep) { + try l.skip(ty); + continue; + } + try l.fresh(E, &out[i]); + l.value(E, ty, &out[i]) catch |err| { + if (err != error.Incompatible) return err; + if (droppable) { + out[i] = null; + l.note("{s}[{d}] left out", .{ l.field, i }); + } else failed = true; + }; + } + if (failed) return error.Incompatible; + } + + fn fields(l: *Self, comptime T: type, s: SrcType, out: *T, existing: bool) DecodeError!void { + const names = comptime dumpFields(T); + const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed"; + const outer = l.field; + defer l.field = outer; + if (l.same) { + if (s.fields.len != names.len) return error.BadDump; + inline for (names, 0..) |field, k| { + l.field = field; + try l.oneField(T, field, s.fields[k].ty, out, packed_struct, existing); + } + return; + } + var found: std.StaticBitSet(@max(1, names.len)) = .empty; + var failed = false; + for (s.fields) |f| { + const k: ?usize = inline for (names, 0..) |field, k| { + if (std.mem.eql(u8, f.name, field)) break k; + } else null; + const at = k orelse { + try l.skip(f.ty); + continue; + }; + found.set(at); + inline for (names, 0..) |field, i| if (i == at) { + l.field = field; + l.oneField(T, field, f.ty, out, packed_struct, existing) catch |err| { + if (err != error.Incompatible) return err; + const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; + if (comptime attr.default_value_ptr) |dp| { + @field(out, field) = @as(*const @FieldType(T, field), @ptrCast(@alignCast(dp))).*; + l.note("{s}.{s}: its kind changed; its default taken", .{ @typeName(T), field }); + } else if (existing) { + l.note("{s}.{s}: its kind changed; left as it was", .{ @typeName(T), field }); + } else failed = true; + }; + }; + } + // A field the writer did not have: its default (`fresh`), or + // the value cannot be made. + inline for (names, 0..) |field, i| if (!found.isSet(i)) { + const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; + if (comptime attr.default_value_ptr == null) { + if (!existing) { + l.note("{s}.{s}: not in the dump", .{ @typeName(T), field }); + failed = true; + } + } + }; + if (failed) return error.Incompatible; + } + + fn oneField(l: *Self, comptime T: type, comptime field: []const u8, ty: u32, out: *T, comptime packed_struct: bool, existing: bool) DecodeError!void { + const F = @FieldType(T, field); + const gpa = l.gpa; + defer l.gpa = gpa; + if (comptime allocOf(T, field)) |which| if (@field(l.allocs, which)) |a| { + l.gpa = a; + }; + if (comptime cutArray(T, field)) { + return l.value(F, ty, &@field(out, field)); + } + // A struct held in place in one read as it stands (the root's): + // what it does not hold stays as it is too. + if (comptime kindOf(Canon(F)) == .@"struct" and Canon(F) == F and @typeInfo(F).@"struct".layout != .@"packed") if (existing) { + if (!try l.kindMatches(F, ty)) return error.Incompatible; + return l.fields(F, l.src[ty], &@field(out, field), true); + }; + if (packed_struct) { + var copy: F = undefined; + try l.fresh(F, ©); + try l.value(F, ty, ©); + @field(out, field) = copy; + return; + } + try l.fresh(F, &@field(out, field)); + try l.value(F, ty, &@field(out, field)); + } + + /// The source's value of type `ty` passed over. + fn skip(l: *Self, ty: u32) DecodeError!void { + const s = l.src[ty]; + switch (s.kind) { + .skip, .void => {}, + .bool => _ = try l.take(1), + .int, .@"enum" => _ = try l.take(intBytes(s.bits)), + .float => _ = try l.take(s.bits / 8), + .optional => if ((try l.take(1))[0] != 0) try l.skip(s.child), + .one => if (try l.scalar(u32) == 0) { + _ = try l.made(&.{}, .@"1"); + l.shared.items[l.shared.items.len - 1].dead = true; + try l.skip(s.child); + }, + .slice => if (try l.scalar(u32) == 0) { + const n = try l.scalar(u64); + if (n > 0) { + _ = try l.made(&.{}, .@"1"); + l.shared.items[l.shared.items.len - 1].dead = true; + } + try l.skipMany(s.child, n); + }, + .prefix => try l.skipMany(s.child, try l.scalar(u64)), + .array => try l.skipMany(s.child, s.len), + .@"struct" => for (s.fields) |f| try l.skip(f.ty), + .@"union" => { + const ordinal = try l.scalar(u16); + if (ordinal >= s.fields.len) return error.BadDump; + try l.skip(s.fields[ordinal].ty); + }, + .err => _ = try l.name(), + .error_union => if ((try l.take(1))[0] != 0) try l.skip(s.child) else { + _ = try l.name(); + }, + .custom => _ = try l.take(try l.count()), + .bitset => _ = try l.take((try l.count() + 63) / 64 * 8), + } + } + + fn skipMany(l: *Self, ty: u32, n: u64) DecodeError!void { + const s = l.src[ty]; + if (s.kind == .int or s.kind == .bool) { + const size: u64 = if (s.kind == .bool) 1 else intBytes(s.bits); + if (n > l.bytes.len) return error.EndOfDump; + _ = try l.take(@intCast(n * size)); + return; + } + for (0..try countOf(n)) |_| try l.skip(ty); + } + }; +} + +/// A value `save` made, `gpa`'s; what was let go is said in `notes` when +/// it is given. +pub fn load(comptime T: type, gpa: std.mem.Allocator, bytes: []const u8, notes: ?*std.ArrayList(u8)) DecodeError!T { + var l = try Load(&.{T}).init(gpa, bytes); + defer l.deinit(); + errdefer l.rollback(); + const v = try l.get(T); + if (notes) |n| try n.appendSlice(gpa, l.notes.items); + return v; +} + +test "an optional slice comes back, and the fields after it" { + const S = struct { before: u32 = 0, command: ?[]u8 = null, after: u32 = 0 }; + var text = "sleep 30".*; + const bytes = try save(S, std.testing.allocator, &.{ .before = 1, .command = &text, .after = 2 }); + defer std.testing.allocator.free(bytes); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const back = try load(S, arena.allocator(), bytes, null); + try std.testing.expectEqualStrings("sleep 30", back.command.?); + try std.testing.expectEqual(2, back.after); +} + +test "a dump comes back whole: shared things shared, lists and runs as long as they were" { + const gpa = std.testing.allocator; + const Leaf = struct { n: u21, name: []const u8 }; + const Mode = enum { a, b, c }; + const S = struct { + mode: Mode = .a, + leaf: *Leaf, + same_leaf: *Leaf, + text: []u8, + same_text: []u8, + list: std.ArrayList(u16) = .empty, + maybe: ?Leaf = null, + either: union(enum) { none, some: u32, many: []const u16 } = .none, + ring: [4]u8 = @splat(0), + ring_len: u8 = 0, + room: []i32 = &.{}, + room_len: u8 = 0, + cache: u64 = 7, + alloc: std.mem.Allocator, + flags: packed struct { x: bool = false, y: u3 = 0 } = .{}, + pub const replica = .{ .derived = .{"cache"}, .len = .{ .{ "ring", "ring_len" }, .{ "room", "room_len" } } }; + }; + var leaf: Leaf = .{ .n = 0x1fffff, .name = "leaf" }; + var room = [_]i32{ 4, 5, -1, -1 }; + var text = "shared text".*; + var list: std.ArrayList(u16) = .empty; + defer list.deinit(gpa); + try list.appendSlice(gpa, &.{ 1, 2, 3 }); + const x: S = .{ .mode = .c, .leaf = &leaf, .same_leaf = &leaf, .text = &text, .same_text = &text, .list = list, .maybe = .{ .n = 3, .name = "maybe" }, .either = .{ .many = &.{ 9, 8 } }, .ring = .{ 5, 6, 0xAA, 0xAA }, .ring_len = 2, .room = &room, .room_len = 2, .cache = 99, .alloc = gpa, .flags = .{ .x = true, .y = 5 } }; + const bytes = try save(S, gpa, &x); + defer gpa.free(bytes); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const y = try load(S, arena.allocator(), bytes, null); + try std.testing.expectEqual(digest(S, &x), digest(S, &y)); + try std.testing.expect(y.leaf == y.same_leaf and y.text.ptr == y.same_text.ptr); + try std.testing.expectEqual(@as(u64, 7), y.cache); + try std.testing.expectEqualSlices(u16, &.{ 1, 2, 3 }, y.list.items); + try std.testing.expectEqual(true, y.flags.x); + // A slice its `len` cuts keeps its room. + try std.testing.expectEqual(@as(usize, 4), y.room.len); + try std.testing.expectEqualSlices(i32, &.{ 4, 5 }, y.room[0..2]); + // A fixpoint: what was read is written as it was. + const again = try save(S, gpa, &y); + defer gpa.free(again); + try std.testing.expectEqualSlices(u8, bytes, again); +} + +test "a dump crosses builds: a field added takes its default, one removed is passed over, one changed is left out" { + const gpa = std.testing.allocator; + const Old = struct { + const Color = enum { red, green, blue }; + const Pane = struct { name: []const u8, color: Color = .red, gone: u32 = 1, width: u16 = 0, kind: u32 = 0 }; + panes: [3]?*Pane = @splat(null), + count: u8 = 0, + renamed: u32 = 0, + }; + const New = struct { + // Values renumbered and one added before the others. + const Color = enum { cyan, blue, red, green }; + const Wide = struct { a: u8 }; + const Pane = struct { added: bool = true, color: Color = .cyan, width: u32 = 0, name: []const u8, kind: Wide = .{ .a = 4 } }; + // A pane's required field changed kind: that pane is left out. + const Strict = struct { name: u8 }; + panes: [4]?*Pane = @splat(null), + count: u16 = 0, + renamed_now: u32 = 5, + }; + var one: Old.Pane = .{ .name = "one", .color = .blue, .gone = 9, .width = 300, .kind = 2 }; + var two: Old.Pane = .{ .name = "two", .color = .green }; + const old: Old = .{ .panes = .{ &one, null, &two }, .count = 2, .renamed = 7 }; + const bytes = try save(Old, gpa, &old); + defer gpa.free(bytes); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + // The notes are made where the load's values are. + var notes: std.ArrayList(u8) = .empty; + const new = try load(New, arena.allocator(), bytes, ¬es); + try std.testing.expectEqual(@as(u16, 2), new.count); + try std.testing.expectEqual(@as(u32, 5), new.renamed_now); + const a = new.panes[0].?; + try std.testing.expectEqualStrings("one", a.name); + try std.testing.expectEqual(New.Color.blue, a.color); + try std.testing.expectEqual(@as(u32, 300), a.width); + try std.testing.expect(a.added); + try std.testing.expectEqual(@as(u8, 4), a.kind.a); + try std.testing.expect(new.panes[1] == null and new.panes[3] == null); + try std.testing.expectEqual(New.Color.green, new.panes[2].?.color); + try std.testing.expect(std.mem.indexOf(u8, notes.items, "kind") != null); + + const Strict = struct { + const Pane = struct { name: u8 }; + panes: [3]?*Pane = @splat(null), + count: u8 = 0, + }; + notes.clearRetainingCapacity(); + const strict = try load(Strict, arena.allocator(), bytes, ¬es); + try std.testing.expect(strict.panes[0] == null and strict.panes[2] == null); + try std.testing.expectEqual(@as(u8, 2), strict.count); + try std.testing.expect(std.mem.indexOf(u8, notes.items, "left out") != null); + + // And back: the newer build's dump read by the older one. + const newer = try save(New, gpa, &new); + defer gpa.free(newer); + const back = try load(Old, arena.allocator(), newer, null); + try std.testing.expectEqualStrings("two", back.panes[2].?.name); + try std.testing.expectEqual(@as(u32, 1), back.panes[0].?.gone); + try std.testing.expectEqual(Old.Color.blue, back.panes[0].?.color); +} + +test "a dump that is not one, or is cut short, is refused" { + const gpa = std.testing.allocator; + const S = struct { a: u32, b: []const u8 }; + try std.testing.expectError(error.BadDump, load(S, gpa, "not a dump", null)); + const bytes = try save(S, gpa, &.{ .a = 1, .b = "bee" }); + defer gpa.free(bytes); + // What it made before the end is freed again. + try std.testing.expectError(error.EndOfDump, load(S, gpa, bytes[0 .. bytes.len - 2], null)); +} + +test "a blob is hashed once and folded in as a word; one written in place is not its word, which a Debug build checks; a freed one's word goes" { + const memory = @import("memory.zig"); + const digestOfBlob = struct { + fn of(items: []const u8) u64 { + var h = Hasher.init(0); + hashSlice(u8, items, &h); + return h.final(); + } + }.of; + const gpa = std.testing.allocator; + const File = struct { + pub const replica = .{ .blob = .{"content"} }; + content: []u8, + cursor: u32 = 0, + }; + const text = try gpa.alloc(u8, 64 << 10); + @memset(text, 'a'); + var f: File = .{ .content = text }; + const before = digest(File, &f); + try std.testing.expect(wordOf(@intFromPtr(text.ptr)) != null); + // The same word again: nothing it holds moved. + try std.testing.expectEqual(before, digest(File, &f)); + // Written in place, which a blob never is: its kept word is no longer + // its hash, which a Debug build's digest checks (and panics at) where a + // release build's would fold the old word in. + text[1000] = 'b'; + try std.testing.expect(wordOf(@intFromPtr(text.ptr)).? != digestOfBlob(text)); + text[1000] = 'a'; + try std.testing.expectEqual(digestOfBlob(text), wordOf(@intFromPtr(text.ptr)).?); + // Freed, its word goes: an address used again is hashed again. + memory.freeUnfilled(gpa, text); + try std.testing.expect(wordOf(@intFromPtr(text.ptr)) == null); +} diff --git a/src/web.zig b/src/web.zig index fd6371c0..5e5a0d74 100644 --- a/src/web.zig +++ b/src/web.zig @@ -13,7 +13,7 @@ const pardes = @import("pardes.zig"); const mouse = @import("mouse.zig"); const gpa = std.heap.wasm_allocator; -const embedded_dump = @embedFile("embedded.dump.zon"); +const embedded_dump = @embedFile("embedded.dump"); const input_capacity = 64 * 1024; const attr_tagline: u16 = 1 << 7; @@ -484,7 +484,7 @@ fn writeFile(_: ?*anyopaque, _: u8, path: []const u8, bytes: []const u8) void { } fn writeDump(_: ?*anyopaque, bytes: []const u8) void { - const name = "pardes-dump.zon"; + const name = "pardes.dump"; host_download(name.ptr, name.len, bytes.ptr, bytes.len); } |
