summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 00:51:28 -0300
committerGabriel Schneider <[email protected]>2026-11-20 01:04:49 -0300
commit4bcf2055cb97d0c69fc94643b3e43b579d6fa30c (patch)
treecc845bdc25436aca800fb4791a9709ad913f3d32 /core
parent8e04bc6370916ac328f61ad0df6e3babeb9789b4 (diff)
downloadpardes-4bcf2055cb97d0c69fc94643b3e43b579d6fa30c.tar.gz
pardes-4bcf2055cb97d0c69fc94643b3e43b579d6fa30c.zip
term: a kitty image's medium (a file, a temporary file, shared memory) is the session's read and a recorded answer on the tape, so every replica's emulator is handed the bytes the session read, or its failure, and never reads its own machine: ghostty's loader gets the bytes through `LoadingImage.read_medium` (an Edit in build/patches.zig, ghostty's own read handed to it, the report in docs/ghostty-kitty-medium.md), which the terminal module sets to the core's `Term.media`, a `tape.valueIn(.medium, ...)` site (a new Site; `valueIn` is `value` with the answer's slices in the loader's allocator, no yield inside a feed), so a read crosses to a client as a file read does (inline under 1 MiB, by hash at or over it), a temporary file is deleted and shared memory unlinked once, on the session, and the program is answered by the session alone; a replica no longer loads by direct data only: `Term.direct`, `State.direct` and the `direct` entry go (`media` in their place); under screen state the read stays the authority's (a screen-state replica feeds no emulator); FIRST the test through the replica's surface (a session and a bytes replica, four images shown, by a file, a temporary file, shared memory and a missing file): on the base the digests parted (expected 8810391091473565423, found 2236913079067993401: the replica's loader said "unsupported medium" and did not move its cursor), now they agree, the terminals dump the same bytes, the temporary file and the object are gone and the replica read nothing (ghostty's FileNotFound warned once); tui term-test 17/17, kitty-test 45/45, core-test 856/1007 (151 skipped), shell-test 1043/1046, unit-test -Dtest-filter=bytes: 23/23 (the perf check's BaselineHarnessMismatch, as on the base)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
-rw-r--r--core/Term.zig22
-rw-r--r--core/replica.zig49
-rw-r--r--core/tape.zig9
-rw-r--r--core/term/term.zig2
-rw-r--r--core/term/terminal.zig27
5 files changed, 85 insertions, 24 deletions
diff --git a/core/Term.zig b/core/Term.zig
index 89773f77..aa05a68f 100644
--- a/core/Term.zig
+++ b/core/Term.zig
@@ -102,10 +102,9 @@ pub const State = struct {
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const parts = try walk.load(iface.Parts, arena.allocator(), bytes[8..][0..n], null);
+ o.media(media);
out.* = .{ .impl = try o.load(gpa, bytes[8 + n ..], &parts) };
writes +%= 1;
- // A replica's (a join's, a closed terminal brought back in a step).
- if (tape.replaying() or dump.terminal_gpa != null) o.direct(out.impl);
}
};
@@ -125,14 +124,27 @@ pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State {
const o = ops orelse return error.NoTerminal;
const s = try gpa.create(State);
errdefer gpa.destroy(s);
+ o.media(media);
s.* = .{ .impl = try o.open(gpa, cols, rows) };
writes +%= 1;
- // A replica's emulator loads its program's images by direct data alone
- // (P32): what is read off the session's machine is the session's.
- if (tape.replaying()) o.direct(s.impl);
return s;
}
+/// A kitty image's medium (a file, a temporary file, shared memory: the
+/// 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.
+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 });
+}
+
+fn readMedium(read: *const fn (*const anyopaque) iface.MediumError![]u8, ctx: *const anyopaque) iface.MediumError![]u8 {
+ return read(ctx);
+}
+
/// It and what it holds, freed, all of it with its own allocator (the
/// one it was opened with): the allocator a caller hands is not used.
pub fn close(s: *State, _: std.mem.Allocator) void {
diff --git a/core/replica.zig b/core/replica.zig
index a52061f4..14757d9a 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -4427,6 +4427,55 @@ test "bytes: a session's output past a unit's bound is cut into units under it,
try sameTerminals(b.a, b.r);
}
+test "bytes: a kitty image shown by a file, a temporary file or shared memory is the session's read, and the replica is handed its bytes: the same image, the cursor moved past it, the temporary file and the shared memory gone once, a missing file refused alike" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const shm = struct {
+ extern "c" fn shm_open(name: [*:0]const u8, flag: c_int, mode: std.c.mode_t) c_int;
+ extern "c" fn shm_unlink(name: [*:0]const u8) c_int;
+ extern "c" fn ftruncate(fd: c_int, len: i64) c_int;
+ };
+ // ghostty warns of the missing file, on the session alone.
+ const level = std.testing.log_level;
+ std.testing.log_level = .err;
+ defer std.testing.log_level = level;
+ const b = try BytesPair.init();
+ defer b.deinit();
+ const pixels: [12]u8 = @splat(0x7f); // 2 by 2, RGB
+ const pid = std.c.getpid();
+ var names: [4][80]u8 = undefined;
+ const file = try std.fmt.bufPrintSentinel(&names[0], "/tmp/pardes-replica-{d}-file.rgb", .{pid}, 0);
+ const temp = try std.fmt.bufPrintSentinel(&names[1], "/tmp/tty-graphics-protocol-pardes-replica-{d}.rgb", .{pid}, 0);
+ const object = try std.fmt.bufPrintSentinel(&names[2], "/pardes-replica-{d}", .{pid}, 0);
+ const missing = try std.fmt.bufPrintSentinel(&names[3], "/tmp/pardes-replica-{d}-missing.rgb", .{pid}, 0);
+ for ([_][:0]const u8{ file, temp }) |path| {
+ const fd = std.c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(std.c.mode_t, 0o600));
+ try std.testing.expect(fd >= 0);
+ defer _ = std.c.close(fd);
+ try std.testing.expectEqual(@as(isize, pixels.len), std.c.write(fd, &pixels, pixels.len));
+ }
+ defer for ([_][:0]const u8{ file, temp }) |path| {
+ _ = std.c.unlink(path);
+ };
+ {
+ const fd = shm.shm_open(object, @bitCast(std.c.O{ .ACCMODE = .RDWR, .CREAT = true, .TRUNC = true }), 0o600);
+ try std.testing.expect(fd >= 0);
+ defer _ = std.c.close(fd);
+ try std.testing.expectEqual(@as(c_int, 0), shm.ftruncate(fd, pixels.len));
+ try std.testing.expectEqual(@as(isize, pixels.len), std.c.write(fd, &pixels, pixels.len));
+ }
+ defer _ = shm.shm_unlink(object);
+ const enc = std.base64.standard.Encoder;
+ var line: [256]u8 = undefined;
+ var b64: [128]u8 = undefined;
+ for ([_][:0]const u8{ file, temp, object, missing }, "ftsf", 7..) |path, medium, id|
+ try b.output(try std.fmt.bufPrint(&line, "\x1b_Ga=T,t={c},i={d},f=24,s=2,v=2;{s}\x1b\\ shown\r\n", .{ medium, id, enc.encode(&b64, path) }));
+ try agrees(b.a, b.r);
+ try sameTerminals(b.a, b.r);
+ try std.testing.expect(std.c.access(file, std.c.F_OK) == 0);
+ try std.testing.expect(std.c.access(temp, std.c.F_OK) != 0);
+ try std.testing.expect(shm.shm_open(object, @bitCast(std.c.O{ .ACCMODE = .RDONLY }), 0) < 0);
+}
+
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);
diff --git a/core/tape.zig b/core/tape.zig
index d977b53e..f705636f 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -135,6 +135,9 @@ pub const Site = enum(u16) {
/// join (panes.reloadTerminals), which every replica's own reload
/// compares.
reload,
+ /// 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,
};
/// What the sites return. wire.semantics hashes their shape with the
@@ -1296,6 +1299,12 @@ pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn"
return (on orelse &none).authority(f, args);
}
+/// `value` whose answer's slices are `a`'s on a replay, as `site`'s are:
+/// what its caller keeps (a kitty image's medium, Term.media).
+pub fn valueIn(comptime s: Site, a: std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
+ return (on orelse &none).call(s, a, f, args, false);
+}
+
/// What `authority` was asked: `f` and its arguments as they are.
fn question(comptime f: anytype, args: anytype) u64 {
var h = std.hash.Wyhash.init(@intFromPtr(&f));
diff --git a/core/term/term.zig b/core/term/term.zig
index b18f5875..59511727 100644
--- a/core/term/term.zig
+++ b/core/term/term.zig
@@ -32,7 +32,7 @@ pub const ops: iface.Ops = .{
.snapshot = cast(iface.Ops, "snapshot", terminal_.snapshot),
.load = cast(iface.Ops, "load", terminal_.load),
.reload = cast(iface.Ops, "reload", terminal_.reload),
- .direct = cast(iface.Ops, "direct", terminal_.direct),
+ .media = terminal_.media,
.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 055c8981..e57193a6 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -195,11 +195,6 @@ pub const State = struct {
/// the step reads the grid it had, as its replicas' replays do, and the
/// rows sent them for it (Screens.fill) are of that grid's numbering.
resize_to: ?struct { cols: u16, rows: u16 } = null,
- /// Its program's kitty images by direct data alone (`direct`): a
- /// replica's emulator, which is not the session's. A file, a temporary
- /// file or shared memory is the session's machine's (and a temporary
- /// one is deleted by its first reader): its image is absent here.
- direct: bool = false,
/// The stream over the emulator, its answers to the program and its
/// size reports routed here, PNG decoded: what `create` and a dump's
@@ -343,11 +338,11 @@ pub fn reload(s: *State) !u64 {
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, s.direct };
+ 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], .direct = kept[3] };
+ 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);
@@ -357,8 +352,8 @@ pub fn reload(s: *State) !u64 {
/// What a join compares of a snapshot (`reload`): every record of it but
/// its kitty images' (tags 8 to 10, which ghostty's framing makes optional:
/// snapshot/record.zig, a u16 tag, a u32 length, a u32 CRC and the payload,
-/// after a 10-byte envelope): an image by file or shared memory is the
-/// session's alone (P32, `direct`), and no digest reads an image.
+/// after a 10-byte envelope): an animation's current frame is stepped by
+/// each side's own draws, and no digest reads an image.
fn joinHash(bytes: []const u8) u64 {
const envelope = 10;
const head = 10;
@@ -379,7 +374,7 @@ fn joinHash(bytes: []const u8) u64 {
/// 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 = if (out.direct) .direct else .allWithTempDir(tempDir());
+ if (out.vt.screens.get(which)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir());
};
const whole = out.wire();
switch (continuation) {
@@ -409,14 +404,10 @@ fn install(out: *State, continuation: ghostty_vt.snapshot.Continuation, saved: *
return whole;
}
-/// Its program's kitty images by direct data alone from now on: the
-/// emulator of a replica that is not the session (P32).
-pub fn direct(s: *State) void {
- if (comptime !images_enabled) return;
- s.direct = true;
- inline for (.{ .primary, .alternate }) |which| {
- if (s.vt.screens.get(which)) |screen| screen.kitty_images.image_limits = .direct;
- }
+/// How every terminal's kitty loader has a medium's bytes from now on
+/// (ghostty's `read_medium`, build/patches.zig).
+pub fn media(read: Term.Media) void {
+ if (comptime images_enabled) ghostty_vt.kitty.graphics.LoadingImage.read_medium = read;
}
/// The allocator it was opened with, its own from then on.