diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 11:14:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 15:57:34 -0300 |
| commit | 044da0f75ab5fb5be0213e512741860d95dad8e5 (patch) | |
| tree | 98c1dd8ddf2095ead2e783577124313b88eae9ef /src/Stream.zig | |
| parent | c4f3749c058121ba086a851188691e7ae40d2018 (diff) | |
| download | pardes-044da0f75ab5fb5be0213e512741860d95dad8e5.tar.gz pardes-044da0f75ab5fb5be0213e512741860d95dad8e5.zip | |
tape: every input the core consumes recorded in turn order, replayed into replicas that must reach the same digests; the core's leaks of render, clock and host closed
The recorder (src/tape.zig) is null unless PARDES_RECORD=<prefix> names a tape (<prefix><pid>.tape); the local path pays a null check a call. It records a tree at the host boundary, both ways: each call the host makes into the core (`Input`: update, serveFs, pump, perform, the shell's acknowledgements and failures, a Restore, ...) with its arguments, and each call the core makes out -- a Host function (the vtable wrapped generically while recording) or a syscall of its own (`tape.site` for every old `turn.yield()`, `tape.value` for a clock or environment read) -- with its result; `done`/`ret` close each, a 9P request served while a step is out lands inside that step's syscall, and fields a host writes between calls (native_images, present_skipped, ...) are found by a diff at every crossing. After each call the host made at the top, the session's digest (src/walk.zig), one per field of the core, its stream and each pane, file and places.
`PARDES_REPLAY=<tape> pardes` (main.zig) replays it with the same build into three replicas: two at different addresses (the first's memory held while the second runs) and one that never draws. A host call answers what it answered, a site returns what it returned, nothing is performed and no syscall is made; `turn.yield` panics in a replica, so a syscall no site covers is a stack trace; a call the recording did not make, or a digest that differs, is a divergence that names the fields. The headless replica steps over the presents it does not make, applying what the host told the core inside them.
What the replicas found, fixed:
- Render wrote core state. The view's geometry (wrap map, context rows, body layer rows) is File.geometry, a lazy cache keyed by a hash of what it is made of (revision, scroll, size, tags, tables read, context declarations, the session's view: wrap, layered context, row metrics, rect); the render makes it afresh, a step reading it makes it on demand (wrapAt, wrapRow, ensureCursorVisible). Context declarations, notice bands and the header's reveal are the step's (sync), the tables' reading the tick's (a first look of 4096 lines, a count, wherever it is asked; the rest a frame's clock budget a tick), and a view's paint-ahead is asked of the host as the frame is drawn rather than queued as an effect.
- The clock: advance's wakes are the recording session's (a picture decoding or a fade adds no tick to a replica), as are the budgets of Places, Names and Tables (`tape.over`), a message's time of day (the stamp; msg_said_ms and the unattended expiry use the session clock), File.mtime, and the pump reads its clock whether or not it waits.
- The host: the session directory (getcwd, asked once at init), $SHELL and its existence, $HOME for `~`, `Shell` naming one that is not there, a missing interpreter, a directory refused above, a deleted file, a pointer seen through a sliding pane (Pardes.pointerCell).
- Worker handles (futures, the Io), the 9P server's state, the store file every window shares (a replica keeps its own in memory) and Recent read from it are authority-only; frames, animations and the render's caches local. Query rows' paints count apart from a view's.
Terminals replay from their taped pty bytes and are digested as their active screen, cursor and scroll; kitty images are left out until phase 5's screen-state terminals. tagnav.golden: the header's caret reveal now runs every step, as a pane's does, not once per frame drawn, so after `g l B B` typed in one burst the bar stays scrolled as a human typing it sees it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/Stream.zig')
| -rw-r--r-- | src/Stream.zig | 82 |
1 files changed, 66 insertions, 16 deletions
diff --git a/src/Stream.zig b/src/Stream.zig index da7d12eb..8d9663dd 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -104,7 +104,13 @@ pub const ParseJob = struct { } }; -const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const ParseTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *ParseJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// The colours of the text a scroll is heading into, worked out on a /// worker before it gets there, as a game streams in the level ahead of @@ -134,12 +140,21 @@ pub const PaintJob = struct { } }; -const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const PaintTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *PaintJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// A paint's generation has the top bit, so it can never be a search's or /// a parse's (those count from 0 in `gen`, and it would take two billion /// searches to reach it). const paint_bit: u32 = 1 << 31; +/// A query rows' paint's has the next bit too: counted apart from the +/// paints a view's frames ask for (`rows_count`), which are its own. +const rows_bit: u32 = 1 << 30; const max_paint_bytes = 512 << 10; /// Where a worker hands a batch: false when it was turned away (the inbox @@ -172,6 +187,19 @@ pub const Job = struct { const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io }; +/// What the replay digest leaves out (src/walk.zig): the workers' timings, +/// and the colours painted ahead of a view for its frames, which is the +/// render's own prefetch (a replica that draws nothing paints none). +pub const replica = .{ + // The worker a search runs on, which the host starts. + .authority = .{"task"}, + .local = .{ + "started_ns", "first_ns", "finished_ns", "worker_ns", "parse_ns", + "paint", "paint_retired", "paint_count", "ahead", "ahead_for", + "painted_ahead", "painted_on_loop", "ahead_seen", "ahead_ask", + }, +}; + gen: u32 = 0, state: State = .idle, kind: Kind = .grep, @@ -210,6 +238,10 @@ paint: ?PaintTask = null, /// loop for half a second). No new paint starts while it is out. paint_retired: ?PaintTask = null, paint_count: u32 = 0, +rows_count: u32 = 0, +/// The paint a frame asked for, its worker started once the frame is +/// presented (`askAhead`): starting one is a thread, not a frame's to wait. +ahead_ask: ?u32 = null, ahead: []u8 = &.{}, ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, /// Stretches a view took painted ahead, and painted on the loop itself @@ -321,7 +353,17 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont const gen = paint_bit | (s.paint_count & ~paint_bit); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; s.paint = .{ .job = job }; - p.emit(.{ .search = .{ .gen = gen } }); + // Asked of the host once the frame is presented, not queued as an + // effect of the session's: a replica that draws nothing asks for none. + s.ahead_ask = gen; +} + +/// The worker for the paint the last frame asked for: the host starts it +/// after the present (Pardes.pump), off the keystroke's way to the screen. +pub fn askAhead(p: *Pardes) void { + const gen = p.stream.ahead_ask orelse return; + p.stream.ahead_ask = null; + if (p.host.vtable.search) |f| f(p.host.ctx, gen); } /// A file's colours for the query rows that show its lines, the whole @@ -363,8 +405,8 @@ pub fn paintRows(p: *Pardes, path: []const u8) void { }; s.rows_asked[s.rows_asked_at] = key; s.rows_asked_at +%= 1; - s.paint_count +%= 1; - const gen = paint_bit | (s.paint_count & ~paint_bit); + s.rows_count +%= 1; + const gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit)); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source }; s.rows_paint = .{ .job = job }; p.emit(.{ .search = .{ .gen = gen } }); @@ -1145,22 +1187,30 @@ const testing = std.testing; /// worker, hand what it posts to the core. const TestShell = struct { q: host_io.Inbox, + p: ?*Pardes = null, fn init() TestShell { return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; } + /// A shell's search: a worker started for the generation, as the tty's. const vtable: pardes.Host.VTable = .{ .search = struct { - fn f(_: ?*anyopaque, _: u32) void {} + fn f(ctx: ?*anyopaque, gen: u32) void { + const t: *TestShell = @ptrCast(@alignCast(ctx.?)); + start(testing.io, testing.allocator, t.p.?, gen, &t.q, host_io.Inbox.pushSearch); + } }.f }; - fn attach(p: *Pardes) void { - p.host = .{ .vtable = &vtable }; + fn attach(t: *TestShell, p: *Pardes) void { + t.p = p; + p.host = .{ .ctx = t, .vtable = &vtable }; } fn pump(t: *TestShell, p: *Pardes) void { + // What the last frame asked for, as Pardes.pump asks after a present. + askAhead(p); while (p.nextEffect()) |e| switch (e) { - .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch), + .search => p.perform(e), else => {}, }; var batch: host_io.Inbox.Batch = .{}; @@ -1226,7 +1276,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1328,7 +1378,7 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1367,7 +1417,7 @@ test "Stream: a scroll's colours are painted ahead on a worker, so PageDown afte pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1410,7 +1460,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" { pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1464,7 +1514,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1508,7 +1558,7 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1604,7 +1654,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them, p.gpa.free(pane.file.?.path); pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelPaint(); |
