From 46952caa8e7087c27dea55b25f067e1bad06b859 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 06:12:36 -0300 Subject: replica: the tty client draws as the session's own terminal does: its loop waits in the session's own wait (Shell.waitInput: an input drawn at once, the session's units and deltas its output, drawn a frame a tick at most but for the echo of its own input, its glide and jump trail on the tick grid), and its glide and trail frames are the last frame presented again, moved on (as ownFrame), not a whole render of its view each tick; a unit of the session's that comes while it is idle is replayed over the copy taken ahead and kept for a rollback (Replica.since; let go at rebase_after) instead of the copy let go and walked again, a replay that moved nothing a digest covers draws no frame, and a rollback (toConfirmed) draws again what it put back. tty, ReleaseFast, 50,000-line file, client instructions against the own path's (before -> after; own unchanged): type 47.8M -> 17.3M (own 11.6M; terminal reads a key 7.7 -> 1.3, as own), page-down 103.3M -> 49.4M (52.6M), wheel 54.5M -> 30.8M (41.6M), open 411.2M -> 68.6M (76.8M), cat 20k 1,605M -> 130.8M with the session's deltas a frame (142.5M), key-down 6.36M -> 6.19M (3.86M), edit 20.35M -> 20.36M (14.56M), resize 8.25M -> 8.26M (5.72M): what is left on the small gestures is the copy a megabyte walked ahead of each input (copy_us p50 113 us); PARDES_REPLICA_STATS adds draws and again; load 1.6-10, wall to retake quiet; core-test 819/958, shell-test 993/996, behaviour-test 269/273 and --replay (the base's four addon ABI tests), process-test 2 of 144 (lspcomplete, lspcompletemoved), pane-test 180/181, replicas diverged 0 unequal 0 and deltas checked/wrong n/0, daemon.py plain, --far, --games, --window-clients Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/tui/tui.zig | 62 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 25 deletions(-) (limited to 'src/tui') diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 919eeded..6d323183 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -758,30 +758,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 {}; @@ -789,9 +793,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); @@ -1604,6 +1605,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(); }; @@ -1729,6 +1731,16 @@ const Shell = struct { }, .replica_mail => 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) { -- cgit v1.3 From 96e8398a38bba02e0ea5a9e59c14016dba270531 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 06:20:16 -0300 Subject: session: a detached session's terminal deltas go a unit a frame, as a terminal's frame paces its output (waitInput), not a unit and a delta for every pty read: the unit sent at the step boundary is its clients' frame (drawn_ns), a client's acknowledgement of a delta no longer counts as an input (Peers.urgent: an input or a join wakes the step at once, an ack waits for the next frame), and its clients' echo is the frame's after an input (pardes.frame_ns, where a terminal of its own keeps its second); a session with no client and the own frontend's loop are as they were. cat of 20,000 lines in a shell pane of a client, tty, ReleaseFast, per cat: the session's own instructions on top 313.8M -> 74.8M (boundaries 9,165 -> 1,515 and units 11,460 -> 3,807 over the whole run), the client's 1,605M -> 145.7M with the client's loop change below it (own 115.3M); settled wall min 71.6 -> 35.1 ms, median 179.6 -> 131.9 ms (own 16.8 / 17.9; load 4-27, to retake quiet); window client's cat session 113.3M -> 81.5M Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/replica.zig | 5 +++++ src/tui/tui.zig | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'src/tui') diff --git a/src/replica.zig b/src/replica.zig index dbdc3146..2cc7dd55 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1923,6 +1923,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, @@ -1972,6 +1976,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); } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 6d323183..7cc45e25 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1107,6 +1107,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); @@ -1580,9 +1584,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; @@ -1729,7 +1737,9 @@ 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 -- cgit v1.3