summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 06:12:36 -0300
committerGabriel Schneider <[email protected]>2026-11-18 08:01:10 -0300
commit46952caa8e7087c27dea55b25f067e1bad06b859 (patch)
tree32faa1fe2a6f79af05b3b189ab452af04a6be29f /src
parent22b5ada4c099618e72af3d33dc274a7c272dd227 (diff)
downloadpardes-46952caa8e7087c27dea55b25f067e1bad06b859.tar.gz
pardes-46952caa8e7087c27dea55b25f067e1bad06b859.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
-rw-r--r--src/replica.zig37
-rw-r--r--src/tui/tui.zig62
2 files changed, 65 insertions, 34 deletions
diff --git a/src/replica.zig b/src/replica.zig
index f84e61e8..dbdc3146 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -850,13 +850,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) {
@@ -929,7 +927,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);
@@ -940,6 +941,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 {
@@ -953,6 +955,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;
}
@@ -971,6 +979,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
@@ -2936,6 +2946,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).
@@ -3031,6 +3045,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;
@@ -3080,12 +3098,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 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) {