From c6234f250ee6f94bdeb57e1d77045d74149325fa Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 09:23:54 -0300 Subject: pair: under PARDES_PAIR `pardes-gui FILE` is a window client and a session with no SDL (the last step's design 1.2, Z1's rest): the child of a fork before SDL_Init EXECS the `pardes` beside it (`--paired=FD` first, the window's arguments after; its end of the socket pair kept across the exec), since a window binary's session loop is woken through SDL (gui.zig `sdlWake` is the Inbox's and the Peers' wake) and the terminal's binary needs none; with no `pardes` beside it the window is its own session as before; the session registers itself (and starts the daemon when none runs); the window's close button ends the session when it was its last client and no Detach was typed in any (a CLOSE: the client's bye says so, wire byte 2, protocol 24), a killed window leaves it running (a LOSS), with no daemon it ends with its last link; a client dialling again after an Upgrade that moved the protocol now stops at the daemon's "run the same pardes at both ends" as at "runs another build" (it dialled 20 s in vain); test/daemon.py `gui_pair` (in --gui, alone --pair-gui; on the base: the window was its own session) and `upgrade_clients` (in --upgrade when a pardes-gui is beside each build); the window's start, ReleaseFast, medium.zig, offscreen test mode, one build alternated, instructions:u (perf stat total, perf record split by pid): own 387.9-393.3 M, pair 397.5-398.3 M = window client 379.8-380.5 M + session 17.6-17.9 M (wall best 260 ms own, 282 pair, NOT judged, load 12-14); idle pair 0 wakeups in 2 s on the session side, terminal client 0, a window client unmeasurable offscreen (SDL's offscreen driver polls ~920/s, the own window the same); tui core-test 858/1013 (155 skipped), shell-test 1053/1056, term 17/17, pane 183/184, kitty 45/45, behaviour 275/275 and --replay 275/275, process-test 0 of 144 failed, soaks --clients 5 and --bytes --clients 5 diverged 0 unequal 0 corpus 22, daemon.py plain, --far, --games, --window-clients, --gui and --upgrade both ways (the base built once, pfx-old) pass, gui unit-test 1650/1654 with the perf check's BaselineHarnessMismatch its one failure (loads 5-25) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/replica.zig b/core/replica.zig index 781bd211..8ca505ec 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1672,6 +1672,8 @@ pub const Peer = struct { /// Its bye said it joins again for its view (a divergence's rejoin): /// kept for it (views.away), not closed. Its reader's, read after its end. again: bool = false, + /// ...or that its window was closed (Peers.closed). + close: bool = false, /// Its number in the session's `Screens`. sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). @@ -1931,7 +1933,9 @@ pub const Peer = struct { switch (t) { .hello => return false, .bye => { - peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + const b = (wire.decodeClient(tag, payload) catch return false).bye; + peer.again = b.again; + peer.close = b.close; return false; }, .ping => { @@ -2054,6 +2058,10 @@ pub const Peers = struct { /// No frontend of its own (the host says): with no client either, the /// session is unattended. detached: bool = false, + /// Its last client left by closing its window, and no Detach was typed + /// in any (`kept`): a pair's session ends there (runtime/session.zig). + closed: bool = false, + kept: bool = 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, @@ -2353,6 +2361,7 @@ pub const Peers = struct { core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); ps.closeViewOf(core, peer, peer.again); if (ps.count() == 0) core.unattended = ps.detached; + if (ps.count() == 0 and peer.close and !ps.kept) ps.closed = true; ps.seq += 1; ps.unit(0, ps.seq); ps.letGo(peer); @@ -2457,6 +2466,7 @@ pub const Peers = struct { /// (`left`), its last word; the client goes, and its reader says so. pub fn detach(ps: *Peers, k: u8) bool { for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { + ps.kept = true; var out: [wire.max_name + 8]u8 = undefined; peer.last(wire.encodeServer(&out, .{ .left = ps.name[0..@min(ps.name.len, wire.max_name)] }) catch unreachable); return true; @@ -2927,10 +2937,11 @@ pub fn askCheck(fd: c_int) bool { return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); } -/// The client's last word: `again`, it joins again for its view at once. -pub fn bye(fd: c_int, again: bool) void { +/// The client's last word: `again`, it joins again for its view at once; +/// `close`, its window was closed. +pub fn bye(fd: c_int, how: @FieldType(wire.ClientMsg, "bye")) void { var out: [16]u8 = undefined; - if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} + if (wire.encodeClient(&out, .{ .bye = how })) |b| _ = writeAll(fd, b) else |_| {} } /// What the session streams a client after its dump, as its frontend's loop @@ -3043,7 +3054,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o /// 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); + bye(j.fd, .{}); _ = libc.close(j.fd); var jj = j; jj.rest.deinit(gpa); @@ -3064,7 +3075,10 @@ fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, 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) { + // Another build, or another protocol (the daemon moved with its + // session): no dial again will do. + const words = std.mem.sliceTo(why, 0); + if (std.mem.indexOf(u8, words, "runs another build") != null or std.mem.indexOf(u8, words, "run the same pardes at both ends") != null) { _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; return null; } @@ -3081,6 +3095,8 @@ pub const Link = struct { r: *Replica, fd: c_int, quit: bool = false, + /// ...because its window was closed: said in its bye. + close: bool = false, /// The session hung up. gone: bool = false, /// Something to draw: an input applied, a unit taken. -- cgit v1.3