From fdf74e199de7d0fc648f912875323f3dc920ffc2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=:` and `--attach=:` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 94 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 8 deletions(-) (limited to 'src/detached') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 097cbee7..347d45d1 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,9 +9,11 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); -/// 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 = 14; +/// 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; /// 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 @@ -52,6 +54,12 @@ pub const ClientTag = enum(u8) { hello = 0x01, bye = 0x02, ping = 0x03, + /// A replica's word about a terminal's state (src/screen_state.zig): the + /// delta it holds, or rows it asks for. Opaque here. + screen_ack = 0x04, + /// What is the session's digest, in stream order (a replica checks + /// itself against it once quiet). + check = 0x05, key = 0x10, mouse = 0x11, @@ -109,14 +117,65 @@ pub const ServerTag = enum(u8) { refused = 0x02, /// A ping's value back. pong = 0x03, + /// A piece of the join's dump (src/dump.zig): the welcome says how long + /// it is whole. + dump = 0x04, + /// A unit of the authority's stream (src/replica.zig): its origin, seq, + /// clocks and tape. + unit = 0x05, + /// A terminal's delta, or rows asked for (src/screen_state.zig), applied + /// before the next unit. Opaque here. + screen = 0x06, + /// The session's digest (u64le), after every unit before it. + check = 0x07, }; pub const ServerMsg = union(ServerTag) { - welcome: struct { pid: u32, session: []const u8 }, + /// 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 }, refused: []const u8, pong: u64, + dump: []const u8, + unit: []const u8, + screen: []const u8, + check: u64, }; +/// A message's header, for a payload sent on its own (a unit, a dump's +/// piece): no copy of it into a message buffer. +pub fn header(tag: u8, len: usize) [header_len]u8 { + var h: [header_len]u8 = undefined; + h[0] = tag; + std.mem.writeInt(u32, h[1..5], @intCast(len), .little); + return h; +} + +/// The other way: what a client is told, its slices borrowing `payload`. +pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { + var r: std.Io.Reader = .fixed(payload); + const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) { + .welcome => .{ .welcome = .{ + .pid = try r.takeInt(u32, .little), + .session = try r.take(try r.takeInt(u16, .little)), + .client = try r.takeInt(u16, .little), + .view = try r.takeByte(), + .dump_len = try r.takeInt(u64, .little), + .gmtoff = try r.takeInt(i32, .little), + .digest = try r.takeInt(u64, .little), + } }, + .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, + .pong => .{ .pong = try r.takeInt(u64, .little) }, + .dump => return .{ .dump = payload }, + .unit => return .{ .unit = payload }, + .screen => return .{ .screen = payload }, + .check => .{ .check = try r.takeInt(u64, .little) }, + }; + try end(&r); + return msg; +} + pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { var w: std.Io.Writer = .fixed(out); const at = try beginMessage(&w, @backingInt(std.meta.activeTag(msg))); @@ -124,9 +183,16 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { .welcome => |h| { try w.writeInt(u32, h.pid, .little); try putSlice16(&w, h.session); + try w.writeInt(u16, h.client, .little); + try w.writeByte(h.view); + try w.writeInt(u64, h.dump_len, .little); + try w.writeInt(i32, h.gmtoff, .little); + try w.writeInt(u64, h.digest, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), + .dump, .unit, .screen => |bytes| try w.writeAll(bytes), + .check => |v| try w.writeInt(u64, v, .little), } try finishMessage(&w, at); return w.buffered(); @@ -157,6 +223,8 @@ pub const ClientMsg = union(enum) { bye, /// A round trip's probe, answered `pong` with the same value. ping: u64, + screen_ack: []const u8, + check, event: pardes.Event, }; @@ -366,6 +434,8 @@ 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), + .check => {}, .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -432,6 +502,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { } }, .bye => .bye, .ping => .{ .ping = try r.takeInt(u64, .little) }, + .screen_ack => return .{ .screen_ack = payload }, + .check => .check, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -492,7 +564,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 => 0, + .bye, .ping, .check => 0, + .screen_ack => |bytes| bytes.len, .event => |ev| switch (ev) { .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, .key => |k| k.text.len, @@ -547,7 +620,10 @@ 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' }, 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 }, 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); try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag); } @@ -622,8 +698,10 @@ test "detached wire: a session is never told to do a frontend's remembering" { // them, and the machine that reports them is the one already holding the // core. const allowed = [_][]const u8{ - // Who this connection is, its round trip, and that it is finished. - "hello", "bye", "ping", + // 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", // ...and everything a person can do to a window. "key", "mouse", "resize", "paste", "command", "pdf_scroll", -- cgit v1.3