summaryrefslogtreecommitdiff
path: root/src/detached/wire.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 05:58:13 -0300
committerGabriel Schneider <[email protected]>2026-10-30 10:44:19 -0300
commitddb2554fff073eed0b73a43825b2c007c895bc4d (patch)
tree77db8d82afd23dc6e0395bc4ba21525bc6c8dfcd /src/detached/wire.zig
parent541ebfa62ed966d74e1b7e93eb0d62e536d50e7d (diff)
downloadpardes-ddb2554fff073eed0b73a43825b2c007c895bc4d.tar.gz
pardes-ddb2554fff073eed0b73a43825b2c007c895bc4d.zip
replica: a far client's content cache and zstd: the session lists its join's blobs by BLAKE3 hash first (wire 16 `blobs`), the client finds each in the core it leaves or on disk ($XDG_CACHE_HOME/pardes/blobs, hashed again on load, a bad file a miss, the least recently used evicted past 1 GiB) and asks for the rest (`want`), and everything after the welcome goes in zstd envelopes (level 6 over 8 MiB for the join, an undo snapshot being a near copy of its text, then level 3), so `ssh -C` leaves the default command; a far client (through ssh, or PARDES_REPLICA_FAR) keeps what it was sent and, at its end, what it made; a hangup ends a remote client the normal way; through the box a client holding the blobs is drawn in 2.4 s where main took 7.0 to 11.9, and a first join of a session with undo in 7.1 s where main took 11.9; libzstd 1.5.7 vendored, its allocations on c_heap, a second copy refused by shared-libs-check; tests for the cache's verify-on-load, its eviction and a far join's blobs sent once; test/daemon.py's far clients
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/detached/wire.zig')
-rw-r--r--src/detached/wire.zig56
1 files changed, 41 insertions, 15 deletions
diff --git a/src/detached/wire.zig b/src/detached/wire.zig
index 78f6e76e..2f2b95c8 100644
--- a/src/detached/wire.zig
+++ b/src/detached/wire.zig
@@ -9,11 +9,13 @@ const builtin = @import("builtin");
const pardes = @import("../pardes.zig");
const config = @import("pardes_config");
-/// 15: the session streams a replica its dump and units, and terminal
-/// deltas both ways (phase 5 part B); 14: the hello names a session and
+/// 16: a far client says so, the session sends it its blobs' hashes before
+/// the rest and only the blobs it asks for, all of it in zstd (phase 6's
+/// content cache and zstd notes); 15: the session streams a replica its
+/// dump and units, and terminal deltas both ways (phase 5 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 = 15;
+pub const version: u16 = 16;
/// What makes two cores step differently, hashed: a replica's hello carries
/// its own, and one of another build is refused. The protocol, the release
@@ -60,6 +62,11 @@ pub const ClientTag = enum(u8) {
/// What is the session's digest, in stream order (a replica checks
/// itself against it once quiet).
check = 0x05,
+ /// A client's first word after its hello: whether it is far (through
+ /// ssh), so that it holds blobs by their hash and reads zstd.
+ far = 0x06,
+ /// The join's blobs a far client lacks, a bit each in `blobs` order.
+ want = 0x07,
key = 0x10,
mouse = 0x11,
@@ -128,19 +135,28 @@ pub const ServerTag = enum(u8) {
screen = 0x06,
/// The session's digest (u64le), after every unit before it.
check = 0x07,
+ /// Part of the join's list of blobs, to a far client: each one's place
+ /// in the state, its length and its BLAKE3 hash (src/replica.zig).
+ blobs = 0x08,
+ /// A piece of a far client's stream from here on, zstd-compressed: the
+ /// messages in it are framed as these are. Opaque here.
+ zstd = 0x09,
};
pub const ServerMsg = union(ServerTag) {
/// Who the client is now (its id and view in the session), how long the
/// dump that follows is, and the session's offset from UTC (a replica
- /// guesses the session's time of day).
- welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0 },
+ /// 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: u8 = 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,
unit: []const u8,
screen: []const u8,
check: u64,
+ blobs: []const u8,
+ zstd: []const u8,
};
/// A message's header, for a payload sent on its own (a unit, a dump's
@@ -165,6 +181,7 @@ 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),
+ .blobs = try r.takeInt(u32, .little),
} },
.refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) },
.pong => .{ .pong = try r.takeInt(u64, .little) },
@@ -172,6 +189,8 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg {
.unit => return .{ .unit = payload },
.screen => return .{ .screen = payload },
.check => .{ .check = try r.takeInt(u64, .little) },
+ .blobs => return .{ .blobs = payload },
+ .zstd => return .{ .zstd = payload },
};
try end(&r);
return msg;
@@ -190,10 +209,11 @@ 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.writeInt(u32, h.blobs, .little);
},
.refused => |words| try putSlice16(&w, words),
.pong => |v| try w.writeInt(u64, v, .little),
- .dump, .unit, .screen => |bytes| try w.writeAll(bytes),
+ .dump, .unit, .screen, .blobs, .zstd => |bytes| try w.writeAll(bytes),
.check => |v| try w.writeInt(u64, v, .little),
}
try finishMessage(&w, at);
@@ -227,6 +247,8 @@ pub const ClientMsg = union(enum) {
ping: u64,
screen_ack: []const u8,
check,
+ far: bool,
+ want: []const u8,
event: pardes.Event,
};
@@ -436,8 +458,9 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 {
},
.bye => {},
.ping => |v| try w.writeInt(u64, v, .little),
- .screen_ack => |bytes| try w.writeAll(bytes),
+ .screen_ack, .want => |bytes| try w.writeAll(bytes),
.check => {},
+ .far => |far| try w.writeByte(@intFromBool(far)),
.event => |ev| switch (ev) {
.key => |k| {
try w.writeInt(u32, k.cp, .little);
@@ -506,6 +529,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg {
.ping => .{ .ping = try r.takeInt(u64, .little) },
.screen_ack => return .{ .screen_ack = payload },
.check => .check,
+ .far => .{ .far = try getBool(&r) },
+ .want => return .{ .want = payload },
.key => blk: {
const cp = try r.takeInt(u32, .little);
// `Key.cp` is a u21, and the specials live in the private-use
@@ -566,8 +591,8 @@ const msg_slack = header_len + 64;
pub fn clientBound(msg: ClientMsg) usize {
return msg_slack + switch (msg) {
.hello => |h| h.session.len,
- .bye, .ping, .check => 0,
- .screen_ack => |bytes| bytes.len,
+ .bye, .ping, .check, .far => 0,
+ .screen_ack, .want => |bytes| bytes.len,
.event => |ev| switch (ev) {
.mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0,
.key => |k| k.text.len,
@@ -622,7 +647,7 @@ test "detached wire: every Event variant round-trips" {
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 }, 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);
@@ -703,12 +728,13 @@ test "detached wire: a session is never told to do a frontend's remembering" {
// Who this connection is, its round trip, and that it is finished,
// and which terminal state its replica holds (a protocol's word,
// as a ping is).
- "hello", "bye", "ping", "screen_ack", "check",
+ "hello", "bye", "ping", "screen_ack", "check",
+ // ...and how far it is, and which of the join's blobs it lacks.
+ "far", "want",
// ...and everything a person can do to a window.
- "key", "mouse", "resize",
- "paste", "command", "pdf_scroll",
- "pinch", "touch_scroll", "pointer_leave",
- "focus",
+ "key", "mouse", "resize",
+ "paste", "command", "pdf_scroll", "pinch", "touch_scroll",
+ "pointer_leave", "focus",
};
// One: the tag set is exactly that, named rather than counted, so