From d56973aa92d0de6d84446dff73eba9d5654fe6f4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 03:00:12 -0300 Subject: replica: the delta protocol deleted, with the wire's screen messages (the old road's deletion, piece 3 of 8): core/replica.zig loses the screen-state section (the screen message's head, `Screens`: tracks, peers, flush, fill, filled, take), `Peers.screens`/`msgs`/`flushed`/`screensOut`/`screensTo`, the `.screen` note and its arm in `boundary`, `Peers.input`'s flushes and fills, a joiner's `addPeer` and a leaver's `dropPeer`, `Peer.sid` and its `screen_ack` arm, `Replica.screen`/`screenRequest`/`screenMessage`/`overTerminals`, `Replica.screens`/`screen_out`/`ScreenOf`, `Stats.apply_ns`, `Said.screen` (and its arms in dev/tui/tui.zig and dev/gui/gui.zig: a delta no longer counts as an echo there, nor is its latency noted), the Link's `.screen` arm and `drawn`'s row requests, `sendScreen`, and the `screens` argument of `Authority.init` and `Replica.init` (every caller: core/jj.zig 10, core/tape.zig 2, core/echo.zig 2, test/panes.zig 15, test/perf.zig 2, runtime/client.zig 1, test/rollback_bench.zig 1, test/replicas.zig 10, core/replica.zig 98); a replica's own `made` no longer starts on screen state, and `confirm` and `applyPending` keep only their one-road branches; abi/wire.zig retires the replica's `screen_ack` (0x04) and the session's `screen` (0x06) as gaps (the version moves once, in piece 4); test/daemon.py's round trip passes over units alone; the tests that were the old road's alone go: a delta under a speculation, a terminal sized again by a client's key (screen state's deferred size), pardes's own keys over a terminal's history and a delta while they wait, a unit kept over a copy and a delta after it, a step's rows sent as a new client's holes, the screen-state twin of the 9P pty/run test, and the 9P open over a copy taken ahead (on the one road a unit that feeds a terminal lets the copy go: `Term.writes`, so no copy is held across the run's output: its premise was the old road's); the others that ran on screen state now run on the bytes road (a kitty image: the client's emulator has it as the session's does; prompt marks and Kill; the far client's may_wait walk, its click into a focus-reporting shell, its backed-up queue; a view that sized a terminal closing; a closed terminal brought back; a leave's queued resize before a join); FOUND AND FIXED on the way (those tests on the one road failed: the far click `expected 0, found 1` stopped, and may_wait named `Del`, `DelAbove`, `Up`, `C-w a`, `click shell`, `Look` stopped at `pty_write`): a client's own step that moved the focus in a session whose shell asked for focus events wrote the report (CSI I/O) through pty_write, which a speculation stops at (`keeps_stopping`), so the key or click waited a round trip, and the session's write was on the tape; on screen state both were skipped (core/Core.zig `reportFocus` skipped the write only `speculating and screen_state`, core/tape.zig `hostCall` kept pty_write off the tape only under screen state): both now on every road (a user on the flip's build could hit it: a far client's click, pane switch or Del beside vim or tmux waited a round trip); core/root.zig `machine_lines` for replica.zig 76 -> 74 (Authority.init's getenv and a joiner's `addPeer` failure's shutdown gone); core-test 911/1068 (157 skipped: 7 tests gone, each one skipped there for want of a shell), shell-test 1110/1113 (the 7 gone) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 1314 ++++++------------------------------------------------ 1 file changed, 135 insertions(+), 1179 deletions(-) (limited to 'core/replica.zig') diff --git a/core/replica.zig b/core/replica.zig index 993d9e2c..3f897044 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -164,9 +164,7 @@ pub const Authority = struct { /// 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 { + pub fn init(gpa: Allocator, options: pardes.Options, host: pardes.Host) !*Authority { // A replay gives its core every allocator (tape.zig), and a digest // tells an allocator from none. var opts = options; @@ -177,9 +175,6 @@ pub const Authority = struct { errdefer gpa.destroy(a); a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; errdefer tape.release(&a.t); - 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; // A digest at a unit's end, as the session's (Peers.unit). a.t.no_digest = true; opts.tape = &a.t; @@ -355,208 +350,6 @@ pub const Authority = struct { } }; -// ---- terminals as screen state (core/term/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, - /// Made the first time a terminal is encoded (`encoder`). - enc: ?*Term.Encoder = null, - 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: *Term.State, track: *Term.Track }; - const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{} }; - - pub fn init(gpa: Allocator) Screens { - return .{ .gpa = 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| Term.untrack(tr.track, s.gpa, if (s.live(core, id, tr)) tr.state 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); - if (s.enc) |e| Term.encoderFree(e); - } - - 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; - } - 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 Term.encode(state, try s.encoder(), tr.track, &pp.peer, &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 (!Term.wasRead(state)) 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 Term.encodeRows(state, try s.encoder(), tr.track, 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| { - Term.clearRead(state); - }; - } - - 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). - Term.untrack(tr.track, s.gpa, null); - s.tracks[id] = null; - } - s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = try Term.track(s.gpa) }; - return &s.tracks[id].?; - } - - /// The encoder, made the first time a terminal is encoded. - fn encoder(s: *Screens) !*Term.Encoder { - if (s.enc) |e| return e; - s.enc = try Term.encoder(s.gpa); - return s.enc.?; - } - - /// 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 Term.encodeRows(state, try s.encoder(), tr.track, 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 { @@ -588,8 +381,6 @@ pub const Stats = struct { 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(.{}), @@ -619,7 +410,6 @@ pub const Stats = struct { s.spec_ns.deinit(gpa); s.copy_ns.deinit(gpa); s.rollback_ns.deinit(gpa); - s.apply_ns.deinit(gpa); } }; @@ -689,17 +479,14 @@ pub const Replica = struct { 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 (`Term.grid`'s view) 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. + /// Each terminal's view (`Term.grid`'s view) as the copy was taken: the + /// copy holds no emulator, and a speculation may move a view, so a + /// rollback puts these back. views: [pardes.MAX_PANES]?usize = @splat(null), /// Its panes' serials as the copy was taken, and whether one was being /// torn down then (`panesMoved`). serials: [pardes.MAX_PANES]u32 = @splat(0), retiring: bool = false, - screen_out: std.ArrayListUnmanaged(u8) = .empty, /// A record's bytes by hash coming in pieces (`blobPiece`), and how /// many of them came. incoming: ?tape.Big = null, @@ -709,26 +496,16 @@ pub const Replica = struct { cache: ?Cache = null, io: std.Io = undefined, /// Its predicted echo (core/term/echo.zig), drawn over its core's terminals: - /// fed by its keys, its units and its deltas here, and by `echoFrame`. + /// fed by its keys and its units here, and by `echoFrame`. echo: Echo = .{}, - const ScreenOf = struct { - serial: u32, - rep: *Term.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`). In process, its - /// timing samples kept for the harness that reads them. - pub fn init(gpa: Allocator, id: u16, first: []const u8, screens: bool) !*Replica { + /// A replica of the session whose first unit is `first`, its terminals + /// fed the pty's bytes the units carry. In process, its timing samples + /// kept for the harness that reads them; no predicted echo (a joined + /// client's has one: `made`). + pub fn init(gpa: Allocator, id: u16, first: []const u8) !*Replica { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa), .stats = .{ .samples = true } }; - r.t.screen_state = screens; r.t.core_gpa = r.epoch.allocator(); r.t.checks = false; r.confirm(first); @@ -736,7 +513,6 @@ pub const Replica = struct { r.deinit(); return error.NoCore; } - if (screens) r.core().echo = &r.echo; return r; } @@ -769,8 +545,6 @@ pub const Replica = struct { r.* = .{ .gpa = gpa, .id = id, .view = view orelse 0, .t = tape.memory(gpa), .guess = true }; r.t.skips_render = true; r.t.checks = false; - // Its terminals are the session's deltas (Peers sends them). - r.t.screen_state = true; errdefer { tape.release(&r.t); r.epoch.deinit(); @@ -834,7 +608,6 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); // The state it ends on is whole as it is: the copy let go, its @@ -854,7 +627,6 @@ pub const Replica = struct { 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); @@ -923,8 +695,8 @@ pub const Replica = struct { r.since_check +|= 1; if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); // The pty's bytes fed its emulators: its predicted echo judged - // against them, as a screen-state delta judges it (`screen`). - defer if (!r.t.screen_state and r.diverged == null and tape.feedsTerminal(u.tape)) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c); + // against them. + defer if (r.diverged == null and tape.feedsTerminal(u.tape)) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c); // A copy taken ahead (`ready`), nothing on top: it stays the point a // rollback goes back to, the unit replayed on it and kept in `since`, // not let go and taken again (a megabyte walked) for each unit of @@ -948,11 +720,12 @@ pub const Replica = struct { // nothing ends on again (`neutral`); none, not known. r.last_digest.clearRetainingCapacity(); if (lastDigest(u.tape)) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; - if (r.pending.items[0].terminal and (r.applied > 1 or !r.t.screen_state)) { - // 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. + if (r.pending.items[0].terminal) { + // Right, but what it did wrote an emulator (or owes one a + // write: a grid's size): replayed later (a rollback's + // `since`) it would write it again. 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); r.freePending(r.pending.orderedRemove(0)); @@ -1007,7 +780,7 @@ pub const Replica = struct { // place, and much of it no digest covers (a colour set, a mode, the // pages a reload remakes). if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items) and - (r.t.screen_state or !tape.feedsTerminal(u.tape))) + !tape.feedsTerminal(u.tape)) { // Nothing a digest covers moved: what was applied on top is // what the session will apply on top of it. Replayed when the @@ -1076,7 +849,7 @@ pub const Replica = struct { const c = r.core(); for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; for (c.panes, serials) |slot, n| retiring = retiring or (if (slot) |pane| pane.serial else 0) != n; - if (retiring or (!r.t.screen_state and Term.writes != writes)) { + if (retiring or Term.writes != writes) { r.copy.release(); rollback.Epoch.select(null); return r.clearSince(); @@ -1189,18 +962,14 @@ pub const Replica = struct { c.toView(.at(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.view().active]) |pane| pane.terminal != null else false; + // and stops here (core/echo.zig predicts its echo). The rest + // (pardes's own keys over it, the wheel) reads its emulator, fed + // the same bytes in the same order as the session's: it is applied. c.update(in.event); c.step(); - // Under bytes: it owes an emulator a write it skipped (a grid's - // size), replayed when confirmed. - p.terminal = if (r.t.screen_state) keyboard or r.t.touched else r.t.touched; + // It owes an emulator a write it skipped (a grid's size): replayed + // when confirmed. + p.terminal = r.t.touched; const clean = tape.endSpeculation(&r.t); const dt = nowNs() - t0; // Where a blob was written, should one be: the input that did. @@ -1315,134 +1084,6 @@ pub const Replica = struct { return pardes.views.drawOne(c, r.view, present) catch true; } - /// 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| Term.replicaFree(old.rep, pardes.panes.emulator_gpa); - slot.* = null; - const rep = Term.replica(pardes.panes.emulator_gpa) catch { - r.fail("no memory for a terminal's replica"); - return null; - }; - slot.* = .{ .serial = m.serial, .rep = rep }; - } - 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 => { - // A unit kept to replay (`since`) read the emulator as it is - // now, and its step may have read it (a key over a shell's - // text reads its rows, an output its prompt): replayed over - // this delta it would read another (the soak, a copy taken - // ahead, three clients: another's key that read the rows, - // replayed by a rollback after the full-screen program - // redrew them, came to another state). So the units since go - // before it: let go with the copy when nothing is on top (the - // state is the confirmed one), else replayed now. - if (r.copy.held and r.since.items.len > 0) { - const was = Stepping.begin(&r.t, &r.epoch); - defer was.end(); - if (r.applied == 0) { - r.copy.release(); - rollback.Epoch.select(null); - r.clearSince(); - } else { - r.toConfirmed(); - r.dirty = true; - } - } - const t0 = nowNs(); - const applied = Term.apply(state, sc.rep, 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, &.{}); - }; - if (r.stats.samples) r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; - sc.refused = false; - r.echo.delta(c, m.pane, Term.replicaTop(sc.rep)); - // 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] = pardes.Term.grid(state).view; - 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; - Term.applyRows(state, sc.rep, 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 = Term.shown(state, sc.rep); - var holes: std.ArrayList([2]u64) = .empty; - defer holes.deinit(r.gpa); - Term.missing(sc.rep, 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] }; - Term.blank(state, sc.rep, 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; - } - /// A piece of a record's bytes by hash (Peer.blob): whole and checked /// against its hash, kept for the unit that names it (tape.keepFile), /// and a file's, a far client's, on disk (`cache`) for its next join. @@ -1720,8 +1361,6 @@ pub const Peer = struct { again: bool = false, /// ...or that its window was closed (Peers.closed). close: 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). @@ -2010,13 +1649,6 @@ pub const Peer = struct { 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; @@ -2152,25 +1784,18 @@ pub const Peers = struct { boundary_ns: std.ArrayListUnmanaged(u64) = .empty, cut: u32 = 0, head: [unit_head]u8 = undefined, - /// Its clients' terminals as screen state (core/term/screen_state.zig), while - /// it records: what each was sent. - screens: Screens = undefined, - msgs: std.ArrayListUnmanaged([]u8) = .empty, /// False: its clients' links are in this process's memory, a thread /// for none of them (the soak's, test/replicas.zig): what a client's /// writer would write (its join, then `out`) and its reader read /// (`Peer.take`, then `.gone` posted) are its driver's to do. threads: bool = true, - /// After each flush of a client's terminals, told: the soak's record - /// of each terminal as each version left it (test/replicas.zig). - flushed: ?struct { ctx: *anyopaque, f: *const fn (*anyopaque, *Pardes, *Screens) void } = null, /// Its clients' terminals cross as the pty's bytes in its units, each /// client's emulator its own (P29, P32), not as screen state: every /// session's (runtime/session.zig) but under PARDES_TERM_SCREEN, the /// soak's `--bytes`; its welcome says so to each client. bytes: bool = false, - pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer }; + pub const Note = union(enum) { join: Joining, input: Msg, gone: *Peer, check: *Peer, joined: *Peer }; /// A connection to join, and the view it joins again for, if it does. pub const Joining = daemon.Joining; pub const Msg = struct { peer: *Peer, msg: []u8 }; @@ -2191,13 +1816,13 @@ pub const Peers = struct { ps.lock.unlock(ps.io); switch (note) { .join => |j| _ = libc.close(j.fd), - .input, .screen => |in| ps.gpa.free(in.msg), + .input => |in| ps.gpa.free(in.msg), .gone, .check, .joined => {}, } return; }; ps.mail.store(true, .release); - if (note != .screen) ps.urgent.store(true, .release); + ps.urgent.store(true, .release); ps.lock.unlock(ps.io); if (ps.wake) |w| w(ps.wake_ctx); } @@ -2227,8 +1852,6 @@ pub const Peers = struct { 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 }); ps.t.buf.clearRetainingCapacity(); - // The sizes its steps gave terminals, now: the next flush says so. - if (ps.t.core) |core| pardes.panes.resizeDeferred(core); } /// After each of the session's pumps: its unit sent, the clients that @@ -2239,12 +1862,8 @@ pub const Peers = struct { const t0 = nowNs(); defer if (ps.recording and ps.samples) 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); @@ -2257,20 +1876,6 @@ pub const Peers = struct { ps.input(core, in.peer, in.msg); ps.gpa.free(in.msg); }, - .screen => |in| { - defer ps.gpa.free(in.msg); - // An acknowledgement (or a reset): room again in its flight - // for what its terminals came to since. Its flush at this - // boundary came before it, and at the bound sent nothing: - // with no step after it, that client kept an old screen. - const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse { - ps.screensTo(core, in.peer, .flush); - 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| { @@ -2285,20 +1890,12 @@ pub const Peers = struct { const in = parseInput(msg) orelse return; const t0 = nowNs(); defer if (ps.samples) 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); - // What the step made of a kind's pane (the tick after a key or a - // click into it: another layout) goes now, not at the next pump's: - // one may not come before the client looks. - ps.screensOut(core, .flush); } /// What its steps recorded passed a unit's bound (`tape.unit_max`): a @@ -2309,33 +1906,6 @@ pub const Peers = struct { ps.unit(0, ps.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 { - if (ps.bytes) return; - 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; - if (what == .flush) if (ps.flushed) |h| h.f(h.ctx, core, &ps.screens); - for (ps.msgs.items) |m| { - const head = wire.header(@backingInt(wire.ServerTag.screen), m.len); - peer.send(&.{ &head, m }); - } - } - /// A client on `joining.fd` joined: a dump of the session now, its /// threads started. At a step boundary (`boundary`), or a session's /// first client before its first effects (runtime/session.zig). @@ -2355,12 +1925,9 @@ pub const Peers = struct { if (tape.on != null) return daemon.refuseReplica(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 = !ps.bytes; + // A unit's digest is a word (PARDES_REPLICA_TRACE: one a field). ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; ps.t.cut = .{ .ctx = ps, .f = cutUnit }; - ps.screens = .init(ps.gpa); tape.on = &ps.t; core.tape = &ps.t; ps.recording = true; @@ -2438,11 +2005,6 @@ pub const Peers = struct { slot.* = peer; peer.pid = peerPid(fd); ps.tellClients(); - // 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 }); } @@ -2492,7 +2054,6 @@ pub const Peers = struct { }; log.info("client {d} left '{s}'", .{ peer.id, ps.name }); ps.tellClients(); - ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view.index())); // Cut off, it joins again after the words why (`send`); its reader // has ended and no other thread sets `cut` now. @@ -2535,7 +2096,7 @@ pub const Peers = struct { var i: usize = 0; while (i < ps.inbox.items.len) { const mine = switch (ps.inbox.items[i]) { - .input, .screen => |in| in.peer == peer, + .input => |in| in.peer == peer, .gone, .check, .joined => |p| p == peer, .join => false, }; @@ -2544,7 +2105,7 @@ pub const Peers = struct { continue; } switch (ps.inbox.items[i]) { - .input, .screen => |in| ps.gpa.free(in.msg), + .input => |in| ps.gpa.free(in.msg), else => {}, } _ = ps.inbox.orderedRemove(i); @@ -2662,8 +2223,6 @@ pub const Peers = struct { fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; - pardes.panes.resizeDeferred(core); - ps.screens.deinit(core); tape.on = null; core.tape = core.opts.tape orelse &tape.none; tape.release(&ps.t); @@ -2682,7 +2241,6 @@ pub const Peers = struct { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view.index())); ps.closeViewOf(core, peer, again); - ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; ps.tellClients(); @@ -2690,7 +2248,7 @@ pub const Peers = struct { 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), + .input => |in| ps.gpa.free(in.msg), .gone, .check, .joined => {}, }; ps.inbox.clearRetainingCapacity(); @@ -2704,7 +2262,6 @@ 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); ps.each_join.deinit(ps.gpa); } @@ -3110,18 +2667,6 @@ 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; @@ -3136,9 +2681,9 @@ pub fn bye(fd: c_int, how: @FieldType(wire.ClientMsg, "bye")) void { } /// 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, blob: []u8, ended: u8, left }; +/// takes it: a unit's bytes and a refusal's words on the C heap, the loop's +/// to free. +pub const Said = union(enum) { unit: []u8, checked: u64, refused: []u8, blob: []u8, ended: u8, left }; /// 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 @@ -3149,7 +2694,6 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime 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 }, .blob => .{ .blob = 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 }, @@ -3158,11 +2702,11 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime .pong, .welcome, .dump, .blobs, .zstd => return true, }; if (!post(x, msg)) switch (msg) { - .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), + .unit, .refused, .blob => |b| std.heap.c_allocator.free(b), .checked, .ended, .left => {}, }; // A last word ends the stream. - return msg == .unit or msg == .screen or msg == .checked or msg == .blob; + return msg == .unit or msg == .checked or msg == .blob; } }; rest.run(gpa, fd, ctx, Take.take); @@ -3576,11 +3120,6 @@ pub const Link = struct { // after it in the stream and its digest covers it. if (!c.asked) 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; - }, .blob => |bytes| { defer std.heap.c_allocator.free(bytes); c.r.blobPiece(bytes); @@ -3604,9 +3143,7 @@ pub const Link = struct { } /// 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. + /// written to it): a rollback it may have shown counted. pub fn drawn(c: *Link, drew: bool) void { if (c.r.stats.mispredicted > c.mispredicted) { c.mispredicted = c.r.stats.mispredicted; @@ -3617,10 +3154,6 @@ pub const Link = struct { // What moved before this frame is in it: not drawn again at the // next unit (a stopped input's rollback, under its own frame). c.r.moved = false; - while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) { - c.cut(); - break; - }; } /// The session's unit `bytes`, taken now: how far behind it is. Past @@ -3817,29 +3350,6 @@ fn key(r: *Replica, cp: u21, sent: *std.ArrayListUnmanaged([]u8)) !void { try sent.append(std.testing.allocator, try std.testing.allocator.dupe(u8, msg)); } -/// What the session sends a peer: its terminals' changes (`.flush`), or, -/// around a step's unit (Peers.input), the rows the step read that the -/// peer lacks (`.fill`), after which the caller gives terminals the sizes -/// the step gave them (`panes.resizeDeferred`), then the unit. -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); - }; -} - /// The blob cache of a test, in its own directory (never the user's). fn testCache(tmp: *std.testing.TmpDir, buf: *[4200]u8, cap: u64) !Cache { var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; @@ -3935,34 +3445,21 @@ fn inputOf(buf: []u8, client: u16, seq: u32, ev: pardes.Event) ![]const u8 { } /// 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 { +/// it: the step, then its unit to every replica. +fn takeAll(a: *Authority, rs: []const *Replica, msg: []const u8) !void { const gpa = std.testing.allocator; - for (rs, ps) |r, peer| try deliver(a, screens, peer, r, .flush); 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 { +fn sendAs(r: *Replica, a: *Authority, rs: []const *Replica, 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); + try takeAll(a, rs, msg); } fn shows(p: *Pardes, text: []const u8) !bool { @@ -4044,9 +3541,9 @@ test "an Upgrade tells a client handed to the session and not joined yet that it 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); try std.testing.expectEqual(digestOf(a), r.digest()); var sent: std.ArrayListUnmanaged([]u8) = .empty; @@ -4071,9 +3568,9 @@ test "a replica's own keys apply at once, confirm on the fast path, and end at t 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); var sent: std.ArrayListUnmanaged([]u8) = .empty; defer { @@ -4108,13 +3605,13 @@ test "an input's time is its client's: an edit's second confirms on the fast pat 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); - const r1 = try Replica.init(gpa, 1, first, false); + const r1 = try Replica.init(gpa, 1, first); defer r1.deinit(); - const r2 = try Replica.init(gpa, 2, first, false); + const r2 = try Replica.init(gpa, 2, first); defer r2.deinit(); var s1: std.ArrayListUnmanaged([]u8) = .empty; var s2: std.ArrayListUnmanaged([]u8) = .empty; @@ -4149,11 +3646,11 @@ test "another client's input first: a replica rolls back, replays it and applies test "the copy taken for an input of its own stays once the input is confirmed: the next takes none, and a rollback replays the first over it" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); - const r = try Replica.init(gpa, 1, first, false); + const r = try Replica.init(gpa, 1, first); defer r.deinit(); var sent: std.ArrayListUnmanaged([]u8) = .empty; defer { @@ -4182,7 +3679,7 @@ test "a client marks its view moved for a unit of the session's whose steps aske // clock, or a ctl write's close, draws nothing; a client presented it // again (the corpus's pet: one picture twice). const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); // A client as a dial joins it: it draws none of its replay. const r = try joinedNow(a, 1, 0); @@ -4207,9 +3704,9 @@ test "the session's pump units that moved nothing, kept under a key applied on t // of them is the bound's is the arrivals' parity, and a pump's unit // while idle turns it. const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); var sent: std.ArrayListUnmanaged([]u8) = .empty; defer { @@ -4255,11 +3752,11 @@ test "a rollback over a Tab it applied measures tabs as its core has them again" // a Tab undone left it, and the next replayed step measured the files' // tabs otherwise (the soak, five clients). const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); - const r = try Replica.init(gpa, 1, first, false); + const r = try Replica.init(gpa, 1, first); defer r.deinit(); const was = r.core().settings.tab; const msg = try gpa.dupe(u8, try r.input(.{ .command = "Tab 2" })); @@ -4275,101 +3772,12 @@ test "a rollback over a Tab it applied measures tabs as its core has them again" try agrees(a, r); } -test "a delta applied under a speculation outlives its rollback: the terminal stays the authority's newest" { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - 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.view().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 deliver(a, &screens, peer, r, .flush); - 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 = .at(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 deliver(a, &screens, peer, r, .flush); - // The rows the core keeps, read once under the copy. - _ = try pardes.edit.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 deliver(a, &screens, peer, r, .flush); - // 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 pardes.edit.shellRows(r.core(), r.core().panes[shell].?); - try key(r, 'x', &sent); - for (0..40) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); - r.confirm((try a.pending()).?); - r.settle(); - try deliver(a, &screens, peer, r, .flush); - r.confirm(try a.local(.{ .focus = true })); - r.settle(); - const n = a.core.panes[shell].?.rows; - const back = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); - const now = try pardes.edit.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 deliver(a, &screens, peer, r, .flush); - 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 Term.dumpScreen(a.core.panes[shell].?.terminal.?, &dump_a.writer); - try Term.dumpScreen(r.core().panes[shell].?.terminal.?, &dump_r.writer); - try std.testing.expectEqualStrings(dump_a.written(), dump_r.written()); - const rows_a = try pardes.edit.shellRows(a.core, a.core.panes[shell].?); - const rows_r = try pardes.edit.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 (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -4392,13 +3800,13 @@ test "a closed terminal brought back on a screen-state replica: its emulator on test "a view that sized a terminal closing leaves nothing queued: the others' replicas copy, type and agree" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, TestHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty }, TestHost.host()); defer a.deinit(); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); - const r1 = try Replica.init(gpa, 1, first, true); + const r1 = try Replica.init(gpa, 1, first); defer r1.deinit(); - const r2 = try Replica.init(gpa, 2, first, true); + const r2 = try Replica.init(gpa, 2, first); defer r2.deinit(); a.core.view().active = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break id; @@ -4436,244 +3844,12 @@ test "a view that sized a terminal closing leaves nothing queued: the others' re try std.testing.expectEqual(digestOf(a), r1.digest()); } -test "a terminal sized again by a client's key: the step reads the grid it had, on both sides" { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - 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.view().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 = .at(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 deliver(a, &screens, peer, r, .flush); - 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 deliver(au, sc, pe, rr, .flush); - const u = try std.testing.allocator.dupe(u8, try au.take(msg)); - defer std.testing.allocator.free(u); - try deliver(au, sc, pe, rr, .fill); - pardes.panes.resizeDeferred(au.core); - 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 deliver(a, &screens, peer, r, .flush); - { - const was = Stepping.begin(&a.t, null); - a.core.toView(.at(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 deliver(a, &screens, peer, r, .fill); - pardes.panes.resizeDeferred(a.core); - r.confirm(back); - r.settle(); - try deliver(a, &screens, peer, r, .flush); - try key(r, 'w', &sent); - try key(r, 'w', &sent); - for (sent.items) |m| try Step.take(a, &screens, peer, r, m); - try deliver(a, &screens, peer, r, .flush); - 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()); -} - -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 (pardes.Term.ops == null) return error.SkipZigTest; // a shell - 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.view().active = shell; - r.confirm(try a.local(.{ .focus = true })); - var line: [64]u8 = undefined; - for (0..200) |i| a.output(.{ .output = .{ .pane = .at(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(pardes.Term.grid(here.terminal).view != null); - // 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 = .at(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(pardes.Term.grid(r.core().panes[shell].?.terminal).view != 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(pardes.Term.grid(a.core.panes[shell].?.terminal).view != null); - try std.testing.expectEqual(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); - // 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 = pardes.Term.grid(a.core.panes[shell].?.terminal).view; - try std.testing.expect(pardes.Term.grid(r.core().panes[shell].?.terminal).view.? < 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, pardes.Term.grid(r.core().panes[shell].?.terminal).view); - 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(pardes.Term.grid(a.core.panes[shell].?.terminal).view, pardes.Term.grid(r.core().panes[shell].?.terminal).view); -} - -test "a unit kept over a copy taken ahead is never replayed over a delta that came after it: the session's key read a shell's rows, the program redrew them" { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - 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.view().active = shell; - r.confirm(try a.local(.{ .focus = true })); - var line: [96]u8 = undefined; - for (0..30) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "line {d} {s}\r\n", .{ i, "words of a length its own"[0 .. i % 20 + 4] }) } }); - r.confirm((try a.pending()).?); - try deliver(a, &screens, peer, r, .flush); - // Out of the shell's tty mode, in the session's view: keys over its text. - r.confirm(try a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } })); - r.settle(); - // The client idle: a copy taken ahead. The session's keys read the rows - // (up, to the line's end): replayed here over the copy, and kept. - r.ready(); - try std.testing.expect(r.copy.held); - for ([_]u21{ 'k', 'k', '$' }) |cp| { - const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = cp, .text = &.{@intCast(cp)} } })); - defer gpa.free(u); - try deliver(a, &screens, peer, r, .fill); - r.confirm(u); - r.settle(); - } - // The program redraws the screen: other rows where those keys read. - a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b[H\x1b[2J" } }); - for (0..30) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); - r.confirm((try a.pending()).?); - r.settle(); - try deliver(a, &screens, peer, r, .flush); - // The client types, and the session's key comes first: a rollback, which - // must not replay the keys that read the rows over the redrawn ones. - var sent: std.ArrayListUnmanaged([]u8) = .empty; - defer { - for (sent.items) |m| gpa.free(m); - sent.deinit(gpa); - } - try key(r, 'h', &sent); - try std.testing.expectEqual(@as(usize, 1), r.applied); - { - const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = '0', .text = "0" } })); - defer gpa.free(u); - try deliver(a, &screens, peer, r, .fill); - r.confirm(u); - r.settle(); - } - try deliver(a, &screens, peer, r, .flush); - r.confirm(try a.take(sent.items[0])); - 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()); -} - test "a unit that closes or tears down a terminal is never kept over a copy taken ahead: a rollback would close its emulator again" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); const text = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.file != null) break id; @@ -4738,9 +3914,9 @@ const BytesPair = struct { fn init() !BytesPair { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host()); errdefer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); var shell: ?pardes.Pane.Id = null; for (a.core.panes, 0..) |slot, id| if (slot) |pane| { if (pane.isTerminal()) shell = .at(id) else if (pane.file != null) a.core.view().active = @intCast(id); @@ -4958,7 +4134,7 @@ test "a Restore tells its clients so before it lets them go (each joins again, s fn post(l: *@This(), msg: Said) bool { switch (msg) { .refused => |w| l.words = w, - .unit, .screen, .blob => |b| std.heap.c_allocator.free(b), + .unit, .blob => |b| std.heap.c_allocator.free(b), .checked, .ended, .left => {}, } return true; @@ -5073,7 +4249,7 @@ fn firstOnPair(bytes: bool) !void { const core = try Pardes.init(gpa, opts); defer core.deinit(); core.host = .{ .ctx = &spawns, .vtable = &Spawns.vtable }; - var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .bytes = bytes, .detached = true }; + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .bytes = true, .detached = true }; defer ps.deinit(); defer live_peers = null; defer ps.dropAll(core); @@ -5123,7 +4299,7 @@ fn firstOnPair(bytes: bool) !void { } else return error.NoShell; core.update(.{ .output = .{ .pane = .at(shell), .bytes = "first$ " } }); ps.boundary(core); - // Its check, answered after every unit and terminal message before it. + // Its check, answered after every unit before it. try std.testing.expect(askCheck(j.fd)); while (!ps.mail.load(.acquire)) std.Thread.yield() catch {}; ps.boundary(core); @@ -5134,9 +4310,6 @@ fn firstOnPair(bytes: bool) !void { fn take(t: *@This(), tag: u8, payload: []const u8) bool { switch (std.enums.fromInt(wire.ServerTag, tag) orelse return false) { .unit => t.r.confirm(payload), - .screen => if (t.r.screen(payload)) |reply| { - _ = sendScreen(t.fd, reply); - }, .check => { t.digest = (wire.decodeServer(tag, payload) catch return false).check; return false; @@ -5357,7 +4530,7 @@ test "bytes: a kitty image shown by a file, a temporary file or shared memory is 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); _ = try a.local(.{ .key = .{ .cp = 'j', .text = "j" } }); const was = Stepping.begin(&a.t, null); @@ -5393,7 +4566,7 @@ test "a replica joined from a dump replays a step that opens a file's pane and s // No rows for a second pane: the Look's pane of a kind is made, the // placement says it has no room (a message in the step that made the // pane, which was not in /log yet), and the pane goes. - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 5, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 5, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const zon = pardes.Kind.loadedOf(&Zon.ops, "/x/zon.so", @splat(9), &words); @@ -5427,7 +4600,7 @@ const Zon = struct { test "a replica joined from a themed dump paints its taglines in the restored theme" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host()); defer a.deinit(); _ = try a.local(.{ .command = "Theme rose_pine_moon" }); const was = Stepping.begin(&a.t, null); @@ -5460,7 +4633,7 @@ test "a host's Adopt and a terminal reader's failure are inputs: a replica makes if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, opened by a key 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const was = Stepping.begin(&a.t, null); const bytes = try pardes.dump.dumpBytes(a.core); @@ -5486,7 +4659,7 @@ test "a host's Adopt and a terminal reader's failure are inputs: a replica makes 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const text = try gpa.alloc(u8, 20000); defer gpa.free(text); @@ -5612,7 +4785,7 @@ test "a far join is sent each blob once: joined again with them on disk or in th 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const text = try gpa.alloc(u8, 30000); defer gpa.free(text); @@ -5707,7 +4880,7 @@ test "a far join is sent each blob once: joined again with them on disk or in th 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); // A file looked at and closed: a jump to it, Recent's, a closed pane. var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; @@ -5759,7 +4932,7 @@ test "a replica joined after a pane closed: the session's jump to it, its Panes 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; @@ -5778,11 +4951,11 @@ test "a replica's Del of a file its session's Recent knows asks the host what th 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" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, PtyHost.host()); 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); + const old = try Replica.init(gpa, 1, first); defer old.deinit(); // A second client, bigger, types into the terminal: it sizes it. const opened = try a.openView(2); @@ -5817,11 +4990,11 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const first = try gpa.dupe(u8, try a.unit(0, 0)); defer gpa.free(first); - const old = try Replica.init(gpa, 1, first, false); + const old = try Replica.init(gpa, 1, first); defer old.deinit(); const opened = try a.openView(2); old.confirm(opened.unit); @@ -5961,9 +5134,9 @@ test "a client cut off for what it left unread is told why and joins again for i test "a client whose session's units come more than two seconds later than its quickest lets its link go, says so once, and joins again; its first unit after a join learns the clocks again" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); var fds: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; defer for (fds) |fd| { @@ -6061,7 +5234,7 @@ test "a client's writer, the client cut off while it waits on the socket, writes 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); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host()); 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. @@ -6090,7 +5263,7 @@ test "a client joined from a dump moves through another view's pane as the sessi test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has drawn its owner" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host()); defer a.deinit(); // The session draws: its owner's colours painted for the view (File.refreshHighlights). { @@ -6126,7 +5299,7 @@ test "a client joined from a dump asks a worker for a `/` query's colours as the }; const gpa = std.testing.allocator; // A view as tall as the text: the frame paints every line of it. - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 500, .file = "build.zig" }, .{ .vtable = &Paints.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 500, .file = "build.zig" }, .{ .vtable = &Paints.vtable }); defer a.deinit(); { const was = Stepping.begin(&a.t, null); @@ -6156,7 +5329,7 @@ test "a client joined from a dump asks a worker for a `/` query's colours as the const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .search = search }; }; const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &Paints.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &Paints.vtable }); defer a.deinit(); { const p = a.core; @@ -6177,49 +5350,11 @@ test "a client joined from a dump asks a worker for a `/` query's colours as the } } -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 (pardes.Term.ops == null) return error.SkipZigTest; // a shell - 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.view().active = shell; - var line: [64]u8 = undefined; - for (0..60) |i| a.output(.{ .output = .{ .pane = .at(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); -} - test "a replica with no code for a kind whose state is not plain shows its pane as a line saying so, and its keys go to the authority" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); // What the host read off the library, as the `addon` event: the // replica's core takes the kind's words, never its code. @@ -6277,9 +5412,9 @@ const Placed = struct { test "a kind's place moved in a step of another view's: a replica's key that reads it guesses the authority's" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); @@ -6333,7 +5468,7 @@ const Jobber = struct { test "a client joined from a dump numbers a kind's next job as the session does" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const jobber = pardes.Kind.loadedOf(&Jobber.ops, "/x/jobber.so", @splat(7), &words); @@ -6350,9 +5485,9 @@ test "a client joined from a dump numbers a kind's next job as the session does" test "a replica's word that opens a kind's pane with no file waits for the authority's open: never mispredicted" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); @@ -6371,7 +5506,7 @@ test "a replica's word that opens a kind's pane with no file waits for the autho test "a client joined from a dump tells a kind each view's facts as the session does, the live one's and another's" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); @@ -6441,10 +5576,10 @@ const BusyHost = struct { test "a kind's job answered at once inside its host's call (no worker free) is taken by a client as the session took it" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &BusyHost.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &BusyHost.vtable }); defer a.deinit(); BusyHost.core = a.core; - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const asker = pardes.Kind.loadedOf(&Asker.ops, "/x/asker.so", @splat(7), &words); @@ -6465,9 +5600,9 @@ test "a kind's job answered at once inside its host's call (no worker free) is t test "a closed terminal with a kitty image brought back on a client: its image drawn under a speculation, rolled back by another's unit, is drawn again from what the emulator holds" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host()); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -6539,40 +5674,35 @@ fn partsApart(a: *Authority, r: *Replica, arena: std.mem.Allocator) ![]const u8 return if (std.mem.eql(u64, w, g)) "" else tape.partsDiffer(g, w, arena); } -test "a terminal's program shows a kitty image under screen state: the client's emulator never has it, and its tag says what the session's does" { +test "a terminal's program shows a kitty image: the client's emulator has it as the session's does, and its tag says what the session's does" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, TestHost.host()); 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); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); 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; - try deliver(a, &screens, peer, r, .flush); try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator())); - // In the session's order: the pump's unit, then what its terminals came - // to; then a step after both, on every side. + // In the session's order: the pump's unit, its bytes fed on every side; + // then a step after it. a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } }); r.confirm((try a.pending()).?); r.settle(); - try deliver(a, &screens, peer, r, .flush); r.confirm(try a.local(.{ .focus = true })); r.settle(); try std.testing.expect(pardes.Term.hasImages(a.core.panes[shell].?.terminal)); - try std.testing.expect(!pardes.Term.hasImages(r.core().panes[shell].?.terminal)); + try std.testing.expect(pardes.Term.hasImages(r.core().panes[shell].?.terminal)); try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator())); try agrees(a, r); const prefix = try pardes.tagline.prefixIn(r.core(), arena.allocator(), r.core().panes[shell].?); try std.testing.expect(std.mem.indexOf(u8, prefix, " petscii:") != null); } -test "a command typed into a shell with prompt marks under screen state: every side notes it as the session's emulator numbers it, and Kill finds it" { +test "a command typed into a shell with prompt marks: every side notes it as its own emulator, fed the same bytes, numbers it, and Kill finds it" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; var arena: std.heap.ArenaAllocator = .init(gpa); @@ -6584,12 +5714,9 @@ test "a command typed into a shell with prompt marks under screen state: every s fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &ShellHost.vtable }, true); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &ShellHost.vtable }); 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); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -6599,7 +5726,6 @@ test "a command typed into a shell with prompt marks under screen state: every s a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } }); r.confirm((try a.pending()).?); r.settle(); - try deliver(a, &screens, peer, r, .flush); a.core.view().active = shell; r.confirm(try a.local(.{ .focus = true })); r.settle(); @@ -6607,7 +5733,7 @@ test "a command typed into a shell with prompt marks under screen state: every s // Exec'd in the shell: typed at its prompt, the command noted. r.confirm(try a.local(.{ .command = "Exec sleep 9" })); r.settle(); - // Its marks cross with the cells (Term.marksOf): numbered alike. + // Its marks are its bytes' on every side: numbered alike. try std.testing.expectEqual(@as(u32, 2), a.core.panes[shell].?.sent_command.?.seq); try std.testing.expectEqual(@as(u32, 2), r.core().panes[shell].?.sent_command.?.seq); try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator())); @@ -6615,7 +5741,6 @@ test "a command typed into a shell with prompt marks under screen state: every s a.output(.{ .output = .{ .pane = .at(shell), .bytes = "sleep 9\r\n\x1b]133;C;aid=pardes\x07" } }); r.confirm((try a.pending()).?); r.settle(); - try deliver(a, &screens, peer, r, .flush); r.confirm(try a.local(.{ .command = "Kill" })); r.settle(); try std.testing.expectEqualStrings("", try partsApart(a, r, arena.allocator())); @@ -6680,7 +5805,6 @@ test "a step of the session's own that reads a terminal right after its output, var at: usize = 0; while (try wire.framed(from.out.items[at..])) |m| : (at += m.total) l.take(switch (std.enums.fromInt(wire.ServerTag, m.tag).?) { .unit => .{ .unit = try std.heap.c_allocator.dupe(u8, m.payload) }, - .screen => .{ .screen = try std.heap.c_allocator.dupe(u8, m.payload) }, else => return error.UnexpectedFrame, }); from.out.clearRetainingCapacity(); @@ -6711,7 +5835,7 @@ test "a step of the session's own that reads a terminal right after its output, test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); @@ -6792,7 +5916,7 @@ const SeenHost = struct { test "a client's step that asks the session's host for something that returns nothing goes on at once, and the session's unit confirms it: nothing rolled back" { const gpa = std.testing.allocator; var h: SeenHost = .{}; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host()); defer a.deinit(); h.core = a.core; const r = try joinedFresh(a); @@ -6812,7 +5936,7 @@ test "a client's step that asks the session's host for something that returns no test "a client's step whose session's host says something into the core during the call it made: its speculation rolled back, then the session's state" { const gpa = std.testing.allocator; var h: SeenHost = .{ .tell = true }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host()); defer a.deinit(); h.core = a.core; const r = try joinedFresh(a); @@ -6835,7 +5959,7 @@ test "a client's ThemeFile waits for the session, whose host loads the file into const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var line: [std.Io.Dir.max_path_bytes + 16]u8 = undefined; var h: SeenHost = .{}; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host()); defer a.deinit(); h.core = a.core; const r = try joinedFresh(a); @@ -6855,7 +5979,7 @@ test "a client's ThemeFile waits for the session, whose host loads the file into test "a client's yank and paste of the system clipboard are its own frontend's: its view counts them, at once and as the session does, and the session's host is asked neither" { const gpa = std.testing.allocator; var h: SeenHost = .{}; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host()); defer a.deinit(); h.core = a.core; _ = try a.unit(0, 0); @@ -6888,7 +6012,7 @@ test "a client's yank and paste of the system clipboard are its own frontend's: test "a client's own Zerox and Del of a file pane are instant: what Recent keeps of the file is what its open learned, not a question to the disk; Recent has its place as the session's always did" { const gpa = std.testing.allocator; var h: SeenHost = .{}; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, h.host()); defer a.deinit(); h.core = a.core; var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; @@ -6931,7 +6055,7 @@ const Still = struct { test "a client's n in a kind's pane is a motion: it steps to the next place a result row holds at once, asking the disk nothing of words that might name files" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); @@ -7255,7 +6379,7 @@ const Walk = struct { test "a far client's every word and key, on a file's pane, a kind's and a shell's, and the gestures between them beside a shell that asked for focus events: what waits is in may_wait, with why, and nothing else waits" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, FullHost.host(), true); + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, FullHost.host()); defer a.deinit(); var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; @@ -7296,13 +6420,9 @@ test "a far client's every word and key, on a file's pane, a kind's and a shell' a.core.view().active = id; _ = a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } }) catch {}; }; - var screens: Screens = .init(gpa); - defer screens.deinit(a.core); - const peer = try screens.addPeer(); const r = try joinedFresh(a); defer r.deinit(); r.core().kinds.ops[r.core().kinds.byName("still").?] = &Still.ops; - try deliver(a, &screens, peer, r, .flush); r.settle(); try std.testing.expectEqual(id, r.core().view().active); if (on == .shell) try std.testing.expect(pardes.Term.modes(r.core().panes[id].?.terminal).focus); @@ -7384,7 +6504,7 @@ test "a far client's click into a shell whose program asked for focus events is const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_write = write }; }; Told.len = 0; - const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, .{ .vtable = &Told.vtable }, true); + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, .{ .vtable = &Told.vtable }); defer a.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -7392,12 +6512,8 @@ test "a far client's click into a shell whose program asked for focus events is a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b[?1004h$ " } }); _ = (try a.pending()).?; _ = try a.local(.{ .command = "New" }); - var screens: Screens = .init(gpa); - defer screens.deinit(a.core); - const peer = try screens.addPeer(); const r = try joinedFresh(a); defer r.deinit(); - try deliver(a, &screens, peer, r, .flush); r.settle(); Told.len = 0; const rect = r.core().view().rects[shell]; @@ -7411,10 +6527,8 @@ test "a far client's click into a shell whose program asked for focus events is try std.testing.expectEqual(@as(u32, 0), r.stats.stopped); try std.testing.expectEqual(@as(usize, shell), r.core().view().active); for (sent.items) |m| { - try deliver(a, &screens, peer, r, .flush); 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(); } @@ -7426,7 +6540,7 @@ test "a far client's click into a shell whose program asked for focus events is test "a far client's own Look of ~/x, bare Shell and a word that runs in the session's directory read the session's facts: its speculation asks its own machine's environment nothing" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const r = try joinedFresh(a); defer r.deinit(); @@ -7464,7 +6578,7 @@ test "a far client's replay of the session's DumpDir takes the session's answer // The session's machine: a directory it may not write in. if (std.c.mkdir(ro, 0o500) != 0) return error.NoDir; defer _ = std.c.chmod(ro, 0o700); - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); const r = try joinedFresh(a); defer r.deinit(); @@ -7522,7 +6636,7 @@ test "a far client's replay of the session's Fonts and Font takes the session's if (fonts.list(arena.allocator(), null).len == session_faces and client_named.len > 0 and std.mem.eql(u8, client_named[0].path, session_first)) return error.SkipZigTest; _ = Shell.setenv("HOME", session_home, 1); // A window's view: the font words are a window's. - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .windowed = true }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .windowed = true }, TestHost.host()); defer a.deinit(); const r = try joinedFresh(a); defer r.deinit(); @@ -7545,7 +6659,7 @@ test "a far client's replay of the session's Fonts and Font takes the session's // cover; an answer let go (not quiet then) covers nothing. test "an idle client's check answered covers the units that came while it was asked: it asks no other until something changes" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host()); defer a.deinit(); const r = try joinedNow(a, 1, 0); defer r.deinit(); @@ -7594,7 +6708,7 @@ test "an idle client's check answered covers the units that came while it was as // to look at, at its own focus-in (upgrade.noticeNewer). test "a far client's window found again is instant: its refocus asks the session's host nothing, and the session's unit confirms it" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, FullHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, FullHost.host()); defer a.deinit(); const r = try joinedFresh(a); defer r.deinit(); @@ -7643,7 +6757,7 @@ const Pointed = struct { // client's speculation). test "a far client's drag in a kind's pane it grabbed, the pane since too short for a row of body: the kind is told its edge, at once and as on the session" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const pointed = pardes.Kind.loadedOf(&Pointed.ops, "/x/pointed.so", @splat(8), &words); @@ -7744,7 +6858,7 @@ test "a far client's frame drawn under a speculation that rolls back asks a pict } }; const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host()); defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const drawn = pardes.Kind.loadedOf(&Drawn.ops, "/x/drawn.so", @splat(9), &words); @@ -7804,7 +6918,7 @@ test "a far client's click into a shell whose queue is backed up: the session's } const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_write = write }; }; - const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, .{ .vtable = &Told.vtable }, true); + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, .{ .vtable = &Told.vtable }); defer a.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -7812,12 +6926,8 @@ test "a far client's click into a shell whose queue is backed up: the session's a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b[?1004h$ " } }); _ = (try a.pending()).?; _ = try a.local(.{ .command = "New" }); - var screens: Screens = .init(gpa); - defer screens.deinit(a.core); - const peer = try screens.addPeer(); const r = try joinedFresh(a); defer r.deinit(); - try deliver(a, &screens, peer, r, .flush); r.settle(); // A 9P write into the shell, past the ring: the rest parked, on both. const big = try gpa.alloc(u8, pardes.memory.limits.effect_cap * pardes.effect_write_cap + 100); @@ -7848,10 +6958,8 @@ test "a far client's click into a shell whose queue is backed up: the session's try std.testing.expectEqual(@as(usize, 0), r.applied); for (r.stats.stops) |st| if (st.n > 0) try std.testing.expectEqualStrings("pty_write", st.name); for (sent.items) |m| { - try deliver(a, &screens, peer, r, .flush); 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(); } @@ -7912,7 +7020,7 @@ test "a client joined from a dump makes a `/` query's rows as the session does, } }; const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }); defer a.deinit(); WorkerSearch.init(a); defer WorkerSearch.q.deinit(); @@ -7939,7 +7047,7 @@ test "a client joined from a dump makes a `/` query's rows as the session does, // elsewhere on a core that drew than on one that did not. test "a client joined from a dump lands a click on a rail's tick as the session does, the session's ticks drawn before the places changed" { const gpa = std.testing.allocator; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host()); defer a.deinit(); _ = try a.local(.{ .command = "Search /pub" }); const owner: usize = 0; @@ -8006,7 +7114,7 @@ test "a far client's step that asks for a search goes on at once, and a session const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; var path_buf: [std.Io.Dir.max_path_bytes + 8]u8 = undefined; const path = try std.fmt.bufPrint(&path_buf, "{s}/a.zig", .{dir}); - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = path }, .{ .vtable = &LoopSearch.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = path }, .{ .vtable = &LoopSearch.vtable }); defer a.deinit(); LoopSearch.core = a.core; const r = try joinedFresh(a); @@ -8053,7 +7161,7 @@ test "a far client's replay of a message its session's host stamped takes the se const was = if (std.c.getenv("PARDES_NOTIME")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null; defer if (was) |v| gpa.free(v); defer _ = if (was) |v| Shell.setenv("PARDES_NOTIME", v, 1) else @import("dev").host_io.testUnsetenv("PARDES_NOTIME"); - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &Stamping.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, .{ .vtable = &Stamping.vtable }); defer a.deinit(); Stamping.core = a.core; const r = try joinedFresh(a); @@ -8082,9 +7190,9 @@ test "a client joined from a dump takes a 9P pty/run in a shell as the session d fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }); defer a.deinit(); - const r = try Replica.init(gpa, 1, try a.unit(0, 0), false); + const r = try Replica.init(gpa, 1, try a.unit(0, 0)); defer r.deinit(); const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); @@ -8146,158 +7254,6 @@ test "a client joined from a dump takes a 9P pty/run in a shell as the session d try agrees(a, late); } -// The same run with the terminals crossing as screen state (a session's -// clients', until the switch to bytes): a replica's emulator is the -// deltas', its marks not the session's. -test "a client of screen-state terminals takes a 9P pty/run in a shell as the session does" { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - const gpa = std.testing.allocator; - const RunHost = struct { - fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool { - return false; - } - fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} - const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; - }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, 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; - const Step = struct { - var second: ?*Replica = null; - fn take(s: *Authority, sc: *Screens, p: u16, x: *Replica, u: []const u8) !void { - const gpa_ = std.testing.allocator; - const kept = try gpa_.dupe(u8, u); - defer gpa_.free(kept); - // The unit first, its screens at the frame after (Peers). - x.confirm(kept); - x.settle(); - if (second) |y| { - y.confirm(kept); - y.settle(); - } - try deliver(s, sc, p, x, .flush); - if (second) |y| try deliver(s, sc, p + 1, y, .flush); - } - fn serve(s: *Authority, sc: *Screens, p: u16, x: *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply { - const was = Stepping.begin(&s.t, null); - const reply = s.core.serveFs(req); - s.core.pump(s.host) catch {}; - was.end(); - s.seq += 1; - try take(s, sc, p, x, try s.unit(0, s.seq)); - return reply; - } - fn output(s: *Authority, sc: *Screens, p: u16, x: *Replica, pane: u8, bytes: []const u8) !void { - s.output(.{ .output = .{ .pane = .at(pane), .bytes = bytes } }); - try take(s, sc, p, x, (try s.pending()).?); - } - }; - const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; - try Step.output(a, &screens, peer, r, shell, prompt ++ "ls\r\n\x1b]133;C;aid=pardes\x07a b\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt); - // A second client joins now, from the session's dump. - const joined = (try a.join(2, null)).?; - defer gpa.free(joined.dump); - try Step.take(a, &screens, peer, r, joined.unit); - const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); - defer late.deinit(); - const peer2 = try screens.addPeer(); - try deliver(a, &screens, peer2, late, .flush); - Step.second = late; - defer Step.second = null; - const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); - for (0..2) |_| { - const h = (try Step.serve(a, &screens, peer, r, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle; - _ = try Step.serve(a, &screens, peer, r, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" }); - try Step.output(a, &screens, peer, r, shell, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt); - const answer = try Step.serve(a, &screens, peer, r, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }); - try std.testing.expectEqualStrings("exit 0\n", a.core.fsPayload(answer)); - _ = try Step.serve(a, &screens, peer, r, .{ .tag = 4, .op = .release, .node = node, .handle = h, .opened = true }); - } - try agrees(a, r); - try agrees(a, late); -} - -// daemon.py --clients' client C (2026-11-20): idle, a copy taken ahead; the -// session's script opens pty/run and its command prints, replayed over the -// copy and kept; the client types into the shell, its speculation stops at -// the pty write, and the rollback replays the open again over a record the -// copy never held (it holds the opens as far as `opens_len`), whose line -// and output the epoch had freed: freed again there, the client's C heap -// was corrupt. -test "a 9P open the session made over a copy taken ahead, replayed after a rollback: the record past the opens the copy holds is written over, never freed again" { - if (pardes.Term.ops == null) return error.SkipZigTest; // a shell - const gpa = std.testing.allocator; - const RunHost = struct { - fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool { - return false; - } - fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} - const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; - }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, 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; - const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; - a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt } }); - r.confirm((try a.pending()).?); - try deliver(a, &screens, peer, r, .flush); - r.ready(); - try std.testing.expect(r.copy.held and r.core().fs.opens_len == 0); - const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); - const Serve = struct { - fn serve(s: *Authority, x: *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply { - const was = Stepping.begin(&s.t, null); - const reply = s.core.serveFs(req); - s.core.pump(s.host) catch {}; - was.end(); - s.seq += 1; - const u = try std.testing.allocator.dupe(u8, try s.unit(0, s.seq)); - defer std.testing.allocator.free(u); - x.confirm(u); - x.settle(); - return reply; - } - }; - const h = (try Serve.serve(a, r, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle; - _ = try Serve.serve(a, r, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "echo x\n" }); - a.output(.{ .output = .{ .pane = .at(shell), .bytes = "echo x\r\n\x1b]133;C;aid=pardes\x07x\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } }); - r.confirm((try a.pending()).?); - r.settle(); - // Kept over the copy: the record past its opens holds the run's output. - try std.testing.expect(r.copy.held and r.since.items.len > 0); - try std.testing.expectEqualStrings("x\n", r.core().fs.opens[h - 1].what.run.output); - var sent: std.ArrayListUnmanaged([]u8) = .empty; - defer { - for (sent.items) |m| gpa.free(m); - sent.deinit(gpa); - } - // A key into the shell: its speculation stops at the pty write, and - // the copy is put back, the units since replayed (the open again). - try key(r, 'e', &sent); - try std.testing.expect(r.applied == 0 and r.stats.stopped > 0); - r.confirm(try a.take(sent.items[0])); - r.settle(); - try deliver(a, &screens, peer, r, .flush); - _ = try Serve.serve(a, r, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); - if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); - try std.testing.expect(r.diverged == null and r.quiet()); - try agrees(a, r); -} - test "a script's half line held over a copy taken ahead, its next write run and its rest moved to the front in place, put back by a rollback: the line replayed is the session's" { var n = try NinepJoin.init("one\n", TestHost.host()); defer n.deinit(); @@ -8341,7 +7297,7 @@ test "a far client's Del with panes above and below is applied at once, and its } const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .now = now }; }; - const a = try Authority.init(gpa, .{ .cols = 80, .rows = 30, .file = "mise.toml" }, .{ .vtable = &Clocked.vtable }, false); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 30, .file = "mise.toml" }, .{ .vtable = &Clocked.vtable }); defer a.deinit(); _ = try a.local(.{ .command = "New" }); const mid = a.core.view().active; @@ -8393,7 +7349,7 @@ const NinepJoin = struct { serial: u32, fn init(text: []const u8, host: pardes.Host) !NinepJoin { - const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, host, false); + const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, host); errdefer a.deinit(); const was = Stepping.begin(&a.t, null); const pane = try a.core.setTestFile(text); @@ -8691,7 +7647,7 @@ const RunJoin = struct { /// The session at its shell's first prompt, and the shell's slot. fn init(n: *NinepJoin) !u8 { - const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &vtable }, false); + const a = try Authority.init(std.testing.allocator, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &vtable }); n.* = .{ .a = a, .serial = 0 }; _ = try a.unit(0, 0); const shell: u8 = for (a.core.panes, 0..) |slot, id| { -- cgit v1.3 From 70c9e3fbf9cdc7139b4da0142f3a44741a7e1491 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 03:03:25 -0300 Subject: replica: no road to choose, and the wire at 30 (the old road's deletion, piece 4 of 8; the coordinator's decision: the welcome's road byte goes, no dead field stays on the wire): `Peers.bytes` (set by runtime/session.zig unless PARDES_TERM_SCREEN), the welcome's `bytes` byte (abi/wire.zig: its encode, its decode, the round-trip test's byte), `Joined.bytes` and `joinOn`'s copy of it, `road` (fromDump's, ownBoot's, confirmBoot's, a test's: a replica's `made` arms its echo already, and no replica starts on screen state since piece 3), and ownBoot's read of PARDES_TERM_SCREEN; so PARDES_TERM_SCREEN and PARDES_TERM_BYTES name nothing in the tree; abi/wire.zig's version is 30, its history line saying both of this line's wire changes (the screen messages retired as gaps in piece 3, the welcome's byte here); test/daemon.py's `WIRE` 30; firstOnPair's argument is now what it chose, `by_dump` (a client joined by the session's dump, or one that makes its own core); core/root.zig `machine_lines` for replica.zig 74 -> 73; core-test 911/1068 (157 skipped), shell-test 1110/1113 Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wire.zig | 14 +++++++------- core/replica.zig | 53 ++++++++++++++--------------------------------------- core/root.zig | 2 +- runtime/session.zig | 6 +----- test/daemon.py | 2 +- test/replicas.zig | 1 - 6 files changed, 24 insertions(+), 54 deletions(-) (limited to 'core/replica.zig') diff --git a/abi/wire.zig b/abi/wire.zig index ab974e35..48b3ae51 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -10,6 +10,9 @@ const pardes = @import("core"); const config = @import("pardes_options"); const build_info = @import("build_info"); +/// 30: the terminals' one road: the screen-state messages are retired (a +/// replica's `screen_ack`, 0x04, and the session's `screen`, 0x06: gaps) +/// and a welcome says no road (its `bytes` byte gone); /// 29: a session's terminals reach its clients as the pty's bytes in its /// units, each client's emulator its own; a welcome says the road (`bytes`; /// screen state under PARDES_TERM_SCREEN), which the client loads its dump @@ -54,7 +57,7 @@ const build_info = @import("build_info"); /// 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 = 29; +pub const version: u16 = 30; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -221,7 +224,7 @@ pub const ServerMsg = union(ServerTag) { /// 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: pardes.Pardes.View.Id = .at(0), dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, bytes: bool = false, blobs: u32 = 0 }, + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: pardes.Pardes.View.Id = .at(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, @@ -256,7 +259,6 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), .semantics = try r.takeInt(u64, .little), - .bytes = try r.takeByte() != 0, .blobs = try r.takeInt(u32, .little), } }, .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, @@ -287,7 +289,6 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); try w.writeInt(u64, h.semantics, .little); - try w.writeByte(@intFromBool(h.bytes)); try w.writeInt(u32, h.blobs, .little); }, .refused => |words| try putSlice16(&w, words), @@ -729,14 +730,13 @@ test "detached wire: every Event variant round-trips" { 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", .bytes = true } }); + 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, 1, 0, 0, 0, 0 }, f.payload); + try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload); const back = (try decodeServer(f.tag, f.payload)).welcome; try testing.expectEqualStrings("work", back.session); try testing.expectEqual(@as(u32, 42), back.pid); - try testing.expect(back.bytes); try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); } diff --git a/core/replica.zig b/core/replica.zig index 3f897044..55e7a1f7 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1789,11 +1789,6 @@ pub const Peers = struct { /// writer would write (its join, then `out`) and its reader read /// (`Peer.take`, then `.gone` posted) are its driver's to do. threads: bool = true, - /// Its clients' terminals cross as the pty's bytes in its units, each - /// client's emulator its own (P29, P32), not as screen state: every - /// session's (runtime/session.zig) but under PARDES_TERM_SCREEN, the - /// soak's `--bytes`; its welcome says so to each client. - bytes: bool = false, pub const Note = union(enum) { join: Joining, input: Msg, gone: *Peer, check: *Peer, joined: *Peer }; /// A connection to join, and the view it joins again for, if it does. @@ -1992,7 +1987,7 @@ pub const Peers = struct { // (a pair's client, its own boot: `writeJoin`). const digest = tape.digestOf(core); peer.digest = digest; - 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 = peer.view, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len), .bytes = ps.bytes } }) catch unreachable).len; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = peer.view, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; if (ps.threads) peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; if (ps.threads) peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; if (ps.threads and (peer.writer == null or peer.reader == null)) { @@ -2316,9 +2311,6 @@ pub const Joined = struct { /// Its replica is its own boot, the welcome not read yet (`ownBoot`, /// `confirmBoot`). booted: bool = false, - /// Its session's terminals come as the pty's bytes, not screen state - /// (the welcome's word, Peers.bytes). - bytes: bool = false, }; /// A blob a far join was sent: where it is in the dump, and its hash. @@ -2399,7 +2391,6 @@ fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Ha r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; r.j.semantics = w.semantics; - r.j.bytes = w.bytes; if (r.boot == w.digest) { r.done = true; return false; @@ -2803,16 +2794,14 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o return fromDump(gpa, j, o, have, why, t0); } -/// The replica of the dump `j` brought, on the road its welcome says, its -/// load checked by the session's digest, and what it takes of `j`; null, -/// and why in `why`, when it does not load as the session. +/// The replica of the dump `j` brought, its load checked by the session's +/// digest (its emulators' among it), and what it takes of `j`; null, and +/// why in `why`, when it does not load as the session. fn fromDump(gpa: Allocator, j: *Joined, o: pardes.Options, have: ?*Have, why: []u8, t0: u64) ?*Replica { const r = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| { _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; return null; }; - // Before the check: on the bytes road the digest covers its emulators. - road(r, j.bytes); if (!r.loaded(j.digest)) { r.deinit(); _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; @@ -2822,14 +2811,6 @@ fn fromDump(gpa: Allocator, j: *Joined, o: pardes.Options, have: ?*Have, why: [] return r; } -/// Its terminals its own emulators, fed the pty's bytes its units carry -/// (`bytes`, its session's Peers.bytes), or the session's screen state; -/// its echo predicted on either (core/echo.zig). -fn road(r: *Replica, bytes: bool) void { - r.t.screen_state = !bytes; - r.core().echo = &r.echo; -} - /// What a replica joined by `j` takes of it, and of the environment. fn takeJoined(r: *Replica, j: *Joined, have: ?*Have, t0: u64) void { // PARDES_NO_ECHO: no predicted echo (the gates' baseline). @@ -2867,10 +2848,7 @@ fn ownBoot(gpa: Allocator, j: *Joined, fd: c_int, o: pardes.Options, why: *[600] _ = std.fmt.bufPrint(why, "the session hung up\x00", .{}) catch {}; return null; } - // Its session is this process's child, on the road this environment - // says (runtime/session.zig): the welcome says it again. - j.* = .{ .fd = fd, .pid = 0, .client = r.id, .view = r.view, .gmtoff = gmtoff(), .dump = &.{}, .digest = digest, .semantics = wire.semantics, .booted = true, .bytes = std.c.getenv("PARDES_TERM_SCREEN") == null }; - road(r, j.bytes); + j.* = .{ .fd = fd, .pid = 0, .client = r.id, .view = r.view, .gmtoff = gmtoff(), .dump = &.{}, .digest = digest, .semantics = wire.semantics, .booted = true }; takeJoined(r, j, null, t0); return r; } @@ -2889,7 +2867,6 @@ pub fn confirmBoot(gpa: Allocator, j: *Joined, r: *Replica, o: pardes.Options, w if (j.dump.len == 0) { r.id = j.client; r.view = j.view; - road(r, j.bytes); return r; } defer { @@ -4230,9 +4207,10 @@ fn ownBootAgainst(edited: bool) !void { /// A session's first client on a socket pair (runtime/session.zig's, then /// the client's first join on its end), the session's first effects after: /// its boot shell spawned, its first bytes output; the client quiet and -/// checked. Bytes: the client loaded as the soak's bytes clients are -/// (`replica.join` loads the screen-state kind until the switch). -fn firstOnPair(bytes: bool) !void { +/// checked. `by_dump`: the client joined by the session's dump, as any +/// client loads one (`fromDump`); else it makes its own core (Options.pair, +/// `confirmBoot`). +fn firstOnPair(by_dump: bool) !void { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; const Spawns = struct { @@ -4249,7 +4227,7 @@ fn firstOnPair(bytes: bool) !void { const core = try Pardes.init(gpa, opts); defer core.deinit(); core.host = .{ .ctx = &spawns, .vtable = &Spawns.vtable }; - var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .bytes = true, .detached = true }; + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .detached = true }; defer ps.deinit(); defer live_peers = null; defer ps.dropAll(core); @@ -4257,12 +4235,10 @@ fn firstOnPair(bytes: bool) !void { ps.join(core, .{ .fd = fds[0], .own = true }); var j: Joined = undefined; var why: [600]u8 = @splat(0); - const r = if (bytes) r: { - // Joined by its dump as any client loads one (`fromDump`): the - // welcome says the road, and its load is checked on it. + const r = if (by_dump) r: { + // Joined by its dump as any client loads one (`fromDump`). j = joinOn(gpa, fds[1], null, &why, null, null, null) orelse return error.JoinFailed; defer gpa.free(j.dump); - try std.testing.expect(j.bytes); break :r fromDump(gpa, &j, opts, null, &why, nowNs()) orelse { std.debug.print("the join: {s}\n", .{std.mem.sliceTo(&why, 0)}); return error.JoinFailed; @@ -4333,7 +4309,7 @@ fn firstOnPair(bytes: bool) !void { defer arena.deinit(); const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false); try std.testing.expect(std.mem.indexOf(u8, rows[0], "first$") != null); - if (!bytes) return; + if (!by_dump) return; // One that comes after the shell's bytes: every side reloads. var late: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket; @@ -5767,7 +5743,7 @@ test "a step of the session's own that reads a terminal right after its output, core.view().active = shell; // The session as runtime/session.zig runs it; the client's link in // memory. - var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .threads = false, .bytes = true }; + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .threads = false }; defer ps.deinit(); defer ps.dropAll(core); var fds: [2]c_int = undefined; @@ -5795,7 +5771,6 @@ test "a step of the session's own that reads a terminal right after its output, try whole.appendSlice(gpa, peer.dump[pos..]); const r = try Replica.initDump(gpa, w.client, w.view.index(), whole.items, .{ .cols = 80, .rows = 24, .boot = .tty }); defer r.deinit(); - road(r, w.bytes); try std.testing.expect(r.loaded(w.digest)); var link: Link = .{ .r = r, .fd = fds[1] }; defer link.deinit(gpa); diff --git a/core/root.zig b/core/root.zig index 66630b2d..171bdfef 100644 --- a/core/root.zig +++ b/core/root.zig @@ -632,7 +632,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ .{ "jj.zig", 11, "jj run under tape.site(.jj) on an io of its own (the tape's spawns nothing) with this process's environment, the signal mask cleared as Stream's children's: the session's, a replica replays its answer; or on a worker for `Changes`, its answer an input, with the wall clock it was made at (tape.value(.clock) on the loop) for its descriptions' ages; a test's shell inbox (dev)" }, .{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" }, .{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" }, - .{ "replica.zig", 74, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, + .{ "replica.zig", 73, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, .{ "rollback.zig", 1, "a test helper's directory" }, .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, diff --git a/runtime/session.zig b/runtime/session.zig index 9499da9c..75e8efcc 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -86,11 +86,7 @@ pub fn Loop(comptime Frontend: type) type { defer l.prompt_rcs.deinit(); l.fs = ninep_io.start(l.io, l.gpa, l.core); defer if (l.fs) |f| f.deinit(l.gpa); - // Its clients' terminals by the pty's bytes (Peers.bytes), each - // client's emulator its own; PARDES_TERM_SCREEN (a working name, - // the way back until the screen state's road is deleted): as - // screen state. Its welcome tells each client which. - l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name(), .bytes = std.c.getenv("PARDES_TERM_SCREEN") == null }; + l.peers = .{ .gpa = l.gpa, .io = l.io, .name = daemon.name() }; l.inbox = .{ .io = l.io, .gpa = l.gpa, .lsp_allocator = l.allocs.lsp }; defer l.inbox.deinit(); // A search's worker may be waiting on the queue for room. diff --git a/test/daemon.py b/test/daemon.py index 3a481c00..c5c39be3 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -257,7 +257,7 @@ class Replica: """A replica's connection (abi/wire.zig): length-prefixed messages, a tag byte and a little-endian u32 length. It is a near client: its first word after the hello says so.""" - WIRE = 29 + WIRE = 30 def __init__(self, path, timeout=10): self.path, self.timeout = path, timeout diff --git a/test/replicas.zig b/test/replicas.zig index 46eff641..aa10d4a9 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -1296,7 +1296,6 @@ const Soak = struct { pub fn open(s: *Soak, l: *SessionLoop) !pardes.Host { s.l = l; l.peers.threads = false; - l.peers.bytes = true; const host: pardes.Host = .{ .ctx = s, .vtable = &vtable }; l.core.host = host; return host; -- cgit v1.3 From 94561bf0e13d034384eae01d2cb9d48a8fe0a502 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 10:39:52 -0300 Subject: tape: no screen-state branch left, and a terminal's kitty images are each side's own emulator's again (the old road's deletion, piece 6 of 8): core/tape.zig loses `Tape.screen_state` and every branch on it (a terminal's output recorded without its bytes, the positions and prompt checks skipped, `screenReplica`, `screenAuthority`, `screenState`, `viewOnly`, which on the one road did nothing), and the tape site `images`; core/panes.zig its replica branches (a feed skipped and the session's positions taken, a view's scroll or follow noted, a resize left to the authority, the reloads at a join skipped); core/Term.zig a terminal's digest that returned early under screen state, and the medium read that went past the tape; core/dump.zig a join's dump always holds each terminal whole (`slim_terminals` stays for the exit dump); the tests' `t.screen_state = false` lines (4 in core/replica.zig, 3 in test/replicas.zig); THE IMAGES (the brief's choice): `Pane.term_images` and its taped read after each output (pardes-image-loop's cure for a screen-state client, whose emulator never held an image) go, the tag reads `Term.hasImages` again (core/tagline.zig), and a terminal's digest holds whether it has an image (core/term/terminal.zig `digest`: one bool among its facts), so the fact the tag shows is covered by the digest it is computed from (S1's rule) rather than by a pane field copied after each output, and a parting over it is named at the terminal; abi/wire.zig's line for 30 says so (the dump's shape changed with it); the image-loop's test runs on the one road (the client's emulator has the image, its tag says what the session's does); core-test 911/1068 (157 skipped), shell-test 1110/1113, term-test 15/15, pane-test 181/182, kitty-test 45/45, the soak's small run and `--echo` rc 0 (diverged 0, unequal 0, corpus 22) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/wire.zig | 6 ++++-- core/Core.zig | 1 - core/Term.zig | 8 +------ core/dump.zig | 17 ++++++--------- core/echo.zig | 2 +- core/panes.zig | 30 +++----------------------- core/replica.zig | 4 ---- core/tagline.zig | 2 +- core/tape.zig | 57 +++----------------------------------------------- core/term/terminal.zig | 3 ++- test/replicas.zig | 3 --- 11 files changed, 21 insertions(+), 112 deletions(-) (limited to 'core/replica.zig') diff --git a/abi/wire.zig b/abi/wire.zig index 48b3ae51..8f7a7bdb 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -11,8 +11,10 @@ const config = @import("pardes_options"); const build_info = @import("build_info"); /// 30: the terminals' one road: the screen-state messages are retired (a -/// replica's `screen_ack`, 0x04, and the session's `screen`, 0x06: gaps) -/// and a welcome says no road (its `bytes` byte gone); +/// replica's `screen_ack`, 0x04, and the session's `screen`, 0x06: gaps), +/// a welcome says no road (its `bytes` byte gone), and a pane's +/// `term_images` and its tape site leave the dump (each side's emulator +/// answers; a terminal's digest holds whether it has an image); /// 29: a session's terminals reach its clients as the pty's bytes in its /// units, each client's emulator its own; a welcome says the road (`bytes`; /// screen state under PARDES_TERM_SCREEN), which the client loads its dump diff --git a/core/Core.zig b/core/Core.zig index 4c8c98dd..91f647d4 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1685,7 +1685,6 @@ fn updateInner(p: *Pardes, ev: Event) void { pane.shell_spoke = true; ctlfs.events.notePtyOutput(p, o.pane.index(), o.bytes); pane.feedOutput(p, o.bytes); - pane.term_images = if (p.tape.screen_state) p.tape.value(.images, pardes.Term.hasImages, .{pane.terminal}) else pardes.Term.hasImages(pane.terminal); ctlfs.pty.noteMarks(p, o.pane.index(), pane); exec.flushReplEnter(p, o.pane.index(), true); }, diff --git a/core/Term.zig b/core/Term.zig index 86c7518b..968a8d68 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -68,11 +68,7 @@ pub const State = struct { written: bool = true, /// The emulator as its program has made it, for the replay digest. - /// 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. pub fn digest(s: *const State, h: *walk.Hasher) void { - if (tape.screenState()) return; s.digestScreen(h, true); } @@ -135,10 +131,8 @@ pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State { /// session's machine's) as its emulator reads it, made the tape's answer /// (P30: the device's answer as it was, as a file's bytes are): the session /// reads it, a temporary file deleted and shared memory unlinked there once, -/// and every replica is handed the same bytes, or the same failure. Under -/// screen state a replica feeds no emulator: the read is the session's. +/// and every replica is handed the same bytes, or the same failure. fn media(ctx: *const anyopaque, read: *const fn (*const anyopaque) iface.MediumError![]u8, gpa: std.mem.Allocator) iface.MediumError![]u8 { - if (tape.screenState()) return read(ctx); return tape.valueIn(.medium, gpa, readMedium, .{ read, ctx }); } diff --git a/core/dump.zig b/core/dump.zig index 18ac28f7..c3742a21 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -255,16 +255,12 @@ pub fn image(p: *Pardes, name: []const u8) ![]u8 { return pardes.Db.image(gpa, name, buf.written(), w.blobs.items, panes, names.written()); } -/// A dump for a screen-state replica's join (core/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. +/// A dump for a replica's join (core/replica.zig): its blobs' bytes left +/// out and noted in `deferred` (they are the session's, alive while pinned: +/// memory.zig `pin`), each terminal whole (as the join's step reloaded it +/// on every side: panes.reloadTerminals), its sequence in flight with it. +/// What is left is the session's mutable state, made in its step. pub fn dumpJoin(p: *Pardes, deferred: *std.ArrayListUnmanaged(walk.Deferred)) ![]u8 { - // Under bytes, each terminal whole (as the join's step reloaded it on - // every side: panes.reloadTerminals), its sequence in flight with it. - slim_terminals = p.tape.screen_state; - defer slim_terminals = false; // The join's file reads are the session's host's, between its steps: // with its io, which the core's tape holds (a walk's hook takes no more). join_files = p.tape.io; @@ -272,8 +268,7 @@ pub fn dumpJoin(p: *Pardes, deferred: *std.ArrayListUnmanaged(walk.Deferred)) ![ return dumpWith(p, deferred); } -/// `dumpJoin`'s under screen state and `dumpExit`'s: a terminal dumps its -/// size, not its emulator. +/// `dumpExit`'s: a terminal dumps its size, not its emulator. pub var slim_terminals = false; /// `dumpJoin`'s: a plain kind's pane carries its file (SlotRecord.file), /// read with this io. diff --git a/core/echo.zig b/core/echo.zig index e2a6521c..3b59788d 100644 --- a/core/echo.zig +++ b/core/echo.zig @@ -49,7 +49,7 @@ const never = std.math.maxInt(u64); 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 +/// authority's numbering), 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 }; diff --git a/core/panes.zig b/core/panes.zig index d58bf30b..a2e383c1 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -134,11 +134,6 @@ pub const Pane = struct { /// cell), and its kitty graphics drawn as glyph art (Petscii). term_mouse: ?Term.Coordinate = null, term_petscii: bool = false, - /// Whether its terminal holds a kitty image, as the session's emulator - /// said after its program's last output (taped under screen state: a - /// client's emulator is fed the session's cells, never an image). Its - /// tag says so (tagline.zig), on every side. - term_images: bool = false, gpa: std.mem.Allocator, serial: u32 = 0, /// What the control filesystem keeps of this pane -- addr and limit, the @@ -642,7 +637,6 @@ pub const Pane = struct { } } else if (pane.addon == null) { // the vt scrolls in SHELL rows; convert through the edit buffer - if (tape.viewOnly("scroll a terminal")) return; const state = pane.terminal orelse return; const off: i32 = @intCast(Term.grid(state).offset); Term.scroll(state, pane.gridRow(pane.surfRow(off) + delta) - off); @@ -743,12 +737,6 @@ pub const Pane = struct { const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.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 pane.pointerRowCount() > 0; - // A replica's emulator is what the authority's deltas make it; what - // pardes keeps over it moves as the authority's feed told it. - if (tape.screenReplica()) { - if (has_positions and pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(.at(id), positions_lost); - 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 @@ -768,7 +756,6 @@ pub const Pane = struct { /// pointer's rows) moved as the rows move (`Term.Told`). True when one /// could not be kept (no memory for its pin). fn followRows(pane: *Pane, state: *Term.State, bytes: []const u8) bool { - const on_replica = tape.screenReplica(); const Group = struct { overlay: ?*Text.EditBuffer, cursor: *i32, selection: *Text.CharSel }; var groups: [Text.history_max * 2 + 1]Group = undefined; groups[0] = .{ @@ -879,13 +866,7 @@ pub const Pane = struct { track[k] = at.grid; k += 1; }; - // A replica's emulator is fed by its authority's deltas: it is told - // what the authority's feed told. - const fed: Term.Told = if (on_replica) told: { - @memset(rows_after, 0); - @memset(erased_after, false); - break :told .{ .rows = rows_after, .erased = erased_after, .cleared = false, .alive = false, .failed = false }; - } else Term.feed(state, bytes, track, rows_after, erased_after); + const fed = Term.feed(state, bytes, track, rows_after, erased_after); const after = tape.value(.positions, keepTold, .{Term.Told{ .rows = fed.rows, .erased = fed.erased, .cleared = fed.cleared, .alive = fed.alive, .failed = failed or fed.failed }}); if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged) failed = after.failed; @@ -967,7 +948,7 @@ pub const Pane = struct { } fn feedLost(state: *Term.State, bytes: []const u8) bool { - if (!tape.screenReplica()) _ = Term.feed(state, bytes, &.{}, &.{}, &.{}); + _ = Term.feed(state, bytes, &.{}, &.{}, &.{}); return true; } @@ -981,14 +962,11 @@ pub const Pane = struct { /// Its terminal's view snapped back onto live output. pub fn followOutput(pane: *Pane) void { - if (tape.viewOnly("follow a terminal")) return; const state = pane.terminal orelse return; Term.follow(state); } - /// Its terminal's grid reflowed to `cols` by `rows`: on a screen-state - /// replica by its authority (the next delta says), on such an authority - /// once the step's unit is out (`resizeDeferred`). + /// Its terminal's grid reflowed to `cols` by `rows`. pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { // A client's speculation does what its replay does (its // selections) but the grid: it waits for no round trip. Under bytes @@ -999,7 +977,6 @@ pub const Pane = struct { pane.clearPointerSelection(button); pane.sel[button].state = .none; }; - if (tape.screenReplica()) return; if (tape.speculating()) return tape.touchTerminal(); Term.resize(state, cols, rows); } @@ -1152,7 +1129,6 @@ pub const positions_lost = "terminal edit position reset: out of memory"; /// session's snapshot's hash is taped, and each replica's own compared /// with it: a check of the whole emulator at every join. pub fn reloadTerminals(p: *Pardes) void { - if (tape.screenState()) return; for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| { if (!tape.value(.reload, written, .{state})) continue; // Its cells' size in pixels as a feed sets it (`feedOutput`): a diff --git a/core/replica.zig b/core/replica.zig index 55e7a1f7..e9e29de0 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -4053,7 +4053,6 @@ test "bytes: a client joins from a dump with its terminals whole, and every side try std.testing.expect(pardes.Term.writes != reloads); const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); defer late.deinit(); - late.t.screen_state = false; late.core().echo = null; try std.testing.expectEqual(joined.digest, late.digest()); b.a.output(.{ .output = .{ .pane = b.shell, .bytes = ";30mthe rest of it\x1b[0m\r\n" } }); @@ -4377,7 +4376,6 @@ test "bytes: a join reloads only the terminals written since their last reload: try std.testing.expectEqual(2, pardes.Term.reloads.n - before); const late = try Replica.initDump(gpa, 3, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); defer late.deinit(); - late.t.screen_state = false; late.core().echo = null; try std.testing.expectEqual(joined.digest, late.digest()); // Output into both until each has pruned its history (whole pages, by @@ -7190,7 +7188,6 @@ test "a client joined from a dump takes a 9P pty/run in a shell as the session d r.confirm(joined.unit); const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); defer late.deinit(); - late.t.screen_state = false; late.core().echo = null; try std.testing.expectEqual(joined.digest, late.digest()); const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); @@ -7376,7 +7373,6 @@ const NinepJoin = struct { const joined = (try n.a.join(1, null)).?; defer std.testing.allocator.free(joined.dump); const r = try Replica.initDump(std.testing.allocator, 1, joined.view, joined.dump, .{ .cols = 80, .rows = 24 }); - r.t.screen_state = false; r.core().echo = null; n.late = r; try std.testing.expectEqual(joined.digest, r.digest()); diff --git a/core/tagline.zig b/core/tagline.zig index 26f83fba..02d1b277 100644 --- a/core/tagline.zig +++ b/core/tagline.zig @@ -160,7 +160,7 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co if (pane.terminal != null) { const chosen = pane.term_petscii; const on = chosen or !p.view().frontend.native_images; - if (chosen or pane.term_images) + if (chosen or pardes.Term.hasImages(pane.terminal)) return std.fmt.allocPrint(arena, "{s} petscii:{s}", .{ (try shownDir(arena, pane)), if (on) "on" else "off" }); } return (try shownDir(arena, pane)); diff --git a/core/tape.zig b/core/tape.zig index 5056f225..1cbf8aed 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -140,9 +140,6 @@ pub const Site = enum(u16) { /// A kitty image's medium as a terminal's emulator read it (a file, a /// temporary file, shared memory: Term.media), its bytes or its failure. medium, - /// Whether a terminal holds a kitty image after its program's output - /// (Pane.term_images): a screen-state replica's emulator never has one. - images, }; /// What the sites return. wire.semantics hashes their shape with the @@ -311,13 +308,6 @@ pub const Tape = struct { dump_check: bool = false, dumps: usize = 0, dump_bytes: usize = 0, - /// Screen-state terminals (core/term/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, /// The session's (replica.Peers): called between two of its inputs once /// what it recorded since its last unit passes `unit_max`, to send that /// as a unit: a pump of a flood's reads is cut into units under it. @@ -423,12 +413,7 @@ pub const Tape = struct { t.noteWrites(); t.noteHost(core); } - // A terminal's output crosses as screen state, not as its bytes. - if (t.screen_state and input.* == .update and input.update.args[0] == .output) { - var bare = input.*; - bare.update.args[0].output.bytes = &.{}; - t.record(.input, Input, &bare); - } else t.record(.input, Input, input); + t.record(.input, Input, input); return .{ .tape = t }; } @@ -806,12 +791,12 @@ pub const Tape = struct { // output) is the session's answer, kept, and a replica whose // emulators are fed the same bytes asks its own and compares: a // check a unit, for nothing (P30: a digest, not state). - if (comptime s == .positions or s == .prompt) if (!t.screen_state) { + if (comptime s == .positions or s == .prompt) { const mine = @call(.auto, f, full); t.scratch.clearRetainingCapacity(); walk.encode(R, &mine, &t.scratch.writer) catch @panic("tape: out of memory"); if (!std.mem.eql(u8, t.scratch.written(), t.last_ret.bytes[t.last_ret.at..])) t.diverge("what this replica's terminal told ({t}) is not what the session's did", .{s}); - }; + } 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}); @@ -885,17 +870,6 @@ pub const Tape = struct { return t.depth > 0 or t.replay or t.speculating; } - /// A replica's core under screen-state terminals: its emulators are - /// written by `apply` alone, never by its own steps. - pub fn screenReplica(t: *const Tape) bool { - return t.screen_state and (t.replay or t.speculating); - } - - /// An authority recording for screen-state replicas. - pub fn screenAuthority(t: *const Tape) bool { - return t.screen_state and !t.replay and !t.speculating; - } - /// What a speculation reached is the authority's (`why`): the input is /// undone and waits for the authority's order. pub fn poison(t: *Tape, why: []const u8) void { @@ -913,14 +887,6 @@ pub const Tape = struct { return true; } - /// A speculation that moves a terminal's view: it may (its rollback - /// puts the view back, replica.zig `views`); a screen-state replica's - /// says it did (a delta sets the view again). - pub fn viewOnly(t: *Tape, comptime _: []const u8) bool { - if (t.speculating and t.screen_state) t.touched = true; - return false; - } - /// A speculation read a terminal's rows or moved its view (`Tape.touched`). pub fn touchTerminal(t: *Tape) void { if (t.speculating) t.touched = true; @@ -1356,19 +1322,6 @@ pub fn stepping() bool { return (on orelse &none).stepping(); } -/// Screen-state terminals, on either side (`Tape.screen_state`). -pub fn screenState() bool { - return (on orelse &none).screen_state; -} - -pub fn screenReplica() bool { - return (on orelse &none).screenReplica(); -} - -pub fn screenAuthority() bool { - return (on orelse &none).screenAuthority(); -} - pub fn poison(why: []const u8) void { (on orelse &none).poison(why); } @@ -1383,10 +1336,6 @@ pub fn authorityOnly(comptime why: []const u8) bool { return (on orelse &none).authorityOnly(why); } -pub fn viewOnly(comptime why: []const u8) bool { - return (on orelse &none).viewOnly(why); -} - pub fn touchTerminal() void { (on orelse &none).touchTerminal(); } diff --git a/core/term/terminal.zig b/core/term/terminal.zig index 1c54f2ac..f130d7b9 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -211,7 +211,8 @@ pub fn digest(s: *const State, h: *std.hash.Wyhash, view: bool) void { 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 }; + // Whether it holds a kitty image: the tag says so (tagline.zig). + 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, hasImages(s) }; inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); h.update(s.reply[0..s.reply_len]); var buf: [4096]u8 = undefined; diff --git a/test/replicas.zig b/test/replicas.zig index f4d8d450..90f7c10d 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -984,7 +984,6 @@ const Witness = struct { // No client's id: every unit is another's. const rp = try replica.Replica.initDump(gpa, std.math.maxInt(u16), d.view, d.bytes, .{ .cols = d.cols, .rows = d.rows, .distrusted = &distrusted }); rp.guess = false; - rp.t.screen_state = false; rp.core().echo = null; rp.t.local_files = true; return rp; @@ -1950,7 +1949,6 @@ const Soak = struct { } } // Its terminals its own emulators, fed what the units carry. - rp.t.screen_state = false; rp.core().echo = null; rp.stats.samples = true; rp.t.local_files = true; @@ -3533,7 +3531,6 @@ fn corpusCase(dir: []const u8) !bool { defer gpa.free(j.dump); const rp = try replica.Replica.initDump(gpa, 1, j.view, j.dump, .{ .cols = 100, .rows = 22 }); defer rp.deinit(); - rp.t.screen_state = false; var fds: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; defer for (fds) |fd| { -- cgit v1.3 From 8caed8e7d894fee836c95f2ca83fda9b22e1db84 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 14:01:03 -0300 Subject: replica: a client's own join again is judged after its goodbye (pardes-term-join-9, item 1 (a)): a client let go for its lag or after a divergence says bye, shuts its link and dials again naming its view (runtime/client.zig `run`), and the session takes its join from the daemon's hand-over and its bye from the old link's reader thread, in no order between them; judged first, `viewFor` found the view not away and opened a NEW one (the client's layout, scroll and focus lost, its old view then kept away 30 s for nobody), or at the cap refused it (a divergence's rejoin is not patient and then fails "could not join again"); now a join naming a view whose link has hung up (`hungUp`: POLLHUP or POLLRDHUP, the client having shut its link before it dialled) is held on that peer (`Peer.rejoin`) and posted again when the link is let go (`letGo`), its view away by then, a view whose link is up being another client's as before, and a Restore keeps a held join as it keeps the inbox's; measured with a raw replica that says bye and at once joins again naming its view, pinned to one CPU: the base 1 of 60 in another view and 27 refused, this 0 of 120; the cut-off test dials again before its old link's end is heard (fails on the base with `error.NotHeld`); root.zig's machine_lines for replica.zig 73 -> 76 (the session's own join, no replayed step); not covered: a relay that delays each connection on its own (test/delay_relay.py) can still bring the hello before the old link's hang-up Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 46 +++++++++++++++++++++++++++++++++++++++------- core/root.zig | 2 +- 2 files changed, 40 insertions(+), 8 deletions(-) (limited to 'core/replica.zig') diff --git a/core/replica.zig b/core/replica.zig index e9e29de0..ae1ae6c3 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1379,6 +1379,9 @@ pub const Peer = struct { /// Cut off (`send`): the words why are the last it is sent, and its /// view is kept for it (views.away), as for a bye's `again`. cut: bool = false, + /// Its client's join again for its view, come before this link's end + /// was heard: judged once it is let go (`Peers.join`, `letGo`). + rejoin: ?Peers.Joining = null, /// 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 @@ -1722,6 +1725,13 @@ pub fn dying() void { }; } +/// Whether the other end of socket `fd` has hung up (shut, or closed). +fn hungUp(fd: c_int) bool { + const rdhup = if (@hasDecl(libc.POLL, "RDHUP")) libc.POLL.RDHUP else 0; + var p = [1]libc.pollfd{.{ .fd = fd, .events = libc.POLL.IN | rdhup, .revents = 0 }}; + return libc.poll(&p, 1, 0) == 1 and p[0].revents & (libc.POLL.HUP | rdhup) != 0; +} + /// The process at the other end of unix socket `fd` (Linux's SO_PEERCRED), /// or 0: a client's through ssh is ssh's. fn peerPid(fd: c_int) libc.pid_t { @@ -1905,6 +1915,16 @@ pub const Peers = struct { /// threads started. At a step boundary (`boundary`), or a session's /// first client before its first effects (runtime/session.zig). pub fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + // A join for a view whose client's link has hung up is that client + // joining again (runtime/client.zig `run`: its bye, its link shut, + // then its dial): its goodbye comes on that link and this join + // through the daemon, in no order between them. Held until that + // link is let go, its view kept for it by then; a view whose link + // is up is another client's. + if (joining.view) |v| for (ps.list) |slot| if (slot) |p| if (p.view.index() == v and p.rejoin == null and hungUp(p.fd)) { + p.rejoin = joining; + return; + }; const fd = joining.fd; const t0 = nowNs(); defer { @@ -2106,6 +2126,7 @@ pub const Peers = struct { _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(io); + if (peer.rejoin) |j| ps.post(.{ .join = j }); ps.gpa.destroy(peer); } @@ -2157,6 +2178,10 @@ pub const Peers = struct { _ = ps.inbox.orderedRemove(i); } ps.lock.unlock(ps.io); + for (ps.list) |slot| if (slot) |p| if (p.rejoin) |j| { + again.append(ps.gpa, j) catch continue; + p.rejoin = null; + }; ps.drop(core, false); ps.restored = true; for (again.items) |j| ps.post(.{ .join = j }); @@ -5007,13 +5032,21 @@ test "a client cut off for what it left unread is told why and joins again for i // welcome, the dump with its blobs in place); and what it queued for one, // down its link in order, the link's end after it if it was cut off. const MemoryLinks = struct { - fn join(ps: *Peers, core: *Pardes, view: ?u8, fd: *c_int) !struct { r: *Replica, peer: *Peer } { + /// `old`: the link it had, whose end the session hears after this + /// join came: held until then. + fn join(ps: *Peers, core: *Pardes, view: ?u8, fd: *c_int, old: ?*Peer) !struct { r: *Replica, peer: *Peer } { const gpa = std.testing.allocator; var fds: [2]c_int = undefined; if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; fd.* = fds[1]; ps.handed(fds[0], view); ps.boundary(core); + if (old) |o| { + for (ps.list) |slot| if (slot) |p| if (p.fd == fds[0]) return error.NotHeld; + ps.post(.{ .gone = o }); + ps.boundary(core); + ps.boundary(core); + } const peer = for (ps.list) |slot| { if (slot) |p| if (p.fd == fds[0]) break p; } else return error.Refused; @@ -5061,7 +5094,7 @@ test "a client cut off for what it left unread is told why and joins again for i defer ps.deinit(); defer ps.dropAll(core); var fd: c_int = -1; - const first = try MemoryLinks.join(&ps, core, null, &fd); + const first = try MemoryLinks.join(&ps, core, null, &fd, null); defer _ = libc.close(fd); var link: Link = .{ .r = first.r, .fd = fd }; defer link.deinit(gpa); @@ -5088,13 +5121,12 @@ test "a client cut off for what it left unread is told why and joins again for i // It was told why, and joins again (as a divergence's client does)... try std.testing.expect(link.rejoins()); try std.testing.expect(std.mem.indexOf(u8, link.notice(), "behind") != null); - // ...once its old link's end is heard: its view kept for it meanwhile. - ps.post(.{ .gone = first.peer }); - ps.boundary(core); - try std.testing.expect(pardes.views.isAway(core, view.index())); + // ...its old link shut, at once (runtime/client.zig `run`): a join that + // comes before that link's end is heard, on another connection, waits + // for it, its view kept for it meanwhile. first.r.deinit(); _ = libc.close(fd); - const back = try MemoryLinks.join(&ps, core, view.index(), &fd); + const back = try MemoryLinks.join(&ps, core, view.index(), &fd, first.peer); defer back.r.deinit(); try std.testing.expectEqual(view, back.peer.view); link.reset(back.r, &.{ .fd = fd, .pid = 0, .client = back.r.id, .view = view.index(), .gmtoff = 0, .dump = &.{} }); diff --git a/core/root.zig b/core/root.zig index 0e02a9bc..fb606909 100644 --- a/core/root.zig +++ b/core/root.zig @@ -632,7 +632,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ .{ "jj.zig", 11, "jj run under tape.site(.jj) on an io of its own (the tape's spawns nothing) with this process's environment, the signal mask cleared as Stream's children's: the session's, a replica replays its answer; or on a worker for `Changes`, its answer an input, with the wall clock it was made at (tape.value(.clock) on the loop) for its descriptions' ages; a test's shell inbox (dev)" }, .{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" }, .{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" }, - .{ "replica.zig", 73, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, + .{ "replica.zig", 76, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, .{ "rollback.zig", 1, "a test helper's directory" }, .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, -- cgit v1.3 From b0e96978b619e1111ee2fb30e3c4a48f9fac38b3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 14:14:39 -0300 Subject: pty: a shell forks at its pane's grid, and the core hands its host every pty's size, pixels too (pardes-term-join-9, item 2 (B), points (a) and (c)): the spawn host call took no size and both hosts forked at the session's live screen (dev/Ptys.zig `spawn`, runtime/macos.zig `spawnShell`: `core.view().screen_h/w`), so a shell started again into a pane already sized (pty/ctl's exec, a finished command pane run again by exec.zig `runCommand`, a Restore's `revivePane`) ran at the screen's size and nothing resized it, the grid unchanged and `sync` emitting nothing (`stty size` said 50 160 in a 44x158 pane); and a pty's pixels were the live view's (host_io.zig `ptyWinsize` read `core.view().cell_pixels`) whichever view sized it; now `Effect.PtySize` (cells and pixels) is what the core gives its host for `spawn` and `pty_resize`, made by `Core.ptySize` from the pane's grid (or 9P's numbers) at the cell of the view that sizes it, a spawn performed after its step's `sync` reading the laid-out grid, and the hosts fork and size from it alone (`Ptys.take` sizes an adopted shell from it too: 1x1 then the size, once, its forced redraw kept, an ioctl to the size a pty has already sending no SIGWINCH); test (test/process/terminals.zig, a new case): a Term pane's shell, the same shell started again by pty/ctl's exec and a finished command pane run again each say the grid their pty/ctl reads back (fails on the base: 50 160 against 44 158); not covered: a new command pane made over 9P is placed a step after its fork, so its first size can still be the screen's Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 12 +++++- core/Effect.zig | 11 ++++-- core/ninep/harness.zig | 2 +- core/replica.zig | 4 +- core/root.zig | 1 + dev/Host.zig | 4 +- dev/Ptys.zig | 27 +++++++------- dev/gui/gui.zig | 2 +- dev/host_io.zig | 42 +++++++++------------ runtime/macos.zig | 10 ++--- test/process/terminals.zig | 93 ++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 155 insertions(+), 53 deletions(-) (limited to 'core/replica.zig') diff --git a/core/Core.zig b/core/Core.zig index 91f647d4..8616aa35 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2013,6 +2013,16 @@ pub fn sync(p: *Pardes) void { tagline.revealHeader(p); } +/// Pane `id`'s pty as its host is to size it: its grid (or `cells`, a +/// size 9P's pty/ctl gave), in pixels at the cell of the view that sizes +/// it: a program sizes its kitty graphics by them. +pub fn ptySize(p: *const Pardes, id: Pane.Id, cells: ?[2]u16) pardes.PtySize { + const pane = p.panes[id.index()] orelse return .{ .cols = 1, .rows = 1 }; + const c = cells orelse .{ pane.cols, pane.rows }; + const cell = (p.views[pane.sized_by] orelse p.views[p.live].?).cell_pixels; + return .{ .cols = @max(c[0], 1), .rows = @max(c[1], 1), .w_px = std.math.cast(u16, @as(u32, c[0]) * cell.w) orelse 0, .h_px = std.math.cast(u16, @as(u32, c[1]) * cell.h) orelse 0 }; +} + /// The view typing goes into is the one its terminal is sized for /// (`sizes`). fn typedInto(p: *Pardes) void { @@ -2888,7 +2898,7 @@ test "a spawn is for the pane it was made for, not whatever took the slot since" var command: [32]u8 = undefined; var core: *Pardes = undefined; const vtable: Host.VTable = .{ .spawn = spawn }; - fn spawn(_: ?*anyopaque, id: pardes.Pane.Id, _: []const u8) void { + fn spawn(_: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void { const pane = id.index(); spawns += 1; const line = core.panes[pane].?.command.?; diff --git a/core/Effect.zig b/core/Effect.zig index cabfe8ea..abfe4711 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -22,6 +22,10 @@ const Host = pardes.Host; /// to the shell itself when the shell is in the foreground. pub const PtySignal = enum(u8) { int, term, hup, quit, kill }; +/// A pty's size as the core gives it to its host: in cells, and in pixels +/// at the cell of the view that sizes its pane (Core.ptySize). +pub const PtySize = struct { cols: u16, rows: u16, w_px: u16 = 0, h_px: u16 = 0 }; + /// IO the core wants done. Payloads are inline (fixed buffers): effects are /// queued values with no lifetime ties back into the core. pub const WatchMode = enum { reconcile, baseline_disk }; @@ -118,14 +122,15 @@ fn performInner(p: *Pardes, e: Effect) void { defer panes.File.syncTwins(p); switch (e) { // The host reads the pane (its command, its shell) as it forks, - // so the slot must still hold the pane the spawn was for. - .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice() }) orelse { + // so the slot must still hold the pane the spawn was for. It forks + // at the pane's grid: laid out by the step that asked (`sync`). + .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice(), p.ptySize(s.pane, null) }) orelse { p.fallback.spawned[s.pane.index()] = true; }, // A pane with no child is silent: nothing invents output on its // screen, and the bytes are dropped rather than transcribed. .write => |w| _ = p.tape.host(p, .pty_write, .{ w.pane, w.bytes.slice() }), - .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, r.cols, r.rows }), + .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, p.ptySize(r.pane, .{ r.cols, r.rows }) }), // A signal for a pane that closed, its slot retaken in the same // step, would reach another pane's child. .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .pty_signal, .{ s.pane, s.sig }) orelse {}, diff --git a/core/ninep/harness.zig b/core/ninep/harness.zig index cf75d80d..1449ae54 100644 --- a/core/ninep/harness.zig +++ b/core/ninep/harness.zig @@ -153,7 +153,7 @@ pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { /// none. pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; -fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} +fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void {} pub const FakeTty = struct { taken: bool, diff --git a/core/replica.zig b/core/replica.zig index ae1ae6c3..3aac5e82 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3298,7 +3298,7 @@ const TestHost = struct { /// A host with ptys whose pump polls for a frame, as a client's does. const PtyHost = struct { - fn resized(_: ?*anyopaque, _: pardes.Pane.Id, _: u16, _: u16) void {} + fn resized(_: ?*anyopaque, _: pardes.Pane.Id, _: pardes.PtySize) void {} fn poll(_: ?*anyopaque) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; fn host() pardes.Host { @@ -4238,7 +4238,7 @@ fn firstOnPair(by_dump: bool) !void { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; const Spawns = struct { - fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void { + fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void { const n: *u32 = @ptrCast(@alignCast(ctx.?)); n.* += 1; } diff --git a/core/root.zig b/core/root.zig index fb606909..99fb4845 100644 --- a/core/root.zig +++ b/core/root.zig @@ -66,6 +66,7 @@ pub const CellPixels = @import("Event.zig").CellPixels; pub const Event = @import("Event.zig").Event; /// What goes out (Effect.zig). pub const PtySignal = @import("Effect.zig").PtySignal; +pub const PtySize = @import("Effect.zig").PtySize; pub const WatchMode = @import("Effect.zig").WatchMode; pub const effect_path_cap = @import("Effect.zig").effect_path_cap; pub const effect_write_cap = @import("Effect.zig").effect_write_cap; diff --git a/dev/Host.zig b/dev/Host.zig index cb7aefe1..52205518 100644 --- a/dev/Host.zig +++ b/dev/Host.zig @@ -30,9 +30,9 @@ pub const VTable = struct { /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, - spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8) void = null, + spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, bytes: []const u8) void = null, - pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cols: u16, rows: u16) void = null, + pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, size: pardes.PtySize) void = null, pty_signal: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, sig: pardes.PtySignal) void = null, close_pty: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, tty_taken: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, diff --git a/dev/Ptys.zig b/dev/Ptys.zig index 583e3c38..5b8705ef 100644 --- a/dev/Ptys.zig +++ b/dev/Ptys.zig @@ -45,16 +45,15 @@ pub fn Calls(comptime Ctx: type) type { fn of(ctx: ?*anyopaque) *Ctx { return @ptrCast(@alignCast(ctx.?)); } - pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const s = of(ctx); - s.ptys.spawn(s.core, id, cwd); + s.ptys.spawn(s.core, id, cwd, size); } pub fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { of(ctx).ptys.write(id, bytes); } - pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const s = of(ctx); - s.ptys.resize(s.core, id, cols, rows); + pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, size: pardes.PtySize) void { + of(ctx).ptys.resize(id, size); } pub fn signal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { of(ctx).ptys.signal(id, sig); @@ -82,14 +81,14 @@ pub fn Calls(comptime Ctx: type) type { } /// Pane `id`'s shell, in `cwd` (empty: where its pane's directory says), -/// or the adopted one waiting for it. -pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8) void { +/// at `size`, or the adopted one waiting for it. +pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const pane = id.index(); - if (p.adoptions.take(id)) |a| return p.take(core, pane, a); + if (p.adoptions.take(id)) |a| return p.take(core, pane, a, size); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = p.slots[pane] != null; - const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { + const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, size) catch |err| { if (running) return core.restartFailed(id, err); return core.shellFailed(id, err); }; @@ -107,7 +106,7 @@ pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8 /// 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 take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { +fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption, size: pardes.PtySize) void { p.close(.at(pane)); p.gens[pane] +%= 1; p.slots[pane] = .{ .fd = a.fd, .pid = a.pid }; @@ -117,7 +116,7 @@ fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { if (host_io.shellCwd(a.pid, &lbuf)) |wd| core.setCwd(.at(pane), wd); const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (core.panes[pane]) |pn| p.resize(core, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); + p.resize(.at(pane), size); p.read(core, pane); } @@ -144,9 +143,9 @@ pub fn write(p: *Ptys, id: pardes.Pane.Id, bytes: []const u8) void { if (p.slots[id.index()]) |pt| _ = host_io.writeFd(pt.fd, bytes); } -pub fn resize(p: *Ptys, core: *const pardes.Pardes, id: pardes.Pane.Id, cols: u16, rows: u16) void { +pub fn resize(p: *Ptys, id: pardes.Pane.Id, size: pardes.PtySize) void { const pt = p.slots[id.index()] orelse return; - const ws = host_io.ptyWinsize(core, cols, rows); + const ws = host_io.ptyWinsize(size); _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -290,7 +289,7 @@ fn testing(queue: *Inbox) Ptys { } fn testShell(p: *Ptys, pane: u8) !libc.pid_t { - const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", 24, 80); + const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); p.slots[pane] = .{ .fd = child.file.handle, .pid = child.pid }; p.slots[pane].?.reader = try p.queue.startReader(.{ .handle = child.file.handle, .flags = .{ .nonblocking = false } }, .at(pane), p.gens[pane]); return child.pid; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index bfe658f9..d9d38f06 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -7677,7 +7677,7 @@ test "a detached GUI authority follows terminal directory changes with no window defer ptys.deinit(); var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .watches = undefined, .peers = &no_peers }; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); const until = std.Io.Clock.awake.now(io).toMilliseconds() + 2_000; diff --git a/dev/host_io.zig b/dev/host_io.zig index 419c4570..f189c8ea 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -420,13 +420,8 @@ pub const exec_command_max = pardes.exec.command_max; /// A pane's pty size, in cells and in pixels: a program sizes its kitty /// graphics by the pixels (yazi's previews; pardes nested in a pane). -pub fn ptyWinsize(core: ?*const pardes.Pardes, cols: u16, rows: u16) posix.winsize { - var ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; - if (core) |c| { - ws.xpixel = std.math.cast(u16, @as(u32, cols) * c.view().cell_pixels.w) orelse 0; - ws.ypixel = std.math.cast(u16, @as(u32, rows) * c.view().cell_pixels.h) orelse 0; - } - return ws; +pub fn ptyWinsize(size: pardes.PtySize) posix.winsize { + return .{ .row = size.rows, .col = size.cols, .xpixel = size.w_px, .ypixel = size.h_px }; } pub fn forkShell( @@ -435,8 +430,7 @@ pub fn forkShell( prompt_rcs: *const Shell.PromptFiles, bin: []const u8, cwd: []const u8, - rows: u16, - cols: u16, + size: pardes.PtySize, ) !Child { const native_cwd = filesystem.localPath(cwd) orelse cwd; if (std.mem.indexOfScalar(u8, native_cwd, 0) != null) return error.InvalidPath; @@ -494,7 +488,7 @@ pub fn forkShell( else .terminal; const envp = ChildEnv.build(&env_slots, paging); - const ws = ptyWinsize(core, cols, rows); + const ws = ptyWinsize(size); // The child says here why it could not become the shell (its chdir or // its exec failed, as for a script whose interpreter is not there): the // pipe closes on exec, so the parent reads nothing when the shell runs, @@ -1672,9 +1666,9 @@ test "the child environment replaces the launcher's terminal identity exactly on test "shell spawn rejects invalid directories before creating a child" { const rcs: Shell.PromptFiles = .{}; - try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", 24, 80)); + try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", .{ .cols = 80, .rows = 24 })); const too_long = @as([4096]u8, @splat('x')); - try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, 24, 80)); + try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, .{ .cols = 80, .rows = 24 })); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1683,12 +1677,12 @@ test "shell spawn rejects invalid directories before creating a child" { const directory = directory_buf[0..try tmp.dir.realPath(std.testing.io, &directory_buf)]; var path_buf: [4096]u8 = undefined; const file = try std.fmt.bufPrint(&path_buf, "{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, .{ .cols = 80, .rows = 24 })); const explicit_file = try std.fmt.bufPrint(&path_buf, "/n/os{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, .{ .cols = 80, .rows = 24 })); const missing = try std.fmt.bufPrint(&path_buf, "{s}/missing/" ++ (std.mem.asBytes(&@as([50][6]u8, @splat("child/".*)))), .{directory}); try std.testing.expect(missing.len > 256); - try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, 24, 80)); + try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, .{ .cols = 80, .rows = 24 })); } test "a forked shell is told which terminal it is talking to" { @@ -1711,7 +1705,7 @@ test "a forked shell is told which terminal it is talking to" { _ = Shell.setenv("TERM", "dumb", 1); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1739,7 +1733,7 @@ test "shell spawn uses an explicit OS directory longer than 256 bytes" { var explicit_buf: [4096]u8 = undefined; const explicit = try std.fmt.bufPrint(&explicit_buf, "/n/os{s}", .{directory}); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1768,7 +1762,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A wrapper exec'd over the shell (timeout standing in for 9ns) runs // another, two directories down, in the foreground. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1787,7 +1781,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A job of the shell's own, gone into a/b, leaves the pane where the // shell is. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1812,7 +1806,7 @@ test "shell cwd rejects truncation and preserves an owned child path longer than const directory = path_buf[0..try tmp.dir.realPathFile(io, nested, &path_buf)]; try std.testing.expect(directory.len > 1024); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1839,7 +1833,7 @@ test "a shell whose directory was removed is not named `... (deleted)`" { var path_buf: [4096]u8 = undefined; const directory = path_buf[0..try tmp.dir.realPathFile(io, "gone", &path_buf)]; const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1949,7 +1943,7 @@ test "a command pane's child is its shell running the one line, and its exit sta _ = try panes.newCommand(core, id, "", "printf 'said '; exit 7"); while (core.nextEffect()) |_| {} const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); var got: [256]u8 = undefined; var n: usize = 0; @@ -1980,7 +1974,7 @@ test "a command's exit is its process's, told by its watcher, whether or not its const id = core.freeSlot().?; _ = try panes.newCommand(core, id, "", c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); try std.testing.expect(watchExit(child.pid)); var got: ?libc.pid_t = null; @@ -2021,7 +2015,7 @@ test "a command's background job outlives it and its pty, and Kill stops its who const id = core.freeSlot().?; _ = try panes.newCommand(core, id, dir, c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", dir, 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", dir, .{ .cols = 80, .rows = 24 }); if (c.kill) { sleepMs(150); signalTty(child.pid, child.file.handle, .term, true); diff --git a/runtime/macos.zig b/runtime/macos.zig index 23d70b27..3221737a 100644 --- a/runtime/macos.zig +++ b/runtime/macos.zig @@ -1649,14 +1649,14 @@ fn hostState(ctx: ?*anyopaque) *State { return @ptrCast(@alignCast(ctx.?)); } -fn spawnShell(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { +fn spawnShell(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const pane = id.index(); const st = hostState(ctx); const core = st.core; // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = st.ptys[pane] != null; - const child = host_io.forkShell(core, pane, &st.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { + const child = host_io.forkShell(core, pane, &st.prompt_rcs, core.shellBin(), cwd, size) catch |err| { if (running) return core.restartFailed(id, err); return core.shellFailed(id, err); }; @@ -1685,11 +1685,11 @@ fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { if (st.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); } -fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { +fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, size: pardes.PtySize) void { const pane = id.index(); const st = hostState(ctx); const pt = st.ptys[pane] orelse return; - const ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + const ws = host_io.ptyWinsize(size); _ = posix.system.ioctl(pt.file.handle, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -2339,7 +2339,7 @@ test "mac Restore cancels a PTY reader waiting for output capacity" { while (st.core.nextEffect()) |_| {} st.inbox.acceptReader(.at(1), 1); for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(.at(1), 1, try gpa.dupe(u8, "retained output")); - const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40); + const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", .{ .cols = 40, .rows = 12 }); st.gens[0] = 1; st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } }; defer { diff --git a/test/process/terminals.zig b/test/process/terminals.zig index d9c3bcca..151b221b 100644 --- a/test/process/terminals.zig +++ b/test/process/terminals.zig @@ -16,6 +16,7 @@ pub const cases = .{ .{ "terminals: a program's prompt shows and holds still, input sent unsubmitted shows, and ^U takes the screen back to what it was", prompt }, .{ "terminals: what a program prints past a screenful stays in the terminal's body, and its prompt after it shows", history }, .{ "terminals: a control byte through pty/data reaches a program in raw mode, which then carries on", rawKey }, + .{ "terminals: a shell started into a pane runs at the pane's grid, not the screen's: a new terminal, its shell started again by pty/ctl's exec, a finished command pane run again", sizedFork }, }; fn has(hay: []const u8, needle: []const u8) bool { @@ -78,6 +79,98 @@ const Other = struct { } }; +/// What `stty size` in terminal `term` says, beside the grid its pty/ctl +/// reads back, each as "rows cols". +fn sttySize(a: std.mem.Allocator, c: *process.Conn, term: u32) ![2][]const u8 { + var lines = std.mem.splitScalar(u8, try run(a, c, term, "stty size\n"), '\n'); + _ = lines.next(); + return .{ lines.next() orelse "", try grid(a, c, term) }; +} + +/// Pane `n`'s grid, as pty/ctl reads it back ("winsize C R"): "rows cols". +fn grid(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const u8 { + var w = std.mem.tokenizeAny(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/pty/ctl", .{n})), " \n"); + _ = w.next(); + const cols = w.next() orelse ""; + return std.fmt.allocPrint(a, "{s} {s}", .{ w.next() orelse "", cols }); +} + +/// The lines of pane `n`'s body that are two numbers (`stty size`'s). +fn sizeLines(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{n})), '\n'); + while (rows.next()) |row| { + var w = std.mem.tokenizeScalar(u8, std.mem.trim(u8, row, " \r"), ' '); + const r = w.next() orelse continue; + const k = w.next() orelse continue; + if (w.next() != null) continue; + _ = std.fmt.parseInt(u16, r, 10) catch continue; + _ = std.fmt.parseInt(u16, k, 10) catch continue; + try out.append(a, try std.fmt.allocPrint(a, "{s} {s}", .{ r, k })); + } + return out.items; +} + +/// A shell takes runs: its first prompt, or the one started again. +fn prompted(a: std.mem.Allocator, c: *process.Conn, term: u32) !void { + const deadline = eh.nowMs() + 10_000; + while (!std.mem.eql(u8, try run(a, c, term, "true\n"), "exit 0\n")) { + if (eh.nowMs() > deadline) return error.NoPrompt; + process.sleep(50); + } +} + +fn sizedFork(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var d = try process.Detached.start(gpa, exe, "sized", .{}); + defer d.deinit(); + const f = &d.files; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const c = try process.Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const text = (try serials(a, c))[0]; + try c.write("/exec", "Shell bash\n"); + // A terminal beside the text: its grid is its rect less the tag, never + // the screen's 160x50. + try c.write("/exec", "Term\n"); + const term = try c.newest(gpa); + try prompted(a, c, term); + var got = try sttySize(a, c, term); + try expect(!std.mem.eql(u8, got[1], "50 160")); + try expectEqualStrings(got[1], got[0]); + // Its shell started again: the pane is sized already, and nothing + // resizes it after. + const pid = try run(a, c, term, "echo $$\n"); + try c.write(f.at("/pane/{d}/pty/ctl", .{term}), "exec\n"); + const again_by = eh.nowMs() + 10_000; + while (true) { + const now = run(a, c, term, "echo $$\n") catch ""; + if (std.mem.startsWith(u8, now, "exit 0\n") and !std.mem.eql(u8, now, pid)) break; + if (eh.nowMs() > again_by) return error.NeverStartedAgain; + process.sleep(50); + } + got = try sttySize(a, c, term); + try expectEqualStrings(got[1], got[0]); + // A command pane, then the same line again: the finished pane is run + // again (exec.zig `runCommand`), a pane sized already. (The new one is + // placed a step after its fork: its first size can be the screen's.) + try c.exec(text, "stty size"); + var words = std.mem.tokenizeAny(u8, try c.pane(a, text, "exec"), " \n"); + var last: []const u8 = ""; + while (words.next()) |w| last = w; + const cmd = try std.fmt.parseInt(u32, last, 10); + for (1..3) |k| { + if (k == 2) try c.exec(text, "stty size"); + const by = eh.nowMs() + 10_000; + while ((try sizeLines(a, c, cmd)).len < k) { + if (eh.nowMs() > by) return error.NoSize; + process.sleep(50); + } + if (k == 2) try expectEqualStrings(try grid(a, c, cmd), (try sizeLines(a, c, cmd))[1]); + } +} + fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var d = try process.Detached.start(gpa, exe, "run", .{}); defer d.deinit(); -- cgit v1.3