diff options
| -rw-r--r-- | abi/wire.zig | 20 | ||||
| -rw-r--r-- | core/dump.zig | 3 | ||||
| -rw-r--r-- | core/panes.zig | 5 | ||||
| -rw-r--r-- | core/replica.zig | 288 | ||||
| -rw-r--r-- | core/root.zig | 2 | ||||
| -rw-r--r-- | runtime/client.zig | 26 | ||||
| -rw-r--r-- | runtime/main.zig | 4 | ||||
| -rw-r--r-- | test/daemon.py | 2 |
8 files changed, 309 insertions, 41 deletions
diff --git a/abi/wire.zig b/abi/wire.zig index 658cff23..cde9413e 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -10,6 +10,10 @@ const pardes = @import("core"); const config = @import("pardes_options"); const build_info = @import("build_info"); +/// 27: 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 (26 is the terminals' +/// line's); /// 25: the 9P state a session's requests made (its open records, each /// pane's, the panes the last look or exec touched, a header's /// truncation) is in its join's dump and its digest (core/fs.zig Namespace); @@ -41,7 +45,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 = 25; +pub const version: u16 = 27; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -115,6 +119,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, @@ -311,6 +321,7 @@ pub const ClientMsg = union(enum) { far: bool, want: []const u8, stamp: [2]i64, + boot: u64, event: pardes.Event, }; @@ -522,7 +533,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)), @@ -603,6 +614,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 @@ -659,7 +671,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. @@ -786,6 +798,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 4fc7fc4d..18ac28f7 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 174af241..3d6e8754 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1186,9 +1186,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 92d3ea90..554ef327 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; @@ -2288,21 +2334,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, @@ -2337,8 +2373,10 @@ pub const Peers = struct { peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = .at(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 = peer.view, .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; @@ -2361,6 +2399,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; @@ -2599,6 +2672,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. @@ -2622,13 +2698,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); @@ -2642,6 +2720,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, @@ -2676,6 +2755,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; @@ -2768,11 +2851,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) { @@ -3036,6 +3119,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| { @@ -3052,7 +3136,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 + sameBuild(gpa, (dial(gpa, path, session, why, have, null) orelse // Handed to the session as it upgraded: it is there again soon. if (std.mem.eql(u8, std.mem.sliceTo(why, 0), upgrading)) redial(gpa, path, session, why, have, true, null) else null) orelse return null, why)) orelse return null; defer { @@ -3063,15 +3147,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. @@ -3081,9 +3171,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. @@ -3189,6 +3337,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(); @@ -3227,6 +3378,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) { @@ -3246,7 +3406,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; } @@ -3546,7 +3708,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; }; @@ -3677,7 +3839,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)); } @@ -4532,6 +4694,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 @@ -4563,7 +4777,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; @@ -4572,10 +4786,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 7941889e..c550b0fa 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -190,7 +190,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) { @@ -231,6 +233,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 0751f6b0..b0338831 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -1812,6 +1812,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 d3ba6e3d..5744b099 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -243,7 +243,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 = 25 + WIRE = 27 def __init__(self, path, timeout=10): self.path, self.timeout = path, timeout |
