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/File.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/File.zig')
| -rw-r--r-- | src/File.zig | 104 |
1 files changed, 89 insertions, 15 deletions
diff --git a/src/File.zig b/src/File.zig index e4bca000..d795eaa1 100644 --- a/src/File.zig +++ b/src/File.zig @@ -104,6 +104,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Snapshot) void { } pub const History = struct { + pub const replica = .{ .len = .{ .{ "undo", "undo_len" }, .{ "redo", "redo_len" } } }; undo: [limits.undo_max]Snapshot = undefined, undo_len: usize = 0, redo: [limits.undo_max]Snapshot = undefined, @@ -137,6 +138,10 @@ pub const History = struct { /// A file pane's backing: owned content, its derived caches, and undo history. pub const State = struct { + /// What the replay digest leaves out (src/walk.zig): what is made again + /// from the text whenever it is wanted. + pub const replica = .{ .derived = .{ "line_starts", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_history", "tables" } }; + path: []u8, content: []u8, revision: u32 = 0, @@ -1252,7 +1257,8 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void { if (f.saved_hash) |h| if ((f.saved_len orelse new.len) == new.len and std.hash.Wyhash.hash(0, new) == h) { f.saved_revision = f.revision; }; - f.mtime = pardes.ctlfs.events.now(); + // The clock the session that recorded read (src/tape.zig). + f.mtime = pardes.tape.value(.mtime, pardes.ctlfs.events.now, .{}); if (f.line_starts.len > 0) p.gpa.free(f.line_starts); f.line_starts = starts; if (f.highlights.len > 0) p.tree_sitter_gpa.free(f.highlights); @@ -1647,6 +1653,8 @@ pub fn redo(p: *Pardes, pane: *Pane) void { /// `<file> deleted on disk (Save writes it again)`, logged `changed /// <serial> deleted`. pub fn deleted(p: *Pardes, id: u8) void { + const taped = pardes.tape.enter(p, .{ .file_deleted = id }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; const f = if (pane.file) |*file| file else return; if (f.disk_gone) return; @@ -1703,6 +1711,17 @@ pub fn changed(p: *Pardes, id: u8, bytes: []const u8) void { /// re-highlight the visible window of any file whose syntax went stale /// (edit, scroll, load) — visible-range-first so big files stay snappy +/// The declarations a TreeContext pane's context rows are drawn from, for +/// its text as it is now: found in the step (Pardes.sync), never only by a +/// render, since the view's geometry is made of them (`geometry`). +pub fn refreshContext(p: *Pardes, pane: *Pane, f: *State) void { + if (!f.tree_context or !supportsContext(pane) or f.context_revision == f.revision) return; + const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch return; + if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); + f.context_declarations = declarations; + f.context_revision = f.revision; +} + pub fn refreshHighlights(p: *Pardes) void { const tz = tracy.zone(@src(), "refreshHighlights"); defer tz.end(); @@ -1710,12 +1729,6 @@ pub fn refreshHighlights(p: *Pardes) void { const pane = slot orelse continue; if (pane.file == null) continue; const f = &pane.file.?; - if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) { - const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue; - if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); - f.context_declarations = declarations; - f.context_revision = f.revision; - } if (f.mini != null) { f.syntax_dirty = false; continue; @@ -1907,18 +1920,79 @@ pub fn visualRow(line: []const u8, col: usize, width: usize) VisualRow { } } -pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 { - const width = wrapWidth(pane, wrap); +/// The body's text as it is shown: its geometry's rows, each a line's +/// number and its stretch of the line. +pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 { + geometry(pane); + const out = try arena.alloc(u8, pane.geometry_len); + const filled = fillBody(out, pane, f, wrapWidth(pane, pane.view.wrap), false); + std.debug.assert(filled == out.len); + return out; +} + +/// The pane's view geometry for its state as it is now: the wrap map (the +/// line, column and pad each body row starts at), the context rows, and how +/// many rows the body layer has. Made again when anything it is made of +/// changes (`geometryKey`), by whoever asks first, the render or a step that +/// reads it: so a core that never draws has the geometry of one that does +/// (docs/next-daemon.md). What it reads of the session is the pane's `view`, +/// which each step sets. +pub fn geometry(pane: *Pane) void { + const f = if (pane.file) |*file| file else return; + const key = geometryKey(pane, f); + if (pane.geometry_key == key) return; + pane.geometry_key = key; + pane.body_rows = 0; + pane.body_visible_rows = 0; + pane.context_row_limit = null; + pane.geometry_len = mapRows(pane, f); + // A body with context rows is a layer taller than the grid's rows, its + // context rows at the tagline's height (body_layer.joinBody). + const v = pane.view; + if (!v.layered or pane.collapsed or v.rect_h <= pane.tag_rows) return; + const logical_rows = @min(@as(usize, pane.rows) + pane.context_rows, pane.wrap_line.len); + if (pane.context_rows == 0 or logical_rows <= pane.context_rows) return; + pane.body_rows = @intCast(logical_rows); + const bh: usize = @max(1, v.body_h); + const th: usize = std.math.clamp(v.tagline_h, 1, bh); + const body_pixels = @as(usize, v.rect_h - pane.tag_rows) * bh; + const limit: u16 = @intCast(@min(pane.context_rows, (body_pixels -| bh) / th)); + pane.context_row_limit = limit; + pane.body_visible_rows = @intCast(@min(logical_rows, limit + @max(1, (body_pixels -| @as(usize, limit) * th) / bh))); + pane.geometry_len = mapRows(pane, f); +} + +/// One walk of the rows the body shows, recording the wrap map: the bytes +/// its text takes. +fn mapRows(pane: *Pane, f: *State) usize { + const width = wrapWidth(pane, pane.view.wrap); pane.body_wrapped = width > 0; f.tables.ensure(pane.gpa, f); prepareContextRows(pane, f); - pane.wrap_n = 0; + return fillBody(null, pane, f, width, true); +} - const len = fillBody(null, pane, f, width, false); - const out = try arena.alloc(u8, len); - const filled = fillBody(out, pane, f, width, true); - std.debug.assert(filled == out.len); - return out; +/// Everything the geometry is made of, hashed: the text (its revision and +/// length), where it is scrolled, the pane's size, its tables as far as +/// they have been read, its context declarations, the session's view. +fn geometryKey(pane: *const Pane, f: *const State) u64 { + const t = &f.tables; + const facts = .{ + f.revision, f.content.len, f.scroll, pane.hscroll, + pane.rows, pane.cols, pane.tag_rows, pane.collapsed, + pane.query != null, f.pager, f.mini != null, f.output != null, + if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context_revision orelse 0, + f.context_declarations.len, + f.line_starts.len, t.revision orelse 0, t.list.items.len, if (t.pending) |r| r.at else std.math.maxInt(usize), + pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, + pane.view.rect_h, + }; + // One hash of them laid out as words: every reader of the map asks for + // this key, a hundred times a frame. + var words: [facts.len]u64 = undefined; + inline for (facts, &words) |fact, *w| w.* = if (@TypeOf(fact) == bool) @intFromBool(fact) else if (@typeInfo(@TypeOf(fact)).int.signedness == .signed) @bitCast(@as(i64, fact)) else fact; + // Never the "made for nothing" key a reset leaves (`geometry_key = 0`). + return std.hash.Wyhash.hash(0x9e0, std.mem.sliceAsBytes(&words)) | 1; } /// Run the file-body row walk. With no destination it is the exact sizing |
