diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 01:51:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 03:13:15 -0300 |
| commit | 6118ad214bb74744e7b38004b34e4487a6635be1 (patch) | |
| tree | 16c5f4c3d0e3df31d7a7ffab2a9726f619be43fd /src | |
| parent | fdf74e199de7d0fc648f912875323f3dc920ffc2 (diff) | |
| download | pardes-6118ad214bb74744e7b38004b34e4487a6635be1.tar.gz pardes-6118ad214bb74744e7b38004b34e4487a6635be1.zip | |
replica clients' terminals as screen state: the session's Peers keep a Screens (rterm's), a peer each: the rows a pump's steps read before its unit, the deltas after it, and before each client input's step, the rows it read before its unit; a joiner's terminals afresh after its dump; acks, rows asked and resets from the client (0x04) taken at the boundary; units carry no pty bytes and a word of digest
The client applies a delta or rows (0x06) in stream order and asks for the rows it shows and lacks after each frame; its dump's emulators are loaded on the C heap (walk.zig `.alloc` of a pane's `terminal`, dump.zig `terminal_gpa`), as `terminal.create` makes a screen replica's. Its load checked under its own tape (no emulator in the digest).
tty.zig: with replica clients, a wait takes one step of a person's input and none after a pty's output (`split`): the next is held for the next wait, so a step reads the terminals as the deltas after each unit bring them.
Screens: `dropPeer` (clients come and go), its numbers reused.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/dump.zig | 6 | ||||
| -rw-r--r-- | src/panes.zig | 2 | ||||
| -rw-r--r-- | src/replica.zig | 115 | ||||
| -rw-r--r-- | src/tty/tty.zig | 87 | ||||
| -rw-r--r-- | src/walk.zig | 1 |
5 files changed, 185 insertions, 26 deletions
diff --git a/src/dump.zig b/src/dump.zig index cb639568..152495e2 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -129,6 +129,10 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { return null; } +/// What a load makes a terminal's emulator with, when not the loader's +/// own: a screen-state replica's are the C heap's (src/replica.zig). +pub var terminal_gpa: ?std.mem.Allocator = null; + // ---- a whole session to and from a dump (acme keeps this in rows.c) ---- // // A dump (src/walk.zig) is everything the session is but what is the @@ -231,7 +235,7 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: p.fs.deinit(gpa); gpa.destroy(p); } - l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa }; + l.allocs = .{ .image = p.image_gpa, .pdf = p.pdf_gpa, .terminal = terminal_gpa }; try l.into(Pardes, p); const read = l.shared.items.len; const mounts = try l.get([]const filesystem.Mount); diff --git a/src/panes.zig b/src/panes.zig index 64851787..20523de6 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -34,7 +34,7 @@ pub const Pane = struct { .len = .{ .{ "msg", "msg_len" }, .{ "msg_older", "msg_older_len" }, .{ "question", "question_len" }, .{ "shell_tail", "shell_tail_len" }, .{ "sel_snap", "nsel_snap" } }, // A picture's and a PDF's memory is their own allocators' (a dump // loaded makes it there). - .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" } }, + .alloc = .{ .{ "image", "image" }, .{ "pdf", "pdf" }, .{ "terminal", "terminal" } }, }; /// The words that throw unsaved text away (builtins.zig warnModified). pub const Discarding = enum { Exit, Restore, get, Del, Delcol }; diff --git a/src/replica.zig b/src/replica.zig index 5c965b2b..7bf0574f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -292,6 +292,7 @@ pub const Screens = struct { enc: screen_state.Encoder, tracks: [pardes.MAX_PANES]?Tracked = @splat(null), peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, + free_peers: std.ArrayListUnmanaged(u16) = .empty, out: std.ArrayListUnmanaged(u8) = .empty, /// Nanoseconds: what each flush spent encoding, for every peer. encode_ns: u64 = 0, @@ -311,6 +312,7 @@ pub const Screens = struct { s.gpa.destroy(p); } s.peers.deinit(s.gpa); + s.free_peers.deinit(s.gpa); s.out.deinit(s.gpa); s.enc.deinit(); } @@ -323,12 +325,21 @@ pub const Screens = struct { /// A new peer, with nothing yet: its first delta of each terminal /// starts it afresh. pub fn addPeer(s: *Screens) !u16 { + if (s.free_peers.pop()) |again| return again; const p = try s.gpa.create([pardes.MAX_PANES]PeerOf); p.* = @splat(.{}); try s.peers.append(s.gpa, p); return @intCast(s.peers.items.len - 1); } + /// Peer `peer` gone: what it was sent forgotten, its number free for + /// the next `addPeer` (a socket's clients come and go). + pub fn dropPeer(s: *Screens, peer: u16) void { + for (s.peers.items[peer]) |*pp| pp.peer.deinit(s.gpa); + s.peers.items[peer].* = @splat(.{}); + s.free_peers.append(s.gpa, peer) catch {}; + } + /// What `peer` is owed of every terminal, each a message onto `out` /// (owned): nothing for one it has as it is, or with `bound` in flight. pub fn flush(s: *Screens, core: *Pardes, peer: u16, out: *std.ArrayListUnmanaged([]u8)) !void { @@ -558,6 +569,8 @@ pub const Replica = struct { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; r.t.headless = true; + // Its terminals are the session's deltas (Peers sends them). + r.t.screen_state = true; errdefer { tape.release(&r.t); r.epoch.deinit(); @@ -572,6 +585,10 @@ pub const Replica = struct { o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator; // The session's store and Recent are the authority's. o.store_path = null; + // Its emulators off the epoch, as `terminal.create` makes a screen + // replica's: deltas write them while a copy is held. + pardes.dump.terminal_gpa = terminal.emulator_gpa; + defer pardes.dump.terminal_gpa = null; const c = try pardes.dump.load(epoch, o, bytes, null); // What loading queued (a watch, a theme asked for) is the // authority's errand, done there already: the session was dumped @@ -878,7 +895,7 @@ pub const Replica = struct { /// Whether its core loaded from the dump is the session as it was /// dumped, by the welcome's `digest`. pub fn loaded(r: *Replica, want: u64) bool { - return tape.digestOf(r.core()) == want; + return r.digest() == want; } /// Its own view drawn and presented through `present`, between steps @@ -1109,6 +1126,8 @@ pub const Peer = struct { fd: c_int, id: u16, view: u8, + /// Its number in the session's `Screens`. + sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). seq: u32 = 0, reader: ?std.Thread = null, @@ -1181,9 +1200,13 @@ pub const Peer = struct { peer.send(&.{pong}); return true; }, - // ponytail: rterm's acks (src/screen_state.zig) go to its - // session state once terminals cross as screens. - .screen_ack => return true, + // Its word about a terminal (an ack, rows asked, a reset), to + // the session's Screens at the next boundary. + .screen_ack => { + const msg = peer.peers.gpa.dupe(u8, payload) catch return false; + peer.peers.post(.{ .screen = .{ .peer = peer, .msg = msg } }); + return true; + }, .check => { peer.peers.post(.{ .check = peer }); return true; @@ -1241,8 +1264,13 @@ pub const Peers = struct { boundary_ns: std.ArrayListUnmanaged(u64) = .empty, cut: u32 = 0, head: [unit_head]u8 = undefined, + /// Its clients' terminals as screen state (src/screen_state.zig), while + /// it records: what each was sent. + screens: Screens = undefined, + msgs: std.ArrayListUnmanaged([]u8) = .empty, - pub const Note = union(enum) { join: c_int, input: struct { peer: *Peer, msg: []u8 }, gone: *Peer, check: *Peer }; + pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer }; + pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { var n: usize = 0; @@ -1256,7 +1284,7 @@ pub const Peers = struct { ps.lock.unlock(ps.io); switch (note) { .join => |fd| _ = libc.close(fd), - .input => |in| ps.gpa.free(in.msg), + .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check => {}, } return; @@ -1298,8 +1326,12 @@ pub const Peers = struct { const t0 = nowNs(); defer if (ps.recording) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; if (ps.recording) { + // The rows the pump's steps read, then its unit, then what the + // terminals came to (the next steps read that). + ps.screensOut(core, .fill); ps.seq += 1; ps.unit(0, ps.seq); + ps.screensOut(core, .flush); } if (!ps.mail.load(.acquire)) return; ps.lock.lockUncancelable(ps.io); @@ -1312,6 +1344,13 @@ pub const Peers = struct { ps.input(core, in.peer, in.msg); ps.gpa.free(in.msg); }, + .screen => |in| { + defer ps.gpa.free(in.msg); + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + defer ps.gpa.free(reply); + const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); + in.peer.send(&.{ &head, reply }); + }, .gone => |peer| ps.leave(core, peer), .check => |peer| { var out: [16]u8 = undefined; @@ -1325,12 +1364,41 @@ pub const Peers = struct { const in = parseInput(msg) orelse return; const t0 = nowNs(); defer ps.input_ns.append(ps.gpa, nowNs() - t0) catch {}; + // The terminals as this step reads them, then the step, the rows + // it read, its unit (part B terminal notes' order). + ps.screensOut(core, .flush); core.toView(peer.view); core.update(in.event); core.step(); + ps.screensOut(core, .fill); ps.unit(peer.id, in.seq); } + const ScreensOut = enum { flush, fill }; + + /// What each client is owed of the terminals (`Screens.flush`), or the + /// rows a step read that it lacks (`Screens.fill`, then `filled`), each + /// a screen message queued for it. + fn screensOut(ps: *Peers, core: *Pardes, comptime what: ScreensOut) void { + for (ps.list) |slot| if (slot) |peer| ps.screensTo(core, peer, what); + if (what == .fill) ps.screens.filled(core); + } + + fn screensTo(ps: *Peers, core: *Pardes, peer: *Peer, comptime what: ScreensOut) void { + defer { + for (ps.msgs.items) |m| ps.gpa.free(m); + ps.msgs.clearRetainingCapacity(); + } + (switch (what) { + .flush => ps.screens.flush(core, peer.sid, &ps.msgs), + .fill => ps.screens.fill(core, peer.sid, &ps.msgs), + }) catch return; + for (ps.msgs.items) |m| { + const head = wire.header(@backingInt(wire.ServerTag.screen), m.len); + peer.send(&.{ &head, m }); + } + } + fn refuse(fd: c_int, why: []const u8) void { var out: [600]u8 = undefined; if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {} @@ -1348,6 +1416,11 @@ pub const Peers = struct { if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)"); ps.t = tape.memory(ps.gpa); ps.t.unit_digests = true; + // Terminals cross as screen state: a unit carries no pty bytes, + // and its digest is a word (PARDES_REPLICA_TRACE: one a field). + ps.t.screen_state = true; + ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; + ps.screens = .init(ps.gpa); tape.on = &ps.t; ps.recording = true; } @@ -1402,6 +1475,11 @@ pub const Peers = struct { return; } slot.* = peer; + // Its terminals afresh, after its dump and before any unit. + if (ps.screens.addPeer()) |sid| { + peer.sid = sid; + ps.screensTo(core, peer, .flush); + } else |_| _ = libc.shutdown(fd, 2); log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, bytes.len >> 10, (nowNs() - t0) / 1000 }); if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq }); } @@ -1411,6 +1489,7 @@ pub const Peers = struct { slot.* = null; }; log.info("client {d} left '{s}'", .{ peer.id, ps.name }); + ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); core.closeView(peer.view); if (ps.count() == 0) core.unattended = ps.alone_unattended; @@ -1438,7 +1517,7 @@ pub const Peers = struct { var i: usize = 0; while (i < ps.inbox.items.len) { const mine = switch (ps.inbox.items[i]) { - .input => |in| in.peer == peer, + .input, .screen => |in| in.peer == peer, .gone, .check => |p| p == peer, .join => false, }; @@ -1446,7 +1525,10 @@ pub const Peers = struct { i += 1; continue; } - if (ps.inbox.items[i] == .input) ps.gpa.free(ps.inbox.items[i].input.msg); + switch (ps.inbox.items[i]) { + .input, .screen => |in| ps.gpa.free(in.msg), + else => {}, + } _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(io); @@ -1465,6 +1547,7 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; + ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; tape.release(&ps.t); @@ -1478,13 +1561,14 @@ pub const Peers = struct { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); core.closeView(peer.view); + ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; ps.stop(core); ps.lock.lockUncancelable(ps.io); for (ps.inbox.items) |note| switch (note) { .join => |fd| _ = libc.close(fd), - .input => |in| ps.gpa.free(in.msg), + .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check => {}, }; ps.inbox.clearRetainingCapacity(); @@ -1498,6 +1582,7 @@ pub const Peers = struct { ps.taken.deinit(ps.gpa); ps.input_ns.deinit(ps.gpa); ps.boundary_ns.deinit(ps.gpa); + ps.msgs.deinit(ps.gpa); } fn writeStats(ps: *Peers, path: []const u8) void { @@ -1627,6 +1712,18 @@ pub fn sendInput(fd: c_int, msg: []const u8) bool { return writeAll(fd, msg[head_len..]); } +/// A message `Replica.screen` or `screenRequest` made, to the session. +pub fn sendScreen(fd: c_int, msg: []const u8) bool { + var buf: [64]u8 = undefined; + if (wire.header_len + msg.len > buf.len) { + const head = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + return writeAll(fd, &head) and writeAll(fd, msg); + } + buf[0..wire.header_len].* = wire.header(@backingInt(wire.ClientTag.screen_ack), msg.len); + @memcpy(buf[wire.header_len..][0..msg.len], msg); + return writeAll(fd, buf[0 .. wire.header_len + msg.len]); +} + /// Asks the session's digest, answered in stream order. pub fn askCheck(fd: c_int) bool { var out: [16]u8 = undefined; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 43994645..2eafc785 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -158,6 +158,8 @@ pub const Command = struct { replica_mail, /// A replica client's: what the session streamed (replica.Client). unit: []u8, + /// ...a terminal's delta or rows, before the units after it. + screen: []u8, /// The session's digest, asked by PARDES_REPLICA_CHECK. checked: u64, link_gone, @@ -1353,6 +1355,12 @@ const Remote = struct { core.present_skipped = skipped; core.needs_frame = needs; c.dirty = false; + // The history rows it shows and does not hold (a scroll back), + // asked for: blank until they come. + while (c.r.screenRequest()) |req| if (!replica.sendScreen(c.fd, req)) { + c.quit = true; + break; + }; } }; @@ -1371,10 +1379,12 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo const d = (wire.decodeServer(tag, payload) catch return false).check; l.postEvent(.{ .checked = d }) catch {}; }, + .screen => { + const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; + l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); + }, .refused => return false, - // ponytail: a terminal's delta (rterm's, src/screen_state.zig) - // applies here once terminals cross as screens. - .screen, .pong, .welcome, .dump => {}, + .pong, .welcome, .dump => {}, } return true; } @@ -1517,7 +1527,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se _ = libc.shutdown(joined.fd, 2); while (true) { while (loop.tryEvent() catch null) |ev| switch (ev) { - .unit => |b| std.heap.c_allocator.free(b), + .unit, .screen => |b| std.heap.c_allocator.free(b), .paste => |b| gpa.free(@constCast(b)), else => {}, }; @@ -2024,6 +2034,10 @@ const Shell = struct { /// speculations stand on (a replica takes such a unit as is). mail_only: bool = false, waited: bool = false, + /// This wait's steps so far, and the input held for the next (`split`). + took_input: bool = false, + took_output: bool = false, + held: ?@TypeOf(Command.value) = null, /// This process a replica client of a session elsewhere (`remote`): /// its inputs go to the replica, never straight to the core. remote: ?*Remote = null, @@ -2468,6 +2482,8 @@ const Shell = struct { } s.input_seen = false; s.output_seen = false; + s.took_input = false; + s.took_output = false; s.mail_only = true; defer s.waited = true; // Paced, what is not an input (a frame's ack, a pty's output) is @@ -2500,25 +2516,32 @@ const Shell = struct { fn waitOnce(s: *Shell, wait: u32) void { var batch: usize = 0; - if (wait != 0) { - // A virtual clock moves a frame a wake while anything moves - // continuously (the glide), as the real one would. - s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; - requestWake(s.io, wait); - } - pardes.turn.rest(); - const first = s.loop.nextEvent() catch { + const first = if (s.held) |held| held: { + s.held = null; + break :held held; + } else next: { + if (wait != 0) { + // A virtual clock moves a frame a wake while anything moves + // continuously (the glide), as the real one would. + s.wake_to = s.core.nextWake() orelse if (s.core.continuous()) s.core.now_ns + pardes.animation.frame_ns else 0; + requestWake(s.io, wait); + } + pardes.turn.rest(); + const got = s.loop.nextEvent() catch { + pardes.turn.wake(); + s.core.quit = true; + return s.reloadWatched(); + }; pardes.turn.wake(); - s.core.quit = true; - return s.reloadWatched(); + break :next got; }; - pardes.turn.wake(); batch = 1; // A run of queued motions is one move to its last cell: each would // otherwise redo hover and selection work the next one undoes. var motion: ?@TypeOf(Command.value) = null; var ev = first; while (true) { + if (s.split(ev)) break; if (ev == .mouse and (ev.mouse.type == .motion or ev.mouse.type == .drag)) { if (motion) |m| if (m.mouse.button != ev.mouse.button or m.mouse.type != ev.mouse.type or @as(u3, @bitCast(m.mouse.mods)) != @as(u3, @bitCast(ev.mouse.mods))) @@ -2542,6 +2565,31 @@ const Shell = struct { s.reloadWatched(); } + /// With replica clients, a wait (one pump, one unit) takes one step of + /// a person's input, and none after a pty's output: a step reads the + /// terminals as the clients' deltas bring them, sent after each unit + /// (docs/next-daemon.md, part B terminal notes). One that must wait is + /// held for the next. Whether `ev` is. + fn split(s: *Shell, ev: @TypeOf(Command.value)) bool { + if (!s.peers.recording) return false; + switch (ev) { + .pty_read => { + s.took_output = true; + return false; + }, + .key_press => if (s.in_paste) return false, + .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {}, + else => return false, + } + if (s.took_output or s.took_input) { + s.held = ev; + s.input_seen = true; + return true; + } + s.took_input = true; + return false; + } + fn apply(s: *Shell, event: @TypeOf(Command.value)) bool { const core = s.core; const tz_event = tracy.zone(@src(), "event"); @@ -2626,6 +2674,15 @@ const Shell = struct { } std.heap.c_allocator.free(bytes); }, + .screen => |bytes| { + if (s.remote) |c| { + if (c.r.screen(bytes)) |reply| if (!replica.sendScreen(c.fd, reply)) { + c.quit = true; + }; + c.dirty = true; + } + std.heap.c_allocator.free(bytes); + }, .checked => |d| if (s.remote) |c| c.checked(d), .link_gone => if (s.remote) |c| { c.quit = true; diff --git a/src/walk.zig b/src/walk.zig index 33247d42..2dc0a721 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -966,6 +966,7 @@ pub const DecodeError = error{ EndOfDump, BadDump, Incompatible, OutOfMemory }; pub const Allocators = struct { image: ?std.mem.Allocator = null, pdf: ?std.mem.Allocator = null, + terminal: ?std.mem.Allocator = null, }; fn allocOf(comptime T: type, comptime name: []const u8) ?[]const u8 { |
