summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 15:49:10 -0300
committerGabriel Schneider <[email protected]>2026-11-20 19:42:24 -0300
commit774efcc950662cd20c1b4a41d002ce421067564f (patch)
tree3dd309f7b9c006e4c3393e003179dff84d2f3f85
parent8a8ce0a6740e9a98ea37a70d401c3ba3fffe0ee1 (diff)
downloadpardes-774efcc950662cd20c1b4a41d002ce421067564f.tar.gz
pardes-774efcc950662cd20c1b4a41d002ce421067564f.zip
pair: the boot as input zero (the last step's design 3, way a2): under PARDES_PAIR the client no longer waits for its session's init, dump and socket before its first frame: it makes its own core from the same options and arguments (replica.ownBoot, Replica.initBoot: Pardes.init on no tape, as the session's start, its file read on its own machine, then the view the session's join opens for it, Peers.viewFor, shared by both), draws it, and says its digest before its `far` (a new client word, `boot`, 0x0a: 0x09 is the server's zstd envelope, which a stream's reader unpacks whichever way it runs; wire protocol 26, after the other line's 25); the session's writer sends no dump when the joiner's boot digest is its own at the join, and the client, reading the welcome once its first frame is drawn (replica.confirmBoot, client.zig `confirm`, the start's inputs held until then: Link.held, Link.release), goes on from its own core, or, unequal (the file edited between the two reads, anything else a start reads otherwise), loads the dump in its place and starts again; what made boots unequal by construction, found by the digest's parts compared in a scratch build: a join's view settled the session's first effects (Pardes.settle in openView: its boot shell spawned and acknowledged inside the join), which a client cannot perform; the effects queued before a join are now performed after it, on the tape the client is sent (viewFor, queueFirst), so the boot shell's spawn and first bytes reach the client as units, as P32 asked, and the dump path gets them so too; and a boot stepped on the replica's own tape read the machine as a replica's step would (the shell, the directory): Pardes.init runs on no tape, the view on the replica's tape (no terminal reloaded there, as on the session's screen-state tape); confirmed over 100 starts of a file and 100 of a shell (see the report); a boot's terminals are made on the C heap (panes.emulatorGpa reads dump.terminal_gpa); a window's pair's client boots unwindowed, as the terminal binary its session execs (main.zig `pair`, Options.windowed); tests: core/replica.zig "a pair's client makes its own core ...: no dump is sent" and "... the file edited between ...: the dump is sent, loaded in its place, and the client shows the session's file", and firstOnPair's screen-state client now its own boot, confirmed with no dump, the boot shell's spawn and first bytes after, digests equal, no reload; the start (ReleaseFast, medium.zig, one build alternated, NOT quiet: loads 8-16): the pair's first frame best 9.5 ms against the own path's 9.9 (median 10.2 against 11.4), the client 45.0-53.8M instructions:u (the bar 56.7M), its session 17.2-17.6M beside it, the own path 53.5M bit-equal to the base's; confirmed 100 of 100 starts of a file and 100 of 100 of a shell, a window pair 17 of 17; tui core-test 863/1018 (155 skipped), shell-test 1060/1063, term 17/17, pane 183/184, kitty 45/45, behaviour-test and --replay 275/275, process-test 0 of 144 failed, as a pair (PARDES_PAIR=1) 16 of 144 failed (the base 17), both soaks diverged 0, unequal 0, checked/wrong 0, unlisted 0, corpus 22 of 22, no leak, daemon.py plain, --far, --pair, --relink, --launch, --games, --window-clients, --gui, --upgrade both ways (new->old failed once on a client's key under load, then passed twice) pass, an idle pair 0 and 0 wakeups in 2 s, gui unit-test 1656/1660 with only BaselineHarnessMismatch, the frame and perf gate against tree2 PASS (77/77 frames, corpus2x -0.008%, 121 per-operation counts within 0.1%), macOS lib (Linux host), aarch64-macos and FreeBSD build (loads 5-23)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--abi/wire.zig19
-rw-r--r--core/dump.zig3
-rw-r--r--core/panes.zig5
-rw-r--r--core/replica.zig288
-rw-r--r--core/root.zig2
-rw-r--r--runtime/client.zig26
-rw-r--r--runtime/main.zig4
-rw-r--r--test/daemon.py2
8 files changed, 308 insertions, 41 deletions
diff --git a/abi/wire.zig b/abi/wire.zig
index 6c86685d..fe587847 100644
--- a/abi/wire.zig
+++ b/abi/wire.zig
@@ -10,6 +10,9 @@ const pardes = @import("core");
const config = @import("pardes_options");
const build_info = @import("build_info");
+/// 26: a pair's first client makes its own core from the session's
+/// options and says its digest (`boot`) before its `far`: the session sends
+/// no dump when its own at the join has the same (25 is another line's);
/// 24: a bye's byte says the client's window was closed (2), which ends a
/// pair's session at its last client;
/// 23: a pane of a kind's frame names its rasters by key, their pixels
@@ -38,7 +41,7 @@ const build_info = @import("build_info");
/// 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 = 24;
+pub const version: u16 = 26;
/// The terminal's build and the window's step alike (`semantics`).
const family = switch (config.platform) {
@@ -112,6 +115,12 @@ pub const ClientTag = enum(u8) {
/// guessed them for the input that follows: its time, which the step
/// reads (core/replica.zig).
stamp = 0x08,
+ /// A pair's first client's word before its `far`: the digest of the
+ /// core it made itself, from the session's options (Options.pair):
+ /// the session's at the join the same, no dump is sent. Not 0x09: a
+ /// stream's reader (replica.zig `Framer`) unpacks that tag as zstd
+ /// whichever way the stream runs.
+ boot = 0x0a,
key = 0x10,
mouse = 0x11,
@@ -308,6 +317,7 @@ pub const ClientMsg = union(enum) {
far: bool,
want: []const u8,
stamp: [2]i64,
+ boot: u64,
event: pardes.Event,
};
@@ -519,7 +529,7 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 {
},
// 0 it leaves, 1 it joins again, 2 its window was closed.
.bye => |b| try w.writeByte(if (b.close) 2 else @intFromBool(b.again)),
- .ping => |v| try w.writeInt(u64, v, .little),
+ .ping, .boot => |v| try w.writeInt(u64, v, .little),
.screen_ack, .want => |bytes| try w.writeAll(bytes),
.check => {},
.far => |far| try w.writeByte(@intFromBool(far)),
@@ -600,6 +610,7 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg {
.far => .{ .far = try getBool(&r) },
.want => return .{ .want = payload },
.stamp => .{ .stamp = .{ try r.takeInt(i64, .little), try r.takeInt(i64, .little) } },
+ .boot => .{ .boot = 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
@@ -656,7 +667,7 @@ 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, .far, .stamp => 0,
+ .bye, .ping, .check, .far, .stamp, .boot => 0,
.screen_ack, .want => |bytes| bytes.len,
.event => |ev| switch (ev) {
// Its hits' places (14 bytes each) past the slack.
@@ -783,6 +794,8 @@ test "detached wire: a session is never told to do a frontend's remembering" {
"hello", "bye", "ping", "screen_ack", "check",
// ...and how far it is, and which of the join's blobs it lacks.
"far", "want",
+ // ...and the digest of the core it made itself (a pair's).
+ "boot",
// ...and when its next input was, by the session's clocks.
"stamp",
// ...and everything a person can do to a window.
diff --git a/core/dump.zig b/core/dump.zig
index 7ce54bce..d45e0d8a 100644
--- a/core/dump.zig
+++ b/core/dump.zig
@@ -66,7 +66,8 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 {
}
/// What a load makes a terminal's emulator with, when not the loader's
-/// own: a screen-state replica's are the C heap's (core/replica.zig).
+/// own: a screen-state replica's are the C heap's (core/replica.zig), and
+/// so are those a pair's client's own boot makes (panes.emulatorGpa).
pub var terminal_gpa: ?std.mem.Allocator = null;
// ---- a whole session to and from a dump (acme keeps this in rows.c) ----
diff --git a/core/panes.zig b/core/panes.zig
index 1c103bd1..1f98208b 100644
--- a/core/panes.zig
+++ b/core/panes.zig
@@ -1187,9 +1187,10 @@ pub const emulator_gpa = std.heap.c_allocator;
/// What a terminal's emulator is made with, by `createTerminal` or a pane's
/// load (dump.loadPane, a closed terminal brought back): `gpa`, but on a
-/// replica `emulator_gpa`.
+/// replica `emulator_gpa`, and in a replica's core being made (its dump's
+/// load, a pair's client's own boot) `dump.terminal_gpa`.
pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator {
- return if (tape.replaying()) emulator_gpa else gpa;
+ return if (tape.replaying()) emulator_gpa else pardes.dump.terminal_gpa orelse gpa;
}
// ---- the session's pane slots: making, finding, closing and freeing panes, and their prompts ----
diff --git a/core/replica.zig b/core/replica.zig
index 49787c76..9e2437a9 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -750,8 +750,23 @@ pub const Replica = struct {
/// an image a frame late at worst... or reads freed pixels, which no test
/// session has.
pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica {
+ return made(gpa, id, view, opts, bytes);
+ }
+
+ /// A pair's first client's replica (Options.pair): its core made here
+ /// from the options the session is made from, its file read here (a
+ /// near client's read of its own machine), the view the session's join
+ /// opens for it opened (`Peers.viewFor`): the session's core at its
+ /// join, which the welcome confirms by its digest (`confirmBoot`). Its
+ /// id the session's first client's.
+ pub fn initBoot(gpa: Allocator, opts: pardes.Options) !*Replica {
+ return made(gpa, 1, null, opts, null);
+ }
+
+ /// `initDump`'s, or with no dump `initBoot`'s.
+ fn made(gpa: Allocator, id: u16, view: ?u8, opts: pardes.Options, dump: ?[]const u8) !*Replica {
const r = try gpa.create(Replica);
- r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true };
+ r.* = .{ .gpa = gpa, .id = id, .view = view orelse 0, .t = tape.memory(gpa), .guess = true };
r.t.skips_render = true;
r.t.checks = false;
// Its terminals are the session's deltas (Peers sends them).
@@ -773,9 +788,29 @@ pub const Replica = struct {
// replica's: deltas write them while a copy is held.
pardes.dump.terminal_gpa = pardes.panes.emulator_gpa;
defer pardes.dump.terminal_gpa = null;
- const c = try pardes.dump.load(epoch, o, bytes, null);
+ const c = if (dump) |bytes| try pardes.dump.load(epoch, o, bytes, null) else boot: {
+ // The addons the session's start finds (runtime/session.zig).
+ o.addons = pardes.Addon.scan();
+ // On no tape, as the session's start is (one recording to a
+ // file aside): what it reads of the machine (the directory, the
+ // shell, the file) it reads as the session does, not as a
+ // replica's step would.
+ const c = init: {
+ const was = Stepping.begin(&tape.none, &r.epoch);
+ defer was.end();
+ break :init try pardes.Pardes.init(epoch, o);
+ };
+ // Its view opened on its own tape, as the session's join opens
+ // it on the one its clients are sent: its terminals, screen
+ // state, not reloaded there either.
+ tape.adoptCore(&r.t, c);
+ const was = Stepping.begin(&r.t, &r.epoch);
+ defer was.end();
+ r.view = Peers.viewFor(c, null) orelse return error.NoView;
+ break :boot c;
+ };
// The chrome's colours are local, never dumped: the restored theme's.
- pardes.colors.finishThemeInitialization(c);
+ if (dump != null) pardes.colors.finishThemeInitialization(c);
// It runs a plain kind's code of its own (Addon.plainOps), loaded
// at its first pane, each opened here from its record; another's
// panes show a line.
@@ -1705,6 +1740,11 @@ pub const Peer = struct {
/// Its first word after its hello: whether it is far (through ssh), its
/// blobs asked for by hash and its stream in zstd (`farJoin`).
far: ?bool = null,
+ /// A pair's client's word before that: the digest of the core it made
+ /// itself (`boot`); the session's at the join (`digest`) the same, its
+ /// dump is not sent.
+ boot: ?u64 = null,
+ digest: u64 = 0,
/// A far client's answer to its join's list: the blobs it lacks.
want: ?[]u8 = null,
/// A far client's stream's compressor, and an envelope's worth of its
@@ -1719,8 +1759,11 @@ pub const Peer = struct {
peer.lock.lockUncancelable(io);
while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock);
const far = peer.far;
+ const boot = peer.boot;
peer.lock.unlock(io);
if (far orelse return false) return peer.farJoin();
+ // Its client made this core itself (a pair's): nothing to send.
+ if (boot == peer.digest) return true;
var pos: usize = 0;
for (peer.blobs.items) |b| {
if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false;
@@ -1957,8 +2000,9 @@ pub const Peer = struct {
peer.peers.post(.{ .check = peer });
return true;
},
- // What its join's writer waits for (`writeJoin`, `farJoin`).
- .far, .want => {
+ // What its join's writer waits for (`writeJoin`, `farJoin`); a
+ // pair's boot comes before its far.
+ .far, .want, .boot => {
const io = peer.peers.io;
const msg = wire.decodeClient(tag, payload) catch return false;
const bits = if (msg == .want) peer.peers.gpa.dupe(u8, msg.want) catch return false else null;
@@ -1970,6 +2014,8 @@ pub const Peer = struct {
return false;
}
peer.want = b;
+ } else if (msg == .boot) {
+ peer.boot = msg.boot;
} else peer.far = msg.far;
peer.cond.signal(io);
return true;
@@ -2279,21 +2325,11 @@ pub const Peers = struct {
ps.recording = true;
ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
}
- // Someone looks at it now: its messages go by keys, not the clock.
- core.unattended = false;
- // Its own view again, kept for it since it left (views.away), or one
- // new. ponytail: a divergence's redial that came before its old
- // link's end (posted by that link's reader, microseconds after the
- // bye; the redial is a dial and the daemon's hand-over later) finds
- // its view not kept yet, and takes a new one.
- const back = if (joining.view) |v| core.rejoinView(.at(v)) else false;
- const k = (if (back) joining.view else if (core.openView()) |o| o.index() else null) orelse {
+ const k = viewFor(core, joining.view) orelse {
core.unattended = ps.count() == 0 and ps.detached;
if (ps.count() == 0) ps.stop(core);
return daemon.refuseReplica(fd, "that session has as many views as it takes");
};
- // Its own process draws it.
- core.undrawn |= @as(u8, 1) << @intCast(k);
ps.seq += 1;
ps.unit(0, ps.seq);
// The session's mutable state, dumped now; its blobs (file texts,
@@ -2328,8 +2364,10 @@ pub const Peers = struct {
peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = lag_max, .dump = bytes, .blobs = blobs, .dump_gpa = core.gpa };
ps.next_id +%= 1;
if (ps.next_id == 0) ps.next_id = 1;
- // The session's digest as dumped, for the joiner to check its load by.
+ // The session's digest as dumped, for the joiner to check its load by
+ // (a pair's client, its own boot: `writeJoin`).
const digest = tape.digestOf(core);
+ peer.digest = digest;
peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = .at(k), .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len;
if (ps.threads) peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null;
if (ps.threads) peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null;
@@ -2352,6 +2390,41 @@ pub const Peers = struct {
if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq });
}
+ /// A joiner's view in `core`: the one kept for it since it left
+ /// (views.away), or one new; someone looks at the session now (its
+ /// messages go by keys, not the clock), and that view is its own
+ /// process's to draw. The session's join and a pair's client's own boot
+ /// (`Replica.initBoot`) alike, so that the two cores are the same: what
+ /// was queued before the join (a pair's session's first effects, its
+ /// boot shell's spawn among them) is performed after it, on the tape
+ /// its client is sent, not by the view's settle, as a client that made
+ /// its core itself performs none of them.
+ /// ponytail: a divergence's redial that came before its old link's end
+ /// (posted by that link's reader, microseconds after the bye; the
+ /// redial is a dial and the daemon's hand-over later) finds its view
+ /// not kept yet, and takes a new one.
+ fn viewFor(core: *Pardes, back: ?u8) ?u8 {
+ const held = core.gpa.alloc(pardes.Effect, core.effects_len) catch return null;
+ defer core.gpa.free(held);
+ for (held, 0..) |*e, i| e.* = core.effects[(core.effects_head + i) % core.effects.len];
+ core.effects_len = 0;
+ defer queueFirst(core, held);
+ core.unattended = false;
+ const k = (if (back) |v| (if (core.rejoinView(.at(v))) v else null) else null) orelse (core.openView() orelse return null).index();
+ core.undrawn |= @as(u8, 1) << @intCast(k);
+ return k;
+ }
+
+ /// `held` queued again ahead of what is queued now, in its order.
+ fn queueFirst(core: *Pardes, held: []const pardes.Effect) void {
+ const cap = core.effects.len;
+ const n = @min(held.len, cap - core.effects_len);
+ const head = (core.effects_head + cap - n) % cap;
+ for (held[0..n], 0..) |e, i| core.effects[(head + i) % cap] = e;
+ core.effects_head = head;
+ core.effects_len += n;
+ }
+
fn leave(ps: *Peers, core: *Pardes, peer: *Peer) void {
for (&ps.list) |*slot| if (slot.* == peer) {
slot.* = null;
@@ -2580,6 +2653,9 @@ pub const Joined = struct {
sent_bytes: u64 = 0,
/// How long its dial and load took, ms (`join`).
ms: u64 = 0,
+ /// Its replica is its own boot, the welcome not read yet (`ownBoot`,
+ /// `confirmBoot`).
+ booted: bool = false,
};
/// A blob a far join was sent: where it is in the dump, and its hash.
@@ -2603,13 +2679,15 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha
_ = libc.close(fd);
return said(why, "nothing answers at {s}", .{path});
}
- return joinOn(gpa, fd, session, why, have, view);
+ return joinOn(gpa, fd, session, why, have, view, null);
}
/// `dial`'s, on a connection made: the hello, then the join. The
/// connection is closed if the join fails. No session named: no hello, a
-/// pair's (Options.pair), whose session took it as its first client.
-fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined {
+/// pair's (Options.pair), whose session took it as its first client. `boot`:
+/// a pair's client that made its core itself and said so (`boot`): nothing
+/// said here, and its welcome's digest the same, no dump comes.
+fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Have, view: ?u8, boot_digest: ?u64) ?Joined {
var out: [wire.hello_max + 16]u8 = undefined;
const hello = if (session) |name| wire.encodeClient(&out, .{ .hello = .{ .session = name, .view = view } }) catch {
_ = libc.close(fd);
@@ -2623,6 +2701,7 @@ fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Ha
gpa: Allocator,
why: []u8,
have: ?*Have,
+ boot: ?u64,
failed: ?[]const u8 = null,
welcomed: bool = false,
done: bool = false,
@@ -2657,6 +2736,10 @@ fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Ha
r.j.gmtoff = w.gmtoff;
r.j.digest = w.digest;
r.j.semantics = w.semantics;
+ if (r.boot == w.digest) {
+ r.done = true;
+ return false;
+ }
r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch return r.fail("out of memory for the session's dump");
r.welcomed = true;
r.j.blobs = if (r.have != null) w.blobs else 0;
@@ -2749,11 +2832,11 @@ fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Ha
}
const maxInt = std.math.maxInt(usize);
};
- if (!writeAll(fd, out[0 .. hello.len + far.len])) {
+ if (boot_digest == null and !writeAll(fd, out[0 .. hello.len + far.len])) {
_ = libc.close(fd);
return said(why, "the session hung up", .{});
}
- var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have };
+ var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have, .boot = boot_digest };
defer r.deinit();
j.rest.run(gpa, fd, &r, Read.take);
if (r.failed != null or !r.done) {
@@ -3014,6 +3097,7 @@ pub fn otherBuild(why: []const u8) bool {
/// keeps the blobs it was sent (`Have.store`), and how long it all took is
/// `j.ms`. Null, and why in `why`.
pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o: pardes.Options, have: ?*Have, why: *[600]u8, old: ?*Replica) ?*Replica {
+ if (old == null) if (o.pair) |fd| return ownBoot(gpa, j, fd, o, why);
if (old) |prev| if (have) |h| h.leaving(prev.core());
const t0 = nowNs();
if (old) |prev| {
@@ -3030,7 +3114,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
} else j.* = (if (o.join_view) |v|
redial(gpa, path, session, why, have, true, v)
else
- sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why)) orelse return null;
+ sameBuild(gpa, dial(gpa, path, session, why, have, null) orelse return null, why)) orelse return null;
defer {
if (have) |h| h.store(gpa, j) else gpa.free(j.dump);
j.dump = &.{};
@@ -3039,15 +3123,21 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
_ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
return null;
};
- // PARDES_NO_ECHO: no predicted echo (the gates' baseline).
- if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false;
- // PARDES_REPLICA_STATS: its timing samples kept, for its line.
- r.stats.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
if (!r.loaded(j.digest)) {
r.deinit();
_ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
return null;
}
+ takeJoined(r, j, have, t0);
+ return r;
+}
+
+/// What a replica joined by `j` takes of it, and of the environment.
+fn takeJoined(r: *Replica, j: *Joined, have: ?*Have, t0: u64) void {
+ // PARDES_NO_ECHO: no predicted echo (the gates' baseline).
+ if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false;
+ // PARDES_REPLICA_STATS: its timing samples kept, for its line.
+ r.stats.samples = std.c.getenv("PARDES_REPLICA_STATS") != null;
r.gmtoff = j.gmtoff;
// On the session's machine: a file read sent by hash is read here;
// far, it goes to the disk for the next join.
@@ -3057,9 +3147,67 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
r.io = h.io;
}
j.ms = (nowNs() - t0) / std.time.ns_per_ms;
+}
+
+/// A pair's first join (Options.pair): its own core made (`Replica.initBoot`)
+/// and its digest said before its `far`, nothing waited for: the welcome is
+/// read once its first frame is drawn (`confirmBoot`). Its welcome's words
+/// meanwhile are what the session's first join gives: its first client's
+/// id, this process's time zone (the same machine), this build.
+fn ownBoot(gpa: Allocator, j: *Joined, fd: c_int, o: pardes.Options, why: *[600]u8) ?*Replica {
+ const t0 = nowNs();
+ const r = Replica.initBoot(gpa, o) catch |err| {
+ _ = std.fmt.bufPrint(why, "this client could not start the session's file itself: {t}\x00", .{err}) catch {};
+ return null;
+ };
+ const digest = r.digest();
+ var out: [64]u8 = undefined;
+ const said_boot = wire.encodeClient(&out, .{ .boot = digest }) catch unreachable;
+ const said_far = wire.encodeClient(out[said_boot.len..], .{ .far = false }) catch unreachable;
+ if (!writeAll(fd, out[0 .. said_boot.len + said_far.len])) {
+ r.deinit();
+ _ = std.fmt.bufPrint(why, "the session hung up\x00", .{}) catch {};
+ return null;
+ }
+ j.* = .{ .fd = fd, .pid = 0, .client = r.id, .view = r.view, .gmtoff = gmtoff(), .dump = &.{}, .digest = digest, .semantics = wire.semantics, .booted = true };
+ takeJoined(r, j, null, t0);
return r;
}
+/// A pair's first client's welcome (`ownBoot`), read once its first frame is
+/// drawn: the session's core at its join the one this client made itself
+/// (the same digest), `r` goes on and nothing more comes; or not (the file
+/// changed between the two reads, anything else a start reads otherwise):
+/// the dump the session sent instead, loaded, `r` let go, and the dump's
+/// replica returned in its place. Null, and why in `why`, when the session
+/// hung up or runs another build, or its dump would not load.
+pub fn confirmBoot(gpa: Allocator, j: *Joined, r: *Replica, o: pardes.Options, why: *[600]u8) ?*Replica {
+ const t0 = nowNs();
+ const digest = j.digest;
+ j.* = sameBuild(gpa, joinOn(gpa, j.fd, null, why, null, null, digest) orelse return null, why) orelse return null;
+ if (j.dump.len == 0) {
+ r.id = j.client;
+ r.view = j.view;
+ return r;
+ }
+ defer {
+ gpa.free(j.dump);
+ j.dump = &.{};
+ }
+ const loaded = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| {
+ _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
+ return null;
+ };
+ if (!loaded.loaded(j.digest)) {
+ loaded.deinit();
+ _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
+ return null;
+ }
+ takeJoined(loaded, j, null, t0);
+ r.deinit();
+ return loaded;
+}
+
/// `j`, when its session runs this build; else its link let go, and why in
/// `why`: the daemon judged the hello by what the session last said, which
/// an upgrade under way may not have said yet.
@@ -3164,6 +3312,9 @@ pub const Link = struct {
/// moved on (its own motion), for PARDES_REPLICA_STATS.
draws: u32 = 0,
again: u32 = 0,
+ /// A pair's first join, its own boot not confirmed yet: its inputs
+ /// kept here, not sent (`release`; client.zig `confirm`).
+ held: ?std.ArrayListUnmanaged(u8) = null,
fn time(c: *const Link) u64 {
return c.now orelse nowNs();
@@ -3202,6 +3353,15 @@ pub const Link = struct {
}
}
+ /// What was held (`held`) sent, or, its replica not the one they were
+ /// pended on (`send` false), dropped.
+ pub fn release(c: *Link, gpa: Allocator, send: bool) void {
+ var h = c.held orelse return;
+ c.held = null;
+ defer h.deinit(gpa);
+ if (send and !writeAll(c.fd, h.items)) c.cut();
+ }
+
pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void {
const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..];
for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) {
@@ -3221,7 +3381,9 @@ pub const Link = struct {
c.input_ns = c.time();
// Sent first: the session steps it while this replica speculates.
const msg = c.r.pend(ev) catch return;
- if (!sendInput(c.fd, msg)) c.cut();
+ if (c.held) |*h| {
+ h.appendSlice(c.r.gpa, msg[head_len..]) catch c.cut();
+ } else if (!sendInput(c.fd, msg)) c.cut();
c.r.applyPended(ev);
c.dirty = true;
}
@@ -3521,7 +3683,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined {
peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer});
defer ps.letGo(peer);
var why: [300]u8 = @splat(0);
- return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse {
+ return joinOn(std.testing.allocator, fds[1], "t", &why, have, null, null) orelse {
std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)});
return error.JoinFailed;
};
@@ -3652,7 +3814,7 @@ test "a client joining again after a divergence is told at once that its session
var fds: [2]c_int = undefined;
try std.testing.expectEqual(0, libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, &fds));
_ = libc.close(fds[1]);
- try std.testing.expect(joinOn(std.testing.allocator, fds[0], "gone", &why, null, 1) == null);
+ try std.testing.expect(joinOn(std.testing.allocator, fds[0], "gone", &why, null, 1, null) == null);
try std.testing.expect(ended(&why));
}
@@ -4480,6 +4642,58 @@ test "bytes: a session's first client on its pair, taken before its first effect
try firstOnPair(true);
}
+test "a pair's client makes its own core from the session's options, its file read itself: the welcome confirms it by the digest and no dump is sent" {
+ try ownBootAgainst(false);
+}
+
+test "a pair's client's own core, the file edited between the session's read and its own: the dump is sent, loaded in its place, and the client shows the session's file" {
+ try ownBootAgainst(true);
+}
+
+/// A session's first client on its pair making its core itself
+/// (`ownBoot`), the file it opens `edited` after the session read it.
+fn ownBootAgainst(edited: bool) !void {
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var path_buf: [std.Io.Dir.max_path_bytes + 8]u8 = undefined;
+ const path = try std.fmt.bufPrint(&path_buf, "{s}/a.txt", .{dir});
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "the session's start\n" });
+ var fds: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
+ const opts: pardes.Options = .{ .cols = 80, .rows = 24, .file = path };
+ const core = try Pardes.init(gpa, opts);
+ defer core.deinit();
+ core.host = TestHost.host();
+ var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .detached = true };
+ defer ps.deinit();
+ defer live_peers = null;
+ defer ps.dropAll(core);
+ ps.join(core, .{ .fd = fds[0] });
+ // Edited after the session read it, before its client does.
+ if (edited) try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "edited between\n" });
+ var o = opts;
+ o.pair = fds[1];
+ var j: Joined = undefined;
+ var why: [600]u8 = @splat(0);
+ const booted = join(gpa, &j, "", "", o, null, &why, null) orelse return error.JoinFailed;
+ const r = confirmBoot(gpa, &j, booted, o, &why) orelse {
+ std.debug.print("the welcome: {s}\n", .{std.mem.sliceTo(&why, 0)});
+ return error.JoinFailed;
+ };
+ defer r.deinit();
+ defer {
+ _ = libc.close(j.fd);
+ j.rest.deinit(gpa);
+ }
+ try std.testing.expectEqual(!edited, r == booted);
+ try std.testing.expectEqual(tape.digestOf(core), r.digest());
+ // What the client shows is the session's file as the session read it.
+ try std.testing.expectEqualStrings("the session's start\n", r.core().panes[0].?.file.?.content);
+}
+
/// A session's first client on a socket pair (runtime/session.zig's, then
/// the client's first join on its end), the session's first effects after:
/// its boot shell spawned, its first bytes output; the client quiet and
@@ -4511,7 +4725,7 @@ fn firstOnPair(bytes: bool) !void {
var j: Joined = undefined;
var why: [600]u8 = @splat(0);
const r = if (bytes) r: {
- j = joinOn(gpa, fds[1], null, &why, null, null) orelse return error.JoinFailed;
+ j = joinOn(gpa, fds[1], null, &why, null, null, null) orelse return error.JoinFailed;
defer gpa.free(j.dump);
const r = try Replica.initDump(gpa, j.client, j.view, j.dump, opts);
r.t.screen_state = false;
@@ -4520,10 +4734,20 @@ fn firstOnPair(bytes: bool) !void {
} else r: {
var o = opts;
o.pair = fds[1];
- break :r join(gpa, &j, "", "", o, null, &why, null) orelse {
+ // Its own core, from the same options, before any byte of the
+ // session's; the welcome then says it is the session's: no dump.
+ const booted = join(gpa, &j, "", "", o, null, &why, null) orelse {
std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)});
return error.JoinFailed;
};
+ try std.testing.expect(j.booted);
+ const r = confirmBoot(gpa, &j, booted, o, &why) orelse {
+ std.debug.print("the welcome: {s}\n", .{std.mem.sliceTo(&why, 0)});
+ return error.JoinFailed;
+ };
+ try std.testing.expect(r == booted);
+ try std.testing.expectEqual(@as(usize, 0), j.dump.len);
+ break :r r;
};
defer r.deinit();
defer {
diff --git a/core/root.zig b/core/root.zig
index 5300ce93..80495436 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -630,7 +630,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{
.{ "hunks.zig", 1, "a test helper's file read" },
.{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" },
.{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" },
- .{ "replica.zig", 73, "the client and session engines: sockets, threads, clocks, the blob cache, traces" },
+ .{ "replica.zig", 75, "the client and session engines: sockets, threads, clocks, the blob cache, traces" },
.{ "rollback.zig", 1, "a test helper's directory" },
.{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" },
.{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" },
diff --git a/runtime/client.zig b/runtime/client.zig
index 329bdab3..d20e64d1 100644
--- a/runtime/client.zig
+++ b/runtime/client.zig
@@ -185,7 +185,9 @@ pub fn Loop(comptime Frontend: type) type {
upgrade.locate();
while (true) {
link.reset(s.r, &s.joined);
- l.fe.start(l);
+ if (s.joined.booted) {
+ if (!l.confirm(s)) return .failed;
+ } else l.fe.start(l);
var done: std.atomic.Value(bool) = .init(false);
const reader = try std.Thread.spawn(.{}, read, .{ l, s, &done });
while (!link.quit) {
@@ -226,6 +228,28 @@ pub fn Loop(comptime Frontend: type) type {
}
}
+ /// A pair's first join, its replica its own boot (replica.ownBoot): its
+ /// start's inputs held and its first pass drawn, then the session's
+ /// welcome read: the same core, what was held is sent; another (the
+ /// dump came), the dump's replica in place of the boot's, started
+ /// again. False, and why in `s.why`: no session to go on with.
+ fn confirm(l: *L, s: *Start) bool {
+ const link = &l.link;
+ link.held = .empty;
+ l.fe.start(l);
+ _ = l.before();
+ const r = replica.confirmBoot(s.gpa, &s.joined, s.r, s.o, &s.why) orelse {
+ link.release(s.gpa, false);
+ return false;
+ };
+ link.release(s.gpa, r == s.r);
+ if (r == s.r) return true;
+ s.r = r;
+ link.reset(r, &s.joined);
+ l.fe.start(l);
+ return true;
+ }
+
/// The session's stream, onto the frontend's queue: its messages,
/// and its end.
fn read(l: *L, s: *Start, done: *std.atomic.Value(bool)) void {
diff --git a/runtime/main.zig b/runtime/main.zig
index a21eb83f..2bc02a23 100644
--- a/runtime/main.zig
+++ b/runtime/main.zig
@@ -1795,6 +1795,10 @@ fn pair(init: std.process.Init, opts: *pardes.Options, args: []const [:0]const u
var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined;
var o = opts.*;
o.pair = fds[1];
+ // The client makes its core from its session's options (replica.ownBoot):
+ // a window's session is the terminal binary it execs, whose views are
+ // not windowed until a window says so (its first input).
+ o.windowed = false;
clientOf(init, o, daemon.replicaSocket(&sock_buf) orelse "", name, false) catch |err| die(init.io, "{t}", .{err});
std.process.exit(0);
}
diff --git a/test/daemon.py b/test/daemon.py
index b28d16ea..1107e502 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -241,7 +241,7 @@ class Replica:
"""A replica's connection (abi/wire.zig): length-prefixed
messages, a tag byte and a little-endian u32 length. It is a near
client: its first word after the hello says so."""
- WIRE = 24
+ WIRE = 26
def __init__(self, path, timeout=10):
self.path, self.timeout = path, timeout