diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 21:00:30 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 21:53:52 -0300 |
| commit | d00c40694dc239854263616f710b4a197d25d145 (patch) | |
| tree | 3e7a980510e6c37507e0b60f1632cefc67d1c364 /src | |
| parent | e6a114eed043dd1ca50be1cab912662fa9150fda (diff) | |
| download | pardes-d00c40694dc239854263616f710b4a197d25d145.tar.gz pardes-d00c40694dc239854263616f710b4a197d25d145.zip | |
views: a client that joins again has its own view back, layout, focus and the panes opened in it: an Upgrade's clients and one whose bye says it joins again (a divergence's, wire 19's `bye.again`) leave their view kept for them (`views_away`, digested and dumped, so an Upgrade's new build has it; taped `awayView`), the client names it in its next hello (`Hello.view`, handed on by the daemon with the connection) and its join takes it back (taped `rejoinView`) rather than a copy of the session's; the live view is never a kept one, `views.count` counts the others, and a join with every view taken lets one go (every Upgrade made each client's own panes leave its view: the hunt's 80 ms cell lost `+Search` and `+New`)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/daemon.zig | 14 | ||||
| -rw-r--r-- | src/detached/wire.zig | 29 | ||||
| -rw-r--r-- | src/gui/gui.zig | 13 | ||||
| -rw-r--r-- | src/pardes.zig | 31 | ||||
| -rw-r--r-- | src/replica.zig | 138 | ||||
| -rw-r--r-- | src/tape.zig | 4 | ||||
| -rw-r--r-- | src/tty/tty.zig | 25 | ||||
| -rw-r--r-- | src/views.zig | 83 |
8 files changed, 263 insertions, 74 deletions
diff --git a/src/daemon.zig b/src/daemon.zig index 467af8b8..b93f5d43 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -379,10 +379,13 @@ pub const Note = union(enum) { /// stub's pipe; for a window session, both -1: open a window. attach: struct { tty: c_int, stub: c_int }, adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize }, - /// A replica's connection, its hello taken (src/replica.zig `Peers`). - replica: c_int, + /// A replica's connection, its hello taken (src/replica.zig `Peers`), + /// and the view it joins again for, if it does. + replica: Joining, }; +pub const Joining = struct { fd: c_int, view: ?u8 = null }; + /// Hands every note the daemon sends on to `deliver`, on a thread of its /// own, until the link ends (`quit`). Called each loop: started once. pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { @@ -409,7 +412,8 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no } }); for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); } else if (std.mem.eql(u8, verb, "replica")) { - if (got.nfds >= 1) deliver(ctx, .{ .replica = got.fds[0] }); + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); } else if (std.mem.eql(u8, verb, "adopted")) { var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; @@ -951,7 +955,9 @@ const Daemon = struct { } else return refuse(r, std.fmt.bufPrint(&why, "no session called '{s}'", .{h.session}) catch "no such session"); const t = &d.conns[target]; if (t.semantics != h.semantics) return refuse(r, std.fmt.bufPrint(&why, "session '{s}' runs another build of pardes (semantics {x:0>16}, this client's {x:0>16}): a replica needs the same build at both ends, as `pardes --version` shows", .{ h.session, t.semantics, h.semantics }) catch "another build"); - if (!send(t.fd, "replica", &.{r.fd})) { + var words: [16]u8 = undefined; + const note = if (h.view) |k| std.fmt.bufPrint(&words, "replica {d}", .{k}) catch unreachable else "replica"; + if (!send(t.fd, note, &.{r.fd})) { d.hangUp(@intCast(target)); return refuse(r, "that session does not answer"); } diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 6090dedb..337211b7 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,6 +9,8 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); +/// 19: a client joining again names the view it had in its hello, and a +/// bye says whether it joins again for it (views.away); /// 18: a replica's input is stamped with its guess of the session's /// clocks, the time its step reads (phase 6's instant interaction notes); /// 17: a pointer's hit carries the place it was made on (Layer.Place) and @@ -22,7 +24,7 @@ const config = @import("pardes_config"); /// part B); 14: the hello names a session and /// carries the semantics hash, and a ping is answered (phase 5's /// groundwork); 13: the frames went (phase 3). -pub const version: u16 = 18; +pub const version: u16 = 19; /// The terminal's build and the window's step alike (`semantics`). const family = switch (config.platform) { @@ -134,12 +136,15 @@ pub const Hello = struct { semantics: u64 = semantics, /// Which session (its 9P name, as `--attach=` names it). session: []const u8, + /// The view it had, joining again for it (a divergence's rejoin, an + /// Upgrade's): the daemon hands it on with the connection. 0xff: none. + view: ?u8 = null, }; /// A session's name on the wire, as the daemon keeps it. pub const max_name = 128; /// The longest hello there is, header and all. -pub const hello_max = header_len + 2 + 8 + 2 + max_name; +pub const hello_max = header_len + 2 + 8 + 2 + max_name + 1; /// Daemon or session -> client. pub const ServerTag = enum(u8) { @@ -268,7 +273,8 @@ pub fn helloVersion(payload: []const u8) Error!u16 { /// one call and no longer. pub const ClientMsg = union(enum) { hello: Hello, - bye, + /// The client's end: `again`, it joins again for its view at once. + bye: struct { again: bool = false }, /// A round trip's probe, answered `pong` with the same value. ping: u64, screen_ack: []const u8, @@ -498,8 +504,9 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 { try w.writeInt(u64, h.semantics, .little); if (h.session.len > max_name) return error.Overlong; try putSlice16(&w, h.session); + try w.writeByte(h.view orelse 0xff); }, - .bye => {}, + .bye => |b| try w.writeByte(@intFromBool(b.again)), .ping => |v| try w.writeInt(u64, v, .little), .screen_ack, .want => |bytes| try w.writeAll(bytes), .check => {}, @@ -573,8 +580,12 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { if (n > max_name) return error.BadValue; break :blk try r.take(n); }, + .view = switch (try r.takeByte()) { + 0xff => null, + else => |k| k, + }, } }, - .bye => .bye, + .bye => .{ .bye = .{ .again = try getBool(&r) } }, .ping => .{ .ping = try r.takeInt(u64, .little) }, .screen_ack => return .{ .screen_ack = payload }, .check => .check, @@ -693,7 +704,8 @@ test "detached wire: every Event variant round-trips" { try testing.expectEqual(semantics, got.hello.semantics); try testing.expectEqualStrings("work", got.hello.session); } - try testing.expectEqual(ClientMsg.bye, try roundClient(&buf, .bye)); + try testing.expect((try roundClient(&buf, .{ .bye = .{ .again = true } })).bye.again); + try testing.expectEqual(@as(?u8, 2), (try roundClient(&buf, .{ .hello = .{ .session = "work", .view = 2 } })).hello.view); try testing.expectEqual(@as(u64, 0xfeed_beef_0000_0001), (try roundClient(&buf, .{ .ping = 0xfeed_beef_0000_0001 })).ping); { // The other way: framed the same, its tag the ServerTag's number. @@ -920,8 +932,9 @@ test "detached wire: values a field cannot mean are refused" { std.mem.writeInt(u16, hello[10..12], max_name + 1, .little); try testing.expectError(error.BadValue, decodeClient(@backingInt(ClientTag.hello), &hello)); std.mem.writeInt(u16, hello[10..12], max_name, .little); - try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), hello[0 .. hello.len - 1])).hello.session.len); - try testing.expect(header_len + hello.len - 1 == hello_max); + hello[hello.len - 1] = 0xff; + try testing.expectEqual(@as(usize, max_name), (try decodeClient(@backingInt(ClientTag.hello), &hello)).hello.session.len); + try testing.expect(header_len + hello.len == hello_max); } // A NaN scroll distance. `pinch` multiplies into a zoom the pane keeps. { diff --git a/src/gui/gui.zig b/src/gui/gui.zig index dd21680f..d3acf454 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2752,7 +2752,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ 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) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + 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); @@ -2878,7 +2878,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(joined.fd); + if (!link.gone) replica.bye(joined.fd, r.diverged != null); // The reader ends with the link: what it took meanwhile is let go. _ = libc.shutdown(joined.fd, 2); reader.join(); @@ -2901,7 +2901,7 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ // 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) orelse Said.no(io, "could not join again: {s}", .{std.mem.sliceTo(&why, 0)}); + 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)}); @@ -4553,13 +4553,14 @@ const Shell = struct { } /// An upgraded session's views, a window each again: the Gui's is the - /// live view's, the others open as `--attach` opens one. + /// live view's, the others open as `--attach` opens one; a replica + /// client's, kept for its rejoin (views.away), none. fn resumeWindows(s: *Shell) void { const live = s.core.view; s.gview = live; for (0..pardes.views.max) |i| { const k: u8 = @intCast(i); - if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + if (k != live and pardes.views.isOpen(s.core, k) and !pardes.views.isAway(s.core, k)) s.openAnotherWindow(k); s.windowed |= @as(u8, 1) << @intCast(live); } s.core.toView(live); @@ -5362,7 +5363,7 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } }); }, // Joined at the next step boundary (Peers, any thread). - .replica => |fd| s.peers.handed(fd), + .replica => |j| s.peers.handed(j.fd, j.view), } } diff --git a/src/pardes.zig b/src/pardes.zig index 22f446a5..edab4a58 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -4732,6 +4732,8 @@ pub const Pardes = struct { /// there are, one a window or terminal; each not live is in `parked`. view: u8 = 0, views_open: u8 = 1, + /// The open views kept for their clients' rejoin (views.away). + views_away: u8 = 0, parked: [views.max]?*views.Parked = @splat(null), /// The views no frontend here draws: a replica client's, which its own /// process draws (src/replica.zig). A pump skips them. @@ -5706,6 +5708,14 @@ pub const Pardes = struct { pub fn openView(p: *Pardes) ?u8 { const taped = tape.enter(p, .open_view); defer tape.leave(p, taped); + // Every view taken: one kept for a client that has not come back + // for it (views.away) goes first. ponytail: no timer lets one go; + // until its place is wanted it costs a parked slot, and a terminal + // it sized last keeps that size until another view types into it. + if (@popCount(p.views_open) == views.max and p.views_away != 0) { + views.close(p, @ctz(p.views_away)); + p.sync(); + } // What was queued since the last pump (a leaving client's view // closed, a 9P request's step) done first, as every client of the // tape does it here: a join dumps the session next, and a core @@ -5740,6 +5750,27 @@ pub const Pardes = struct { p.settle(); } + /// The client of view `k` gone, to join again for it (an Upgrade's + /// rejoin, a divergence's): its view kept as it is (views.away). + pub fn awayView(p: *Pardes, k: u8) void { + const taped = tape.enter(p, .{ .away_view = k }); + defer tape.leave(p, taped); + views.away(p, k); + } + + /// A client back for view `k`, kept for it (`awayView`): its own again, + /// as it left it; false when it was let go meanwhile. Settled as + /// `openView` is, for the join's dump next. + pub fn rejoinView(p: *Pardes, k: u8) bool { + const taped = tape.enter(p, .{ .rejoin_view = k }); + defer tape.leave(p, taped); + if (!views.isAway(p, k)) return false; + p.views_away &= ~(@as(u8, 1) << @intCast(k)); + p.settle(); + p.needs_frame = true; + return true; + } + /// Into view `k`, before the input its client sent. pub fn toView(p: *Pardes, k: u8) void { if (k == p.view) return; diff --git a/src/replica.zig b/src/replica.zig index da0c3e64..4f84712f 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1414,6 +1414,9 @@ pub const Peer = struct { fd: c_int, id: u16, view: u8, + /// 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, /// Its number in the session's `Screens`. sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). @@ -1626,7 +1629,11 @@ pub const Peer = struct { fn take(peer: *Peer, tag: u8, payload: []const u8) bool { const t = std.enums.fromInt(wire.ClientTag, tag) orelse return false; switch (t) { - .hello, .bye => return false, + .hello => return false, + .bye => { + peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + return false; + }, .ping => { var out: [16]u8 = undefined; const v = (wire.decodeClient(tag, payload) catch return false).ping; @@ -1747,12 +1754,14 @@ pub const Peers = struct { msgs: std.ArrayListUnmanaged([]u8) = .empty, /// Clients waiting to join while the session's texts are hashed /// (`joins`), the texts and the thread hashing them. - held: std.ArrayListUnmanaged(c_int) = .empty, + held: std.ArrayListUnmanaged(Joining) = .empty, texts: std.ArrayListUnmanaged([]const u8) = .empty, hasher: ?std.Thread = null, rounds: u8 = 0, - pub const Note = union(enum) { join: c_int, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; + pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer, hashed }; + /// A connection to join, and the view it joins again for, if it does. + pub const Joining = struct { fd: c_int, view: ?u8 = null }; pub const Msg = struct { peer: *Peer, msg: []u8 }; pub fn count(ps: *const Peers) usize { @@ -1766,7 +1775,7 @@ pub const Peers = struct { ps.inbox.append(ps.gpa, note) catch { ps.lock.unlock(ps.io); switch (note) { - .join => |fd| _ = libc.close(fd), + .join => |j| _ = libc.close(j.fd), .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check, .joined, .hashed => {}, } @@ -1778,9 +1787,10 @@ pub const Peers = struct { } /// A replica's connection the daemon handed over (its hello taken - /// there): joined at the next step boundary. Any thread. - pub fn handed(ps: *Peers, fd: c_int) void { - ps.post(.{ .join = fd }); + /// there), and the view its hello says it had: joined at the next step + /// boundary. Any thread. + pub fn handed(ps: *Peers, fd: c_int, view: ?u8) void { + ps.post(.{ .join = .{ .fd = fd, .view = view } }); } /// What was recorded since the last unit, to every client as one, its @@ -1824,9 +1834,9 @@ pub const Peers = struct { ps.mail.store(false, .release); ps.lock.unlock(ps.io); for (ps.taken.items) |note| switch (note) { - .join => |fd| { - ps.held.append(ps.gpa, fd) catch { - refuse(fd, "out of memory"); + .join => |j| { + ps.held.append(ps.gpa, j) catch { + refuse(j.fd, "out of memory"); continue; }; ps.joins(core); @@ -1922,7 +1932,7 @@ pub const Peers = struct { } } ps.rounds = 0; - for (ps.held.items) |fd| ps.join(core, fd); + for (ps.held.items) |j| ps.join(core, j); ps.held.clearRetainingCapacity(); } @@ -1939,7 +1949,8 @@ pub const Peers = struct { ps.texts.clearRetainingCapacity(); } - fn join(ps: *Peers, core: *Pardes, fd: c_int) void { + fn join(ps: *Peers, core: *Pardes, joining: Joining) void { + const fd = joining.fd; const t0 = nowNs(); defer { ps.join_ns = @max(ps.join_ns, nowNs() - t0); @@ -1964,7 +1975,13 @@ pub const Peers = struct { } // Someone looks at it now: its messages go by keys, not the clock. core.unattended = false; - const k = core.openView() orelse { + // Its own view again, kept for it since it left (views.away), or one + // new. ponytail: a divergence's redial that came before its old + // link's end (posted by that link's reader, microseconds after the + // bye; the redial is a dial and the daemon's hand-over later) finds + // its view not kept yet, and takes a new one. + const back = if (joining.view) |v| core.rejoinView(v) else false; + const k = (if (back) joining.view else core.openView()) orelse { core.unattended = ps.count() == 0 and ps.alone_unattended; if (ps.count() == 0) ps.stop(core); return refuse(fd, "that session has as many views as it takes"); @@ -2035,7 +2052,7 @@ pub const Peers = struct { log.info("client {d} left '{s}'", .{ peer.id, ps.name }); ps.screens.dropPeer(peer.sid); core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - ps.closeViewOf(core, peer); + ps.closeViewOf(core, peer, peer.again); if (ps.count() == 0) core.unattended = ps.alone_unattended; ps.seq += 1; ps.unit(0, ps.seq); @@ -2043,11 +2060,12 @@ pub const Peers = struct { if (ps.count() == 0) ps.stop(core); } - /// A gone client's view closed (its slot let go already): the last one - /// of a session no one else watches leaves the session its layout, for - /// its rejoin (an Upgrade's, a diverged one's) to find. - fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer) void { - if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view); + /// A gone client's view closed (its slot let go already), or kept for + /// it (`again`: it joins again for it, views.away): the last one of a + /// session no one else watches leaves the session its layout, for a + /// client that comes after to find. + fn closeViewOf(ps: *Peers, core: *Pardes, peer: *Peer, again: bool) void { + if (again) core.awayView(peer.view) else if (ps.count() == 0 and ps.alone_unattended) core.leaveView(peer.view) else core.closeView(peer.view); } /// Its threads ended and joined, its link closed. @@ -2114,7 +2132,8 @@ pub const Peers = struct { const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; _ = libc.nanosleep(&ts, null); } - ps.dropAll(core); + // 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. @@ -2149,21 +2168,26 @@ pub const Peers = struct { /// Every client let go (the session's end, a Restore): each sees its /// link end. pub fn dropAll(ps: *Peers, core: *Pardes) void { + ps.drop(core, false); + } + + /// ...each view kept for its client if `again` (an Upgrade's). + fn drop(ps: *Peers, core: *Pardes, again: bool) void { ps.unhashedDone(); - for (ps.held.items) |fd| _ = libc.close(fd); + for (ps.held.items) |j| _ = libc.close(j.fd); ps.held.clearRetainingCapacity(); ps.rounds = 0; for (&ps.list) |*slot| if (slot.*) |peer| { slot.* = null; core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); - ps.closeViewOf(core, peer); + ps.closeViewOf(core, peer, again); ps.screens.dropPeer(peer.sid); ps.letGo(peer); }; ps.stop(core); ps.lock.lockUncancelable(ps.io); for (ps.inbox.items) |note| switch (note) { - .join => |fd| _ = libc.close(fd), + .join => |j| _ = libc.close(j.fd), .input, .screen => |in| ps.gpa.free(in.msg), .gone, .check, .joined, .hashed => {}, }; @@ -2238,8 +2262,9 @@ pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; /// Connected to the replica socket at `path`, its hello said for /// `session`, welcomed, and the dump read whole: what a client joins with. /// A far client (`have`) is sent its dump's blobs by hash first, and asks -/// for those it holds nowhere. Errors are said in `why`. -pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have) ?Joined { +/// for those it holds nowhere. Errors are said in `why`. `view`: the one +/// it had, joining again for it. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have, view: ?u8) ?Joined { const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); if (fd < 0) return said(why, "no socket", .{}); var addr: libc.sockaddr.un = .{ .path = @splat(0) }; @@ -2252,14 +2277,14 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, ha _ = libc.close(fd); return said(why, "nothing answers at {s}", .{path}); } - return joinOn(gpa, fd, session, why, have); + return joinOn(gpa, fd, session, why, have, view); } /// `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) ?Joined { +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 } }) catch { + const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session, .view = view } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); }; @@ -2690,10 +2715,10 @@ pub fn askCheck(fd: c_int) bool { return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); } -/// The client's last word. -pub fn bye(fd: c_int) void { +/// The client's last word: `again`, it joins again for its view at once. +pub fn bye(fd: c_int, again: bool) void { var out: [16]u8 = undefined; - if (wire.encodeClient(&out, .bye)) |b| _ = writeAll(fd, b) else |_| {} + if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} } /// What follows the dump on the client's connection, each message handed @@ -2985,13 +3010,14 @@ pub const Link = struct { /// `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). - pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have, patient: bool) ?Joined { + /// `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)) |j| { + if (dial(gpa, path, session, why, have, view)) |j| { if (j.semantics == wire.semantics) return j; - bye(j.fd); + bye(j.fd, false); _ = libc.close(j.fd); var jj = j; jj.rest.deinit(gpa); @@ -3013,7 +3039,7 @@ pub const Link = struct { 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) == null); + try std.testing.expect(Link.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); } @@ -3777,7 +3803,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; const Serve = struct { fn run(p: *Peer) void { - var hello: [wire.header_len + 13]u8 = undefined; + var hello: [wire.header_len + 14]u8 = undefined; var got: usize = 0; while (got < hello.len) { const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); @@ -3791,7 +3817,7 @@ fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); defer ps.letGo(peer); var why: [300]u8 = @splat(0); - return joinOn(std.testing.allocator, fds[1], "t", &why, have) orelse { + return joinOn(std.testing.allocator, fds[1], "t", &why, have, null) orelse { std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); return error.JoinFailed; }; @@ -4059,6 +4085,44 @@ test "a client leaves, and one joins: what the leave queued (a terminal's resize try agrees(a, joiner); } +test "a client that diverged joins again for its view: kept for it meanwhile, its pane in it again; the other client agrees" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const old = try Replica.init(gpa, 1, first, false); + defer old.deinit(); + const opened = try a.openView(2); + old.confirm(opened.unit); + var buf: [256]u8 = undefined; + old.confirm(try a.take(try inputOf(&buf, 2, 1, .{ .command = "New" }))); + const made = a.core.active; + // Its bye says it joins again: its view is kept, as every replica keeps it. + var was = Stepping.begin(&a.t, null); + a.core.awayView(opened.view); + was.end(); + old.confirm(try a.unit(0, 100)); + try agrees(a, old); + // Back for it: its own, its pane in it, in the dump it loads. + was = Stepping.begin(&a.t, null); + try std.testing.expect(a.core.rejoinView(opened.view)); + was.end(); + old.confirm(try a.unit(0, 101)); + const back = try joinedNow(a, 3, opened.view); + defer back.deinit(); + try std.testing.expectEqual(digestOf(a), back.digest()); + a.view_of[3] = opened.view; + const u = try gpa.dupe(u8, try a.take(try back.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + defer gpa.free(u); + old.confirm(u); + back.confirm(u); + try std.testing.expectEqual(made, a.core.active); + try std.testing.expect(pardes.layout.findPane(a.core, made) != null); + try agrees(a, old); + try agrees(a, back); +} + /// An input of a replica's in the session's order, as `Peers.input` takes /// it: the terminals flushed to every peer, the step, the rows it read that /// each peer holds as holes (`fill`), then its unit to every replica. diff --git a/src/tape.zig b/src/tape.zig index 4759d598..74576eed 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -67,6 +67,8 @@ pub const Input = union(enum) { open_view, close_view: u8, leave_view: u8, + away_view: u8, + rejoin_view: u8, to_view: u8, /// What a window says of its font and its opacity (gui.zig). take_font_request, @@ -630,6 +632,8 @@ pub const Tape = struct { .open_view => _ = p.openView(), .close_view => |k| p.closeView(k), .leave_view => |k| p.leaveView(k), + .away_view => |k| p.awayView(k), + .rejoin_view => |k| _ = p.rejoinView(k), .to_view => |k| p.toView(k), .take_font_request => _ = p.takeFontRequest(), .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit), diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 430c020d..4c054021 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -153,7 +153,7 @@ pub const Command = struct { files_changed, fs_ready, /// A replica's connection, handed over by the daemon (Peers). - replica: posix.fd_t, + replica: daemon.Joining, /// A replica's reader posted: the step boundary takes it in. replica_mail, /// A replica client's: what the session streamed (replica.Link). @@ -1333,7 +1333,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se 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) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + 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); @@ -1445,7 +1445,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se if (r.diverged != null) link.quit = true; } if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(joined.fd); + if (!link.gone) replica.bye(joined.fd, r.diverged != null); // The link's reader ends with the link, posting as it goes: what it // and the terminal posted is let go until it has. Its end is read // before the drain, not after: its last post (`link_gone`) comes @@ -1482,7 +1482,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se // 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) orelse { + 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)}); }; @@ -1916,8 +1916,8 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { }; loop.postEvent(.{ .adopted = .{ .pid = 0, .fd = -1 } }) catch {}; }, - .replica => |fd| loop.postEvent(.{ .replica = fd }) catch { - _ = libc.close(fd); + .replica => |j| loop.postEvent(.{ .replica = j }) catch { + _ = libc.close(j.fd); }, } } @@ -2338,7 +2338,7 @@ const Shell = struct { /// An upgraded session's terminals, each its view's again (`primary`, /// run's, entered already); in a fresh session each but the primary a /// view of its own, as `--attach` makes one. A view left with none - /// closes. + /// closes, but a replica client's, kept for its rejoin (views.away). fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { if (comptime builtin.is_test) unreachable; const core = s.core; @@ -2348,8 +2348,11 @@ const Shell = struct { } s.terms = @splat(null); if (rec.nttys == 0) { - pardes.views.only(core); + // The live view alone (never one kept for a rejoin) is the + // session's own. + for (0..pardes.views.max) |k| if (k != core.view and pardes.views.isOpen(core, @intCast(k)) and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); s.terms[core.view] = primary; + primary.feed.view = core.view; return; } for (rec.ttys[0..rec.nttys]) |t| { @@ -2386,7 +2389,7 @@ const Shell = struct { sc.feed.view = t.view; s.terms[t.view] = sc; } - for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null) core.closeView(@intCast(k)); + for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null and !pardes.views.isAway(core, @intCast(k))) core.closeView(@intCast(k)); if (s.terms[core.view] == null) s.terms[core.view] = primary; } @@ -2623,8 +2626,8 @@ const Shell = struct { }, .attach => |a| s.attachTerminal(a.tty, a.stub, null), .adopted => |a| s.adoptShell(a.pid, a.fd), - .replica => |fd| { - s.peers.handed(fd); + .replica => |j| { + s.peers.handed(j.fd, j.view); s.input_seen = true; }, .replica_mail => s.input_seen = true, diff --git a/src/views.zig b/src/views.zig index ac274621..58d7a1f7 100644 --- a/src/views.zig +++ b/src/views.zig @@ -173,6 +173,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { }; // Nothing fails from here. p.views_open &= take | bit(p.view); + p.views_away &= p.views_open; // The live view's slot owning nothing, as `installed` makes it. for (p.panes) |s| if (s) |pane| { if (take != 0) pane.pv[p.view] = fresh(pane); @@ -214,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// The live view alone, as view 0: every other closed. A session loaded /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { + p.views_away = 0; for (0..max) |k| if (k != p.view) close(p, @intCast(k)); emptied(p, p.view); const k = p.view; @@ -255,8 +257,20 @@ pub fn isOpen(p: *const Pardes, k: u8) bool { return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; } +/// Kept for its client's rejoin (`away`). +pub fn isAway(p: *const Pardes, k: u8) bool { + return k < max and p.views_away & (@as(u8, 1) << @intCast(k)) != 0; +} + +/// The views someone looks at: open, none kept for a rejoin. pub fn count(p: *const Pardes) usize { - return @popCount(p.views_open); + return @popCount(p.views_open & ~p.views_away); +} + +/// An open view but `k` someone looks at (`count`), if there is one. +fn looked(p: *const Pardes, k: u8) ?u8 { + for (0..max) |i| if (i != k and isOpen(p, @intCast(i)) and !isAway(p, @intCast(i))) return @intCast(i); + return null; } /// More than one view: the one test the single client's path pays. @@ -448,9 +462,8 @@ const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; pub fn closeKeeping(p: *Pardes, k: u8) void { if (!isOpen(p, k) or count(p) == 1) return; to(p, k); - const other: u8 = for (0..max) |i| { - if (i != k and isOpen(p, @intCast(i))) break @intCast(i); - } else unreachable; + // Never into one kept for a rejoin (`away`): that one is its client's. + const other = looked(p, k).?; const slot = p.parked[other].?; // Its size too: the terminals it sized keep theirs, no shell redrawn for // a size it is given back at the rejoin. @@ -469,11 +482,12 @@ pub fn closeKeeping(p: *Pardes, k: u8) void { } /// View `k` gone, and all it held; the live one moves to another first. -/// The last view stays: a session without a client keeps its own. +/// The last view someone looks at stays: a session without a client keeps +/// its own. One kept for a rejoin (`away`) goes whenever it is closed. pub fn close(p: *Pardes, k: u8) void { - if (!isOpen(p, k) or count(p) == 1) return; + if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; if (k == p.view) { - const other: u8 = for (0..max) |i| { + const other: u8 = looked(p, k) orelse for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; park(p); @@ -493,6 +507,19 @@ pub fn close(p: *Pardes, k: u8) void { if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); }; p.views_open &= ~(@as(u8, 1) << @intCast(k)); + p.views_away &= ~(@as(u8, 1) << @intCast(k)); +} + +/// View `k`'s client gone a moment, to join again for it (an Upgrade's +/// rejoin, a divergence's): the view kept as it is, layout, focus and the +/// panes opened in it, until its client is back (Pardes.rejoinView) or a +/// join wants its place (Pardes.openView). Dumped with the rest, so an +/// Upgrade's new build has it too. The live view is never one: the session +/// goes into another someone looks at, or into a copy of this one of its own. +pub fn away(p: *Pardes, k: u8) void { + if (!isOpen(p, k)) return; + if (k == p.view) if (looked(p, k) orelse open(p)) |other| to(p, other); + p.views_away |= bit(k); } /// The session's end: what each view not live holds. @@ -541,7 +568,7 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { } for (0..max) |i| { const k: u8 = @intCast(i); - if (k == live or !isOpen(p, k) or p.undrawn & (@as(u8, 1) << @intCast(k)) != 0) continue; + if (k == live or !isOpen(p, k) or (p.undrawn | p.views_away) & bit(k) != 0) continue; // No post_present: what a shell acknowledges there moves the // session's pointer and hover, which are the live view's to move; // a view's panels are acknowledged when it is live and drawn. @@ -836,6 +863,46 @@ test "the last client's view going leaves the session its layout, which a rejoin try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } +test "a client's view kept for its rejoin: through an Upgrade's dump, each client back in its own; a join with no room lets one go" { + const gpa = testing.allocator; + const opts: pardes.Options = .{ .tty_only = true, .cols = 100, .rows = 30 }; + const p = try Pardes.init(gpa, opts); + defer p.deinit(); + _ = try p.setTestFile("one\ntwo\n"); + // Two clients, each a pane of its own in its own view. + const ks: [2]u8 = .{ p.openView().?, p.openView().? }; + var made: [2]usize = undefined; + for (ks, &made) |k, *m| { + p.toView(k); + p.update(.{ .command = "New" }); + m.* = p.active; + } + // Both let go to upgrade, the live view one of theirs: theirs are kept, + // and the session's own is live. + for (ks) |k| p.awayView(k); + try testing.expectEqual(@as(usize, 1), count(p)); + try testing.expect(!isAway(p, p.view)); + // The new build loads the dump; each client is back in its own view. + const bytes = try pardes.dump.dumpBytes(p); + defer gpa.free(bytes); + const q = try pardes.dump.load(gpa, opts, bytes, null); + defer q.deinit(); + for (ks, made, 0..) |k, m, i| { + try testing.expect(isAway(q, k)); + try testing.expect(q.rejoinView(k)); + q.toView(k); + try testing.expectEqual(m, q.active); + try testing.expect(layout.findPane(q, m) != null); + try testing.expect(layout.findPane(q, made[1 - i]) == null); + } + try testing.expect(!q.rejoinView(ks[0])); + // A client that never comes back: its view goes when a join has no room. + p.toView(0); + try testing.expectEqual(@as(?u8, 3), p.openView()); + try testing.expectEqual(ks[0], p.openView()); + try testing.expect(!isAway(p, ks[0]) and isAway(p, ks[1])); +} + test "drawing the other views leaves the live one as it was, to the digest" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); |
