From 236c9f51134adf04932f7653ed0262a0c9253708 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:04:43 -0300 Subject: replica: a client's join, the terminal's and the window's alike, is replica.join (the session dialled, its build checked, its dump loaded and checked, or joined again for its view once its link ends, the core it leaves held meanwhile), where tui.zig and gui.zig each spelled out the dial, the check, the load, the redial and their timing; Link.reset takes the join, and Link.load, Link.joinedAs and Link.redial go Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/gui/gui.zig | 21 ++------ src/replica.zig | 161 ++++++++++++++++++++++++++++++++------------------------ src/tui/tui.zig | 25 ++------- 3 files changed, 100 insertions(+), 107 deletions(-) (limited to 'src') diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a850cdca..3800c794 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -25,7 +25,6 @@ const ninep_io = @import("../9p_io.zig"); const daemon = @import("../daemon.zig"); const upgrade = @import("../upgrade.zig"); const replica = @import("../replica.zig"); -const wire = @import("../detached/wire.zig"); const tracy = @import("../tracy.zig"); pub const c = @import("gui_c"); // SDL3 and font.h, translated by build.zig @@ -2343,10 +2342,6 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; defer have_store.deinit(); const have: ?*replica.Have = if (far) &have_store else null; - var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - var joined = replica.dial(gpa, to.path, to.session, &why, have, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); - const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); pardes.image.start(io, allocs.image); @@ -2360,8 +2355,8 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ var o = opts_in; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + var joined: replica.Joined = undefined; + var r = replica.join(gpa, &joined, to.path, to.session, o, have, &why, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); var client: Client = .{ .link = .{ .r = r, .fd = joined.fd }, .io = io, .gpa = gpa, .texts = .init(gpa) }; defer client.deinit(); @@ -2401,15 +2396,13 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ // A join a pass: the first, and one after the replica diverged or the // session let it go to upgrade. The window stays as it is. while (true) { - r.gmtoff = joined.gmtoff; const core = r.core(); shell.core = core; shell.gview = joined.view; shell.surface = null; g.hit_core = core; frame_probe = &core.frames; - link.reset(r, joined.fd); - link.joinedAs(&joined, dial_ms); + link.reset(r, &joined); client.ended = false; // ponytail: why it joined again is the window's title (and its // log) until a key, where a terminal's client draws it over its @@ -2491,13 +2484,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ return; } // The session again, the core it leaves held meanwhile. - if (have) |h| h.leaving(r.core()); - dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = replica.Link.redial(gpa, to.path, to.session, &why, have, r.diverged == null, joined.view) orelse Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); - if (have) |h| h.left(); - r.deinit(); - r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + r = replica.join(gpa, &joined, to.path, to.session, o, have, &why, r) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); } } diff --git a/src/replica.zig b/src/replica.zig index ba5ca55f..e567587c 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -2262,6 +2262,8 @@ pub const Joined = struct { from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0, + /// How long its dial and load took, ms (`join`). + ms: u64 = 0, }; /// A blob a far join was sent: where it is in the dump, and its hash. @@ -2765,6 +2767,90 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime rest.run(gpa, fd, ctx, Take.take); } +/// 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"; + +/// A replica client's join, the terminal's and the window's alike (their +/// `remote`): the session at `path` dialled, its build checked to be this +/// one's, and its dump loaded into a replica checked against the session +/// as it was dumped. Or, `old` given, joined again once old's link has +/// ended, for old's view (`redial`): the core it leaves held meanwhile so +/// that its blobs are not sent again (`Have.leaving`), and let go before +/// the new one loads. The dump is let go, a far join's to the thread that +/// 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) |prev| if (have) |h| h.leaving(prev.core()); + const t0 = nowNs(); + if (old) |prev| { + j.* = redial(gpa, path, session, why, have, prev.diverged == null, j.view) orelse { + var words: [600]u8 = undefined; + const said_ = std.mem.sliceTo(why, 0); + @memcpy(words[0..said_.len], said_); + _ = std.fmt.bufPrint(why, "could not join again: {s}\x00", .{words[0..said_.len]}) catch {}; + return null; + }; + if (have) |h| h.left(); + prev.deinit(); + } else j.* = 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 = &.{}; + } + const r = 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; + }; + // 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; + } + r.gmtoff = j.gmtoff; + j.ms = (nowNs() - t0) / std.time.ns_per_ms; + return r; +} + +/// `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. +fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined { + if (j.semantics == wire.semantics) return j; + bye(j.fd, false); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; +} + +/// The session again, after a divergence or an upgrade: an upgrade's +/// takes the time its exec and load take, and its daemon's, so it is +/// dialled every 100 ms for 20 s. Null, and why in `why`. +/// `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 { + var tries: u32 = 0; + while (true) : (tries += 1) { + why.* = @splat(0); + if (dial(gpa, path, session, why, have, view)) |j| return sameBuild(gpa, j, why); + if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; + return null; + } + if (!patient or tries > 200) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } +} + /// A replica client's link to its session, the terminal's (tui.zig /// `remote`) and the window's (gui.zig `remote`): its replica, its socket, /// what the session said, and what it checks. Drawing is the frontend's. @@ -2812,32 +2898,6 @@ pub const Link = struct { join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{}, join_ms: u64 = 0, - pub const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; - - /// The replica the dial's dump makes, checked against the session as - /// it was dumped; null, and why in `why`. The dump is let go, a far - /// join's to the thread that keeps the blobs it was sent (`Have.store`). - pub fn load(gpa: Allocator, joined: *Joined, o: pardes.Options, why: []u8, have: ?*Have) ?*Replica { - defer { - if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump); - joined.dump = &.{}; - } - const r = Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { - _ = 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(joined.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; - } - return r; - } - /// The test knobs the environment sets (PARDES_REPLICA_CHECK, /// PARDES_TEST_DIVERGE). pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void { @@ -2850,16 +2910,12 @@ pub const Link = struct { if (c.refused) |words| std.heap.c_allocator.free(words); } - /// How its join went, its dial and load `ms`. - pub fn joinedAs(c: *Link, j: *const Joined, ms: u64) void { - c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes }; - c.join_ms = ms; - } - - /// A join's link, from its start. - pub fn reset(c: *Link, r: *Replica, fd: c_int) void { + /// Join `j`'s link, from its start, and how that join went. + pub fn reset(c: *Link, r: *Replica, j: *const Joined) void { c.r = r; - c.fd = fd; + c.fd = j.fd; + c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes }; + c.join_ms = j.ms; c.quit = false; c.gone = false; c.dirty = true; @@ -3011,45 +3067,12 @@ pub const Link = struct { } return false; } - - /// The session again, after a divergence or an upgrade: an upgrade's - /// takes the time its exec and load take, and its daemon's, so it is - /// dialled every 100 ms for 20 s. A far client's (`have`) holds the - /// core it leaves meanwhile (`Have.leaving`): its blobs are not sent - /// again. Null, and why in `why`. - /// `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). - pub 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); - if (dial(gpa, path, session, why, have, view)) |j| { - if (j.semantics == wire.semantics) return j; - bye(j.fd, false); - _ = libc.close(j.fd); - var jj = j; - jj.rest.deinit(gpa); - gpa.free(j.dump); - _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; - return null; - } - if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { - _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; - return null; - } - if (!patient or tries > 200) return null; - const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; - _ = libc.nanosleep(&ts, null); - } - } }; test "a client joining again after a divergence is told at once that its session is gone" { var why: [600]u8 = undefined; const t0 = nowNs(); - try std.testing.expect(Link.redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); + try std.testing.expect(redial(std.testing.allocator, "/nonexistent/pardes-replica.sock", "gone", &why, null, false, 1) == null); try std.testing.expect(nowNs() - t0 < 100 * std.time.ns_per_ms); } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 2a107dc4..ff0e8334 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -17,7 +17,6 @@ const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); const upgrade = @import("../upgrade.zig"); const replica = @import("../replica.zig"); -const wire = @import("../detached/wire.zig"); extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; @@ -1235,10 +1234,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; defer have_store.deinit(); const have: ?*replica.Have = if (far) &have_store else null; - var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - var joined = replica.dial(gpa, path, session, &why, have, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{replica.Link.upgrade_words}); - const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); pardes.image.start(io, allocs.image); @@ -1252,9 +1247,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var o = opts; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - var r = replica.Link.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - // Its join's time, the dial and the load (PARDES_REPLICA_STATS). - var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); + var joined: replica.Joined = undefined; + var r = replica.join(gpa, &joined, path, session, o, have, &why, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); @@ -1302,14 +1296,12 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se // A join a pass: the first, and one after the replica diverged or the // session let it go to upgrade. The terminal stays as it is. while (true) { - r.gmtoff = joined.gmtoff; sh.core = r.core(); sh.from = joined.view; sh.terms = @splat(null); sh.terms[joined.view] = &screen; screen.feed.view = joined.view; - link.reset(r, joined.fd); - link.joinedAs(&joined, dial_ms); + link.reset(r, &joined); var reader_done: std.atomic.Value(bool) = .init(false); const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); // What it is, then its size, are its view's first inputs. @@ -1383,19 +1375,10 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se } // The session again, the core it leaves held meanwhile: its blobs // are not sent again. - if (have) |h| h.leaving(r.core()); - dial_t0 = std.Io.Clock.awake.now(io).nanoseconds; - joined = replica.Link.redial(gpa, path, session, &why, have, r.diverged == null, joined.view) orelse { - screen.leave(); - Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); - }; - if (have) |h| h.left(); - r.deinit(); - r = replica.Link.load(gpa, &joined, o, &why, have) orelse { + r = replica.join(gpa, &joined, path, session, o, have, &why, r) orelse { screen.leave(); Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; - dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms)); } } -- cgit v1.3