diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-22 23:20:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-22 23:20:38 -0300 |
| commit | e6dd9c5c3a60e3609e99ab2076b5a2509a014a66 (patch) | |
| tree | 59e10d535fbb5ddf1a971c391e0e6b0981a6d409 | |
| parent | 6270f531cd89130a26ee3d5423a740db707f8888 (diff) | |
| parent | 9e5b54cfdf73728745d2aeaafac00d823114543e (diff) | |
| download | pardes-e6dd9c5c3a60e3609e99ab2076b5a2509a014a66.tar.gz pardes-e6dd9c5c3a60e3609e99ab2076b5a2509a014a66.zip | |
integration (a base for the next rounds, not yet the gathered line): the pair's round 12 on the viewport's line with a client's way back: pardes-m60-base (`pyquvrsv`: pardes-viewport + pardes-upgrade-back) and pardes-pair-12 (`mlutslkp` on `ntlpyokw` on `zsquynqy` on `wowqsyxt`, on pardes-raw-tty: a 9P request that changes the core waits for the pty output read before it (`Turn.awaitOutput`: pagercolor's cause on both paths); a pair's client takes the tests' clock; the pair rows name the side that went; the soak with a pair among its sides); no conflict; core-test 918/1078 (160 skipped), shell-test 1120/1123 (2026-11-22)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | core/Turn.zig | 78 | ||||
| -rw-r--r-- | core/root.zig | 1 | ||||
| -rw-r--r-- | dev/Inbox.zig | 8 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 2 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 14 | ||||
| -rw-r--r-- | runtime/9p_io.zig | 7 | ||||
| -rw-r--r-- | test/daemon.py | 6 | ||||
| -rw-r--r-- | test/replicas.zig | 77 |
8 files changed, 180 insertions, 13 deletions
diff --git a/core/Turn.zig b/core/Turn.zig index 249c6795..d0e49d44 100644 --- a/core/Turn.zig +++ b/core/Turn.zig @@ -43,6 +43,15 @@ restores: u64 = 0, /// Bumped each time a language server's answer (or its lack) is taken /// in (`lspResponse`): a write that asked one waits for this. lsp_answers: u64 = 0, +/// Reads out of the ptys (a pane's reader, as it reads) and those applied +/// or dropped since (`outputApplied`): a write waits for the output read +/// before it (`awaitOutput`). +reads: std.atomic.Value(u64) = .init(0), +applied: std.atomic.Value(u64) = .init(0), +/// The editor gave the turn up to wait for input (`listen`), not for a +/// call of its own (`rest`, `yield`: a save through its own mount, whose +/// write it would never answer applying output meanwhile). +listening: bool = false, /// All of the above, and `out` reaching zero. settled: std.Io.Condition = .init, /// Every `yield`, ever: a replica's step makes none (each syscall of its @@ -69,12 +78,22 @@ pub fn rest(t: *Turn) void { t.release(); } +/// The editor is about to wait for input, the output its readers read +/// among it (its loop's wait): `rest`, and a write may wait for that +/// output meanwhile (`awaitOutput`). +pub fn listen(t: *Turn) void { + if (t.io == null) return; + t.listening = true; + t.release(); +} + /// The editor takes the turn back -- once every step that is out in a /// syscall has returned, since a step of the editor's may change what /// those still point into. pub fn wake(t: *Turn) void { const io = t.io orelse return; t.mutex.lockUncancelable(io); + t.listening = false; while (t.out != 0) t.settled.waitUncancelable(io, &t.mutex); } @@ -123,6 +142,32 @@ pub fn bump(t: *Turn, counter: *u64) void { t.settled.broadcast(io); } +/// A pty's read applied, or dropped: a write waiting on it told. +pub fn outputApplied(t: *Turn) void { + _ = t.applied.fetchAdd(1, .acq_rel); + if (t.io) |io| t.settled.broadcast(io); +} + +/// With the turn: waits, turn given up meanwhile, until every read out of +/// a pty before now is applied, as a terminal shows a program's output +/// before what it led to: `cmd | pardes -` typed in a pane writes its +/// pager after the shell echoed the line and started the command, and +/// the editor, a read a step, could still hold that echo (a client's keys +/// come a step each, so a shell's echo comes a key a read): the pager's +/// layout then saw the shell's line not yet ended, and its emulator's +/// resize cleared that line as a prompt the shell would draw again +/// (ghostty's Screen.clearPromptForRedraw). Only while the editor waits +/// in its loop (`listening`): resting in a call of its own it applies +/// nothing, and the request goes first, as before. A read its reader +/// drops off the turn wakes nobody: seen at the next recheck. +pub fn awaitOutput(t: *Turn) void { + const mark = t.reads.load(.acquire); + while (t.listening and t.applied.load(.acquire) < mark) t.settled.waitTimeout(t.io.?, &t.mutex, .{ .duration = .{ .raw = .fromMilliseconds(25), .clock = .awake } }) catch |err| switch (err) { + error.Timeout => {}, + error.Canceled => return, + }; +} + /// With the turn: waits, turn given up meanwhile, until `counter` has /// moved past `seen`; stopping ends the wait early. pub fn awaitPast(t: *Turn, counter: *const u64, seen: u64) void { @@ -139,3 +184,36 @@ fn release(t: *Turn) void { t.mutex.unlock(t.io.?); if (woken) if (t.wake_parked) |f| f(t.wake_ctx); } + +test "a write waits for the output read before it while the editor listens, not while it rests in a call of its own" { + const io = std.testing.io; + var t: Turn = .{}; + t.start(io); + _ = t.reads.fetchAdd(2, .acq_rel); + // A save through its own mount: the editor rests in it, applies + // nothing, and the write it waits on is served at once. + t.rest(); + _ = t.take(); + t.awaitOutput(); + try std.testing.expectEqual(@as(u64, 0), t.applied.load(.acquire)); + t.give(); + // Its loop's wait: the write waits for both reads, applied meanwhile. + t.wake(); + t.listen(); + const editor = try std.Thread.spawn(.{}, struct { + fn apply(turn: *Turn) void { + for (0..2) |_| { + turn.wake(); + turn.outputApplied(); + turn.listen(); + } + } + }.apply, .{&t}); + _ = t.take(); + t.awaitOutput(); + try std.testing.expectEqual(@as(u64, 2), t.applied.load(.acquire)); + t.give(); + editor.join(); + t.wake(); + t.stop(); +} diff --git a/core/root.zig b/core/root.zig index 3bca5e03..a476f90f 100644 --- a/core/root.zig +++ b/core/root.zig @@ -443,6 +443,7 @@ test { _ = @import("Handover.zig"); _ = @import("walk.zig"); _ = @import("views.zig"); + _ = Turn; // A window's build links the window's C (font.c over FreeType and // HarfBuzz) into each of its programs, and their allocator (ui_malloc // and the rest) is gui.zig's. diff --git a/dev/Inbox.zig b/dev/Inbox.zig index d733b971..1df516cb 100644 --- a/dev/Inbox.zig +++ b/dev/Inbox.zig @@ -118,7 +118,9 @@ pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { /// One of `pane`'s reads applied, or dropped unapplied. pub fn applied(q: *Inbox, pane: pardes.Pane.Id) void { const n = &q.unapplied[pane.index()]; - if (n.load(.acquire) > 0) _ = n.fetchSub(1, .acq_rel); + if (n.load(.acquire) == 0) return; + _ = n.fetchSub(1, .acq_rel); + pardes.turn.outputApplied(); } pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { @@ -316,8 +318,10 @@ fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void { }, }; if (n == 0) continue; - // Counted at once: out of the pty, the poll no longer sees it. + // Counted at once: out of the pty, the poll no longer sees it, + // nor a write after it (Turn.awaitOutput). _ = q.unapplied[pane.index()].fetchAdd(1, .acq_rel); + _ = pardes.turn.reads.fetchAdd(1, .acq_rel); const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| { q.applied(pane); failure = err; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 3193d966..b00073fa 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -2696,7 +2696,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { s.held = null; if (held) |h| sev = h; // The wait is the 9P connections' turn with the core. - pardes.turn.rest(); + pardes.turn.listen(); const got = held != null or c.SDL_WaitEventTimeout(&sev, ms); pardes.turn.wake(); // A virtual clock: while a render-time animation moves, each loop diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 5d75722f..d1bbe140 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -791,6 +791,9 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, .client = &c, .peers = &no_peers, .ptys = &no_ptys, + // Its own motions (the glide) on the case's clock, as a session's + // own terminal's (`Own.open`). + .test_clock = host_io.testClock(), }; tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start }; no_inbox.wake_fn = Shell.wakeForQueue; @@ -1490,11 +1493,16 @@ const Shell = struct { } else next: { if (wait != 0) { // A virtual clock moves a frame a wake while the tty's own - // motions move (the glide), as the real one would. - s.wake_to = s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0; + // motions move (the glide), as the real one would: a + // client's from where it is, its core's clock and wakes + // the session's, still between units. + s.wake_to = if (s.client != null) + (if (s.moving()) (s.test_clock orelse 0) + pardes.frame_ns else 0) + else + s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0; requestWake(s.io, wait); } - pardes.turn.rest(); + pardes.turn.listen(); const got = s.loop.nextEvent() catch { pardes.turn.wake(); if (s.client) |c| c.link.quit = true; diff --git a/runtime/9p_io.zig b/runtime/9p_io.zig index 1079b102..50011690 100644 --- a/runtime/9p_io.zig +++ b/runtime/9p_io.zig @@ -183,8 +183,13 @@ pub const Listener = struct { const refused = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO); return conn.reply(&refused, ""); } - const quiet = pardes.turn.take(); + _ = pardes.turn.take(); defer pardes.turn.give(); + // What changes the core comes after the output already read out of + // the ptys (a pane's `cmd | pardes -`: its pager after the line's + // echo); quiet asked after that wait, which gave the turn up. + if (pardes.ctlfs.needsQuiet(l.core, req)) pardes.turn.awaitOutput(); + const quiet = pardes.turn.out == 0; // A connection a Restore is cutting (`reset`) is the old editor's // client: its fids name the old editor's panes and opens. if (l.cutting(conn)) { diff --git a/test/daemon.py b/test/daemon.py index d1406ae1..fcaae3d7 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -925,7 +925,11 @@ def pair_rows(binary): t, c, nine, session, pane = started('lone', dict(env, PARDES_DAEMON='off')) typed_detach(t, nine, pane) time.sleep(1) - assert c.poll() is None and alive(session), 'a Detach with no daemon is refused' + # Which went, if one did: the client's exit and its screen's last + # words, the session's life (its stderr is /dev/null: no log). + went = [w for w, gone in (('the client', c.poll() is not None), ('the session', not alive(session))) if gone] + assert not went, (f'a Detach with no daemon is refused: {" and ".join(went)} went (client exit {c.poll()}, ' + f'session {"alive" if alive(session) else "gone"}); the terminal\'s last bytes: {t.text()[-600:]!r}') nine.socket.close() t.close() terms.remove(t) diff --git a/test/replicas.zig b/test/replicas.zig index a68ce222..8f3e5ca5 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -51,6 +51,12 @@ var trace = false; /// `--witness`: the witness judged at every run's end, not only after a /// divergence (`Witness`). var witness_always = false; +/// `--pair`: client 0 is the session's pair client (Options.pair: a +/// terminal's `pardes` as a client and its session): joined before the +/// session's first effects, its core its own boot (replica.ownBoot), the +/// welcome's digest its check (confirmBoot), the session's own frontend +/// gone (no typist, its view the pair's). +var pair_mode = false; /// The PDF the soak opens (`--pdf`), absolute. var pdf_path: ?[]const u8 = null; const frame_us = 16_667; @@ -246,6 +252,10 @@ const Totals = struct { /// (PARDES_TEST_DIVERGE's), and clients that left; quiet checks. joins: u64 = 0, refused: u64 = 0, + /// A pair's client's own boot the session's core at its join (`--pair`), + /// or not (its dump loaded instead, the fields that differ said). + boots: u64 = 0, + boots_unequal: u64 = 0, rejoined: u64 = 0, detached: u64 = 0, agree: u64 = 0, @@ -1173,6 +1183,9 @@ const Soak = struct { /// `Peers.detach` shut its link; the session's own view's is its /// terminal's, which the soak has none of). detach_view: ?u8 = null, + /// `--pair`: the session's end of client 0's link, its first client's + /// (`options`). + pair_fd: c_int = -1, // ---- the soak ---- /// The soak's time (us): the last message's. @@ -1219,7 +1232,7 @@ const Soak = struct { /// its own (Core.render: drawn whenever a frame is owed and someone /// sees it, and a terminal is always seen). fn watchedHost(_: ?*anyopaque) bool { - return true; + return !pair_mode; } fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { @@ -1289,7 +1302,10 @@ const Soak = struct { // ---- the session loop's frontend ---- - pub fn options(_: *Soak, _: *pardes.Options) void {} + /// A pair's: its first client on the link `run` made. + pub fn options(s: *Soak, o: *pardes.Options) void { + if (s.pair_fd >= 0) o.pair = s.pair_fd; + } pub fn open(s: *Soak, l: *SessionLoop) !pardes.Host { s.l = l; @@ -1354,6 +1370,8 @@ const Soak = struct { // let go); the first leaves when it is done; the second is put off // the session once (PARDES_TEST_DIVERGE) and joins again for its view. for (s.clients, 0..) |*cl, c| { + // A pair's client joined at the loop's start. + if (pair_mode and c == 0) continue; cl.dialling = true; const at: u64 = switch (c) { 0, 1 => s.random.uintLessThan(u64, 100_000), @@ -1365,7 +1383,7 @@ const Soak = struct { }; try net.send(.{ .at = at, .n = 0, .kind = .dial, .who = @intCast(c + 1), .bytes = &.{} }); } - try net.send(.{ .at = s.random.uintLessThan(u64, 200_000), .n = 0, .kind = .own, .who = 0, .bytes = &.{} }); + if (!pair_mode) try net.send(.{ .at = s.random.uintLessThan(u64, 200_000), .n = 0, .kind = .own, .who = 0, .bytes = &.{} }); try net.send(.{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); try net.send(.{ .at = session_at, .n = 0, .kind = .output, .who = 2, .bytes = &.{} }); try net.send(.{ .at = flood_at, .n = 0, .kind = .output, .who = 1, .bytes = &.{} }); @@ -1933,7 +1951,7 @@ const Soak = struct { try s.totals.add(&old.stats); old.deinit(); } - const rp = try replica.Replica.initDump(gpa, cl.id, cl.view, m.bytes, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); + const rp = if (pair_mode and c == 0 and !s.has_link[0]) try s.booted(cl, m.bytes) else try replica.Replica.initDump(gpa, cl.id, cl.view, m.bytes, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); cl.r = rp; // In process: its clock reads are the session's machine's, and the // units' heads carry the soak's clock (`stamp`). @@ -1978,6 +1996,29 @@ const Soak = struct { if (cl.left > 0 and cl.left == s.r.steps) try net.send(.{ .at = m.at + typistGap(s.random), .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }); } + /// A pair's client's replica: its core its own boot from the options + /// the session was started with (`run`'s), as the pair's client makes + /// it (replica.ownBoot), kept when the welcome's digest is its own + /// (confirmBoot); else the session's dump, loaded, and the fields of + /// the two cores that differ said. + fn booted(s: *Soak, cl: *Client, dump: []const u8) !*replica.Replica { + s.totals.boots += 1; + const boot = try replica.Replica.initBoot(gpa, .{ .cols = 100, .rows = 30, .boot = .tty, .distrusted = &cl.distrusted }); + if (boot.digest() == cl.digest) { + boot.id = cl.id; + boot.view = cl.view; + return boot; + } + defer boot.deinit(); + s.totals.boots_unequal += 1; + const rp = try replica.Replica.initDump(gpa, cl.id, cl.view, dump, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + Witness.fieldsDiffer(boot.core(), rp.core(), &w); + std.debug.print("seed {d} rtt {d} ms: the pair's own boot is not the session's core at its join; differ: {s}", .{ s.r.seed, s.r.rtt_ms, w.buffered() }); + return rp; + } + /// At quiescence: every client's digest the session's, its terminals /// the session's cell for cell, its files' texts byte for byte. fn finish(s: *Soak) !void { @@ -2114,6 +2155,12 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { var clients: [8]Client = undefined; for (clients[0..r.clients], 0..) |*cl, c| { cl.* = .{ .kind = if (c % 2 == 0) .tty else .window, .left = r.steps, .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) }; + // A pair's client is the terminal the session was started at the + // size of (main.zig `pair`). + if (pair_mode and c == 0) { + cl.cols = 100; + cl.rows = 30; + } var name: [300]u8 = undefined; cl.check = try gpa.dupeSentinel(u8, try std.fmt.bufPrint(&name, "{s}/check-{d}", .{ dir, c }), 0); _ = libc.unlink(cl.check); @@ -2157,9 +2204,23 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { .flood = flood, .yes_len = yes.len, .own_left = r.steps, - .typing = r.clients + 1, + .typing = r.clients + @intFromBool(!pair_mode), .media = &media, }; + // A pair's client's link, made before the session (main.zig `pair`): + // its end handed to the session's options, the soak reading the other + // as for a dial (`.dial`). + if (pair_mode) { + const cl = &clients[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; + _ = libc.fcntl(fds[0], libc.F.SETFL, @as(c_int, @bitCast(libc.O{ .NONBLOCK = true }))); + cl.sfd = fds[0]; + cl.cfd = fds[1]; + cl.handed = libc.dup(fds[0]); + if (cl.handed < 0) return error.NoSocket; + soak.pair_fd = cl.handed; + } // The session: THE session loop, from its start to its end. try SessionLoop.run(init, .{ .cols = 100, .rows = 30, .boot = .tty }, &soak); if (soak.err) |err| return err; @@ -3248,6 +3309,10 @@ pub fn main(init: std.process.Init) !void { witness_always = true; continue; } + if (std.mem.eql(u8, name, "--pair")) { + pair_mode = true; + continue; + } if (i + 1 >= args.len) return error.MissingValue; i += 1; const value = args[i]; @@ -3313,6 +3378,8 @@ pub fn main(init: std.process.Init) !void { pardes.Term.reloads = .{}; for (first..first + seeds) |seed| try run(init, .{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .flood_mb = flood_mb }, dir, &t); std.debug.print("rtt {d}: {d} reloads of a terminal at joins, every side (the session's own too), {d} us each, {d} us at worst; {d} kitty images in clients at quiescence, each the session's pixels\n", .{ rtt, pardes.Term.reloads.n, pardes.Term.reloads.ns / @max(1, pardes.Term.reloads.n) / 1000, pardes.Term.reloads.worst_ns / 1000, t.images_equal }); + if (pair_mode) std.debug.print("rtt {d}: the pair's client booted its own core {d} times, {d} of them not the session's at its join\n", .{ rtt, t.boots, t.boots_unequal }); + if (t.boots_unequal > 0) t.unequal += t.boots_unequal; const rollbacks = t.mispredicted + t.foreign; std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ rtt, t.inputs, t.speculated, |
