summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 07:42:20 -0300
committerGabriel Schneider <[email protected]>2026-11-19 11:40:11 -0300
commitd2f854de898cb84776e9f56ece0ec9e9c548e27d (patch)
tree26a39f40fce8f74d0f7b26f1e4b911a5fb1b1eaf /runtime
parent2d30f3781ab40c38710835806a1029359509d27b (diff)
downloadpardes-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 'runtime')
-rw-r--r--runtime/client.zig233
-rw-r--r--runtime/root.zig2
2 files changed, 235 insertions, 0 deletions
diff --git a/runtime/client.zig b/runtime/client.zig
new file mode 100644
index 00000000..28ed6a52
--- /dev/null
+++ b/runtime/client.zig
@@ -0,0 +1,233 @@
+//! A replica client's loop: the terminal's (dev/tui `runClient`) and the
+//! window's (dev/gui `clientSession`) alike, each over a frontend of its
+//! own. Its own core, loaded from the session's dump, applies its input at
+//! once (core/replica.zig); the session's units are the truth it rolls back
+//! to. The link and the replica are inside it; what a frontend does is:
+//!
+//! per pass, in its order:
+//! - `own(l) bool`: the frontend's own work over the frame it shows,
+//! before anything is drawn (a window's size posted, its font
+//! followed, a touchpad's rows, the pointer, the timedemo, a test's
+//! feed; a terminal has none). False ends the loop (a test feed's end).
+//! - `again(l) bool`: nothing to draw, but its own motion (a glide, a
+//! blink, a jump's trail) goes on: the last frame again, moved on;
+//! whether it was presented.
+//! - `deadline(l) u32`: when it wants to be woken for its own sake, ms
+//! (`forever`: never); 0 when it holds an input already (a terminal's
+//! queued key) or something is due at once. The loop wakes it a frame
+//! on while an echo is predicted, and for the quiet check.
+//! - `wait(l, ms)`: until an input, a message of the session's stream or
+//! `ms`: its inputs go in through `l.input`, the stream's messages
+//! through `l.take`, in their order.
+//! - `present(fe, surface) bool` (views.Present's): a frame shown, or held
+//! for the frontend's answer (owed); and `drawn() Drawn`, read after it:
+//! whether it wrote anything, and whether its own motion goes on.
+//! per join:
+//! - `start(l)`: a join's first inputs (what it is, its size).
+//! - `said(msg) bool`, `ended()`: the session's stream, from the reader's
+//! thread, onto the frontend's own queue (a terminal's event loop, a
+//! window's inbox and SDL's wake), so that its wait wakes for them.
+//! - `drop()`: what the reader queued, let go once the link has ended.
+const std = @import("std");
+const libc = std.c;
+const pardes = @import("core");
+const replica = pardes.replica;
+
+/// A wait with no wake of its own: until an input or a message.
+pub const forever: u32 = std.math.maxInt(u32);
+const frame_ms: u32 = @intCast(pardes.frame_ns / std.time.ns_per_ms);
+
+/// What a frame showed, read after its present.
+pub const Drawn = struct { drew: bool, moving: bool };
+
+/// The session a client dials: a socket's path or `<host>` (through ssh),
+/// the session's name, and whether it is far (its blobs by hash, its stream
+/// compressed).
+pub const Dial = struct { path: []const u8, session: []const u8, far: bool };
+
+/// What a client holds across its joins: its allocators, the addons it
+/// distrusts, its frame timings (Pardes.frames), the blobs it keeps when
+/// far, and its replica and link as joined. Made in place (`open`).
+pub const Start = struct {
+ gpa: std.mem.Allocator,
+ env: *std.process.Environ.Map,
+ dial: Dial,
+ have_store: replica.Have,
+ have: ?*replica.Have = null,
+ allocs: pardes.memory.Allocators,
+ o: pardes.Options,
+ distrusted: pardes.Addon.Distrusted = .{},
+ frames: pardes.FrameStats = .{},
+ joined: replica.Joined = undefined,
+ r: *replica.Replica = undefined,
+ why: [600]u8 = @splat(0),
+
+ /// The client's process made ready and its first join: false, and why
+ /// (`said`), when it cannot join.
+ pub fn open(s: *Start, init: std.process.Init, opts: pardes.Options, dial: Dial) bool {
+ const io = init.io;
+ // 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: 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 dial.far;
+ s.* = .{
+ .gpa = init.gpa,
+ .env = init.environ_map,
+ .dial = dial,
+ .have_store = if (far) .init(io) else .{ .io = io },
+ .allocs = pardes.memory.init(init.gpa),
+ .o = opts,
+ };
+ if (far) s.have = &s.have_store;
+ pardes.Term.start(s.allocs.image);
+ pardes.Kind.heap = s.allocs.addon;
+ pardes.syntax.start(s.allocs.tree_sitter);
+ s.o.image_allocator = s.allocs.image;
+ s.o.distrusted = &s.distrusted;
+ // Its frame timings, this process's (Pardes.frames).
+ s.o.frames = &s.frames;
+ s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false;
+ return true;
+ }
+
+ pub fn close(s: *Start) void {
+ pardes.Term.stop();
+ pardes.syntax.stop();
+ pardes.memory.deinit();
+ s.have_store.deinit();
+ }
+
+ /// Why it could not join.
+ pub fn said(s: *const Start) []const u8 {
+ return std.mem.sliceTo(&s.why, 0);
+ }
+};
+
+/// How a client's joins ended: it quit, the session let it go, or it could
+/// not join again (`Start.said`).
+pub const Ended = enum { done, gone, failed };
+
+pub fn Loop(comptime Frontend: type) type {
+ return struct {
+ const L = @This();
+
+ link: replica.Link,
+ fe: *Frontend,
+ frames: *pardes.FrameStats,
+
+ pub fn init(fe: *Frontend, s: *Start) !L {
+ var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd }, .fe = fe, .frames = &s.frames };
+ try l.link.fromEnv(s.gpa, s.env);
+ return l;
+ }
+
+ /// The frontend's input: applied at once, and sent to the session.
+ pub fn input(l: *L, ev: pardes.Event) void {
+ l.link.input(ev);
+ }
+
+ /// A message of the session's stream, in its order.
+ pub fn take(l: *L, msg: replica.Said) void {
+ l.link.take(msg);
+ }
+
+ /// A join a pass: the first, and one after the replica diverged or
+ /// the session let it go to upgrade; the frontend stays as it is.
+ pub fn run(l: *L, s: *Start) !Ended {
+ const link = &l.link;
+ while (true) {
+ link.reset(s.r, &s.joined);
+ l.fe.start(l);
+ var done: std.atomic.Value(bool) = .init(false);
+ const reader = try std.Thread.spawn(.{}, read, .{ l, s, &done });
+ while (!link.quit) {
+ const ms = l.before() orelse break;
+ l.fe.wait(l, ms);
+ l.after();
+ }
+ if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
+ if (!link.gone) replica.bye(s.joined.fd, s.r.diverged != null);
+ // The link's reader ends with the link, posting as it goes:
+ // what it posted is let go until it has (a terminal's queue
+ // is bounded: a reader blocked on it would never end).
+ _ = libc.shutdown(s.joined.fd, 2);
+ while (true) {
+ const ended = done.load(.acquire);
+ l.fe.drop();
+ if (ended) break;
+ std.Thread.yield() catch {};
+ }
+ reader.join();
+ _ = libc.close(s.joined.fd);
+ s.joined.rest.deinit(s.gpa);
+ // Why it joins again is in its notice until a key.
+ if (!link.rejoins()) {
+ // What it made since it joined, kept for its next start.
+ if (s.have) |h| h.keep(s.r.core());
+ s.r.deinit();
+ return if (link.gone) .gone else .done;
+ }
+ // The session again, the core it leaves held meanwhile: its
+ // blobs are not sent again.
+ s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse return .failed;
+ }
+ }
+
+ /// The session's stream, onto the frontend's queue: its messages,
+ /// and its end.
+ fn read(l: *L, s: *Start, done: *std.atomic.Value(bool)) void {
+ defer done.store(true, .release);
+ replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, l.fe, Frontend.said);
+ l.fe.ended();
+ }
+
+ /// A pass up to its wait: the frontend's own work, its predicted
+ /// echo judged and caught up (once a frame), its view drawn if
+ /// anything changed (else its own motion moved on), and how long
+ /// it may wait; idle, the copy is taken so that the next input
+ /// needs none. Null: the frontend ended it.
+ pub fn before(l: *L) ?u32 {
+ const link = &l.link;
+ const r = link.r;
+ if (!l.fe.own(l)) return null;
+ if (r.echoFrame()) link.dirty = true;
+ if (link.dirty) l.draw() else if (l.fe.again(l)) link.again += 1;
+ var ms = l.fe.deadline(l);
+ if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) ms = @min(ms, frame_ms);
+ if (link.quietCheck()) |quiet| ms = @min(ms, quiet);
+ if (ms != 0) r.ready();
+ return ms;
+ }
+
+ /// The pass after its wait: its inputs settled; a rollback since it
+ /// drew is drawn again, never moved on from cells the copy put
+ /// back, and so is a frame owed or an echo changed.
+ pub fn after(l: *L) void {
+ const link = &l.link;
+ const r = link.r;
+ r.settle();
+ if (r.moved) {
+ r.moved = false;
+ link.dirty = true;
+ }
+ if (link.owed or r.echo.changed) link.dirty = true;
+ if (r.diverged != null) link.quit = true;
+ }
+
+ /// Its own view drawn by the frontend, outside any step: the
+ /// replica's digest stays the session's (views.drawOne); the frame
+ /// timings are this process's.
+ fn draw(l: *L) void {
+ const frames = l.frames;
+ frames.mark_start = pardes.FrameStats.nowNs();
+ frames.mark_update = frames.mark_start;
+ // A frontend that marks its render's end does so as it presents.
+ frames.mark_render = frames.mark_update;
+ l.link.owed = !l.link.r.draw(.{ .ctx = l.fe, .f = Frontend.present });
+ const shown = l.fe.drawn();
+ frames.record(pardes.FrameStats.nowNs(), shown.moving, l.link.owed);
+ l.link.drawn(shown.drew);
+ }
+ };
+}
diff --git a/runtime/root.zig b/runtime/root.zig
index 3297db5e..61db8cbe 100644
--- a/runtime/root.zig
+++ b/runtime/root.zig
@@ -4,6 +4,7 @@
//! (main.zig, macos.zig) sits beside it as a module of its own, naming it
//! and `core`.
pub const daemon = @import("daemon.zig");
+pub const client = @import("client.zig");
pub const upgrade = @import("upgrade.zig");
pub const crash = @import("crash.zig");
pub const addon_test = @import("addon_test.zig");
@@ -12,6 +13,7 @@ pub const ninep = @import("9p.zig");
test {
_ = daemon;
+ _ = client;
_ = upgrade;
_ = ninep_io;
_ = ninep_io.Client;