diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 04:25:37 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 04:50:24 -0300 |
| commit | 821509e5cbe47e73ce4f4fcdc5d092f3c5e1c986 (patch) | |
| tree | a5801e80b4a9b1bf22462752723f45d5f3df53f0 | |
| parent | 2fadb864051f5afc062606a898d821b702a1692c (diff) | |
| download | pardes-821509e5cbe47e73ce4f4fcdc5d092f3c5e1c986.tar.gz pardes-821509e5cbe47e73ce4f4fcdc5d092f3c5e1c986.zip | |
first client: a session takes a client on a socket it was started with BEFORE its first effects, by the join a later client gets (Peers.join, now pub, called from runtime/session.zig between the frontend's open and the first effects when Options.pair is set), and a client's first join is made on its end of the pair with no hello (replica.join reads Options.pair; joinOn's session name optional), so a first client has every terminal from its first byte (P32); two tests through the replica's surface, a session core with a boot shell and real Peers threads over a socket pair, the first effects after the join: screen state (replica.join end to end) and bytes (loaded as the soak's bytes clients): the spawn taped, the shell's first bytes in the client's terminal, the digests equal at the client's check, and under bytes no reload of any byte (the session's one reload at the join is of the boot shell's emulator before anything was written, the client's none; a client after the bytes: every side reloads); core/root.zig's machine-lines count for replica.zig 70 -> 74 (the tests' sockets)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | core/Core.zig | 5 | ||||
| -rw-r--r-- | core/replica.zig | 132 | ||||
| -rw-r--r-- | core/root.zig | 2 | ||||
| -rw-r--r-- | runtime/session.zig | 4 |
4 files changed, 135 insertions, 8 deletions
diff --git a/core/Core.zig b/core/Core.zig index 9a542940..c3fa6953 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2273,6 +2273,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 14757d9a..bf665a50 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2230,7 +2230,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 { @@ -2576,13 +2579,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 = &.{} }; @@ -2950,7 +2954,8 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime 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 @@ -2971,7 +2976,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 = &.{}; @@ -4389,6 +4394,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 a57934a6..a58a25b5 100644 --- a/core/root.zig +++ b/core/root.zig @@ -629,7 +629,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", 74, "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" }, diff --git a/runtime/session.zig b/runtime/session.zig index 6b359eef..d34a8ea9 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -103,6 +103,10 @@ pub fn Loop(comptime Frontend: type) type { defer l.peers.deinit(); // Before the core goes: each client sees its link end. defer l.peers.dropAll(l.core); + // A first client on the socket it was started with, before its + // first effects: it has every terminal from its first byte, and + // no reload (Core.openView). + if (o.pair) |fd| l.peers.join(l.core, .{ .fd = fd }); l.effects(); // ...and its shells, once a fresh session's own has a directory // to adopt them in. |
