diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 07:42:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 11:40:11 -0300 |
| commit | d2f854de898cb84776e9f56ece0ec9e9c548e27d (patch) | |
| tree | 26a39f40fce8f74d0f7b26f1e4b911a5fb1b1eaf /dev/tui/tui.zig | |
| parent | 2d30f3781ab40c38710835806a1029359509d27b (diff) | |
| download | pardes-d2f854de898cb84776e9f56ece0ec9e9c548e27d.tar.gz pardes-d2f854de898cb84776e9f56ece0ec9e9c548e27d.zip | |
client: one client loop, which the terminal's client and the window's both run (the reorganisation's runners, step 10b, its first part; the design round's rule L): runtime/client.zig holds the loop a replica client runs, its link and its replica inside it, over a frontend of ten calls, each for a reason the two loops had (own: the window's own work over the frame it shows before a pass, its size posted, its font, a touchpad's rows, the pointer, the timedemo, the test feed, whose end ends the loop; again: its own motion, the last frame presented again; deadline: when it wants waking for its own sake, 0 when it holds an input already; wait: until an input, a message or the deadline, inputs in through the loop's input and messages through its take; present and drawn: a frame shown or owed, what it wrote and whether it moves; start: a join's first inputs; said and ended: the session's stream from the reader's thread onto the frontend's own queue; drop: what the reader queued let go once the link ended), and what a client holds across its joins (client.Start: its allocators, the addons it distrusts, its frame timings, the blobs it keeps far, its joined replica), made the same for both; what differed between the two loops is one behaviour now or a value the frontend gives: the copy is taken whenever the wait is not 0 (the window's rule; the terminal's "an event is held already" is a deadline of 0), a frame owed is waited for a frame on in the window's deadline (a skipped swapchain image has nothing that wakes it; a terminal's held frame is answered by the terminal), the timedemo's and the latency trace's waits are the window's deadline, the terminal's capabilities and the window's title and blink are their wait's ends, the frame's record keeps what each wrote (bytes on the terminal, presented in the window) and whether its own motion goes on (the window's; the terminal's false, as before), the reader's queue is drained the terminal's way for both (until the reader has ended, then joined); the window's motion frames are counted in the stats line's `again` as the terminal's were (the one change, a counter); the two old loops, tui.zig runClient with its Client and linkReader and gui.zig's Client, clientReader and clientSession, are each that loop with their frontend now (dev/tui/tui.zig +174 -216 there, dev/gui/gui.zig +329 -334, runtime/client.zig +233 of which 30 are the calls' description; all told +577 -389). Gate: tui core-test 836/979 (143 skipped), shell-test 1014/1017, term-test 15/15, pane-test 183/184, kitty-test 45/45, behaviour-test 275/275 and --replay 275/275, process-test 2 of 144 failed (lspcomplete and lspcompletemoved, the base's); gui unit-test 1612/1616 with the base's BaselineHarnessMismatch; replicas --clients 5 diverged 0, unequal 0, deltas wrong 0, quiet 470/0, 600/0, 764/0, stopped 78/64/78 (the base's, the soak not yet on the loop); daemon.py plain, --far, --games, --window-clients, --upgrade from pardes-tree's pfx-base to this and back; the macOS library for Linux and aarch64-macos and the FreeBSD tui configure; rollback-bench runs; gate-w run3-1 against tree1: frames 77/77 byte-identical in Debug, the alternated windows' pictures identical in all 10 runs, corpus2x +0.128% (PASS), perfops 97 counts within 0.1% (largest image-frame on large +0.029%); its unit-test 1608/1616: the gate was started in the background (`&`), so SIGINT was ignored down to its test shells and the host_io tests whose Ctrl-C must end a foreground job failed (they pass alone, and the whole unit-test passes 1612/1616 when SIGINT is not ignored)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev/tui/tui.zig')
| -rw-r--r-- | dev/tui/tui.zig | 277 |
1 files changed, 118 insertions, 159 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 5903e448..c2f3dbf1 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -16,6 +16,7 @@ const file_watch = @import("../file_watch.zig"); const selection_pipe = @import("core").selection_pipe; const ninep_io = @import("runtime").ninep_io; const daemon = @import("runtime").daemon; +const client_loop = @import("runtime").client; const host_io = @import("../host_io.zig"); const upgrade = @import("runtime").upgrade; const replica = @import("core").replica; @@ -619,44 +620,106 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { try localSession(init, opts, &screen, &loop, &paste_buf); } -/// A replica client's link to its session (`runClient`), drawn here. -const Client = struct { - link: replica.Link, +/// A replica client's terminal (`runClient`): the frontend of the client +/// loop (runtime/client.zig), its view drawn here as the session draws its +/// terminals'. +const Client = client_loop.Loop(Tty); - /// The client's own view drawn on its terminal, outside any step: the - /// replica's digest stays the session's (views.drawOne); the frame - /// statistics are this process's (`Shell.frames`). - fn draw(c: *Client, s: *Shell) void { - const frames = s.frames; - frames.mark_start = pardes.FrameStats.nowNs(); - frames.mark_update = frames.mark_start; - c.link.owed = !c.link.r.draw(.{ .ctx = s, .f = Shell.present }); - frames.mark_render = frames.mark_update; - const drew = frames.bytes > 0; - frames.record(pardes.FrameStats.nowNs(), false, c.link.owed); - c.link.drawn(drew); +const Tty = struct { + sh: *Shell, + screen: *Screen, + loop: *Loop, + joins: *client_loop.Start, + + /// A join's first inputs: what it is, then its size. + pub fn start(t: *Tty, l: *Client) void { + t.sh.core = l.link.r.core(); + t.sh.own = t.joins.joined.view; + l.input(.{ .frontend = .{ .windowed = false } }); + t.screen.size = null; + t.sh.winched(); + t.screen.vx.queueRefresh(); } -}; -/// The session's stream, onto the loop: its units, and its end. -fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loop: *Loop, done: *std.atomic.Value(bool)) void { - defer done.store(true, .release); - const Post = struct { - fn post(l: *Loop, said: replica.Said) bool { - l.postEvent(.{ .said = said }) catch return false; - return true; + /// A terminal's own work is all in its wait. + pub fn own(_: *Tty, _: *Client) bool { + return true; + } + + /// The tty's own motions (its glide, a jump's trail): the last frame + /// again, moved on, as the session's terminal draws them (`ownFrame`), + /// not rendered again. One the terminal's answer holds is skipped (the + /// next tick's moves on). + pub fn again(t: *Tty, l: *Client) bool { + const core = l.link.r.core(); + if (!t.sh.moving() or core.surface.cells.len == 0) return false; + _ = Shell.present(t.sh, &core.surface); + return true; + } + + /// An event already queued is the wait's at once; otherwise the + /// session's own wait: an input drawn at once, the session's units and + /// deltas a frame a tick at most, the tty's own motions on its grid of + /// ticks (`waitInput`). + pub fn deadline(t: *Tty, _: *Client) u32 { + if (t.loop.tryEvent() catch null) |ev| { + t.sh.held = ev; + return 0; } - }; - replica.readLink(gpa, fd, rest, loop, Post.post); - loop.postEvent(.link_gone) catch {}; -} + return client_loop.forever; + } + + /// Its inputs go to the loop through `Shell.feed`, the session's + /// stream through `.said`; the terminal's capabilities, once it has + /// answered, enabled and drawn. + pub fn wait(t: *Tty, l: *Client, ms: u32) void { + Shell.waitInput(t.sh, if (ms == client_loop.forever) 0 else ms); + const screen = t.screen; + if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { + screen.caps_pending = false; + screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; + screen.vx.queueRefresh(); + l.link.dirty = true; + } + } + + pub fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) bool { + const t: *Tty = @ptrCast(@alignCast(ctx.?)); + return Shell.present(t.sh, surface); + } + + /// Whether it wrote anything to the terminal (Shell.present counts + /// the bytes); its motions draw frames the timings do not count. + pub fn drawn(t: *Tty) client_loop.Drawn { + return .{ .drew = t.sh.frames.bytes > 0, .moving = false }; + } + + /// The session's stream, onto the loop. + pub fn said(t: *Tty, msg: replica.Said) bool { + t.loop.postEvent(.{ .said = msg }) catch return false; + return true; + } + + pub fn ended(t: *Tty) void { + t.loop.postEvent(.link_gone) catch {}; + } + + /// What the reader and the terminal posted, let go. + pub fn drop(t: *Tty) void { + while (t.loop.tryEvent() catch null) |ev| switch (ev) { + .said => |msg| switch (msg) { + .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), + .checked => {}, + }, + .paste => |b| t.sh.gpa.free(@constCast(b)), + else => {}, + }; + } +}; /// `pardes --attach=<socket>:<session>` (and `<host>:<session>`, through /// ssh, main.zig): this terminal a replica client of a session elsewhere -/// (docs/next-daemon.md, "Phase 5 part B notes"). Its own core, loaded -/// from the session's dump, applies its keys at once (core/replica.zig); -/// the session's units are the truth it rolls back to. Its own view of the -/// session is drawn here, as the session draws its terminals'. +/// (docs/next-daemon.md, "Phase 5 part B notes"), the client loop over it. pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far_link: bool) !void { const io = init.io; const gpa = init.gpa; @@ -667,33 +730,9 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, std.process.exit(1); } }; - var why: [600]u8 = @splat(0); - // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash, - // those it holds kept from the link, and its stream in zstd. A client - // on this machine's own socket is sent the dump whole, as before: a - // hash and a compression there cost more than the bytes. - const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else far_link; - var have_store: replica.Have = if (far) .init(io) else .{ .io = io }; - defer have_store.deinit(); - const have: ?*replica.Have = if (far) &have_store else null; - const allocs = pardes.memory.init(gpa); - defer pardes.memory.deinit(); - pardes.Term.start(allocs.image); - pardes.Kind.heap = allocs.addon; - pardes.syntax.start(allocs.tree_sitter); - defer { - pardes.Term.stop(); - pardes.syntax.stop(); - } - var o = opts; - o.image_allocator = allocs.image; - var distrusted: pardes.Addon.Distrusted = .{}; - o.distrusted = &distrusted; - // Its frame timings, this process's (Pardes.frames). - var frames: pardes.FrameStats = .{}; - o.frames = &frames; - var joined: replica.Joined = undefined; - var r = replica.join(gpa, &joined, path, session, o, have, &why, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + var start: client_loop.Start = undefined; + if (!start.open(init, opts, .{ .path = path, .session = session, .far = far_link })) Said.no(io, "{s}", .{start.said()}); + defer start.close(); var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); @@ -714,27 +753,27 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); var loop: Loop = .init(io, &screen.tty, &screen.vx); - var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame); + var frame_arena: std.heap.ArenaAllocator = .init(start.allocs.frame); defer frame_arena.deinit(); - var client_link: Client = .{ .link = .{ .r = r, .fd = joined.fd } }; - const link = &client_link.link; - try link.fromEnv(gpa, init.environ_map); - defer link.deinit(gpa); + var tty: Tty = undefined; + var c = try Client.init(&tty, &start); + defer c.link.deinit(gpa); var sh: Shell = .{ .io = io, .gpa = gpa, - .lsp_gpa = allocs.lsp, - .core = r.core(), - .frames = &frames, + .lsp_gpa = start.allocs.lsp, + .core = start.r.core(), + .frames = &start.frames, .prompt_rcs = undefined, .loop = &loop, .frame = &frame_arena, .paste_buf = &paste_buf, .inotify_fd = -1, - .own = joined.view, + .own = start.joined.view, .screen = &screen, - .client = &client_link, + .client = &c, }; + tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start }; pardes.Kind.drawer = .{ .ctx = &sh, .go = Shell.draw }; defer pardes.Kind.drawer = null; @@ -743,96 +782,16 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach(); (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); try screen.vx.queryTerminalSend(screen.tty.writer()); - // A join a pass: the first, and one after the replica diverged or the - // session let it go to upgrade. The terminal stays as it is. - while (true) { - sh.core = r.core(); - sh.own = joined.view; - link.reset(r, &joined); - var reader_done: std.atomic.Value(bool) = .init(false); - const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); - // What it is, then its size, are its view's first inputs. - link.input(.{ .frontend = .{ .windowed = false } }); - screen.size = null; - sh.winched(); - screen.vx.queueRefresh(); - while (!link.quit) { - // Its predicted echo judged and caught up, once a frame. - if (r.echoFrame()) link.dirty = true; - if (link.dirty) { - client_link.draw(&sh); - } else if (sh.moving() and r.core().surface.cells.len != 0) { - // The tty's own motions (its glide, a jump's trail): the last - // frame again, moved on, as the session's terminal draws them - // (`ownFrame`), not rendered again. One the terminal's answer - // holds is skipped (the next tick's moves on). - _ = Shell.present(&sh, &r.core().surface); - link.again += 1; - } - // Idle, its frame drawn: the next key needs no copy taken. - if (loop.tryEvent() catch null) |ev| sh.held = ev else r.ready(); - // The session's own wait: an input drawn at once, the session's - // units and deltas a frame a tick at most, the tty's own motions - // on its grid of ticks; predictions judged a frame at a time. - var wait: u32 = if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) @intCast(pardes.frame_ns / std.time.ns_per_ms) else 0; - if (link.quietCheck()) |ms| wait = if (wait == 0) ms else @min(wait, ms); - Shell.waitInput(&sh, wait); - r.settle(); - // A rollback since it drew (`toConfirmed`): its frame is drawn - // again, never moved on from cells the copy put back. - if (r.moved) { - r.moved = false; - link.dirty = true; - } - if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { - screen.caps_pending = false; - screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; - screen.vx.queueRefresh(); - link.dirty = true; - } - if (link.owed or r.echo.changed) link.dirty = true; - 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, 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 - // just before it, and left queued it ended the next link at once. - _ = libc.shutdown(joined.fd, 2); - while (true) { - const ended = reader_done.load(.acquire); - while (loop.tryEvent() catch null) |ev| switch (ev) { - .said => |said| switch (said) { - .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), - .checked => {}, - }, - .paste => |b| gpa.free(@constCast(b)), - else => {}, - }; - if (ended) break; - std.Thread.yield() catch {}; - } - reader.join(); - _ = libc.close(joined.fd); - joined.rest.deinit(gpa); - // Why it joins again, in a line over its frame until a key. - if (!link.rejoins()) { - // What it made since it joined, kept for its next start. - if (have) |h| h.keep(r.core()); - r.deinit(); - if (link.gone) { - screen.leave(); - Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); - } - return; - } - // The session again, the core it leaves held meanwhile: its blobs - // are not sent again. - r = replica.join(gpa, &joined, path, session, o, have, &why, r) orelse { + switch (try c.run(&start)) { + .done => {}, + .gone => { screen.leave(); - Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); - }; + Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); + }, + .failed => { + screen.leave(); + Said.no(io, "{s}", .{start.said()}); + }, } } @@ -1267,7 +1226,7 @@ const Shell = struct { /// (applied at once there, and sent to the session). fn feed(s: *Shell, ev: pardes.Event) void { const c = s.client orelse return s.core.update(ev); - c.link.input(ev); + c.input(ev); } /// Whether a terminal of this process's shows the session, or @@ -1761,7 +1720,7 @@ const Shell = struct { else => false, }; if (echo) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); - c.link.take(said); + c.take(said); if (said == .refused) return true; } else switch (said) { .unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b), |
