diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 22:05:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 22:26:46 -0300 |
| commit | 12d058283aabb10d02ae86552ed82fcd48ba91af (patch) | |
| tree | 36f21cc4a5b49b8db728631df35589281806366c | |
| parent | 0db34fbf28e70c8c6a3c6025c0888fc5ec75201c (diff) | |
| download | pardes-12d058283aabb10d02ae86552ed82fcd48ba91af.tar.gz pardes-12d058283aabb10d02ae86552ed82fcd48ba91af.zip | |
Term: a terminal's stream keeps the sequence its program began and has not ended (ghostty's continuation, up to 64 KiB: a CSI, an OSC, a UTF-8 codepoint, a kitty graphics chunk), its snapshot carries it and a load feeds it again, so a snapshot may be taken between any two reads (docs: reports/pardes-term-bytes.md, section 1; the brief's step 1a): ghostty promises it for its standard handler only, and a test pins it over pardes's MarkedHandler, a cut at every byte of a CSI, a link, pardes's own OSC 133 marks, a UTF-8 codepoint, a kitty graphics payload and three device queries (on the tree before: the first cut, inside the CSI, came to another terminal); the parse's cost with tracking on, pardes-perf --terminal-mib 8 and 32, instructions:u, three runs each, off and on alternated in one tree: 3,924.06M -> 3,924.09M and 13,202.27M -> 13,202.58M (+0.001%, +0.002%); term-test 16/16
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | core/term/terminal.zig | 146 |
1 files changed, 125 insertions, 21 deletions
diff --git a/core/term/terminal.zig b/core/term/terminal.zig index d99cfb79..f92da3fd 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -201,13 +201,17 @@ pub const State = struct { /// The stream over the emulator, its answers to the program and its /// size reports routed here, PNG decoded: what `create` and a dump's - /// load both do once the emulator is in place. - fn wire(s: *State) void { - s.stream = .init(.{ .handler = .{ .inner = s.vt.vtHandler() }, .allocator = s.gpa }); + /// load both do once the emulator is in place. False when the stream's + /// keeper of a sequence in flight had no room (ghostty goes on without + /// it, swallowing the error): `open` and `load` refuse the terminal then, + /// as at any allocation that fails. + fn wire(s: *State) bool { + s.stream = .init(.{ .handler = .{ .inner = s.vt.vtHandler() }, .allocator = s.gpa, .continuation_max_bytes = continuation_max }); s.stream.handler.inner.effects.write_pty = ptyReport; s.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; s.stream.handler.inner.effects.size = ptySize; if (comptime images_enabled) TerminalImages.installPngDecoder(); + return s.stream.continuation.?.bytes.capacity > 0; } }; @@ -268,14 +272,42 @@ pub fn parts(s: *const State) Term.Parts { }; } +/// The most bytes of a sequence its program began and has not ended (a +/// CSI, an OSC, a UTF-8 codepoint, a kitty graphics chunk: 4 KiB of payload) +/// that its stream keeps, so a snapshot taken between any two reads carries +/// it (`snapshot`). Past it the sequence is lost at a cut, as every one was +/// before. +pub const continuation_max = 64 * 1024; + /// Its emulator as the dump keeps it: ghostty's snapshot (both screens, -/// their history, styles, links and kitty images). A sequence its program -/// had begun and not ended is not kept: the snapshot is at the ground -/// state. `slim`, a join's dump (dump.zig `dumpJoin`): its size alone, an +/// their history, styles, links and kitty images), and the sequence its +/// program had begun and not ended, which `load` feeds again. `slim`, a +/// join's dump under screen state (dump.zig `dumpJoin`): its size alone, an /// emulator made afresh there, which its first delta replaces. pub fn snapshot(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer, slim: bool) !void { if (slim) return w.writeAll(std.mem.asBytes(&[2]u16{ s.vt.cols, s.vt.rows })); - try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = .ground }); + var held: std.Io.Writer.Allocating = .init(gpa); + defer held.deinit(); + s.stream.writeContinuation(&held.writer) catch |err| switch (err) { + // Longer than it keeps: lost, as at the ground state. + error.ContinuationUnavailable, error.ContinuationDisabled => held.clearRetainingCapacity(), + error.WriteFailed => return error.OutOfMemory, + }; + const continuation: ghostty_vt.snapshot.Continuation = if (held.written().len > 0) .{ .bytes = held.written() } else .ground; + try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = continuation }); +} + +/// `snapshot`'s bytes decoded, the emulator not yet at its address. +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 = .{} }; + } + var reader: std.Io.Reader = .fixed(bytes); + return ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ + .max_continuation_bytes = continuation_max, + .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, + }); } /// `snapshot`'s bytes and `parts` back into a new terminal, as `open` @@ -283,22 +315,29 @@ pub fn snapshot(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer, slim pub fn load(gpa: std.mem.Allocator, bytes: []const u8, saved: *const Term.Parts) !*State { const out = try gpa.create(State); errdefer gpa.destroy(out); - var reader: std.Io.Reader = .fixed(bytes); - if (reader.buffer.len == 4) { - const size = std.mem.bytesToValue([2]u16, bytes[0..4]); - out.* = .{ .gpa = gpa, .vt = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .stream = undefined }; - } else { - var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ - .max_continuation_bytes = 0, - .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, - }); - defer decoded.deinit(gpa); - out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; + var decoded = try decode(gpa, bytes); + defer decoded.deinit(gpa); + out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; + if (!install(out, decoded.continuation, saved)) { + out.stream.deinit(); + out.vt.deinit(gpa); + return error.OutOfMemory; } + return out; +} + +/// 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`). +fn install(out: *State, continuation: ghostty_vt.snapshot.Continuation, saved: *const Term.Parts) bool { if (comptime images_enabled) inline for (.{ .primary, .alternate }) |which| { if (out.vt.screens.get(which)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); }; - out.wire(); + const whole = out.wire(); + switch (continuation) { + .ground => {}, + .bytes => |bytes| out.stream.nextSlice(bytes), + } const reply = saved.reply[0..@min(saved.reply.len, out.reply.len)]; @memcpy(out.reply[0..reply.len], reply); out.reply_len = @intCast(reply.len); @@ -321,7 +360,7 @@ pub fn load(gpa: std.mem.Allocator, bytes: []const u8, saved: *const Term.Parts) const screen = out.vt.screens.active; const bottom = screen.pages.scrollbar().offset; screen.scroll(.{ .delta_row = @as(isize, @intCast(saved.view)) - @as(isize, @intCast(bottom)) }); - return out; + return whole; } /// The allocator it was opened with, its own from then on. @@ -755,7 +794,11 @@ pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*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.wire(); + if (!state.wire()) { + state.stream.deinit(); + state.vt.deinit(gpa); + return error.OutOfMemory; + } return state; } @@ -1522,3 +1565,64 @@ test "the luminance table answers exactly what ghostty computes" { } } } + +/// Its snapshot as the dump writes it, `gpa`'s (the tests below). +fn snapshotOf(gpa: std.mem.Allocator, s: *const State) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + try snapshot(s, gpa, &out.writer, false); + return out.toOwnedSlice(); +} + +/// Two terminals as a replica compares them: their snapshots' bytes, what +/// pardes keeps beside the emulator, and the digest. +fn expectSame(gpa: std.mem.Allocator, a: *const State, b: *const State) !void { + const x = try snapshotOf(gpa, a); + defer gpa.free(x); + const y = try snapshotOf(gpa, b); + defer gpa.free(y); + try std.testing.expectEqualSlices(u8, x, y); + var ma: [marks_len]u8 = undefined; + var mb: [marks_len]u8 = undefined; + try std.testing.expectEqualSlices(u8, marksOf(a, &ma), marksOf(b, &mb)); + try std.testing.expectEqualSlices(u8, a.reply[0..a.reply_len], b.reply[0..b.reply_len]); + var ha: std.hash.Wyhash = .init(0); + var hb: std.hash.Wyhash = .init(0); + digest(a, &ha, true); + digest(b, &hb, true); + try std.testing.expectEqual(ha.final(), hb.final()); +} + +test "a snapshot taken inside a sequence carries it: over pardes's own handler the load continues as the terminal fed whole" { + // ghostty promises the continuation for its standard handler; pardes's + // MarkedHandler wraps it (OSC 133 marks, erases): cut at every byte of + // a CSI, an OSC (a link, pardes's own marks), a UTF-8 codepoint, a + // kitty graphics payload (APC) and a device query. + const gpa = std.testing.allocator; + const streams = [_][]const u8{ + "a\x1b[38;2;10;20;30mcolour\x1b[0m b", + "\x1b]8;;https://example.com/x\x1b\\link\x1b]8;;\x1b\\ c", + "x\u{1F469}\u{200D}\u{1F4BB}y \u{6F22}\u{5B57} \u{E9}", + "\x1b_Ga=T,i=7,f=24,s=1,v=1;AAAA\x1b\\ after", + "\x1b]133;A;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07ls\r\n\x1b]133;C;aid=pardes\x07out\r\n\x1b]133;D;0;aid=pardes\x07", + "q\x1b[6n\x1b[c\x1b[16t z", + }; + for (streams) |stream| for (1..stream.len) |cut| { + const whole = try open(gpa, 24, 5); + defer close(whole); + whole.stream.nextSlice(stream[0..cut]); + whole.stream.nextSlice(stream[cut..]); + const cutting = try open(gpa, 24, 5); + defer close(cutting); + cutting.stream.nextSlice(stream[0..cut]); + const bytes = try snapshotOf(gpa, cutting); + defer gpa.free(bytes); + const loaded = try load(gpa, bytes, &parts(cutting)); + defer close(loaded); + loaded.stream.nextSlice(stream[cut..]); + expectSame(gpa, whole, loaded) catch |err| { + std.debug.print("cut at byte {d} of {f}\n", .{ cut, std.ascii.hexEscape(stream, .lower) }); + return err; + }; + }; +} |
