diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 05:26:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 05:30:39 -0300 |
| commit | e29ff73bbb2c198afbb894ee98854da2d9a7be7d (patch) | |
| tree | 17cf56b16272f43fdb789174350681641876e170 /core/replica.zig | |
| parent | 63538d28cee238ff53c731f965c22e615d43bec1 (diff) | |
| parent | fe0aa07c18a9fd5ca6bdcb78e9354a2745e32298 (diff) | |
| download | pardes-e29ff73bbb2c198afbb894ee98854da2d9a7be7d.tar.gz pardes-e29ff73bbb2c198afbb894ee98854da2d9a7be7d.zip | |
integration: the pair's finished steps on the gathered line: pardes-pair-4 (`rustnvoo`: an attach handed to a session as it upgrades is told so and joins the new process; the fast start, the boot as input zero: the wire at 27; a window client exec'd at an Upgrade comes back the size it was) and pardes-merge47 (`rprvwpnv`: the terminals' line at 26 and the library for a browser reader); three textual conflicts: abi/wire.zig's history says 27 and 26 each in its own words and the version is 27, test/daemon.py's WIRE is 27, core/replica.zig's Link keeps both lines' fields (the lag bound's `behind`, `least`, `fresh`, `lagged`; the pair's `held`); core-test 885/1045 (160 skipped), shell-test 1087/1090; its gate: .scratch/sketch/reorg/merge48, the numbers in PARDES.md (2026-11-21)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
| -rw-r--r-- | core/replica.zig | 354 |
1 files changed, 315 insertions, 39 deletions
diff --git a/core/replica.zig b/core/replica.zig index 39ee53b2..56fc4a59 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. @@ -1709,6 +1744,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 @@ -1723,8 +1763,11 @@ pub const Peer = struct { peer.lock.lockUncancelable(io); while (peer.far == null and !peer.closing) 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; @@ -1969,8 +2012,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; @@ -1982,6 +2026,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; @@ -2075,6 +2121,10 @@ pub const Peers = struct { /// hung up) and the session is a pair's not kept (daemon.sessionState): it /// ends there (runtime/session.zig). closed: bool = false, + /// An Upgrade told its clients so (`refuseAll`): a connection handed + /// over before its exec is told the same, never closed by the exec + /// unanswered. Its exec failed: the next boundary takes joins again. + upgrading: std.atomic.Value(bool) = .init(false), /// The worst a join cost the session's step, and the last one's dump. join_ns: u64 = 0, each_join: std.ArrayListUnmanaged(u64) = .empty, @@ -2120,6 +2170,10 @@ pub const Peers = struct { pub fn post(ps: *Peers, note: Note) void { ps.lock.lockUncancelable(ps.io); + if (note == .join and ps.upgrading.load(.monotonic)) { + ps.lock.unlock(ps.io); + return daemon.refuseReplica(note.join.fd, upgrading); + } ps.inbox.append(ps.gpa, note) catch { ps.lock.unlock(ps.io); switch (note) { @@ -2167,6 +2221,7 @@ pub const Peers = struct { /// After each of the session's pumps: its unit sent, the clients that /// came joined, their inputs taken in, the ones gone let go. pub fn boundary(ps: *Peers, core: *Pardes) void { + if (ps.upgrading.load(.monotonic)) ps.upgrading.store(false, .monotonic); if (!ps.recording and !ps.mail.load(.acquire)) return; const t0 = nowNs(); defer if (ps.recording and ps.samples) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {}; @@ -2291,21 +2346,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, @@ -2340,8 +2385,10 @@ pub const Peers = struct { peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = .at(k), .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; @@ -2364,6 +2411,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; @@ -2431,13 +2513,23 @@ pub const Peers = struct { ps.gpa.destroy(peer); } - /// Every client told `why` and let go, its view with it: an Upgrade's - /// exec is next (tui.zig `upgradeNow`). What each was sent goes first, - /// for up to 200 ms; a client then dials the session again. - pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void { + /// Every client told the session is upgrading and let go, its view with + /// it: an Upgrade's exec is next (tui.zig `upgradeNow`). What each was + /// sent goes first, for up to 200 ms; a client then dials the session + /// again. So is a connection handed over and not joined yet, or until + /// the exec (`upgrading`): the exec would close it unanswered, and its + /// client would take that for the session's end. + pub fn refuseAll(ps: *Peers, core: *Pardes) void { + ps.lock.lockUncancelable(ps.io); + ps.upgrading.store(true, .monotonic); + var i: usize = 0; + while (i < ps.inbox.items.len) { + if (ps.inbox.items[i] == .join) daemon.refuseReplica(ps.inbox.orderedRemove(i).join.fd, upgrading) else i += 1; + } + ps.lock.unlock(ps.io); if (ps.count() == 0) return; var out: [600]u8 = undefined; - const words = wire.encodeServer(&out, .{ .refused = why }) catch return; + const words = wire.encodeServer(&out, .{ .refused = upgrading }) catch unreachable; // the words fit for (ps.list) |slot| if (slot) |peer| peer.last(words); ps.flushed200(); // Each joins again for its view, kept in the dump (views.away). @@ -2594,6 +2686,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. @@ -2617,13 +2712,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); @@ -2637,6 +2734,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, @@ -2671,6 +2769,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; @@ -2763,11 +2865,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) { @@ -3007,6 +3109,9 @@ pub fn ended(why: []const u8) bool { return false; } +/// What an Upgrade tells its clients (`Peers.refuseAll`): each dials again. +pub const upgrading = "the session is upgrading to another build of pardes"; + /// The words a client goes with when its session runs another build now. const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; @@ -3028,6 +3133,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| { @@ -3044,7 +3150,9 @@ 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 + // 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 { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; @@ -3053,15 +3161,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. @@ -3071,9 +3185,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. @@ -3094,8 +3266,9 @@ fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined { /// `patient`: after an Upgrade's `refused` alone, while the session is /// away a moment; after a divergence a session that will not answer is /// gone, and the client says so at once (it froze 20 s, 2026-10-30). -/// `view`: the one it had, which the session kept for it (views.away). -fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: u8) ?Joined { +/// `view`: the one it had, which the session kept for it (views.away); +/// none for a first join that met an Upgrade. +fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool, view: ?u8) ?Joined { var tries: u32 = 0; while (true) : (tries += 1) { why.* = @splat(0); @@ -3193,6 +3366,9 @@ pub const Link = struct { fresh: bool = true, /// How far behind it was as it let its link go (`rejoins` says so). lagged: u64 = 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(); @@ -3233,6 +3409,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) { @@ -3252,7 +3437,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; } @@ -3579,7 +3766,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; }; @@ -3710,10 +3897,37 @@ 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)); } +test "an Upgrade tells a client handed to the session and not joined yet that it is upgrading, as it tells its clients; one handed after it is told so at once: each dials again" { + var ps: Peers = .{ .gpa = std.testing.allocator, .io = std.testing.io }; + defer ps.deinit(); + var fds: [2][2]c_int = undefined; + for (&fds) |*f| try std.testing.expectEqual(0, libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM, 0, f)); + // Handed by the daemon, waiting for the next step boundary; then the + // Upgrade's word to the clients (no client joined: count() is 0), and + // a hand-over after it, before the exec. + ps.handed(fds[0][0], null); + ps.refuseAll(undefined); + ps.handed(fds[1][0], null); + try std.testing.expectEqual(0, ps.inbox.items.len); + for (fds) |f| { + defer _ = libc.close(f[1]); + var buf: [600]u8 = undefined; + const n = libc.read(f[1], &buf, buf.len); + try std.testing.expect(n > wire.header_len); + const msg = try wire.decodeServer(buf[0], buf[wire.header_len..@intCast(n)]); + try std.testing.expectEqualStrings(upgrading, msg.refused); + try std.testing.expectEqual(0, libc.read(f[1], &buf, buf.len)); + } + // Its exec failed: the session goes on, and takes joins again. + ps.boundary(undefined); + ps.handed(-1, null); + try std.testing.expectEqual(1, ps.inbox.items.len); +} + test "a replica's own keys apply at once, confirm on the fast path, and end at the authority's digest" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); @@ -4538,6 +4752,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 @@ -4569,7 +4835,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; @@ -4578,10 +4844,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 { |
