summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 10:19:31 -0300
committerGabriel Schneider <[email protected]>2026-11-18 10:19:31 -0300
commita2538c49b1bb69dcd1a6f1350834e13f68c3c05d (patch)
tree92c0cfb229d44974bf34ce6d4caa379142fbae44
parentb2f709d7130571658220636f33b5f89414a5101a (diff)
parent5c5e0102f92938f433a3465c92818be810c1ef78 (diff)
downloadpardes-a2538c49b1bb69dcd1a6f1350834e13f68c3c05d.tar.gz
pardes-a2538c49b1bb69dcd1a6f1350834e13f68c3c05d.zip
integration: addons in a client part 3 (the core's prerequisites and the double free; the PDF kind where it is shown: a plain record and pages as draws) with the client path's round 1: no conflict; tui core-test 820/959 (139 skipped), shell-test 994/997, behaviour-test 274/274, replicas pass, pane-test 180/181; gui unit-test 1588/1592 (its perf check refuses the baseline by compiler version, as on the base), the tui build has no SDL symbol, the frame corpus 74/77 against mbase in Debug and ReleaseFast: frames 58 to 60 show WindowOpacity's notice alone where the base stacked it under InactiveDim's (a PDF render's answer no longer lands between the two ctl writes: accepted, and the baseline recorded anew as mbase2 with those three frames), the perf window -0.405% (threshold 0.5%)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--docs/typ/running.typ11
-rw-r--r--src/gui/gui.zig26
-rw-r--r--src/main.zig17
-rw-r--r--src/replica.zig42
-rw-r--r--src/tui/tui.zig76
5 files changed, 129 insertions, 43 deletions
diff --git a/docs/typ/running.typ b/docs/typ/running.typ
index e78c8f1a..07274157 100644
--- a/docs/typ/running.typ
+++ b/docs/typ/running.typ
@@ -81,9 +81,14 @@ the same). It draws nothing and is 160×50 until an attach or the root
#file("ctl")'s `size` sizes it. Until something attaches, 9P is the only
way in (#doc("scripting")).
-A terminal's detached session (`pardes --detach`) runs on through its
-terminal's hangup while a daemon keeps it. A window's
-(`pardes-gui --detach`) ends on it, as any job does. With no daemon,
+// main.zig, the --detach branch (the exec of the pardes beside it).
+`pardes-gui --detach` runs the `pardes` installed beside it in its place:
+a detached session draws nothing, and a window's binary would start a GPU
+device and its fonts for it. So every detached session is a terminal's
+(`ps` says tty), and a window shows it as a client; only where no `pardes`
+is beside it is the session the window's binary, with no window.
+A terminal's detached session runs on through its terminal's hangup while
+a daemon keeps it; a window's binary's ends on it, as any job does. With no daemon,
`--detach` still starts a session, which nothing can attach to: only 9P
reaches it.
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 3af66b12..a8816494 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -1439,6 +1439,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
// What it is, then its size, are its view's first inputs.
link.input(.{ .frontend = .{ .windowed = true, .native_images = true, .native_image_patches = true } });
var drew = false;
+ var again = false;
while (!link.quit) {
core.frames.period_ns = refreshNs(g);
var own: OwnView = .{ .g = g, .in = &in, .s = &shell, .pointer = drew };
@@ -1451,14 +1452,25 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
if (link.dirty) {
client.draw(&shell);
drew = shell.presented;
- }
+ } else if (again and shell.presented) if (shell.surface) |frame| {
+ // A blink, the window's own motion: the last frame again,
+ // moved on, as the session's own window draws it (pollFrame).
+ g.now_ns = shell.ownNow();
+ shell.presented = renderFrame(g, gpa, core, frame) catch false;
+ drew = shell.presented;
+ };
+ again = false;
// The window's own motions (its glide, its caret, a jump's
// trail) and its blink are on its clock: no unit need come.
const minimized = !test_mode and c.SDL_GetWindowFlags(g.window) & (c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_OCCLUDED) != 0;
const moving = !minimized and frontendMoving(g);
const blink = if (minimized or shell.surface == null) null else blinkNow(g, core.settings.cursor_blink);
const predicting = r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0;
- var ms: c_int = if (test_mode) 16 else -1;
+ // A latency trace's feed paces the loop, as the session's own
+ // window's (waitInput): its poll on stdin is the wait, and SDL's,
+ // which polls a clock and a lock a millisecond at a time with
+ // the offscreen driver, is not waited too (a frame a loop more).
+ var ms: c_int = if (latency_fd >= 0) 0 else if (test_mode) 16 else -1;
if (demoDue(g)) |due| ms = if (ms < 0) due else @min(ms, due);
if (predicting or link.owed) ms = if (ms < 0) frame_ms else @min(ms, frame_ms);
// A touchpad's fraction settling onto a row (scrollRows).
@@ -1472,7 +1484,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
};
if (link.quietCheck()) |wait| ms = if (ms < 0) @intCast(wait) else @min(ms, @as(c_int, @intCast(wait)));
// Idle, its frame drawn: the next input needs no copy taken.
- if (ms != 0) r.ready();
+ if (ms != 0 or latency_fd >= 0) r.ready();
if (shell.surface != null) startGamepads(g);
var sev = std.mem.zeroes(c.SDL_Event);
g.input_seen = false;
@@ -1489,12 +1501,18 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
// Its draws done (api.draw: this window's own pictures).
if (shell.takeDraws()) link.dirty = true;
r.settle();
+ // A rollback since it drew: drawn again, never moved on from
+ // cells the copy put back.
+ if (r.moved) {
+ r.moved = false;
+ link.dirty = true;
+ }
if (titled and link.notice_len == 0) {
titled = false;
_ = c.SDL_SetWindowTitle(g.window, "pardes");
}
if (link.owed or r.echo.changed) link.dirty = true;
- if (moving or (blink != null and (blink.?.edge or timed_out))) link.dirty = true;
+ if (moving or (blink != null and (blink.?.edge or timed_out))) again = true;
if (r.diverged != null) link.quit = true;
}
if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
diff --git a/src/main.zig b/src/main.zig
index 864e2485..08edc0c1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1501,6 +1501,23 @@ fn nativeMain(std_init: std.process.Init) !void {
};
if (attach) |name| if (!resuming) attachTo(init, opts, name);
if (detach) |name| {
+ // A window's binary would start SDL, a GPU device and its fonts for
+ // a window nobody sees (638M instructions, its socket at 255 ms,
+ // 1.2% of a CPU idle): the terminal's binary beside it is the
+ // detached session, as `pardes --detach` (`ps` says tty), its
+ // windows clients. Without one there, it carries on as a window's.
+ if (comptime pardes.platform == .gui) if (!resuming) {
+ var exe: [4096]u8 = undefined;
+ const n = std.c.readlink("/proc/self/exe", &exe, exe.len);
+ if (n > 0) if (std.Io.Dir.path.dirname(exe[0..@intCast(n)])) |bin| if (args.len < 64) {
+ var path_buf: [4104]u8 = undefined;
+ const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/pardes", .{bin}, 0) catch unreachable; // 4096 + 8 fits
+ var argv: [64:null]?[*:0]const u8 = @splat(null);
+ argv[0] = path.ptr;
+ for (args[1..], 1..) |a, k| argv[k] = a.ptr;
+ _ = execvp(path.ptr, &argv);
+ };
+ };
// Said plainly, never a Zig error trace: a name a live session's
// socket holds already (a plain session is named by its pid too).
if (name.len != 0 and !resuming and ninep_io.taken(name)) die(init.io, "a session named {s} is already running", .{name});
diff --git a/src/replica.zig b/src/replica.zig
index d10cdf83..f3ea3eee 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -852,13 +852,11 @@ pub const Replica = struct {
const u = parseUnit(msg) orelse return r.fail("a unit too short to read");
r.stats.units += 1;
if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq);
- // A copy taken ahead (`ready`), nothing on top: the unit moves the
- // state it was of.
- if (r.applied == 0 and r.copy.held) {
- r.copy.release();
- rollback.Epoch.select(null);
- r.clearSince();
- }
+ // A copy taken ahead (`ready`), nothing on top: it stays the point a
+ // rollback goes back to, the unit replayed on it and kept in `since`,
+ // not let go and taken again (a megabyte walked) for each unit of
+ // the session's that comes while the client is idle.
+ const ahead = r.applied == 0 and r.copy.held;
if (r.guess) {
r.stamp = .{ .real_ns = u.real_ns, .mono_ns = u.mono_ns, .at_ns = nowNs() };
if (u.origin == r.id) for (r.pending.items) |p| if (p.seq == u.seq) {
@@ -931,7 +929,10 @@ pub const Replica = struct {
r.recovering = nowNs();
r.toConfirmed();
}
- r.moved = true;
+ // What it shows moved only when what a digest covers did: a unit of
+ // the session's pump that read its clock and moved nothing draws
+ // no frame.
+ if (ends == null or !std.mem.eql(u8, ends.?, r.last_digest.items)) r.moved = true;
if (tape.replayUnit(&r.t, u.tape)) |why| {
trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape});
return r.fail(why);
@@ -942,6 +943,7 @@ pub const Replica = struct {
r.freePending(r.pending.orderedRemove(0));
}
r.dirty = true;
+ if (ahead) r.keepSince(u.tape);
}
fn fail(r: *Replica, why: []const u8) void {
@@ -955,6 +957,12 @@ pub const Replica = struct {
r.since.append(r.gpa, kept) catch return r.fail("out of memory");
if (r.since.items.len >= rebase_after) {
r.stats.rebases += 1;
+ // Nothing on top: it is the confirmed state already.
+ if (r.applied == 0) {
+ r.copy.release();
+ rollback.Epoch.select(null);
+ return r.clearSince();
+ }
r.toConfirmed();
r.dirty = true;
}
@@ -973,6 +981,8 @@ pub const Replica = struct {
const whole = r.copy.restore();
rollback.Epoch.select(null);
if (!whole) return r.fail("out of memory for a rollback");
+ // The frame its host drew is of the memory put back: drawn again.
+ r.moved = true;
// The rows a terminal was read as are no copy's: what the
// speculation built was the epoch's, freed with it, and what
// was there before is of a grid the deltas since moved on
@@ -1915,6 +1925,10 @@ pub const Peers = struct {
taken: std.ArrayListUnmanaged(Note) = .empty,
/// A reader posted: the loop's one test a step while nothing is attached.
mail: std.atomic.Value(bool) = .init(false),
+ /// ...and what it posted was a client's input or a join, which the
+ /// session steps at once; a terminal's acknowledgement waits for the
+ /// session's next frame, as its output does (tui.zig `waitInput`).
+ urgent: std.atomic.Value(bool) = .init(false),
/// Wakes the session's loop (its wait for input) from a reader.
wake_ctx: ?*anyopaque = null,
wake: ?*const fn (?*anyopaque) void = null,
@@ -1964,6 +1978,7 @@ pub const Peers = struct {
return;
};
ps.mail.store(true, .release);
+ if (note != .screen) ps.urgent.store(true, .release);
ps.lock.unlock(ps.io);
if (ps.wake) |w| w(ps.wake_ctx);
}
@@ -2938,6 +2953,10 @@ pub const Link = struct {
/// load took (PARDES_REPLICA_STATS).
join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{},
join_ms: u64 = 0,
+ /// Frames it drew whole (`drawn`), and the last one presented again
+ /// moved on (its own motion), for PARDES_REPLICA_STATS.
+ draws: u32 = 0,
+ again: u32 = 0,
/// The test knobs the environment sets (PARDES_REPLICA_CHECK,
/// PARDES_TEST_DIVERGE).
@@ -3033,6 +3052,10 @@ pub const Link = struct {
if (drew) c.visible += 1;
}
c.dirty = false;
+ c.draws += 1;
+ // What moved before this frame is in it: not drawn again at the
+ // next unit (a stopped input's rollback, under its own frame).
+ c.r.moved = false;
while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) {
c.cut();
break;
@@ -3082,12 +3105,13 @@ pub const Link = struct {
var buf: [1024]u8 = undefined;
const e = &c.r.echo.stats;
var w: std.Io.Writer = .fixed(&buf);
- w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}", .{
+ w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d} draws {d} again {d}", .{
st.inputs, st.speculated, st.stopped, st.units, st.fast,
c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000,
pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000,
pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk,
c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted,
+ c.draws, c.again,
}) catch return;
// What stopped its inputs, last: a word a pair before it.
w.writeAll(" stops ") catch return;
diff --git a/src/tui/tui.zig b/src/tui/tui.zig
index 3938c854..5714ea0c 100644
--- a/src/tui/tui.zig
+++ b/src/tui/tui.zig
@@ -760,30 +760,34 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
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);
- // The tty's own motions (its glide, a jump's trail) drawn a frame
- // a tick while they move, as waitInput wakes the local loop: a
- // wheel's glide was drawn only at the next event. After the
- // draw, which starts a glide, and after the last iteration's
- // wakes, as the latest asked for is the one kept.
- if (sh.moving()) {
- link.dirty = true;
- requestWake(io, @intCast(pardes.frame_ns / std.time.ns_per_ms));
- }
- const first = (loop.tryEvent() catch null) orelse idle: {
- // Idle, its frame drawn: the next key needs no copy taken.
- r.ready();
- break :idle loop.nextEvent() catch break;
- };
- var ev = first;
- var batch: usize = 1;
- while (true) {
- _ = sh.apply(ev);
- if (link.quit or (!sh.in_paste and batch >= 64)) break;
- ev = (loop.tryEvent() catch null) orelse break;
- batch += 1;
+ 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): nothing of it
+ // is written in the core, as `Client.draw` puts it back.
+ const skipped = r.core().present_skipped;
+ Shell.present(&sh, &r.core().surface);
+ r.core().present_skipped = skipped;
+ 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 {};
@@ -791,9 +795,6 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
link.dirty = true;
}
if (link.owed or r.echo.changed) link.dirty = true;
- // Predictions out: judged a frame at a time while they are.
- if (r.echo.n > 0 or r.echo.cursor != null or r.echo.nd > 0) requestWake(io, @intCast(pardes.frame_ns / std.time.ns_per_ms));
- if (link.quietCheck()) |ms| requestWake(io, ms);
if (r.diverged != null) link.quit = true;
}
if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
@@ -1110,6 +1111,10 @@ fn localSession(
try core.pump(host);
// Its unit to each replica client, and theirs taken in.
sh.peers.boundary(core);
+ // Detached, the unit is its clients' frame: its output is paced to
+ // it as a terminal's frame paces it (waitInput), a unit a frame, not
+ // a unit and a delta for every read of a flood.
+ if (sh.peers.recording and sh.detached()) sh.drawn_ns = @intCast(std.Io.Clock.awake.now(io).nanoseconds);
upgrade.locate();
// Asked in the step (the word), done between steps: the session whole.
if (sh.upgrade_asked) |pane| sh.upgradeNow(pane);
@@ -1583,9 +1588,13 @@ const Shell = struct {
// (its frame held for the terminal's answer, FrameAck), or the
// focused pane's output soon after one, its echo (a remote shell's
// an RTT later).
+ // Detached, its clients' echo is the frame's after an input: what a
+ // flood says after that goes a unit a frame (their own echo is
+ // predicted, src/echo.zig), not a unit and a delta a read for a second.
+ const echo_ns: u64 = if (s.detached()) pardes.frame_ns else std.time.ns_per_s;
while (!paced and s.output_seen and !s.input_seen and !s.core.quit and s.test_clock == null) {
if (s.input_ns > s.drawn_ns) break;
- if (s.focused_ns > s.drawn_ns and s.focused_ns -| s.input_ns < std.time.ns_per_s) break;
+ if (s.focused_ns > s.drawn_ns and s.focused_ns -| s.input_ns < echo_ns) break;
const due = s.drawn_ns + pardes.frame_ns;
const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
if (now + std.time.ns_per_ms > due) break;
@@ -1608,6 +1617,7 @@ const Shell = struct {
pardes.turn.rest();
const got = s.loop.nextEvent() catch {
pardes.turn.wake();
+ if (s.client) |c| c.link.quit = true;
s.core.quit = true;
return s.reloadWatched();
};
@@ -1731,8 +1741,20 @@ const Shell = struct {
s.peers.handed(j.fd, j.view);
s.input_seen = true;
},
- .replica_mail => s.input_seen = true,
+ .replica_mail => if (s.peers.urgent.swap(false, .acq_rel)) {
+ s.input_seen = true;
+ },
.said => |said| if (s.client) |c| {
+ // The session's units and deltas are a client's output
+ // (waitInput paces it): a terminal's delta, or the unit
+ // that answers its own input, is its echo, drawn at once.
+ s.output_seen = true;
+ const echo = switch (said) {
+ .screen => true,
+ .unit => |b| if (replica.parseUnit(b)) |u| u.origin == c.link.r.id else false,
+ else => false,
+ };
+ if (echo) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
c.link.take(said);
if (said == .refused) return true;
} else switch (said) {