summaryrefslogtreecommitdiff
path: root/core/term
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 22:06:32 -0300
committerGabriel Schneider <[email protected]>2026-11-19 22:26:54 -0300
commit98b2bc05cd7b6c78952600029939bbc44c5fc56b (patch)
tree01940f7848aaface881a281c98d583948b7b4511 /core/term
parent12d058283aabb10d02ae86552ed82fcd48ba91af (diff)
downloadpardes-98b2bc05cd7b6c78952600029939bbc44c5fc56b.tar.gz
pardes-98b2bc05cd7b6c78952600029939bbc44c5fc56b.zip
Term.reload: a live terminal made again from its own snapshot where it is (the snapshot made, decoded, the old emulator let go, the new one installed with its stream, its sequence in flight and pardes's parts), returning the snapshot's hash: what makes two emulators with other histories of pages equal again, since ghostty prunes its history a page at a time against a limit in bytes and a snapshot does not say how its rows were cut into pages (reports/pardes-term-bytes.md, sections 1 and 2; the brief's step 1b; an Ops entry, the core's Term.reload); the sizing's join check as a test of the tree: plain text, a build's log and heavy colour and links (a truecolour a cell, a link a word, marks piled on letters) past a 1 MiB scrollback at 120x40, cut at random read boundaries: with every side reloaded (the session, a replica there from the start, a joiner loaded from the session's snapshot after) the same snapshot bytes, digest, marks and replies after more bytes and again after two reflows, 6 of 6 joins; without the reload the heavy joiner is another terminal, 2 of 2 (the pages pruned otherwise); term-test 17/17, core-test 848/991 (143 skipped), shell-test 1027/1030
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/term')
-rw-r--r--core/term/term.zig1
-rw-r--r--core/term/terminal.zig167
2 files changed, 164 insertions, 4 deletions
diff --git a/core/term/term.zig b/core/term/term.zig
index 6e36816a..6f62b827 100644
--- a/core/term/term.zig
+++ b/core/term/term.zig
@@ -31,6 +31,7 @@ pub const ops: iface.Ops = .{
.parts = cast(iface.Ops, "parts", terminal_.parts),
.snapshot = cast(iface.Ops, "snapshot", terminal_.snapshot),
.load = cast(iface.Ops, "load", terminal_.load),
+ .reload = cast(iface.Ops, "reload", terminal_.reload),
.feed = cast(iface.Ops, "feed", terminal_.feed),
.grid = cast(iface.Ops, "grid", terminal_.grid),
.viewTo = cast(iface.Ops, "viewTo", terminal_.viewTo),
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index f92da3fd..a4120f5a 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -301,7 +301,7 @@ pub fn snapshot(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer, slim
fn decode(gpa: std.mem.Allocator, bytes: []const u8) !ghostty_vt.snapshot.Decoded {
if (bytes.len == 4) {
const size = std.mem.bytesToValue([2]u16, bytes[0..4]);
- return .{ .terminal = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .continuation = .ground, .history_rows = .{} };
+ return .{ .terminal = try emulator(gpa, @max(1, size[0]), @max(1, size[1]), scrollback_max), .continuation = .ground, .history_rows = .{} };
}
var reader: std.Io.Reader = .fixed(bytes);
return ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{
@@ -326,6 +326,34 @@ pub fn load(gpa: std.mem.Allocator, bytes: []const u8, saved: *const Term.Parts)
return out;
}
+/// Its emulator made again from its own snapshot, where it is, what pardes
+/// keeps beside it kept: what a replica and its session do at a join, so
+/// that both, and the joiner loaded from the session's, hold the same pages
+/// (ghostty cuts rows into pages, and prunes whole ones, by its history,
+/// which a snapshot does not carry). The snapshot's hash, which every side
+/// compares; on an error it is as it was.
+pub fn reload(s: *State) !u64 {
+ const gpa = s.gpa;
+ var bytes: std.Io.Writer.Allocating = .init(gpa);
+ defer bytes.deinit();
+ try snapshot(s, gpa, &bytes.writer, false);
+ var decoded = try decode(gpa, bytes.written());
+ defer decoded.deinit(gpa);
+ var saved = parts(s);
+ var reply: [@typeInfo(@FieldType(State, "reply")).array.len]u8 = undefined;
+ @memcpy(reply[0..saved.reply.len], saved.reply);
+ saved.reply = reply[0..saved.reply.len];
+ const kept = .{ s.changes, s.read, s.resize_to };
+ if (comptime images_enabled) s.images.deinit(gpa);
+ s.stream.deinit();
+ s.vt.deinit(gpa);
+ s.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined, .changes = kept[0] +% 1, .read = kept[1], .resize_to = kept[2] };
+ // Short of room for its sequence in flight it goes on without it (lost
+ // at the next cut), as ghostty's stream does: not worth a terminal.
+ _ = install(s, decoded.continuation, &saved);
+ return std.hash.Wyhash.hash(0, bytes.written());
+}
+
/// A decoded emulator at its address made a terminal: its stream wired,
/// the sequence in flight fed again, what pardes kept beside it put back;
/// whether its stream had all the room it wanted (`wire`).
@@ -770,11 +798,14 @@ pub fn close(state: *State) void {
}
/// A terminal pane's emulator, `cols` by `rows`, as `open` makes one.
-fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal {
+/// How much history a terminal keeps, in bytes of ghostty's pages.
+const scrollback_max = 16 * 1024 * 1024;
+
+fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16, history: usize) !ghostty_vt.Terminal {
return ghostty_vt.Terminal.init(terminalIo(), gpa, .{
.cols = cols,
.rows = rows,
- .max_scrollback_bytes = 16 * 1024 * 1024,
+ .max_scrollback_bytes = history,
.kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0,
// Every kitty medium, as ghostty and kitty take them: a file (t=f),
// a temporary file it then deletes (t=t, in the temp directory and
@@ -789,11 +820,17 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal {
/// A new emulator, `cols` by `rows`, allocated with `gpa`, its own from
/// then on (`State.gpa`).
pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State {
+ return openKeeping(gpa, cols, rows, scrollback_max);
+}
+
+/// `open`, keeping `history` bytes of history (the tests below fill a
+/// smaller one).
+fn openKeeping(gpa: std.mem.Allocator, cols: u16, rows: u16, history: usize) !*State {
const state = try gpa.create(State);
errdefer gpa.destroy(state);
// Every other field its default, never what the heap had: a digest
// reads them, and two replicas must agree (core/replica.zig).
- state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows), .stream = undefined };
+ state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows, history), .stream = undefined };
if (!state.wire()) {
state.stream.deinit();
state.vt.deinit(gpa);
@@ -1626,3 +1663,125 @@ test "a snapshot taken inside a sequence carries it: over pardes's own handler t
};
};
}
+
+/// What the join tests below stream: plain text, a build's log, and heavy
+/// colour and links (a truecolour a cell, a link a word, marks piled on
+/// letters, wide characters: what runs a page out of room for styles,
+/// links and graphemes), `lines` of it, no clear.
+const Corpus = enum { text, logs, heavy };
+
+fn corpus(gpa: std.mem.Allocator, which: Corpus, lines: usize) ![]u8 {
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ errdefer out.deinit();
+ const w = &out.writer;
+ var prng: std.Random.DefaultPrng = .init(@as(u64, @intFromEnum(which)) + 11);
+ const random = prng.random();
+ const words = [_][]const u8{ "the", "quick", "brown", "fox", "jumps", "over", "a", "lazy", "dog", "./core/replica.zig:1234:", "error:", "expected", "\u{6F22}\u{5B57}", "na\u{EF}ve" };
+ for (0..lines) |i| switch (which) {
+ .text => {
+ for (0..random.uintLessThan(usize, 18)) |_| try w.print("{s} ", .{words[random.uintLessThan(usize, words.len)]});
+ try w.print("{d}\r\n", .{i});
+ },
+ .logs => try w.print("[{d:>3}%] \x1b[32mBuilding CXX object\x1b[0m src/CMakeFiles/core.dir/module_{d}.cpp.o{s}\r\n", .{ i * 100 / lines, i, if (i % 9 == 0) " \x1b[1;33mwarning:\x1b[0m unused variable" else "" }),
+ .heavy => switch (i % 3) {
+ 0 => {
+ for (0..100) |x| try w.print("\x1b[38;2;{d};{d};{d}m{c}", .{ (i * 7 + x * 3) % 256, (i * 13 + x * 5) % 256, (x * 11 + i) % 256, @as(u8, @intCast(97 + (x + i) % 26)) });
+ try w.writeAll("\x1b[0m\r\n");
+ },
+ 1 => {
+ for (0..18) |x| try w.print("\x1b]8;;https://example.com/{d}/{d}\x1b\\w{d}\x1b]8;;\x1b\\ ", .{ i, x, x });
+ try w.writeAll("\r\n");
+ },
+ else => {
+ for (0..40) |x| {
+ try w.writeByte(@intCast(97 + (x + i) % 26));
+ for (0..1 + x % 6) |j| try w.print("{u}", .{@as(u21, @intCast(0x300 + (x * 3 + j + i) % 0x6f))});
+ }
+ try w.writeAll(" \u{6F22}\u{5B57}\u{6F22}\u{5B57} \u{1F469}\u{200D}\u{1F4BB}\r\n");
+ },
+ },
+ };
+ return out.toOwnedSlice();
+}
+
+/// `bytes[from..to]` fed in reads of 4 KiB, and two reflows on the way
+/// at `stops` (to a little over half the width and three rows taller, then
+/// back), as the join tests' every side takes them.
+fn feedReflowing(s: *State, bytes: []const u8, from: usize, to: usize, stops: [2]usize) void {
+ const cols = s.vt.cols;
+ const rows = s.vt.rows;
+ var at = from;
+ while (at < to) {
+ if (at == stops[0]) resize(s, cols / 2 + 1, rows + 3, false);
+ if (at == stops[1]) resize(s, cols * 2 - 2, rows - 3, false);
+ var end = @min(to, at + 4096);
+ for (stops) |stop| if (at < stop and stop < end) {
+ end = stop;
+ };
+ s.stream.nextSlice(bytes[at..end]);
+ at = end;
+ }
+}
+
+fn sameSnapshot(gpa: std.mem.Allocator, a: *const State, b: *const State) !bool {
+ const x = try snapshotOf(gpa, a);
+ defer gpa.free(x);
+ const y = try snapshotOf(gpa, b);
+ defer gpa.free(y);
+ return std.mem.eql(u8, x, y);
+}
+
+test "a join at a cut: every side reloaded from its own snapshot holds the same terminal as the joiner, after more bytes and after two reflows; a joiner nobody reloaded for with heavy colour does not" {
+ // The sizing pass's join check (reports/pardes-term-bytes.md, section 2)
+ // in the tree: a scrollback past its limit (pruned a page at a time,
+ // by the history of its pages), cut at a random read boundary, often
+ // inside a sequence.
+ const gpa = std.testing.allocator;
+ var prng: std.Random.DefaultPrng = .init(29);
+ const random = prng.random();
+ var heavy_apart: usize = 0;
+ for ([_]Corpus{ .text, .logs, .heavy }, [_]usize{ 6000, 6000, 3000 }) |which, lines| {
+ const bytes = try corpus(gpa, which, lines);
+ defer gpa.free(bytes);
+ for (0..2) |_| {
+ const cut = bytes.len / 3 + random.uintLessThan(usize, bytes.len / 3);
+ const mid = cut + (bytes.len - cut) / 2;
+ const stops: [2]usize = .{ mid + (bytes.len - mid) / 3, mid + 2 * (bytes.len - mid) / 3 };
+ // The session, and a replica that was there from the start.
+ const session = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(session);
+ const there = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(there);
+ feedReflowing(session, bytes, 0, cut, stops);
+ feedReflowing(there, bytes, 0, cut, stops);
+ // The same join made without the reload: the session as it
+ // would go on, and a joiner loaded from its snapshot.
+ const lineage = try openKeeping(gpa, 120, 40, 1 << 20);
+ defer close(lineage);
+ feedReflowing(lineage, bytes, 0, cut, stops);
+ const before = try snapshotOf(gpa, session);
+ defer gpa.free(before);
+ const unreloaded = try load(gpa, before, &parts(session));
+ defer close(unreloaded);
+ // The join as it is made: every side reloads, the joiner loads
+ // the session's snapshot taken after (the join's dump).
+ const hash = try reload(session);
+ try std.testing.expectEqual(hash, try reload(there));
+ try std.testing.expectEqual(hash, std.hash.Wyhash.hash(0, before));
+ const after = try snapshotOf(gpa, session);
+ defer gpa.free(after);
+ const joiner = try load(gpa, after, &parts(session));
+ defer close(joiner);
+ for ([_]*State{ session, there, joiner, unreloaded, lineage }) |s| feedReflowing(s, bytes, cut, mid, stops);
+ try expectSame(gpa, session, there);
+ try expectSame(gpa, session, joiner);
+ if (!try sameSnapshot(gpa, lineage, unreloaded)) heavy_apart += @intFromBool(which == .heavy);
+ for ([_]*State{ session, there, joiner }) |s| feedReflowing(s, bytes, mid, bytes.len, stops);
+ try expectSame(gpa, session, there);
+ try expectSame(gpa, session, joiner);
+ }
+ }
+ // The fault the reload mends is real: a joiner nobody reloaded for is
+ // another terminal once heavy colour has pruned pages cut otherwise.
+ try std.testing.expectEqual(2, heavy_apart);
+}