diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 08:36:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 08:36:06 -0300 |
| commit | 446cde19a6b6fb7125fc749608cc12b7452dad6e (patch) | |
| tree | a7c2792ecdb1833a2e5a3ef99b9fb6ce96c01a00 /core | |
| parent | 2c2800c93e5417297dd06d3e52d5d336d975560c (diff) | |
| parent | ae3b9e9d19492c827f257da8a224a19d77de51c1 (diff) | |
| download | pardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.tar.gz pardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.zip | |
integration: a session's first client over an inherited socket before its first effects, the pair under PARDES_PAIR, and a session's last word, on the 9P run's fixes and the build's second pass: pardes-first-client (new --detach forks and returns; Options.pair and the join by fd; pardes FILE as a client and a session under the variable; ended CODE and left NAME: a client's own Detach exits 0 with its line again) and pardes-merge33, merged without a textual conflict; tui core-test 858/1013 (155 skipped), shell-test 1053/1056, the window builds; the coordinator's gate in the ledger (reorg/merge34)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/CHANGELOG.md | 9 | ||||
| -rw-r--r-- | core/Core.zig | 5 | ||||
| -rw-r--r-- | core/replica.zig | 196 | ||||
| -rw-r--r-- | core/root.zig | 2 |
4 files changed, 186 insertions, 26 deletions
diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 67fbdf69..faa02ade 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- `pardes new --detach[=NAME]` returns once the session runs (registered + and listening), printing its name, exit 0; the session runs on in a + session of its own. `pardes --detach=NAME` without `new` is still the + session itself, until it ends. - `pardes attach NAME` (and `pardes-gui attach NAME`) is always a client: a process of its own with its own view, whatever the session was started in. A session's own terminal or window is only the one it was @@ -9,8 +13,9 @@ back. Before, an attach to a detached terminal's session handed it this terminal (the attach waiting as its stub), and `pardes-gui attach` asked a detached window's session to open its own window again and exited 0. - A client's Detach ends it with "'NAME' let this client go" and exit 1, - where the stub said "detached from 'NAME'" and exited 0. Bare, `pardes + A client's Detach ends it with "detached from 'NAME', which is still + running" and exit 0, as the stub's did (the session's last word on a + link, `left`); a session's end, with its code (`ended`). Bare, `pardes attach` takes the one detached session, else the one session, of either kind; with several it names them all. diff --git a/core/Core.zig b/core/Core.zig index da1dc4b8..7ebcc020 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2279,6 +2279,11 @@ pub const Options = struct { startup_config: ?[]const u8 = null, ninep_name: []const u8 = "", ninep_tcp: ?[]const u8 = null, + /// This process's end of a socket pair its launch made (runtime/main.zig, + /// PARDES_PAIR): a session's first client is on it, taken before its + /// 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, 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 3f92603c..781bd211 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -1887,6 +1887,16 @@ pub const Peer = struct { peer.cond.signal(io); } + /// Its last message: queued, and its writer ends once it is written. + fn last(peer: *Peer, words: []const u8) void { + const io = peer.peers.io; + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + } + fn writeLoop(peer: *Peer) void { const io = peer.peers.io; defer peer.done.store(true, .release); @@ -2230,7 +2240,10 @@ pub const Peers = struct { } } - fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + /// A client on `joining.fd` joined: a dump of the session now, its + /// threads started. At a step boundary (`boundary`), or a session's + /// first client before its first effects (runtime/session.zig). + pub fn join(ps: *Peers, core: *Pardes, joining: Joining) void { const fd = joining.fd; const t0 = nowNs(); defer { @@ -2401,14 +2414,14 @@ pub const Peers = struct { if (ps.count() == 0) return; var out: [600]u8 = undefined; const words = wire.encodeServer(&out, .{ .refused = why }) catch return; - const io = ps.io; - for (ps.list) |slot| if (slot) |peer| { - peer.send(&.{words}); - peer.lock.lockUncancelable(io); - peer.closing = true; - peer.cond.signal(io); - peer.lock.unlock(io); - }; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + // Each joins again for its view, kept in the dump (views.away). + ps.drop(core, true); + } + + /// What each client was sent goes first, for up to 200 ms. + fn flushed200(ps: *Peers) void { const until = nowNs() + 200 * std.time.ns_per_ms; while (nowNs() < until) { const busy = for (ps.list) |slot| { @@ -2418,8 +2431,6 @@ pub const Peers = struct { const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } - // Each joins again for its view, kept in the dump (views.away). - ps.drop(core, true); } /// A join's dump and pins let go: written, or its client gone. @@ -2442,16 +2453,26 @@ pub const Peers = struct { daemon.clients(pids[0..n]); } - /// Detach typed in client view `k`'s: its link ends (its reader says - /// it is gone, and the next boundary lets it go). + /// Detach typed in client view `k`'s: told it left the session running + /// (`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) { - _ = libc.shutdown(peer.fd, 2); + 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; }; return false; } + /// The session ends with `code`: every client told so (`ended`), its + /// last word, before the links are let go (`dropAll`). + pub fn ended(ps: *Peers, code: u8) void { + var out: [16]u8 = undefined; + const words = wire.encodeServer(&out, .{ .ended = code }) catch unreachable; + for (ps.list) |slot| if (slot) |peer| peer.last(words); + ps.flushed200(); + } + fn stop(ps: *Peers, core: *Pardes) void { if (!ps.recording) return; pardes.panes.resizeDeferred(core); @@ -2575,13 +2596,14 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha } /// `dial`'s, on a connection made: the hello, then the join. The -/// connection is closed if the join fails. -fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { +/// 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 { var out: [wire.hello_max + 16]u8 = undefined; - const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { + const hello = if (session) |name| wire.encodeClient(&out, .{ .hello = .{ .session = name, .view = view } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); - }; + } else out[0..0]; // Its first word, in the same write: whether it is far. const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} }; @@ -2914,7 +2936,7 @@ pub fn bye(fd: c_int, again: bool) void { /// What the session streams a client after its dump, as its frontend's loop /// takes it: a unit's and a terminal message's bytes and a refusal's words /// on the C heap, the loop's to free. -pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8 }; +pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8, blob: []u8, ended: u8, left }; /// The session's stream off `fd`, each message handed to `post` until the /// link ends (a refusal ends it too); `post` says whether it took it (one @@ -2929,13 +2951,16 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime .blob => .{ .blob = std.heap.c_allocator.dupe(u8, payload) catch return false }, .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check }, .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false }, + .ended => .{ .ended = (wire.decodeServer(tag, payload) catch return false).ended }, + .left => .left, .pong, .welcome, .dump, .blobs, .zstd => return true, }; if (!post(x, msg)) switch (msg) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, + .checked, .ended, .left => {}, }; - return msg != .refused; + // A last word ends the stream. + return msg == .unit or msg == .screen or msg == .checked or msg == .blob; } }; rest.run(gpa, fd, ctx, Take.take); @@ -2960,7 +2985,8 @@ pub fn ended(why: []const u8) bool { 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 -/// `runClient`): the session at `path` dialled, its build checked to be this +/// `runClient`): the session at `path` dialled (or, first, the one on its +/// pair, `o.pair`), 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 @@ -2982,7 +3008,7 @@ 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, dial(gpa, path, session, why, have, null) orelse return null, why) orelse return null; + } 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; defer { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; @@ -3084,6 +3110,9 @@ pub const Link = struct { visible: u32 = 0, /// What the session said as it let this client go (the C heap's). refused: ?[]u8 = null, + /// Its last word: it ended, with this code; or this client left it. + ended: ?u8 = null, + left: bool = false, /// A line over its frame, until its next key (`notify`): why it joined /// again. The client's own, never the session's core. notice_buf: [240]u8 = undefined, @@ -3202,6 +3231,14 @@ pub const Link = struct { c.refused = words; c.quit = true; }, + .ended => |code| { + c.ended = code; + c.quit = true; + }, + .left => { + c.left = true; + c.quit = true; + }, } if (c.r.diverged != null) c.quit = true; } @@ -4408,6 +4445,119 @@ test "bytes: a client joins from a dump with its terminals whole, and every side try sameTerminals(b.a, late); } +test "a session's first client on the socket it starts with (Options.pair), joined there with no hello before the session's first effects: the boot shell's spawn and first bytes reach it and the digests agree" { + try firstOnPair(false); +} + +test "bytes: a session's first client on its pair, taken before its first effects, has the boot shell's first bytes with no reload of them (the session's one at the join is of its emulator before any byte, the client's none); a client after them makes every side reload" { + try firstOnPair(true); +} + +/// 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 +/// checked. Bytes: the client loaded as the soak's bytes clients are +/// (`replica.join` loads the screen-state kind until the switch). +fn firstOnPair(bytes: bool) !void { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const Spawns = struct { + fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void { + const n: *u32 = @ptrCast(@alignCast(ctx.?)); + n.* += 1; + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .spawn = spawn }; + }; + var spawns: u32 = 0; + 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, .boot = .tty }; + const core = try Pardes.init(gpa, opts); + defer core.deinit(); + core.host = .{ .ctx = &spawns, .vtable = &Spawns.vtable }; + var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .name = "pair", .bytes = bytes, .detached = true }; + defer ps.deinit(); + defer live_peers = null; + defer ps.dropAll(core); + const reloads = pardes.Term.reloads.n; + ps.join(core, .{ .fd = fds[0] }); + 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; + defer gpa.free(j.dump); + const r = try Replica.initDump(gpa, j.client, j.view, j.dump, opts); + r.t.screen_state = false; + r.core().echo = null; + break :r r; + } else r: { + var o = opts; + o.pair = fds[1]; + break :r join(gpa, &j, "", "", o, null, &why, null) orelse { + std.debug.print("the join failed: {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.expect(r.loaded(j.digest)); + // The first effects, the boot shell's spawn among them; its first bytes. + while (core.nextEffect()) |e| core.perform(e); + try std.testing.expectEqual(@as(u32, 1), spawns); + const shell: u8 = for (core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + core.update(.{ .output = .{ .pane = .at(shell), .bytes = "first$ " } }); + ps.boundary(core); + // Its check, answered after every unit and terminal message before it. + try std.testing.expect(askCheck(j.fd)); + while (!ps.mail.load(.acquire)) std.Thread.yield() catch {}; + ps.boundary(core); + const Take = struct { + r: *Replica, + fd: c_int, + digest: ?u64 = null, + fn take(t: *@This(), tag: u8, payload: []const u8) bool { + switch (std.enums.fromInt(wire.ServerTag, tag) orelse return false) { + .unit => t.r.confirm(payload), + .screen => if (t.r.screen(payload)) |reply| { + _ = sendScreen(t.fd, reply); + }, + .check => { + t.digest = (wire.decodeServer(tag, payload) catch return false).check; + return false; + }, + else => {}, + } + return true; + } + }; + var take: Take = .{ .r = r, .fd = j.fd }; + j.rest.run(gpa, j.fd, &take, Take.take); + r.settle(); + if (r.diverged) |w| std.debug.print("diverged: {s}\n", .{w}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(take.digest.?, r.digest()); + // Bytes: the session's reload at the join, of an emulator that held + // nothing yet (Pardes.init made it, the spawn is a first effect); the + // client loaded it from the dump and replayed every byte since. + try std.testing.expectEqual(reloads + @intFromBool(bytes), pardes.Term.reloads.n); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const rows = try pardes.Term.viewport(r.core().panes[shell].?.terminal.?, arena.allocator(), false); + try std.testing.expect(std.mem.indexOf(u8, rows[0], "first$") != null); + if (!bytes) return; + // One that comes after the shell's bytes: every side reloads. + var late: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &late) != 0) return error.NoSocket; + defer _ = libc.close(late[1]); + ps.join(core, .{ .fd = late[0] }); + try std.testing.expectEqual(reloads + 2, pardes.Term.reloads.n); +} + test "bytes: a session's output past a unit's bound is cut into units under it, between two reads" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; diff --git a/core/root.zig b/core/root.zig index 9509439a..5300ce93 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", 70, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, + .{ "replica.zig", 73, "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" }, |
