diff options
| -rw-r--r-- | core/Term.zig | 13 | ||||
| -rw-r--r-- | core/panes.zig | 18 | ||||
| -rw-r--r-- | core/replica.zig | 68 | ||||
| -rw-r--r-- | core/tape.zig | 6 |
4 files changed, 96 insertions, 9 deletions
diff --git a/core/Term.zig b/core/Term.zig index 89773f77..eabf5b81 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -65,6 +65,12 @@ fn the() *const iface.Ops { /// as it always has (`Parts` by name, then ghostty's snapshot). pub const State = struct { impl: *iface.State, + /// Written since its last reload (a feed, a reflow, a load: what cuts + /// its rows into pages), which a join reloads (panes.reloadTerminals, + /// the session's choice). One not holds the pages its last reload's + /// snapshot loads to, on every side, and so does a joiner loading the + /// same snapshot again from the dump. + 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 @@ -155,7 +161,9 @@ pub fn reload(s: *State) !u64 { reloads.ns += dt; reloads.worst_ns = @max(reloads.worst_ns, dt); } - return the().reload(s.impl); + const hash = try the().reload(s.impl); + s.written = false; + return hash; } /// When its next kitty animation frame is due (ms on the core's clock), as @@ -168,6 +176,7 @@ pub fn due(s: *const State) ?u64 { /// history marked off from what the next shell prints. pub fn revive(s: *State) void { writes +%= 1; + s.written = true; the().revive(s.impl); } @@ -187,6 +196,7 @@ pub fn stop() void { /// went). pub fn feed(s: *State, bytes: []const u8, tracked: []const i32, rows: []i32, erased: []bool) Told { writes +%= 1; + s.written = true; return the().feed(s.impl, bytes, tracked, rows, erased); } @@ -217,6 +227,7 @@ pub fn follow(s: *State) void { /// Its grid reflowed, now or when `resizeDeferred` makes it. pub fn resize(s: *State, cols: u16, rows: u16, later: bool) void { writes +%= 1; + s.written = true; the().resize(s.impl, cols, rows, later); } diff --git a/core/panes.zig b/core/panes.zig index fd152e1e..7d604567 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1120,16 +1120,20 @@ pub const Term = @import("Term.zig"); pub const positions_lost = "terminal edit position reset: out of memory"; /// At a join (Core's openView, rejoinView): under bytes, every live -/// terminal made again from its own snapshot, on every side alike, the -/// session included (Term.reload): an emulator loaded from a snapshot holds -/// its rows in other pages than one fed from its start, and ghostty prunes -/// its history a page at a time, so the joiner, loaded from the session's -/// dump made next, and the replicas there before it, would part. The +/// terminal written since its last reload made again from its own snapshot, +/// on every side alike, the session included (Term.reload): an emulator +/// loaded from a snapshot holds its rows in other pages than one fed from +/// its start, and ghostty prunes its history a page at a time, so the +/// joiner, loaded from the session's dump made next, and the replicas there +/// before it, would part. Which, the session says (`Term.State.written`, +/// taped): one written by nothing since holds the pages its last reload +/// made, on every side, and the joiner loads that snapshot again. The /// 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 // frontend's draw sets its own view's between steps (ansi.zig), a // window's other than a terminal's, and the snapshot holds it. @@ -1144,6 +1148,10 @@ fn sameHash(h: ?u64) ?u64 { return h; } +fn written(s: *Term.State) bool { + return s.written; +} + /// The sizes steps gave terminals (`Pane.resizeGrid`), made: between /// units, on an authority with screen-state replicas (replica.Peers.unit). pub fn resizeDeferred(p: *Pardes) void { diff --git a/core/replica.zig b/core/replica.zig index b4d8edfa..6dcc0281 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -4480,6 +4480,74 @@ test "bytes: a join refused for want of a view reloads every terminal and moves try sameTerminals(b.a, b.r); } +test "bytes: a join reloads only the terminals written since their last reload: one idle since the last join is reloaded on no side, and through a prune of both every side holds the session's terminals, the joiner too" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const b = try BytesPair.init(); + defer b.deinit(); + b.r.confirm(try b.a.local(.{ .command = "Term" })); + b.r.settle(); + const second: pardes.Pane.Id = for (b.a.core.panes, 0..) |slot, id| { + if (id != b.shell.index() and (slot orelse continue).terminal != null) break .at(id); + } else return error.NoTerminal; + const both = [_]pardes.Pane.Id{ b.shell, second }; + // Both written, and a join reloads both. + for (both) |id| b.a.output(.{ .output = .{ .pane = id, .bytes = "written\r\n" } }); + b.r.confirm((try b.a.pending()).?); + const first = (try b.a.join(2, null)).?; + gpa.free(first.dump); + b.r.confirm(first.unit); + // The shell alone written since: the next join reloads it alone, on + // every side; the joiner loads the other's snapshot as it was. + try b.output("the shell's\r\n"); + const before = pardes.Term.reloads.n; + const joined = (try b.a.join(3, null)).?; + defer gpa.free(joined.dump); + try std.testing.expectEqual(1, pardes.Term.reloads.n - before); + b.r.confirm(joined.unit); + 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 + // the history of its pages), a unit every few reads. + var read: std.ArrayListUnmanaged(u8) = .empty; + defer read.deinit(gpa); + const lines = 40_000; + var n: usize = 0; + while (n < lines) : (n += 100) { + read.clearRetainingCapacity(); + for (n..n + 100) |i| try read.print(gpa, "\x1b[3{d}m{d}\x1b[0m line\r\n", .{ i % 8, i }); + for (both) |id| b.a.output(.{ .output = .{ .pane = id, .bytes = read.items } }); + if (n % 2000 == 1900) { + const u = (try b.a.pending()).?; + b.r.confirm(u); + late.confirm(u); + } + } + if (try b.a.pending()) |u| { + b.r.confirm(u); + late.confirm(u); + } + for (both) |id| try std.testing.expect(pardes.Term.grid(b.a.core.panes[id.index()].?.terminal).total < lines); + try agrees(b.a, b.r); + try sameTerminals(b.a, b.r); + try agrees(b.a, late); + try sameTerminals(b.a, late); + // And a join after: both written, both reloaded, every hash compared. + const left = try b.a.leave(2, false); + b.r.confirm(left); + late.confirm(left); + const third = (try b.a.join(4, null)).?; + gpa.free(third.dump); + b.r.confirm(third.unit); + late.confirm(third.unit); + try agrees(b.a, b.r); + try agrees(b.a, late); +} + test "bytes: a session's output past a unit's bound is cut into units under it, between two reads" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; diff --git a/core/tape.zig b/core/tape.zig index ea1a5691..3d2b6f31 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -131,9 +131,9 @@ pub const Site = enum(u16) { /// What a call into a kind did (Kind.Outcome), and a kind's pane's /// tag prefix: the authority's, whose core alone runs the kind. addon, - /// The hash of a terminal's snapshot as the session reloaded it at a - /// join (panes.reloadTerminals), which every replica's own reload - /// compares. + /// Whether the session reloads a terminal at a join (written since its + /// last reload), then the hash of its snapshot as it did + /// (panes.reloadTerminals), which every replica's own reload compares. reload, }; |
