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 --- dev/gui/gui.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'dev/gui/gui.zig') diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 59e70935..36243c71 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1449,7 +1449,15 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes-gui: detached from '{s}', which is still running; come back with `pardes-gui attach {s}`\n", .{ to.session, to.session }) catch "pardes-gui: detached\n") catch {}; std.process.exit(0); }, - .ended => |code| std.process.exit(code), + .ended => |code| { + // A pair's window is its session's parent: its status once it + // is gone, as the terminal's (tui.zig `runClient`). + if (opts_in.pair != null) { + var status: c_int = 0; + while (libc.waitpid(@intCast(start.joined.pid), &status, 0) < 0 and libc.errno(-1) == .INTR) {} + } + std.process.exit(code); + }, } } @@ -1501,6 +1509,7 @@ const StdinFeed = struct { // so a driver exercises the real pointer path (pixel to cell, // tag and body hits). SGR mouse carries only cells. // finger;;;;; + // close: the window's close button // One the read cut short, or what may yet begin one, waits for // the next read. const rest = f.buf[seq_start..len]; @@ -1564,6 +1573,12 @@ const StdinFeed = struct { dispatch(gp, in, &sev); c.SDL_SetModState(before); } + } else if (std.mem.eql(u8, event[0..semi], "close")) { + if (g) |gp| { + var sev = std.mem.zeroes(c.SDL_Event); + sev.type = c.SDL_EVENT_WINDOW_CLOSE_REQUESTED; + dispatch(gp, in, &sev); + } } else if (std.mem.eql(u8, event[0..semi], "finger")) { if (parseFinger(fields)) |finger| if (g) |gp| { var wpw: c_int = 0; @@ -1744,6 +1759,8 @@ const Input = struct { fn close(in: *Input) void { if (in.client) |r| { + // Its bye says so: a pair's session ends at its last client's. + r.loop.link.close = true; r.loop.link.quit = true; in.quit = true; return; -- cgit v1.3 From 2e0eb4d7e2364a8cfe97eb56314af0b4be36be9b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 11:15:26 -0300 Subject: upgrade: an Upgrade carries near clients (the last step's design 1.4, Z3): a client let go by an Upgrade dials again as before, and when the session runs another build (or another protocol) and a newer file is installed at its own start path since it started (upgrade.replaced), the client loop ends `.upgrade` and its frontend lets its screen go and execs that file as `--attach=:` (client.zig execAgain), the same process, for the view the session kept for it (PARDES_JOIN_VIEW, Options.join_view, dialled patiently as a rejoin is): the terminal's is the blink (it leaves the alternate screen and enters it again; no record), the window a new window; a client whose start path holds no newer file keeps today's sentence "session upgraded to a newer pardes: upgrade this client too"; a far client the same, through its forward (PARDES_REPLICA_FAR=1); a pair with no daemon refuses the Upgrade in words ("no daemon keeps this session, so its client could not join it again"), since its first client has no socket to find the session again by (on the base the Upgrade cut the client off and ended the session); test/daemon.py upgrade_clients (a terminal client and a window client, each its own pane in its own view): old->new the base's clients are let go and told to upgrade too (what they do today), new->old they exec the base build and come back in a view of their own (the base reads no PARDES_JOIN_VIEW), new->(this built with -Dembed-sources, another semantics) each is back in ITS view with its own pane; `pair`'s no-daemon Upgrade refused; 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, both soaks diverged 0 unequal 0 corpus 22, daemon.py plain, --far, --games, --window-clients, --gui, --upgrade both ways (with clients) pass, gui unit-test 1650/1654 (the perf check's BaselineHarnessMismatch its one failure), macOS lib (Linux host), aarch64-macos, FreeBSD build (loads 3-15) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Core.zig | 4 ++++ core/replica.zig | 12 +++++++++++- dev/gui/gui.zig | 2 ++ dev/tui/tui.zig | 10 ++++++++++ runtime/client.zig | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- runtime/upgrade.zig | 8 ++++++++ test/daemon.py | 15 ++++++++++++--- 7 files changed, 95 insertions(+), 8 deletions(-) (limited to 'dev/gui/gui.zig') diff --git a/core/Core.zig b/core/Core.zig index 7ebcc020..c08eb883 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2284,6 +2284,10 @@ pub const Options = struct { /// first effects (runtime/session.zig); a client's session is on it, /// its first join made there with no hello (replica.join). pair: ?c_int = null, + /// A client exec'd at an Upgrade (runtime/client.zig `execAgain`, + /// PARDES_JOIN_VIEW): the view it had, which the session kept for it + /// (views.away), joined again for as a rejoin dials (replica.join). + join_view: ?u8 = null, mounts: []const filesystem.Mount = &.{}, config_dir: ?[]const u8 = null, /// Where what outlives the session is kept (Db.zig): the user's state diff --git a/core/replica.zig b/core/replica.zig index 8ca505ec..9edc96d2 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2995,6 +2995,13 @@ pub fn ended(why: []const u8) bool { /// 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 join's failure that is its session's running another build now: a +/// client whose own start path holds a newer one execs that instead +/// (runtime/client.zig). +pub fn otherBuild(why: []const u8) bool { + return std.mem.endsWith(u8, std.mem.sliceTo(why, 0), upgrade_words); +} + /// A replica client's join, the terminal's and the window's alike (their /// `runClient`): the session at `path` dialled (or, first, the one on its /// pair, `o.pair`), its build checked to be this @@ -3019,7 +3026,10 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o }; if (have) |h| h.left(); prev.deinit(); - } else j.* = 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; + } 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; defer { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 36243c71..60176642 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1444,6 +1444,8 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .done => {}, .gone => Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}), .failed => Said.no(io, "{s}", .{start.said()}), + // A new process's window: the build at its start path, exec'd. + .upgrade => |view| Said.no(io, "{s}", .{client_loop.execAgain(&start, view)}), .left => { var said: [600]u8 = undefined; std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes-gui: detached from '{s}', which is still running; come back with `pardes-gui attach {s}`\n", .{ to.session, to.session }) catch "pardes-gui: detached\n") catch {}; diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index bf051e9a..a3889d28 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -817,6 +817,12 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, screen.leave(); Said.no(io, "{s}", .{start.said()}); }, + .upgrade => |view| { + // The blink: its screen left, the build at its start path + // exec'd, which enters it again. + screen.leave(); + Said.no(io, "{s}", .{client_loop.execAgain(&start, view)}); + }, } } @@ -1315,6 +1321,10 @@ const Shell = struct { const core = s.core; var why: [800]u8 = undefined; const path = upgrade.exe() orelse return core.reportFailure(pane, "Upgrade: this process does not know the path it was started from"); + // A client of a session no daemon keeps is a pair's first (no other + // can attach): let go, it has no socket to find the session again + // by, and the session would end with its last link. + if (s.peers.count() > 0 and daemon.link < 0) return core.reportFailure(pane, "Upgrade: no daemon keeps this session, so its client could not join it again; this session stays as it is"); // 9P is served while the new binary is asked (its clients' clunks // and reads after the word are answered), and not after. pardes.turn.rest(); diff --git a/runtime/client.zig b/runtime/client.zig index 1949d618..329bdab3 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -37,6 +37,7 @@ const std = @import("std"); const libc = std.c; const pardes = @import("core"); const replica = pardes.replica; +const upgrade = @import("upgrade.zig"); /// A wait with no wake of its own: until an input or a message. pub const forever: u32 = std.math.maxInt(u32); @@ -92,6 +93,8 @@ pub const Start = struct { s.o.distrusted = &s.distrusted; // Its frame timings, this process's (Pardes.frames). s.o.frames = &s.frames; + // Exec'd at an Upgrade (`execAgain`): for the view it had. + if (init.environ_map.get("PARDES_JOIN_VIEW")) |v| s.o.join_view = std.fmt.parseInt(u8, v, 10) catch null; s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false; return true; } @@ -111,8 +114,44 @@ pub const Start = struct { /// How a client's joins ended: it quit, the session let it go, it could /// not join again (`Start.said`), it left the session running (a Detach in -/// it), or the session ended, with its code. -pub const Ended = union(enum) { done, gone, failed, left, ended: u8 }; +/// it), the session ended, with its code, or it runs another build now and +/// one is installed at this client's start path since it started: the +/// frontend lets its screen go and execs that, for this view (`execAgain`). +pub const Ended = union(enum) { done, gone, failed, left, ended: u8, upgrade: u8 }; + +/// An Upgrade's client again (`Ended.upgrade`), its frontend's screen let +/// go: this process exec'd as the file at its start path, an attach of the +/// same session through the same socket (a far one's forward, its ssh this +/// process's child still), joining for `view` (PARDES_JOIN_VIEW; far, +/// PARDES_REPLICA_FAR). The terminal's blink: no record crosses the exec. +/// Returns only when the exec failed: why. +pub fn execAgain(s: *Start, view: u8) []const u8 { + const path = upgrade.exe() orelse return "Upgrade: this client does not know the path it was started from"; + var at_buf: [4200]u8 = undefined; + const at = std.fmt.bufPrintSentinel(&at_buf, "--attach={s}:{s}", .{ s.dial.path, s.dial.session }, 0) catch return "Upgrade: the session's socket's path is too long to attach again by"; + const argv = [_:null]?[*:0]const u8{ path, at }; + var view_buf: [32]u8 = undefined; + const joining = std.fmt.bufPrintSentinel(&view_buf, "PARDES_JOIN_VIEW={d}", .{view}, 0) catch unreachable; // a u8 fits 32 + var n: usize = 0; + while (libc.environ[n] != null) n += 1; + const envp = s.gpa.allocSentinel(?[*:0]const u8, n + 2, null) catch return "Upgrade: no memory for this client's environment"; + var k: usize = 0; + for (libc.environ[0..n]) |e| { + const entry = std.mem.span(e.?); + if (std.mem.startsWith(u8, entry, "PARDES_JOIN_VIEW=") or std.mem.startsWith(u8, entry, "PARDES_REPLICA_FAR=")) continue; + envp[k] = e; + k += 1; + } + envp[k] = joining.ptr; + k += 1; + if (s.have != null) { + envp[k] = "PARDES_REPLICA_FAR=1"; + k += 1; + } + envp[k] = null; + _ = libc.execve(path, &argv, envp.ptr); + return "Upgrade: the build at this client's start path would not start"; +} pub fn Loop(comptime Frontend: type) type { return struct { @@ -142,6 +181,8 @@ pub fn Loop(comptime Frontend: type) type { /// the session let it go to upgrade; the frontend stays as it is. pub fn run(l: *L, s: *Start) !Ended { const link = &l.link; + // Its start path's file, for an Upgrade's question (`upgrade`). + upgrade.locate(); while (true) { link.reset(s.r, &s.joined); l.fe.start(l); @@ -177,8 +218,11 @@ pub fn Loop(comptime Frontend: type) type { } // The session again, the core it leaves held meanwhile: its // blobs are not sent again. - s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse - return if (replica.ended(&s.why)) .gone else .failed; + s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse { + if (replica.ended(&s.why)) return .gone; + if (replica.otherBuild(&s.why) and upgrade.replaced()) return .{ .upgrade = s.joined.view }; + return .failed; + }; } } diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig index dcc705a5..37d4890b 100644 --- a/runtime/upgrade.zig +++ b/runtime/upgrade.zig @@ -95,6 +95,14 @@ pub fn newer() bool { return true; } +/// Another file at its start path than the one it started from: a build +/// installed there since (`locate` first). A client's question at an +/// Upgrade (client.zig). +pub fn replaced() bool { + const was = started_inode orelse return false; + return (inodeOf(exe() orelse return false) orelse return false) != was; +} + pub fn exe() ?[:0]const u8 { return if (exe_len == 0) null else exe_buf[0..exe_len :0]; } diff --git a/test/daemon.py b/test/daemon.py index 1daebea8..26508a0b 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -649,12 +649,16 @@ def pair(binary): c2 = lone.spawn([binary, '--9p=lone', str(notes)], dict(env, PARDES_DAEMON='off')) nine2, session2 = session_of('lone') wait_for('the lone pair drawn', lambda: b'line 3 of the notes' in lone.text(), timeout=30) + # An Upgrade refused, with why: its client could not find it again. + nine2.write('/ctl', b'Upgrade\n') + wait_for('the Upgrade refused', lambda: 'its client could not join it again' in screen_text(nine2), timeout=20) + assert alive(session2) and c2.poll() is None nine2.socket.close() os.kill(c2.pid, signal.SIGKILL) c2.wait(timeout=10) wait_for('the lone session ended with its last link', lambda: not alive(session2), timeout=20) wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=20) - print(f'pair: with no daemon, client {c2.pid} killed outright, its session {session2} ended; nothing left behind') + print(f'pair: with no daemon, an Upgrade refused in words; client {c2.pid} killed outright, its session {session2} ended; nothing left behind') finally: for p in processes(root): try: @@ -2303,7 +2307,9 @@ def upgrades(old, new): # its terminal and its stub's pipe let go once its stub is told # (two fewer). def level(): - gone = 2 if b'detached from' in term2.text() else 1 + # (A client of a build that execs at an Upgrade comes back: its + # connection is the session's again.) + gone = 2 if b'detached from' in term2.text() else 0 if knows_exec(old) and stub2.poll() is None else 1 return fds_before[0] - 2 - gone + min(state_fds(session), 1) - state_before, fds_before[1] try: wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level(), timeout=5) @@ -2414,7 +2420,10 @@ def upgrades(old, new): install(new, binary) os.kill(session, signal.SIGKILL) stub.wait(timeout=10) - stub2.wait(timeout=10) + try: + stub2.wait(timeout=30) + except subprocess.TimeoutExpired: + raise AssertionError(('the second terminal\'s client on', cmdline(stub2.pid), term2.text()[-1200:])) client.socket.close() term.close() term2.close() -- cgit v1.3