summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 20:56:03 -0300
committerGabriel Schneider <[email protected]>2026-10-30 02:12:12 -0300
commit57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f (patch)
treeec77e56aa09568b592fbbb28fa2c379df922d922
parent3703c2526fb59708b00e12816bd88ffd8a15fc69 (diff)
downloadpardes-57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f.tar.gz
pardes-57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f.zip
replicas' core for a socket: a client's input settles as a `step` (no clock, no wait, no frame) on both sides, every kind of input speculated, the session's clock guessed, views no frontend here draws left undrawn, and a drawn view's selections given back to its slot
pardes.zig: `step`, the middle of a pump, is a client input's own top-level input on the authority and in the replica's speculation, so the two tapes read alike; a pump skips the live view when it is `undrawn` (a replica client's, drawn by its own process); `settle` is the pump's whole middle, the Upgrade's too. tape.zig: a `.step` input; `unattended` is a watched field; a speculation's clock reads answer a guess of the session's clocks (`Guess`); `resync` says the host and its writes again for a joiner; `adoptCore`; a short ret is shown in a trace. views.zig: `drawOne` draws a replica client's own view between steps; `drawParked` gives a drawn view's selections back to its slot, for drawOthers too. rollback.zig: std's lists by walk.isList (a safe build's have a third field). Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--src/pardes.zig66
-rw-r--r--src/rollback.zig3
-rw-r--r--src/tape.zig55
-rw-r--r--src/views.zig72
4 files changed, 152 insertions, 44 deletions
diff --git a/src/pardes.zig b/src/pardes.zig
index 02983c84..cc55d695 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -4710,6 +4710,9 @@ pub const Pardes = struct {
view: u8 = 0,
views_open: u8 = 1,
parked: [views.max]?*views.Parked = @splat(null),
+ /// The views no frontend here draws: a replica client's, which its own
+ /// process draws (src/replica.zig). A pump skips them.
+ undrawn: u8 = 0,
/// per-update scratch (paneCursorLines, selection text); reset each update
scratch: std.heap.ArenaAllocator,
/// The arena each frame's Surface text is built in, reset by `pump`. Hosts
@@ -4739,7 +4742,7 @@ pub const Pardes = struct {
"effects", "effects_head", "effects_len", "in_q", "in_head", "in_len",
// The views not live: written whenever one goes out, so the
// live one's slot is stale (views.zig).
- "parked",
+ "parked", "undrawn",
// How this process was started, and the last dump it made for
// its host to write: a session loaded elsewhere has its own.
"opts", "dump_out",
@@ -6056,13 +6059,6 @@ pub const Pardes = struct {
}
}
- /// What a step leaves queued, its inputs and its effects, taken now:
- /// the session at a step boundary (an Upgrade's dump, upgrade.zig).
- pub fn settle(p: *Pardes) void {
- while (p.nextQueued()) |ev| p.update(ev);
- while (p.nextEffect()) |e| p.perform(e);
- }
-
pub fn pump(p: *Pardes, host: Host) !void {
p.host = host;
const taped = tape.enter(p, .pump);
@@ -6087,15 +6083,7 @@ pub const Pardes = struct {
}
p.frames.mark_start = FrameStats.nowNs();
if (v.now) |clock| p.advance(clock(h.ctx));
- // What a 9P open wrote in a row goes in once the writes pause.
- ctlfs.pane.flushPausedBatch(p);
- // ...and what a /pager open was written before its close.
- ctlfs.ctl.fillClosedPagers(p);
- // ...and the last line of an open closed with no newline after it.
- ctlfs.runClosedLines(p);
- while (p.nextQueued()) |ev| p.update(ev);
- while (p.nextEffect()) |e| p.perform(e);
- turn.bump(&turn.epoch);
+ p.settle();
// A quitting frame has already freed what it would draw.
if (p.quit) return;
if (v.poll_frame) |f| f(h.ctx);
@@ -6111,13 +6099,17 @@ pub const Pardes = struct {
if (v.watched) |f| if (!f(h.ctx)) return;
// The other views first, each into its own window or terminal.
if (views.several(p)) try views.drawOthers(p, h);
- _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept });
- p.frames.mark_update = FrameStats.nowNs();
- p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0;
- const surface = try p.render(p.frame_arena.allocator());
- p.frames.mark_render = FrameStats.nowNs();
- if (v.present) |f| f(h.ctx, surface);
- Stream.askAhead(p);
+ // A replica client's view live (its input the last): its own
+ // process draws it.
+ if (p.undrawn & (@as(u8, 1) << @intCast(p.view)) == 0) {
+ _ = p.frame_arena.reset(.{ .retain_with_limit = memory.limits.frame_kept });
+ p.frames.mark_update = FrameStats.nowNs();
+ p.frames.view = if (p.panes[p.active]) |pane| pane.scroll() else 0;
+ const surface = try p.render(p.frame_arena.allocator());
+ p.frames.mark_render = FrameStats.nowNs();
+ if (v.present) |f| f(h.ctx, surface);
+ Stream.askAhead(p);
+ }
// Settled before post_present: a shell ticks there, and what that
// tick moves owes the next frame.
const skipped = p.present_skipped;
@@ -6129,6 +6121,32 @@ pub const Pardes = struct {
p.frames.record(FrameStats.nowNs(), owed or p.continuous(), skipped);
}
+ /// What a pump does between its wait and its frame: the queued inputs
+ /// and the effects. Also the session at a step boundary, taken now (an
+ /// Upgrade's dump, upgrade.zig).
+ pub fn settle(p: *Pardes) void {
+ // What a 9P open wrote in a row goes in once the writes pause.
+ ctlfs.pane.flushPausedBatch(p);
+ // ...and what a /pager open was written before its close.
+ ctlfs.ctl.fillClosedPagers(p);
+ // ...and the last line of an open closed with no newline after it.
+ ctlfs.runClosedLines(p);
+ while (p.nextQueued()) |ev| p.update(ev);
+ while (p.nextEffect()) |e| p.perform(e);
+ turn.bump(&turn.epoch);
+ }
+
+ /// A replica client's input settled on the authority (src/replica.zig):
+ /// a pump that waits on nothing, reads no clock and draws nothing, so
+ /// the client's own core can apply the same at once and expect the
+ /// authority's tape of it to read the same. The session's own pump
+ /// draws it after.
+ pub fn step(p: *Pardes) void {
+ const taped = tape.enter(p, .step);
+ defer tape.leave(p, taped);
+ p.settle();
+ }
+
/// One request of the control filesystem, from whichever thread has the
/// turn (`pardes.turn`). Not an event: it neither sweeps nor resets
/// anything the editor's own step may be in the middle of using, so it
diff --git a/src/rollback.zig b/src/rollback.zig
index 6496a69f..7216664c 100644
--- a/src/rollback.zig
+++ b/src/rollback.zig
@@ -305,7 +305,8 @@ pub const Copy = struct {
// by a speculation: terminal.zig's guards).
if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return;
if (s.layout == .@"packed") return;
- if (comptime @hasField(T, "items") and @hasField(T, "capacity") and s.field_names.len == 2) {
+ // std's lists (a safe build's have a third field, a lock).
+ if (comptime walk.isList(T)) {
if (v.capacity == 0) return;
_ = try c.allocation(@intFromPtr(v.items.ptr));
const E = @typeInfo(@FieldType(T, "items")).pointer.child;
diff --git a/src/tape.zig b/src/tape.zig
index b1080149..c2ddbb9d 100644
--- a/src/tape.zig
+++ b/src/tape.zig
@@ -36,6 +36,7 @@ pub const Input = union(enum) {
update: pardes.Event,
serve_fs: pardes.ctlfs.Req,
pump,
+ step,
perform: pardes.Effect,
next_effect,
advance: u64,
@@ -93,9 +94,12 @@ pub const Site = enum(u16) {
};
/// Fields a host writes straight into the core between its calls.
-const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" };
+const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" };
const Kind = enum(u8) { input, done, host, ret, set, digest };
+/// A digest record's kind byte (replica.zig reads a unit's last).
+pub const digest_kind: u8 = @backingInt(Kind.digest);
+pub const set_kind: u8 = @backingInt(Kind.set);
const host_fns = @typeInfo(VTable).@"struct".field_names;
/// The `set` that says which Host functions the recording host had.
const set_mask = 0xff;
@@ -163,6 +167,10 @@ pub const Tape = struct {
/// output carries one, at the pty's read rate), not one a field; what
/// differs is then not named.
whole_digest: bool = false,
+ /// A replica client's guess of the session's clocks for a speculation
+ /// (src/replica.zig): its clock reads (a message's stamp, a file's
+ /// mtime) answer this instead of the clocks here.
+ guess: ?Guess = null,
fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void {
t.scratch.clearRetainingCapacity();
@@ -501,6 +509,7 @@ pub const Tape = struct {
.update => |ev| p.update(ev),
.serve_fs => |req| _ = p.serveFs(req),
.pump => p.pump(t.hostOf(t.mask)) catch {},
+ .step => p.step(),
.perform => |e| p.perform(e),
.next_effect => _ = p.nextEffect(),
.advance => |now| p.advance(now),
@@ -758,6 +767,12 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt
t.poisoned = true;
return poisonOf(R);
}
+ if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| {
+ const r = guessed(R, g);
+ t.record(.host, u16, &id);
+ t.record(.ret, R, &r);
+ return r;
+ };
t.record(.host, u16, &id);
t.seeFields();
t.host_side = true;
@@ -768,6 +783,43 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt
return r;
}
+pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 };
+
+/// A clock read of the session's, as a replica client guesses it: a file's
+/// mtime (realtime seconds), a message's clock (monotonic ms), a message's
+/// time of day (in the session's zone).
+fn guessed(comptime R: type, g: Guess) R {
+ if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0;
+ if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms);
+ if (R == [8]u8) {
+ var clock: [8]u8 = "--:--:--".*;
+ const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false;
+ if (notime) return clock;
+ const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400);
+ _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {};
+ return clock;
+ }
+ @compileError("tape: no guess of a clock read as " ++ @typeName(R));
+}
+
+/// A core made outside the tape (a replica client's, from a dump) is the
+/// one it replays into.
+pub fn adoptCore(t: *Tape, p: *Pardes) void {
+ t.core = p;
+ p.host = t.hostOf(t.mask);
+}
+
+/// A replica joins the session recorded on `t` (src/replica.zig): what the
+/// host wrote in the core and which host it is are said again at the next
+/// input, for a core made from a dump, which has neither.
+pub fn resync(t: *Tape) void {
+ t.seen = @splat(0);
+ if (t.core) |p| if (p.host.vtable == &t.vtable) {
+ p.host = t.real;
+ };
+ t.real = .{};
+}
+
/// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent:
/// what the clock said is taped, so a replica stops its work where this did.
pub fn over(since_ns: u64, budget_ns: u64) bool {
@@ -964,6 +1016,7 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void {
const id = std.mem.bytesToValue(u16, payload[0..2]);
w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {};
},
+ .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {},
else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {},
}
w.writeByte('\n') catch {};
diff --git a/src/views.zig b/src/views.zig
index b9760143..361d14a3 100644
--- a/src/views.zig
+++ b/src/views.zig
@@ -508,28 +508,64 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void {
}
for (0..max) |i| {
const k: u8 = @intCast(i);
- if (k == live or !isOpen(p, k)) continue;
- // `unpark` hands each slot's selections to its body; drawn, the
- // view is out again, and they are the slot's again (else a frame
- // drawn would take them from a view, and a replica that draws
- // nothing would keep them).
- var sels: [MAX_PANES]struct { s: @TypeOf(@as(Pane, undefined).body.sels), n: @TypeOf(@as(Pane, undefined).body.nsel) } = undefined;
- for (p.panes, 0..) |s, id| if (s) |pane| {
- sels[id] = .{ .s = pane.pv[k].body.sels, .n = pane.pv[k].body.nsel };
- };
- defer for (p.panes, 0..) |s, id| if (s) |pane| {
- pane.pv[k].body.sels = sels[id].s;
- pane.pv[k].body.nsel = sels[id].n;
- };
- unpark(p, k);
- _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept });
- const surface = try p.render(p.frame_arena.allocator());
- if (v.present) |f| f(h.ctx, surface);
+ if (k == live or !isOpen(p, k) or p.undrawn & (@as(u8, 1) << @intCast(k)) != 0) continue;
// No post_present: what a shell acknowledges there moves the
// session's pointer and hover, which are the live view's to move;
// a view's panels are acknowledged when it is live and drawn.
- copy(&drawn_fields, &p.parked[k].?.view, p);
+ try drawParked(p, k, .{ .ctx = h.ctx, .f = v.present });
+ }
+}
+
+pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) void };
+
+/// Parked view `k` unparked, drawn and presented. `unpark` hands each
+/// slot's selections to its body; drawn, the view is out again, and they
+/// are the slot's again (else a frame drawn would take them from a view,
+/// and a replica that draws nothing would keep them).
+fn drawParked(p: *Pardes, k: u8, present: Present) !void {
+ var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined;
+ for (p.panes, 0..) |s, id| if (s) |pane| {
+ sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel };
+ };
+ unpark(p, k);
+ defer for (p.panes, 0..) |s, id| if (s) |pane| {
+ pane.pv[k].body.sels = sels[id][0];
+ pane.pv[k].body.nsel = sels[id][1];
+ };
+ _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept });
+ const surface = try p.render(p.frame_arena.allocator());
+ if (present.f) |f| f(present.ctx, surface);
+ copy(&drawn_fields, &p.parked[k].?.view, p);
+}
+
+/// View `k` drawn and presented outside any step, by a replica client
+/// (src/replica.zig) whose own it is: as `drawOthers` draws one, the live
+/// view put back after exactly as it was, so the replica's digest is the
+/// authority's still. No post_present, as there.
+pub fn drawOne(p: *Pardes, k: u8, present: Present) !void {
+ if (k == p.view or !isOpen(p, k)) {
+ _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept });
+ const surface = try p.render(p.frame_arena.allocator());
+ if (present.f) |f| f(present.ctx, surface);
+ return;
+ }
+ const live = p.view;
+ park(p);
+ defer {
+ copy(&fields, p, &p.parked[live].?.view);
+ p.view = live;
+ for (p.panes) |s| if (s) |pane| {
+ const pv = &pane.pv[live];
+ copy(&pane_fields, pane, pv);
+ copy(&body_fields, &pane.body, &pv.body);
+ if (!pane.isTerminal()) {
+ pane.cols = pv.cols;
+ pane.rows = pv.rows;
+ }
+ if (pane.file) |*f| f.scroll = pv.scroll;
+ };
}
+ try drawParked(p, k, present);
}
// ---- tests ----