summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 16:55:47 -0300
committerGabriel Schneider <[email protected]>2026-10-29 19:54:26 -0300
commit6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8 (patch)
treedb996dc7f022cdaff9afd8e15774ddd33ac1c607
parentcd560d4e025e46db6aa583f017dd9e503f45659c (diff)
downloadpardes-6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8.tar.gz
pardes-6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8.zip
the replica socket: the daemon listens on pardes-replica.sock, a stream `ssh -L` can forward, refuses another build's hello in words and hands the connection to its session, which welcomes it and answers pings
The daemon binds `pardes-replica.sock` beside its own (SOCK_STREAM, so ssh can forward it; the SEQPACKET socket that carries fds stays local) once it holds its lock. A replica's first message is a hello (detached/wire.zig, now version 14): the protocol version at offset 0, the client's semantics hash and the session's name. The daemon reads exactly that much and no further, so whatever the client sends next is the session's, keeps up to 16 hellos still on their way (ponytail: one that never comes holds its slot until it hangs up), and refuses in words another protocol, a session nobody has, or another build: every session now says its own hash in its registration (daemon protocol 2), so a mismatch never reaches it. An accepted connection goes to the session over its link (SCM_RIGHTS) and the daemon keeps no copy; the session welcomes it with its pid and name on a thread of its own and answers each ping with a pong, until bye or a hangup. ponytail: that stub is all a session does with a replica until phase 5 proper; it is what times a round trip through the handoff and `ssh -L`. The semantics hash (wire.semantics) is phase 1's undone item: the protocol, the release and commit, the platform, the build options, every dependency the manifest pins by its content hash (grammars, ghostty, the Unicode tables: build.zig's new `deps` option) and the walker codec's assumptions (pointer width, byte order). Not the OS: a Linux client of a FreeBSD session is the main case. ponytail: an unstamped build has no commit, so two dev trees of one release with the same options are not told apart. `pardes --version` prints it on a second line. The hello lost its grid (a replica's view takes a resize like any other input). test/daemon.py: a hello of this build welcomed by the session itself (its pid, one fd more in the session and none in the daemon, and back once it hangs up), 50 pings, another build's hello and an unknown session's refused, and `--ping <socket> <session> <pardes> [n]`, which times a hello and n pings on any replica socket (a forwarded one, say). Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--build.zig13
-rw-r--r--docs/typ/building.typ17
-rw-r--r--src/CHANGELOG.md6
-rw-r--r--src/daemon.zig166
-rw-r--r--src/detached/wire.zig155
-rw-r--r--src/main.zig6
-rw-r--r--test/daemon.py110
7 files changed, 426 insertions, 47 deletions
diff --git a/build.zig b/build.zig
index 5e337dfc..24476753 100644
--- a/build.zig
+++ b/build.zig
@@ -13,6 +13,18 @@ const display_version = if (std.mem.endsWith(u8, zon.version, ".0") and std.mem.
else
zon.version;
+/// Every dependency the manifest pins, by its content hash: a grammar,
+/// ghostty or the Unicode tables steps a core differently, so they are part
+/// of a replica's semantics hash (src/detached/wire.zig `semantics`).
+const deps_text = blk: {
+ var text: []const u8 = "";
+ for (@typeInfo(@TypeOf(zon.dependencies)).@"struct".field_names) |name| {
+ const dep = @field(zon.dependencies, name);
+ text = text ++ name ++ " " ++ (if (@hasField(@TypeOf(dep), "hash")) dep.hash else "path") ++ "\n";
+ }
+ break :blk text;
+};
+
pub const Platform = enum { tty, gui, web, macos, esp32p4 };
const PerfBuild = struct {
@@ -2120,6 +2132,7 @@ fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build.
// ...and the commit it was built from, when there is one and the build
// is stamped at all (see `stamp_commit`). Read at configure time.
o.addOption(?[]const u8, "commit", cfg.commit);
+ o.addOption([]const u8, "deps", deps_text);
return o;
}
diff --git a/docs/typ/building.typ b/docs/typ/building.typ
index 152be838..00088d7b 100644
--- a/docs/typ/building.typ
+++ b/docs/typ/building.typ
@@ -31,7 +31,8 @@ leaves that copy out. Zig 0.17 keeps the prefix from the build script, so
so always pass `-Dplatform` while developing. Test, benchmark and run steps build what they need
without installing. `pardes --version` prints `pardes <version>` (from
`build.zig.zon`), plus the commit for release builds and the `~/.local`
-install; `pardes --help` lists every flag.
+install, and on a second line the build's semantics hash, which a replica
+must share with its session; `pardes --help` lists every flag.
The ESP32-P4 firmware is linked in the sibling `../05-zig-p4` toolchain,
which needs ESP-IDF register headers: after building the editor object
@@ -223,9 +224,17 @@ process; `pardes-gui --attach` opens one again. `PARDES_DAEMON=off` (the
snapshot harness) runs a session as before the daemon: in the terminal's
own process, ended by its hangup.
-`src/detached/wire.zig` is the input half of the protocol a remote client
-will speak (version 13): fixed-width little-endian fields, a 5-byte header
-(tag, then a u32 length), the hello and its version, the input events.
+A replica (a client running its own copy of the core, phase 5 of
+`docs/next-daemon.md`) connects to `pardes-replica.sock`, a stream socket
+beside it that `ssh -L` can forward. `src/detached/wire.zig` is its
+protocol (version 14): fixed-width little-endian fields, a 5-byte header
+(tag, then a u32 length), the hello, a ping, the input events. The hello
+names a session and carries the client's semantics hash (the protocol,
+release, commit, platform, build options and every dependency's hash); the
+daemon refuses another build's, or a session nobody has, in words, and
+hands an accepted connection to its session (`SCM_RIGHTS`), which welcomes
+it and from then on is the one it talks to. Today the session answers
+pings and nothing more.
= 9P
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md
index 10cc451d..27337781 100644
--- a/src/CHANGELOG.md
+++ b/src/CHANGELOG.md
@@ -2,6 +2,12 @@
## Unreleased
+- The daemon also listens on `pardes-replica.sock`, a stream socket `ssh
+ -L` can forward, for the replicated clients to come (docs/next-daemon.md,
+ phase 5): a hello names a session and the client's build, another
+ build is refused in words, and an accepted connection is handed to its
+ session, which so far only welcomes it and answers pings.
+ `pardes --version` prints that build's semantics hash on a second line.
- One `pardes --daemon` per user keeps sessions, started by the first
session and gone once it keeps nothing. `pardes` in a terminal forks
its session off and stays as its stub: `Detach` (`SPC s D`), or a
diff --git a/src/daemon.zig b/src/daemon.zig
index ac9e6eb4..702d11b1 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -16,14 +16,22 @@
//!
//! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it
//! carries (SCM_RIGHTS).
+//!
+//! A replica (docs/next-daemon.md, phase 5) comes in on a stream socket of
+//! its own, `pardes-replica.sock`, which `ssh -L` can forward: its hello
+//! (detached/wire.zig) names a session, and once its build is the session's
+//! the connection is handed to that session, which it talks to from then on.
const std = @import("std");
const builtin = @import("builtin");
const libc = std.c;
const posix = std.posix;
const ninep_io = @import("9p_io.zig");
+const wire = @import("detached/wire.zig");
+const log = std.log.scoped(.replica);
pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd;
-const version = 1;
+/// 2: a session's hello carries its semantics hash (wire.semantics).
+const version = 2;
pub const max_fds = 64;
const msg_max = 4096;
/// Idle this long, it exits: long enough for the session that started it to
@@ -31,6 +39,9 @@ const msg_max = 4096;
const linger_ms = 1000;
const max_conns = 64;
const max_held = 256;
+/// Replicas whose hello is still on its way. ponytail: one that never sends
+/// it holds its slot until it hangs up; past 16 a new one is closed at once.
+const max_replicas = 16;
const sun_path_len = ninep_io.sun_path_len;
const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0;
@@ -258,7 +269,7 @@ fn spawn(listener: c_int, lock: c_int) bool {
/// (`watch`) takes the `ok`, and only then is the session kept (`kept`).
fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) bool {
var buf: [256]u8 = undefined;
- const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {s}", .{ version, k, pid, if (detached) "detached" else "attached", named }) catch return false;
+ const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {x} {s}", .{ version, k, pid, if (detached) "detached" else "attached", wire.semantics, named }) catch return false;
return send(fd, words, &.{});
}
@@ -376,6 +387,11 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
.stub = if (got.nfds >= 2) got.fds[1] else -1,
} });
for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f);
+ } else if (std.mem.eql(u8, verb, "replica")) {
+ if (got.nfds >= 1) {
+ if (std.Thread.spawn(.{}, serveReplica, .{got.fds[0]})) |t| t.detach() else |_| _ = libc.close(got.fds[0]);
+ }
+ for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f);
} else if (std.mem.eql(u8, verb, "adopted")) {
var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } };
const a = &note.adopted;
@@ -395,6 +411,49 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
}
}
+fn writeAll(fd: c_int, bytes: []const u8) bool {
+ var at: usize = 0;
+ while (at < bytes.len) {
+ const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal);
+ if (n < 0 and libc.errno(n) == .INTR) continue;
+ if (n <= 0) return false;
+ at += @intCast(n);
+ }
+ return true;
+}
+
+/// A replica the daemon handed over, its hello accepted: welcomed, its pings
+/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it
+/// a replica (the log, the dump, terminal state); this stub is what times a
+/// round trip through the daemon's handoff and `ssh -L`. A message larger
+/// than its buffer ends it.
+fn serveReplica(fd: c_int) void {
+ defer _ = libc.close(fd);
+ log.info("a replica connected to '{s}'", .{name()});
+ defer log.info("a replica of '{s}' left", .{name()});
+ var out: [wire.hello_max]u8 = undefined;
+ const pid: u32 = @intCast(libc.getpid());
+ if (!writeAll(fd, wire.encodeServer(&out, .{ .welcome = .{ .pid = pid, .session = name() } }) catch return)) return;
+ var buf: [msg_max]u8 = undefined;
+ var len: usize = 0;
+ while (true) {
+ while (wire.framed(buf[0..len]) catch return) |f| {
+ switch (wire.decodeClient(f.tag, f.payload) catch return) {
+ .ping => |v| if (!writeAll(fd, wire.encodeServer(&out, .{ .pong = v }) catch return)) return,
+ .bye, .hello => return,
+ .event => {},
+ }
+ std.mem.copyForwards(u8, &buf, buf[f.total..len]);
+ len -= f.total;
+ }
+ if (len == buf.len) return;
+ const n = libc.read(fd, buf[len..].ptr, buf.len - len);
+ if (n < 0 and libc.errno(n) == .INTR) continue;
+ if (n <= 0) return;
+ len += @intCast(n);
+ }
+}
+
/// A session ended by a signal (TERM, HUP, INT): its shells go with it, as
/// at a quit. Async-signal-safe: one send. One killed outright (KILL) or
/// crashed says nothing, and its shells live on for an Adopt.
@@ -610,6 +669,8 @@ const Conn = struct {
kind: ?Kind = null,
pid: libc.pid_t = 0,
detached: bool = false,
+ /// Its build's wire.semantics: a replica's must be the same.
+ semantics: u64 = 0,
name: [128]u8 = undefined,
name_len: usize = 0,
@@ -625,9 +686,18 @@ const Held = struct {
owner: ?u8 = null,
};
+/// A replica's connection until its hello is whole: read no further than
+/// the hello, so what follows it is the session's to read.
+const Replica = struct {
+ fd: c_int = -1,
+ buf: [wire.hello_max]u8 = undefined,
+ len: usize = 0,
+};
+
const Daemon = struct {
conns: [max_conns]Conn = @splat(.{}),
held: [max_held]Held = @splat(.{}),
+ replicas: [max_replicas]Replica = @splat(.{}),
fn idle(d: *const Daemon) bool {
for (d.conns) |c| if (c.fd >= 0) return false;
@@ -668,6 +738,7 @@ const Daemon = struct {
const k = std.meta.stringToEnum(Kind, words.next() orelse "");
const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0;
const state = words.next() orelse "";
+ const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0;
const rest = std.mem.trim(u8, words.rest(), " ");
if (v != version or k == null or pid <= 0) {
// Another build's: it runs without a daemon.
@@ -677,6 +748,7 @@ const Daemon = struct {
c.kind = k;
c.pid = pid;
c.detached = std.mem.eql(u8, state, "detached");
+ c.semantics = semantics;
c.name_len = @min(rest.len, c.name.len);
@memcpy(c.name[0..c.name_len], rest[0..c.name_len]);
return d.reply(i, "ok", &.{});
@@ -796,6 +868,58 @@ const Daemon = struct {
return error.Said;
}
+ /// More of a replica's hello; once it is whole, the connection refused
+ /// in words or handed to its session.
+ fn hear(d: *Daemon, r: *Replica) void {
+ const want = if (r.len < wire.header_len) wire.header_len else wire.header_len + @as(usize, std.mem.readInt(u32, r.buf[1..5], .little));
+ if (want > r.buf.len) return refuse(r, "the first message is not a hello");
+ const n = libc.read(r.fd, r.buf[r.len..want].ptr, want - r.len);
+ if (n < 0 and libc.errno(n) == .INTR) return;
+ if (n <= 0) {
+ _ = libc.close(r.fd);
+ r.* = .{};
+ return;
+ }
+ r.len += @intCast(n);
+ if (r.len < wire.header_len or r.len < wire.header_len + std.mem.readInt(u32, r.buf[1..5], .little)) return;
+ const f = (wire.framed(r.buf[0..r.len]) catch null) orelse return refuse(r, "the first message is not a hello");
+ if (f.tag != @backingInt(wire.ClientTag.hello)) return refuse(r, "the first message is not a hello");
+ var why: [512]u8 = undefined;
+ const v = wire.helloVersion(f.payload) catch 0;
+ if (v != wire.version) return refuse(r, std.fmt.bufPrint(&why, "this client speaks replica protocol {d}, and this daemon {d}: run the same pardes at both ends", .{ v, wire.version }) catch "another protocol");
+ const h = (wire.decodeClient(f.tag, f.payload) catch return refuse(r, "a malformed hello")).hello;
+ const target = for (&d.conns, 0..) |*c, i| {
+ if (c.fd >= 0 and c.kind != null and std.mem.eql(u8, c.named(), h.session)) break i;
+ } else return refuse(r, std.fmt.bufPrint(&why, "no session called '{s}'", .{h.session}) catch "no such session");
+ const t = &d.conns[target];
+ if (t.semantics != h.semantics) return refuse(r, std.fmt.bufPrint(&why, "session '{s}' runs another build of pardes (semantics {x:0>16}, this client's {x:0>16}): a replica needs the same build at both ends, as `pardes --version` shows", .{ h.session, t.semantics, h.semantics }) catch "another build");
+ if (!send(t.fd, "replica", &.{r.fd})) {
+ d.hangUp(@intCast(target));
+ return refuse(r, "that session does not answer");
+ }
+ log.info("a replica handed to '{s}'", .{h.session});
+ _ = libc.close(r.fd);
+ r.* = .{};
+ }
+
+ fn refuse(r: *Replica, why: []const u8) void {
+ var out: [600]u8 = undefined;
+ // A fresh connection's buffer is empty: this never blocks.
+ if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {}
+ _ = libc.close(r.fd);
+ r.* = .{};
+ }
+
+ fn acceptReplica(d: *Daemon, listener: c_int) void {
+ const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC);
+ if (fd < 0) return;
+ for (&d.replicas) |*r| if (r.fd < 0) {
+ r.* = .{ .fd = fd };
+ return;
+ };
+ _ = libc.close(fd);
+ }
+
fn accept(d: *Daemon, listener: c_int) void {
const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC);
if (fd < 0) return;
@@ -807,6 +931,21 @@ const Daemon = struct {
}
};
+/// The replicas' stream socket, bound and listening; -1 when it cannot be
+/// (a runtime directory too long for a socket's path: 104 bytes on FreeBSD).
+/// Ours to replace: the daemon's lock is held.
+fn listenReplicas() c_int {
+ var sock_buf: [sun_path_len:0]u8 = undefined;
+ const sock = place(&sock_buf, "pardes-replica.sock") orelse return -1;
+ _ = libc.unlink(sock);
+ const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
+ if (fd < 0) return -1;
+ const addr = address(sock);
+ if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) == 0 and libc.chmod(sock, 0o600) == 0 and libc.listen(fd, max_replicas) == 0) return fd;
+ _ = libc.close(fd);
+ return -1;
+}
+
/// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the
/// lock (fd 4). Returns only to exit, idle.
pub fn serve() noreturn {
@@ -815,22 +954,33 @@ pub fn serve() noreturn {
ninep_io.setCloexec(4);
const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 };
for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null);
+ const replicas = listenReplicas();
var d: Daemon = .{};
- const slots = 1 + max_conns + max_held;
+ const slots = 2 + max_conns + max_replicas + max_held;
var fds: [slots]libc.pollfd = undefined;
var conn_of: [slots]u8 = undefined;
var held_of: [slots]u16 = undefined;
+ var replica_of: [slots]u8 = undefined;
var buf: [msg_max]u8 = undefined;
while (true) {
var n: usize = 0;
fds[n] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 };
n += 1;
+ // -1 when there is none: poll skips a negative fd.
+ fds[n] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 };
+ n += 1;
const conns_at = n;
for (&d.conns, 0..) |*c, i| if (c.fd >= 0) {
fds[n] = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 };
conn_of[n] = @intCast(i);
n += 1;
};
+ const replicas_at = n;
+ for (&d.replicas, 0..) |*r, i| if (r.fd >= 0) {
+ fds[n] = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 };
+ replica_of[n] = @intCast(i);
+ n += 1;
+ };
const held_at = n;
// No events asked of a master: its hangup (every slave closed, the
// shell and all it ran gone) is reported anyway, and its output is
@@ -858,7 +1008,12 @@ pub fn serve() noreturn {
break;
}
if (fds[0].revents & libc.POLL.IN != 0) d.accept(listener);
- for (fds[conns_at..held_at], conn_of[conns_at..held_at]) |pfd, i| {
+ if (fds[1].revents & libc.POLL.IN != 0) d.acceptReplica(replicas);
+ for (fds[replicas_at..held_at], replica_of[replicas_at..held_at]) |pfd, i| {
+ if (pfd.revents == 0 or d.replicas[i].fd != pfd.fd) continue;
+ d.hear(&d.replicas[i]);
+ }
+ for (fds[conns_at..replicas_at], conn_of[conns_at..replicas_at]) |pfd, i| {
if (pfd.revents == 0 or d.conns[i].fd != pfd.fd) continue;
const got = recv(pfd.fd, &buf) orelse {
d.hangUp(i);
@@ -874,6 +1029,9 @@ pub fn serve() noreturn {
var sock_buf: [sun_path_len:0]u8 = undefined;
_ = libc.close(listener);
if (place(&sock_buf, "pardes-daemon.sock")) |sock| _ = libc.unlink(sock);
+ if (replicas >= 0) {
+ if (place(&sock_buf, "pardes-replica.sock")) |sock| _ = libc.unlink(sock);
+ }
libc._exit(0);
}
diff --git a/src/detached/wire.zig b/src/detached/wire.zig
index 20d75274..097cbee7 100644
--- a/src/detached/wire.zig
+++ b/src/detached/wire.zig
@@ -1,13 +1,41 @@
-//! The framed stream a remote client will speak (docs/next-daemon.md,
-//! phase 5): fixed-width little-endian fields and explicit tags, the hello
-//! and its version, and the input Events. Native struct layout never reaches
-//! the wire. The cell frames that went the other way, and the frontends that
-//! drew them, went with phase 3.
+//! The framed stream a replica speaks (docs/next-daemon.md, phase 5), on
+//! the daemon's `pardes-replica.sock` and then its session's: fixed-width
+//! little-endian fields and explicit tags, the hello and its version, a
+//! ping, and the input Events. Native struct layout never reaches the wire.
+//! The cell frames that went the other way, and the frontends that drew
+//! them, went with phase 3.
const std = @import("std");
+const builtin = @import("builtin");
const pardes = @import("../pardes.zig");
+const config = @import("pardes_config");
-/// 13: the frames, and every message a session sent, are gone (phase 3).
-pub const version: u16 = 13;
+/// 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;
+
+/// 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
+/// and commit, the platform and build options, every dependency's content
+/// hash (grammars, ghostty, Unicode) and the walker codec's assumptions
+/// (pointer width, byte order). Not the OS: a Linux client of a FreeBSD
+/// session is the main case, and what differs there is the host's, taped.
+/// ponytail: an unstamped build (Debug with -Dplatform, every test build)
+/// has no commit, so two dev trees of one release are told apart only by
+/// their options and dependencies; -Dstamp-commit stamps one.
+pub const semantics: u64 = blk: {
+ @setEvalBranchQuota(1_000_000);
+ break :blk std.hash.Wyhash.hash(0, std.fmt.comptimePrint(
+ "wire {d}\npardes {s} {s}\n{t} grammars {} {} {} {} mupdf {} themes {} workspace_tag {} sources {} board {d}x{d}\nusize {d} {t}\n{s}",
+ .{
+ version, config.version, config.commit orelse "unstamped",
+ config.platform, config.syntax_highlighting, config.syntax_zig_grammar,
+ config.syntax_minimal_grammars, config.syntax_full_grammars, config.mupdf,
+ config.theme_animation, config.workspace_tag, config.embed_sources,
+ config.esp32p4_cols, config.esp32p4_rows, @sizeOf(usize),
+ builtin.cpu.arch.endian(), config.deps,
+ },
+ ));
+};
/// EndOfStream: the message ends early; WriteFailed: no room for it.
/// ReadFailed is in only because std.Io.Reader says so: a fixed one never
@@ -23,6 +51,7 @@ pub const header_len = 5; // tag:u8, payload length:u32le
pub const ClientTag = enum(u8) {
hello = 0x01,
bye = 0x02,
+ ping = 0x03,
key = 0x10,
mouse = 0x11,
@@ -59,12 +88,50 @@ const KindTag = enum(u8) { press = 0x00, release = 0x01, motion = 0x02, drag = 0
/// when the rest of the layout is the part that changed.
pub const Hello = struct {
version: u16 = version,
- /// This client's grid. Never zero: a client not sized yet must not
- /// collapse a shared session (getCols).
- cols: u16,
- rows: u16,
+ /// The client's `semantics`: the daemon refuses it unless the session's
+ /// is the same.
+ semantics: u64 = semantics,
+ /// Which session (its 9P name, as `--attach=` names it).
+ session: []const u8,
};
+/// A session's name on the wire, as the daemon keeps it.
+pub const max_name = 128;
+/// The longest hello there is, header and all.
+pub const hello_max = header_len + 2 + 8 + 2 + max_name;
+
+/// Daemon or session -> client.
+pub const ServerTag = enum(u8) {
+ /// The hello is accepted and the connection is the session's now: its pid
+ /// and name.
+ welcome = 0x01,
+ /// Refused, said in words; the connection ends.
+ refused = 0x02,
+ /// A ping's value back.
+ pong = 0x03,
+};
+
+pub const ServerMsg = union(ServerTag) {
+ welcome: struct { pid: u32, session: []const u8 },
+ refused: []const u8,
+ pong: u64,
+};
+
+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)));
+ switch (msg) {
+ .welcome => |h| {
+ try w.writeInt(u32, h.pid, .little);
+ try putSlice16(&w, h.session);
+ },
+ .refused => |words| try putSlice16(&w, words),
+ .pong => |v| try w.writeInt(u64, v, .little),
+ }
+ try finishMessage(&w, at);
+ return w.buffered();
+}
+
/// `Hello.version` alone, read out of the still-undecoded payload.
///
/// Separate from `decodeClient` because of the guarantee the fixed offset above
@@ -88,6 +155,8 @@ pub fn helloVersion(payload: []const u8) Error!u16 {
pub const ClientMsg = union(enum) {
hello: Hello,
bye,
+ /// A round trip's probe, answered `pong` with the same value.
+ ping: u64,
event: pardes.Event,
};
@@ -291,10 +360,12 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 {
switch (msg) {
.hello => |h| {
try w.writeInt(u16, h.version, .little);
- try w.writeInt(u16, h.cols, .little);
- try w.writeInt(u16, h.rows, .little);
+ try w.writeInt(u64, h.semantics, .little);
+ if (h.session.len > max_name) return error.Overlong;
+ try putSlice16(&w, h.session);
},
.bye => {},
+ .ping => |v| try w.writeInt(u64, v, .little),
.event => |ev| switch (ev) {
.key => |k| {
try w.writeInt(u32, k.cp, .little);
@@ -352,10 +423,15 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg {
const msg: ClientMsg = switch (std.enums.fromInt(ClientTag, tag) orelse return error.BadTag) {
.hello => .{ .hello = .{
.version = try r.takeInt(u16, .little),
- .cols = try getCols(&r),
- .rows = try getRows(&r),
+ .semantics = try r.takeInt(u64, .little),
+ .session = blk: {
+ const n = try r.takeInt(u16, .little);
+ if (n > max_name) return error.BadValue;
+ break :blk try r.take(n);
+ },
} },
.bye => .bye,
+ .ping => .{ .ping = try r.takeInt(u64, .little) },
.key => blk: {
const cp = try r.takeInt(u32, .little);
// `Key.cp` is a u21, and the specials live in the private-use
@@ -415,7 +491,8 @@ const msg_slack = header_len + 64;
/// once instead of guessing and retrying.
pub fn clientBound(msg: ClientMsg) usize {
return msg_slack + switch (msg) {
- .hello, .bye => 0,
+ .hello => |h| h.session.len,
+ .bye, .ping => 0,
.event => |ev| switch (ev) {
.mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0,
.key => |k| k.text.len,
@@ -458,12 +535,21 @@ test "detached wire: every Event variant round-trips" {
try testing.expectEqual(@as(u8, 0x1d), @backingInt(ClientTag.pointer_leave));
{
- const got = try roundClient(&buf, .{ .hello = .{ .cols = 80, .rows = 24 } });
+ const got = try roundClient(&buf, .{ .hello = .{ .session = "work" } });
try testing.expectEqual(version, got.hello.version);
- try testing.expectEqual(@as(u16, 80), got.hello.cols);
- try testing.expectEqual(@as(u16, 24), got.hello.rows);
+ try testing.expectEqual(semantics, got.hello.semantics);
+ try testing.expectEqualStrings("work", got.hello.session);
}
try testing.expectEqual(ClientMsg.bye, try roundClient(&buf, .bye));
+ try testing.expectEqual(@as(u64, 0xfeed_beef_0000_0001), (try roundClient(&buf, .{ .ping = 0xfeed_beef_0000_0001 })).ping);
+ {
+ // The other way: framed the same, its tag the ServerTag's number.
+ 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.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag);
+ }
{
const key: pardes.Key = .{ .cp = pardes.Key.page_down, .text = "ü", .ctrl = true, .alt = false, .shift = true };
@@ -530,20 +616,19 @@ test "detached wire: a session is never told to do a frontend's remembering" {
// Adding a name here is meant to be an ARGUMENT, not a formality, and the
// bar is one sentence: A HUMAN DID IT. A keystroke, a click, a pinch, a
// window resized, a paste, a command line executed, a pointer leaving the
- // window, the window's focus — plus the two session words that say who is speaking. A pty's
+ // window, the window's focus — plus the session words that say who is speaking, time the link and end it. A pty's
// output, a worker's answer, a watched file's new bytes and an animation
// tick all fail that bar the same way: nobody did them, a machine reported
// them, and the machine that reports them is the one already holding the
// core.
const allowed = [_][]const u8{
- // Who this connection is, and that it is finished.
- "hello", "bye",
+ // Who this connection is, its round trip, and that it is finished.
+ "hello", "bye", "ping",
// ...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
@@ -663,18 +748,16 @@ test "detached wire: values a field cannot mean are refused" {
@backingInt(ClientTag.pdf_scroll),
&.{pardes.MAX_PANES},
));
- // A zero-column grid would collapse a shared session; an over-wide one is
- // past what this protocol carries.
- try testing.expectError(error.BadValue, decodeClient(
- @backingInt(ClientTag.hello),
- &.{ 1, 0, 0, 0, 24, 0 },
- ));
+ // A session name longer than the daemon keeps one: refused, never cut
+ // to another session's name.
{
- var hello: [6]u8 = undefined;
+ var hello: [12 + max_name + 1]u8 = @splat('x');
std.mem.writeInt(u16, hello[0..2], version, .little);
- std.mem.writeInt(u16, hello[2..4], max_cols + 1, .little);
- std.mem.writeInt(u16, hello[4..6], 24, .little);
+ std.mem.writeInt(u16, hello[10..12], max_name + 1, .little);
try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.hello), &hello));
+ std.mem.writeInt(u16, hello[10..12], max_name, .little);
+ try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), hello[0 .. hello.len - 1])).hello.session.len);
+ try testing.expect(header_len + hello.len - 1 == hello_max);
}
// A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps.
{
diff --git a/src/main.zig b/src/main.zig
index 378dbea8..f310bf3c 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -288,10 +288,12 @@ fn badArgs(io: std.Io, comptime fmt: []const u8, args: anytype) noreturn {
std.process.exit(1);
}
-const version_text = if (pardes.commit) |c|
+/// The second line is what a replica's build must share with its session's
+/// (detached/wire.zig `semantics`).
+const version_text = (if (pardes.commit) |c|
"pardes " ++ pardes.version ++ " (" ++ c ++ ")\n"
else
- "pardes " ++ pardes.version ++ "\n";
+ "pardes " ++ pardes.version ++ "\n") ++ std.fmt.comptimePrint("semantics {x:0>16}\n", .{@import("detached/wire.zig").semantics});
const nested_text =
\\pardes: this shell is already inside pardes, and a pardes inside a pardes
diff --git a/test/daemon.py b/test/daemon.py
index 41cfbaf7..2be3f355 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -6,16 +6,24 @@ private HOME and runtime directory:
from another terminal, N times;
- a session killed outright: its shells live on, held by the daemon, and
another session adopts them as terminal panes;
+- a replica's hello on the daemon's stream socket (phase 5): one of this
+ build handed to its session, which answers it and its pings; another
+ build's refused, and a session nobody has;
- nothing left behind: no session, shell, daemon or socket.
python3 -B test/daemon.py <pardes> [cycles]
python3 -B test/daemon.py --gui <pardes-gui> [cycles] (a hidden window;
needs a Wayland display, and a pardes beside it for the refusal)
+ python3 -B test/daemon.py --ping <socket> <session> <pardes> [n]
+ a hello of <pardes>'s build and n pings on a replica socket (one
+ `ssh -L` forwards, say), timed
"""
import fcntl
import os
import shutil
import signal
+import socket
+import statistics
import struct
import subprocess
import sys
@@ -153,6 +161,103 @@ def fds(pid):
return -1
+def semantics(binary):
+ """What a replica's build must share with its session's (--version)."""
+ out = subprocess.run([binary, '--version'], capture_output=True, check=True).stdout.decode()
+ return int(next(line.split()[1] for line in out.splitlines() if line.startswith('semantics ')), 16)
+
+
+class Replica:
+ """A replica's connection (src/detached/wire.zig): length-prefixed
+ messages, a tag byte and a little-endian u32 length."""
+ WIRE = 14
+
+ def __init__(self, path, timeout=10):
+ self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ self.s.settimeout(timeout)
+ self.s.connect(os.fspath(path))
+
+ def send(self, tag, payload):
+ self.s.sendall(struct.pack('<BI', tag, len(payload)) + payload)
+
+ def take(self, n):
+ got = b''
+ while len(got) < n:
+ more = self.s.recv(n - len(got))
+ if not more:
+ raise EOFError('the connection ended')
+ got += more
+ return got
+
+ def read(self):
+ tag, n = struct.unpack('<BI', self.take(5))
+ return tag, self.take(n)
+
+ def hello(self, session, build):
+ """('welcome', pid, name) or ('refused', why)."""
+ name = session.encode()
+ self.send(0x01, struct.pack('<HQH', self.WIRE, build, len(name)) + name)
+ tag, body = self.read()
+ if tag == 0x01:
+ pid, n = struct.unpack_from('<IH', body)
+ return 'welcome', pid, body[6:6 + n].decode()
+ assert tag == 0x02, (tag, body)
+ n, = struct.unpack_from('<H', body)
+ return 'refused', body[2:2 + n].decode()
+
+ def ping(self, value):
+ """One round trip, in ms."""
+ t = time.monotonic()
+ self.send(0x03, struct.pack('<Q', value))
+ tag, body = self.read()
+ assert (tag, body) == (0x03, struct.pack('<Q', value)), (tag, body)
+ return (time.monotonic() - t) * 1000
+
+ def close(self):
+ self.s.close()
+
+
+def replicas(root, binary, session, daemon):
+ sock = root / 'pardes-replica.sock'
+ wait_for('the replicas\' socket', sock.exists)
+ assert len(os.fspath(sock)) < 104, 'FreeBSD\'s sun_path'
+ ours = semantics(binary)
+ before = fds(session), fds(daemon)
+ t = time.monotonic()
+ r = Replica(sock)
+ said = r.hello('work', ours)
+ hello_ms = (time.monotonic() - t) * 1000
+ # The session answers, so the connection is its own now: the daemon
+ # keeps no copy of it.
+ assert said == ('welcome', session, 'work'), said
+ rtt = [r.ping(i) for i in range(50)]
+ assert fds(session) == before[0] + 1 and fds(daemon) == before[1], (before, fds(session), fds(daemon))
+ r.close()
+ wait_for('the replica\'s end, in the session', lambda: fds(session) == before[0])
+ said = Replica(sock).hello('work', ours ^ 1)
+ assert said[0] == 'refused' and 'another build' in said[1] and f'{ours ^ 1:016x}' in said[1], said
+ said = Replica(sock).hello('nobody', ours)
+ assert said == ('refused', "no session called 'nobody'"), said
+ wait_for('the daemon\'s refused connections closed', lambda: fds(daemon) == before[1])
+ print(f'daemon: a replica handed to its session (hello {hello_ms:.2f} ms, ping median '
+ f'{statistics.median(rtt):.3f} ms); another build, and a session nobody has, refused')
+
+
+def ping(path, session, binary, n):
+ build = semantics(binary)
+ t = time.monotonic()
+ r = Replica(path, timeout=30)
+ said = r.hello(session, build)
+ hello_ms = (time.monotonic() - t) * 1000
+ if said[0] != 'welcome':
+ sys.exit(f'refused: {said[1]}')
+ rtt = sorted(r.ping(i) for i in range(n))
+ r.send(0x02, b'')
+ r.close()
+ print(f'connect and hello {hello_ms:.2f} ms; {n} pings min/median/p90/max '
+ f'{rtt[0]:.2f}/{statistics.median(rtt):.2f}/{rtt[int(n * .9) - 1]:.2f}/{rtt[-1]:.2f} ms')
+
+
def terminals(client):
return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term']
@@ -184,6 +289,7 @@ def main(binary, cycles):
assert len(daemons) == 1, [cmdline(p) for p in processes(root)]
daemon = daemons[0]
started.append(daemon)
+ replicas(root, binary, session, daemon)
shell = terminals(client)[0]
shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1])
started.append(shell_pid)
@@ -401,7 +507,9 @@ def gui(binary, cycles):
if __name__ == '__main__':
- if '--gui' in sys.argv:
+ if sys.argv[1:2] == ['--ping']:
+ ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50)
+ elif '--gui' in sys.argv:
args = [a for a in sys.argv[1:] if a != '--gui']
gui(args[0], int(args[1]) if len(args) > 1 else 50)
else: