From 044da0f75ab5fb5be0213e512741860d95dad8e5 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 11:14:38 -0300 Subject: 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= names a tape (.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= 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 --- src/Stream.zig | 82 ++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 16 deletions(-) (limited to 'src/Stream.zig') 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=)" { 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(); -- cgit v1.3 From 2eb8860b2ca05c426f3962661441aaee13d3be44 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 19:09:26 -0300 Subject: web: the browser shell builds again and loads a dump of a native build: a theme by its name, a field compiled out passed over, no disk asked; touch.dump replaces the hand-written ZON fixture Fixed for wasm32-freestanding: counts as usize, no threads, stb_image, tape or host stat there. touch.mjs follows the renderer's 13 tag-layer fields, helix's retuned colours and the tag column. Co-Authored-By: Claude Opus 5.5 --- build.zig | 12 +++--- build/snap.zig | 2 +- docs/web.md | 4 +- src/FrameStats.zig | 2 +- src/Store.zig | 2 +- src/Stream.zig | 2 +- src/dump.zig | 23 ++++++++--- src/exec.zig | 2 + src/image.zig | 22 ++++++++--- src/tape.zig | 4 ++ src/terminal.zig | 4 +- src/walk.zig | 38 ++++++++++++------ test/web-snapshots/README.md | 2 +- test/web-snapshots/touch-fixture.zig | 58 ++++++++++++++++++++++++++++ test/web-snapshots/touch.dump | Bin 0 -> 96576 bytes test/web-snapshots/touch.dump.zon | 73 ----------------------------------- test/web-snapshots/touch.mjs | 21 ++++++---- tools/web_fixture.py | 34 ++++++++++++++++ 18 files changed, 186 insertions(+), 119 deletions(-) create mode 100644 test/web-snapshots/touch-fixture.zig create mode 100644 test/web-snapshots/touch.dump delete mode 100644 test/web-snapshots/touch.dump.zon create mode 100644 tools/web_fixture.py (limited to 'src/Stream.zig') diff --git a/build.zig b/build.zig index 962b6322..097fb1bf 100644 --- a/build.zig +++ b/build.zig @@ -108,7 +108,7 @@ pub fn build(b: *std.Build) void { const target = if (platform == .esp32p4) b.resolveTargetQuery(esp32p4_target) else requested_target; const requested_optimize = b.standardOptimizeOption(.{}); const static = b.option(bool, "static", "statically link") orelse false; - const dump_path = b.option([]const u8, "dump", "dump .zon embedded into the web shell (-Dplatform=web)"); + const dump_path = b.option([]const u8, "dump", "a Dump (a .dump file) embedded into the web shell (-Dplatform=web)"); // one substring, because that is what a dev loop needs: see the unit-test // step for why the whole-binary run is worth narrowing const test_filters: []const []const u8 = if (b.option([]const u8, "test-filter", "run only tests whose name contains this")) |f| @@ -196,7 +196,7 @@ pub fn build(b: *std.Build) void { // The browser shell is a freestanding wasm core plus ordinary web files. // JavaScript owns the loop and IO; HTML/CSS own rendering. - const web_step = b.step("web", "build the DOM browser shell into zig-out/web (-Dplatform=web -Dtarget=wasm32-freestanding -Ddump=)"); + const web_step = b.step("web", "build the DOM browser shell into zig-out/web (-Dplatform=web -Dtarget=wasm32-freestanding -Ddump=)"); const mupdf_check = b.step("mupdf-check", "compile, link, render, and search docs/design.pdf with MuPDF"); const pdf_bench_step = b.step("pdf-bench", "benchmark real MuPDF page rendering (-- [--json] [--reps N] [--warmup N] [--path FILE] [--page N] [--pages N])"); const pdf_sections_bench_step = b.step("pdf-sections-bench", "benchmark PDF outline/sections paths in ReleaseFast (-Doptimize=ReleaseFast -- [--json] [--reps N] [--warmup N])"); @@ -224,7 +224,7 @@ pub fn build(b: *std.Build) void { if (is_web and !is_web_target) return failBuild(b, web_step, "-Dplatform=web requires -Dtarget=wasm32-freestanding"); if (!is_web and (is_web_target or target.result.os.tag == .emscripten)) return failBuild(b, web_step, "wasm browser targets require -Dplatform=web"); - if (platform == .web and dump_path == null) return failBuild(b, web_step, "-Dplatform=web requires -Ddump= (the browser has no ptys; state replays from an embedded dump)"); + if (platform == .web and dump_path == null) return failBuild(b, web_step, "-Dplatform=web requires -Ddump= (the browser has no ptys; state replays from an embedded dump)"); if (enable_mupdf and is_web) return failBuild(b, web_step, "-Dmupdf=true is supported only by the native tty/Kitty and gui/SDL backends"); // The board's target is pinned (`target` above), so what is asked of // -Dtarget is checked, not what was resolved: another arch is refused, @@ -1140,7 +1140,7 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "pardes-esp32p4", .root_module = root_mod }); b.getInstallStep().dependOn(&b.addInstallFile(obj.getEmittedBin(), "pardes-esp32p4.o").step); - web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); + web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); } else if (platform == .macos) { const header = b.addTranslateC(.{ .root_source_file = b.path("src/macos/pardes.h"), @@ -1308,7 +1308,7 @@ pub fn build(b: *std.Build) void { run_unit.has_side_effects = true; unit_step.dependOn(&run_unit.step); if (testMatch(b, unit_profile, test_filters, unit_step)) |check| check.addArtifactArg(unit_tests); - web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); + web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); } else { var exe_name: []const u8 = if (platform == .gui) "pardes-gui" else "pardes"; if (target.result.os.tag != .linux or target.result.cpu.arch != .x86_64) @@ -1344,7 +1344,7 @@ pub fn build(b: *std.Build) void { const iso_step = b.step("run-isolated", "descend to a pardes with no host but the terminal"); iso_step.dependOn(&run_iso.step); } - web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); + web_step.dependOn(&b.addFail("web needs -Dplatform=web -Dtarget=wasm32-freestanding -Ddump=").step); snap_build.addTty(b, .{ .exe = exe, diff --git a/build/snap.zig b/build/snap.zig index c1a26e64..dad23c53 100644 --- a/build/snap.zig +++ b/build/snap.zig @@ -118,7 +118,7 @@ pub fn addWeb(b: *std.Build) void { "-Dplatform=web", "-Dtarget=wasm32-freestanding", "-Dtree-sitter=zig", - "-Ddump=test/web-snapshots/touch.dump.zon", + "-Ddump=test/web-snapshots/touch.dump", "-p", "zig-out/web-snap-test", }); diff --git a/docs/web.md b/docs/web.md index 0a9783c9..b9d03b13 100644 --- a/docs/web.md +++ b/docs/web.md @@ -57,7 +57,7 @@ zig build web \ -Dplatform=web \ -Dtarget=wasm32-freestanding \ -Dtree-sitter=zig \ - -Ddump=test/web-snapshots/touch.dump.zon + -Ddump=test/web-snapshots/touch.dump ``` The output is in `zig-out/web`. Serve that directory over HTTP; browsers do not @@ -133,7 +133,7 @@ zig build web-harness \ -Dplatform=web \ -Dtarget=wasm32-freestanding \ -Dtree-sitter=zig \ - -Ddump=test/web-snapshots/touch.dump.zon + -Ddump=test/web-snapshots/touch.dump # Real headless Chrome, DOM rendering, and browser touch input. zig build web-e2e diff --git a/src/FrameStats.zig b/src/FrameStats.zig index 2287002e..c27e74af 100644 --- a/src/FrameStats.zig +++ b/src/FrameStats.zig @@ -173,7 +173,7 @@ pub fn record(stats: *FrameStats, end_ns: u64, moving: bool, skipped: bool) void if (stats.armed and @as(u64, gap) * std.time.ns_per_us * 2 > stats.period_ns * 3) stats.jank += 1; if (@as(u64, frame.update_us + frame.render_us + frame.present_us) * std.time.ns_per_us > stats.period_ns) stats.over_budget += 1; if (skipped) stats.skipped += 1; - stats.ring[stats.count % capacity] = frame; + stats.ring[@intCast(stats.count % capacity)] = frame; stats.count += 1; stats.last_end_ns = end_ns; stats.armed = moving; diff --git a/src/Store.zig b/src/Store.zig index 95f5368f..36215234 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -78,7 +78,7 @@ cap: u64 = max_bytes, session: u64 = 0, pid: i32 = 0, /// The file as read so far: its inode, generation and how far. -inode: std.Io.File.INode = 0, +inode: std.Io.File.INode = std.mem.zeroes(std.Io.File.INode), gen: u64 = 0, end: u64 = 0, /// Its length when first read whole: a file past twice that (and 1 MiB) diff --git a/src/Stream.zig b/src/Stream.zig index 8d9663dd..665241df 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -512,7 +512,7 @@ pub fn parseFor(p: *Pardes, id: usize) void { // Here and now: a small text, a host with no workers. var got: Collect = .{ .gpa = p.gpa }; defer got.batches.deinit(p.gpa); - parseWork(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + parseWork(if (comptime pardes.hosted) std.Io.Threaded.global_single_threaded.io() else std.Io.failing, job, &got, Collect.take); for (got.batches.items) |*b| { receive(p, b); b.deinit(p.gpa); diff --git a/src/dump.zig b/src/dump.zig index abc52f5a..c33ac02c 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -142,11 +142,13 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 { /// A session's dump: its core, everything it is but what is the host's, /// the frame's or worked out again (`replica` in each type, src/walk.zig), -/// then its 9P mounts, which live with the 9P server's own state, and which -/// of its panes' files were on disk when it was made (one gone by its load -/// is said deleted: its text is nowhere else now). -const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk }); -const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk }); +/// then its 9P mounts, which live with the 9P server's own state, which of +/// its panes' files were on disk when it was made (one gone by its load is +/// said deleted: its text is nowhere else now), and its theme's name: the +/// setting is a place in this build's ring of themes, another build's +/// ring is another. +const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 }); +const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 }); const OnDisk = [MAX_PANES]bool; fn onDisk(pane: *const pardes.panes.Pane) bool { @@ -176,6 +178,8 @@ pub fn dumpBytes(p: *Pardes) ![]u8 { on.* = onDisk(pane); }; try d.put(OnDisk, &on_disk); + const theme: []const u8 = colors.themes[@min(p.settings.theme, colors.themes.len - 1)].name; + try d.put([]const u8, &theme); return d.finish(); } @@ -233,6 +237,12 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: } const was = try l.get(OnDisk); if (on_disk) |out| out.* = was; + const theme = try l.get([]const u8); + defer gpa.free(theme); + // This build's place for that theme; one it has not, its first. + p.settings.theme = for (colors.themes, 0..) |t, i| { + if (std.mem.eql(u8, t.name, theme)) break i; + } else if (p.settings.theme < colors.themes.len) p.settings.theme else 0; // A mount this process was started with stands; one of the same name // to elsewhere is refused, never silently put over it. for (mounts) |m| { @@ -411,7 +421,8 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool // Its file gone from disk since the dump: its text is nowhere else, so // it is said deleted, and Del, Exit and Restore ask first. One never on // disk (a name not yet saved) comes back as it was. - if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); + // (A host with no disk of its own, the web's, asks nothing.) + if (comptime pardes.hosted) if (was_on_disk and !onDisk(pane)) pardes.panes.File.deleted(p, @intCast(id)); } // ---- one pane to and from a dump: the store's, a handover's ---- diff --git a/src/exec.zig b/src/exec.zig index 8f84d2c6..df397b37 100644 --- a/src/exec.zig +++ b/src/exec.zig @@ -483,6 +483,8 @@ pub fn isFile(path: []const u8) bool { /// What `path` is on this host, null when it is not there; the turn out /// while it asks (it may be inside a mount this editor serves). pub fn kindOf(path: []const u8) ?std.Io.File.Kind { + // A freestanding build has no host to ask. + if (comptime !pardes.hosted) return null; var buf: [4096]u8 = undefined; const z = std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0) catch return null; return pardes.tape.site(.kind, null, kindHost, .{z}); diff --git a/src/image.zig b/src/image.zig index 8f193674..9163730d 100644 --- a/src/image.zig +++ b/src/image.zig @@ -755,6 +755,13 @@ var started = false; extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int; +/// stb_image's header reader; a freestanding build has no stb_image (its +/// decoder is the shim in src/web/zstbi.zig), so no header reads as one. +fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool { + if (comptime !pardes.hosted) return false; + return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1; +} + /// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for /// kitty graphics) wherever ghostty-vt is in the build; stb_image is then /// compiled without them, and without the inflate only its PNG used @@ -787,7 +794,7 @@ fn headerSize(bytes: []const u8) ?Size { var iw: c_int = 0; var ih: c_int = 0; var ic: c_int = 0; - if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; + if (!stbiInfo(bytes, &iw, &ih, &ic)) return null; return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) }; } @@ -828,7 +835,7 @@ pub fn readable(bytes: []const u8) bool { var c: c_int = 0; if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null; - return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1; + return stbiInfo(bytes, &w, &h, &c); } /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the @@ -929,6 +936,9 @@ const GridKey = struct { ascii: bool = true, }; +/// A host's thread; a freestanding build has none to have. +const Decoder = if (pardes.hosted) std.Thread else void; + pub const State = struct { /// What the replay digest leaves out (src/walk.zig): the picture as /// decoded from its bytes, off the loop as the render first wants it, @@ -947,12 +957,12 @@ pub const State = struct { grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 }, grid_key: GridKey = .{}, /// The decode under way off the loop (ensureDecoded), and its answer. - decoder: ?std.Thread = null, + decoder: ?Decoder = null, decoded: std.atomic.Value(bool) = .init(false), answer: ?Decoded = null, pub fn deinit(state: *State, gpa: std.mem.Allocator) void { - if (state.decoder) |t| t.join(); + if (comptime pardes.hosted) if (state.decoder) |t| t.join(); if (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); @@ -1026,11 +1036,11 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![ /// docs' screenshot: all of it once on the frame before). /// ponytail: a thread a picture; a pool if many open at once. fn ensureDecoded(p: *pardes.Pardes, state: *State) void { - if (state.decoder) |t| { + if (comptime pardes.hosted) if (state.decoder) |t| { if (!state.decoded.load(.acquire)) return; t.join(); state.decoder = null; - } + }; if (state.answer) |decoded| { state.answer = null; state.rgba = decoded.rgba; diff --git a/src/tape.zig b/src/tape.zig index d4fb7756..84bdce29 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -163,6 +163,7 @@ pub const Tape = struct { } fn flush(t: *Tape) void { + if (comptime !pardes.hosted) return; // A tape in memory (a replica's, an authority's stream) is taken // by whoever holds it. if (t.fd < 0) return; @@ -710,6 +711,8 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + // No tape where there is no host to record or replay one. + if (comptime !pardes.hosted) return if (yields) out(f, args) else @call(.auto, f, args); const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); const id: u16 = @as(u16, host_fns.len) + @backingInt(s); if (t.replay) { @@ -928,6 +931,7 @@ pub fn digestOf(p: *const Pardes) u64 { /// PARDES_RECORD set: the tape is written from the session's first call. pub fn startFromEnv() void { + if (comptime !pardes.hosted) return; if (on != null) return; const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return); if (prefix.len == 0) return; diff --git a/src/terminal.zig b/src/terminal.zig index ad35784f..bcb70110 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -254,7 +254,7 @@ pub const State = struct { return; } if (bytes.len < 8) return error.EndOfDump; - const n = std.mem.bytesToValue(u64, bytes[0..8]); + const n = std.math.cast(usize, std.mem.bytesToValue(u64, bytes[0..8])) orelse return error.EndOfDump; if (bytes.len - 8 < n) return error.EndOfDump; var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); @@ -797,7 +797,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { // defaults to direct alone, which `kitten icat` and timg detect and // fall back from, base64 through the pty. What the program can read // it could send itself: it runs as the user, under pardes. - .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else .direct, + .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; diff --git a/src/walk.zig b/src/walk.zig index 328557a7..6f9cc34b 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -843,6 +843,10 @@ fn allocOf(comptime T: type, comptime name: []const u8) ?[]const u8 { return null; } +fn countOf(n: u64) DecodeError!usize { + return std.math.cast(usize, n) orelse error.BadDump; +} + /// An allocation a load made: what a reference to it finds, and what a /// failed load frees. const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; @@ -931,6 +935,11 @@ pub fn Load(comptime roots: []const type) type { return std.mem.bytesToValue(T, try l.take(@sizeOf(T))); } + /// A count, as this machine counts (a wasm32 one has 32 bits). + fn count(l: *Self) DecodeError!usize { + return countOf(try l.scalar(u64)); + } + fn name(l: *Self) DecodeError![]const u8 { return l.take(try l.scalar(u16)); } @@ -962,9 +971,9 @@ pub fn Load(comptime roots: []const type) type { }, .optional, .one, .slice, .prefix, .error_union => t.child = try l.scalar(u32), .@"struct", .@"union" => { - const count = try l.scalar(u32); - if (count > l.bytes.len) return error.BadDump; - const list = try a.alloc(SrcField, count); + const nfields = try l.scalar(u32); + if (nfields > l.bytes.len) return error.BadDump; + const list = try a.alloc(SrcField, nfields); for (list) |*f| f.* = .{ .name = try l.name(), .ty = try l.scalar(u32) }; t.fields = list; }, @@ -1073,6 +1082,14 @@ pub fn Load(comptime roots: []const type) type { out.* = .{ .raw = raw }; return; } + // A field this build compiles out (a PDF slot where there is no + // MuPDF): passed over, said only when it held something. + if (comptime kindOf(Canon(T)) == .void) if (!l.same and l.src[ty].kind != .void) { + const held = l.src[ty].kind != .optional or (l.at < l.bytes.len and l.bytes[l.at] != 0); + try l.skip(ty); + if (held) l.note("{s}: not in this build", .{l.field}); + return; + }; if (!try l.kindMatches(T, ty)) return error.Incompatible; const s = l.src[ty]; if (comptime isList(T)) { @@ -1194,8 +1211,7 @@ pub fn Load(comptime roots: []const type) type { } else out.* = try l.errorNamed(@typeInfo(T).error_union.error_set); }, .custom => { - const n = try l.scalar(u64); - const bytes = try l.take(n); + const bytes = try l.take(try l.count()); T.dumpRead(out, l.gpa, bytes) catch |err| { if (err == error.OutOfMemory) return error.OutOfMemory; l.note("{s}: {t}", .{ l.field, err }); @@ -1204,7 +1220,7 @@ pub fn Load(comptime roots: []const type) type { if (comptime @hasDecl(T, "dumpFree")) try l.customs.append(l.own, .{ .ptr = out, .gpa = l.gpa, .free = @ptrCast(&T.dumpFree) }); }, .bitset => { - const bits = try l.scalar(u64); + const bits = try l.count(); out.* = try .initEmpty(l.gpa, bits); const words = (bits + @bitSizeOf(usize) - 1) / @bitSizeOf(usize); if (words > 0) @memcpy(std.mem.sliceAsBytes(out.masks[0..words]), try l.take(words * @sizeOf(usize))); @@ -1232,7 +1248,7 @@ pub fn Load(comptime roots: []const type) type { fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E { if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; - const n = try l.scalar(u64); + const n = try l.count(); if (n == 0) return &.{}; if (n > l.bytes.len) return error.BadDump; const many = try l.gpa.alloc(E, n); @@ -1264,7 +1280,7 @@ pub fn Load(comptime roots: []const type) type { // cannot be read is left out, not the array. const droppable = comptime @typeInfo(E) == .optional and @typeInfo(@typeInfo(E).optional.child) == .pointer; var failed = false; - for (0..n) |i| { + for (0..try countOf(n)) |i| { if (i >= keep) { try l.skip(ty); continue; @@ -1394,8 +1410,8 @@ pub fn Load(comptime roots: []const type) type { .error_union => if ((try l.take(1))[0] != 0) try l.skip(s.child) else { _ = try l.name(); }, - .custom => _ = try l.take(try l.scalar(u64)), - .bitset => _ = try l.take((try l.scalar(u64) + 63) / 64 * 8), + .custom => _ = try l.take(try l.count()), + .bitset => _ = try l.take((try l.count() + 63) / 64 * 8), } } @@ -1407,7 +1423,7 @@ pub fn Load(comptime roots: []const type) type { _ = try l.take(@intCast(n * size)); return; } - for (0..n) |_| try l.skip(ty); + for (0..try countOf(n)) |_| try l.skip(ty); } }; } diff --git a/test/web-snapshots/README.md b/test/web-snapshots/README.md index a7cd865e..999e8fad 100644 --- a/test/web-snapshots/README.md +++ b/test/web-snapshots/README.md @@ -1,6 +1,6 @@ # Browser interaction tests -`zig build web-snap` loads `touch.dump.zon` and runs `touch.mjs` in Chromium. +`zig build web-snap` loads `touch.dump` (`python3 -B tools/web_fixture.py test/web-snapshots/touch.dump` makes it: a native session on `touch-fixture.zig` at 100x30, theme helix, Verbose off) and runs `touch.mjs` in Chromium. The fixed source fixture exercises complete keyword styles, touch Look through the self filesystem, separator resizing, and scrolling without another Look. Assertions locate current pane geometry; no live project source or author paths diff --git a/test/web-snapshots/touch-fixture.zig b/test/web-snapshots/touch-fixture.zig new file mode 100644 index 00000000..b1d5d81e --- /dev/null +++ b/test/web-snapshots/touch-fixture.zig @@ -0,0 +1,58 @@ +pub fn choose(value: u32) []const u8 { + if (value == 42) return "touch fixture"; + return "/virtual/pane/1/body"; +} + +pub const rows = [_]u32{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, +}; \ No newline at end of file diff --git a/test/web-snapshots/touch.dump b/test/web-snapshots/touch.dump new file mode 100644 index 00000000..bcc58352 Binary files /dev/null and b/test/web-snapshots/touch.dump differ diff --git a/test/web-snapshots/touch.dump.zon b/test/web-snapshots/touch.dump.zon deleted file mode 100644 index 230d29e7..00000000 --- a/test/web-snapshots/touch.dump.zon +++ /dev/null @@ -1,73 +0,0 @@ -.{ - .screen = .{ .cols = 100, .rows = 30 }, - .theme = "helix", - .columns = .{.{ .panes = .{0} }}, - .panes = .{.{ - .kind = .file, - .tag = "touch-fixture.zig", - .body = "", - .file = .{ - .path = "touch-fixture.zig", - .content = - \\pub fn choose(value: u32) []const u8 { - \\ if (value == 42) return "touch fixture"; - \\ return "/virtual/pane/1/body"; - \\} - \\ - \\pub const rows = [_]u32{ - \\ 0, - \\ 1, - \\ 2, - \\ 3, - \\ 4, - \\ 5, - \\ 6, - \\ 7, - \\ 8, - \\ 9, - \\ 10, - \\ 11, - \\ 12, - \\ 13, - \\ 14, - \\ 15, - \\ 16, - \\ 17, - \\ 18, - \\ 19, - \\ 20, - \\ 21, - \\ 22, - \\ 23, - \\ 24, - \\ 25, - \\ 26, - \\ 27, - \\ 28, - \\ 29, - \\ 30, - \\ 31, - \\ 32, - \\ 33, - \\ 34, - \\ 35, - \\ 36, - \\ 37, - \\ 38, - \\ 39, - \\ 40, - \\ 41, - \\ 42, - \\ 43, - \\ 44, - \\ 45, - \\ 46, - \\ 47, - \\ 48, - \\ 49, - \\ 50, - \\}; - , - }, - }}, -} diff --git a/test/web-snapshots/touch.mjs b/test/web-snapshots/touch.mjs index 69f8a4a5..3793c0f9 100644 --- a/test/web-snapshots/touch.mjs +++ b/test/web-snapshots/touch.mjs @@ -1,8 +1,9 @@ import assert from "node:assert/strict"; function tags(frame) { - // This fixture has workspace and column bars in its first two rows. - return frame.styleRows.flatMap((cells, y) => y > 1 && /^\s{3}\S/.test(frame.lines[y]) && cells.some((cell) => cell[2] & 128) ? [y] : []); + // This fixture has workspace and column bars in its first two rows; a + // pane's tag text starts after the two-cell marker gutter. + return frame.styleRows.flatMap((cells, y) => y > 1 && /^\s{2}\S/.test(frame.lines[y]) && cells.some((cell) => cell[2] & 128) ? [y] : []); } function pane(frame, name) { @@ -13,14 +14,17 @@ function pane(frame, name) { return { top, bottom, lines: frame.lines.slice(top + 1, bottom) }; } +// helix's keyword and string/number colours (src/themes/helix.zig). +const KW = 0xd776f9, STR = 0xf3cd7a; + function styled(frame) { const source = pane(frame, "touch-fixture.zig"); for (const [line, word, fg, bold] of [ - ["pub fn choose", "fn", 0xcf87e8, false], + ["pub fn choose", "fn", KW, false], ["pub fn choose", "choose", 0xbdbdbd, false], - ["if (value", "if", 0xcf87e8, false], - ["if (value", "42", 0xe3c78a, false], - ["if (value", '"touch fixture"', 0xe3c78a, false], + ["if (value", "if", KW, false], + ["if (value", "42", STR, false], + ["if (value", '"touch fixture"', STR, false], ]) { const row = source.lines.findIndex((text) => text.includes(line)); assert(row >= 0, `missing source line ${line}`); @@ -35,7 +39,7 @@ function styled(frame) { const number = /\b(\d+),/.exec(line); if (!number) continue; for (let x = number.index; x < number.index + number[1].length; x++) { - assert.equal(frame.styleRows[source.top + 1 + row][x][0], 0xe3c78a, `row ${row} number foreground`); + assert.equal(frame.styleRows[source.top + 1 + row][x][0], STR, `row ${row} number foreground`); } } } @@ -79,7 +83,8 @@ export default async function ({ grid, touch, wait, stable, delay, evaluate, mou renderer.render(); result.closedGapBorders=borders(); result.borderChangeInvalidates=renderer.layerRoot.firstChild!==withGap; - const tagValues=[2,2,0,42,1,0,1,1,1,1,0x334455]; + // The 13 fields of a tag layer: a one-line tag, its caret on line 0. + const tagValues=[2,2,0,42,1,0,1,1,1,1,0x334455,1,0]; wasm.pardes_tag_layer_limit=()=>1; wasm.pardes_tag_layer_value=(_,field)=>tagValues[field]; wasm.pardes_tag_layer_cells=()=>1024; diff --git a/tools/web_fixture.py b/tools/web_fixture.py new file mode 100644 index 00000000..06fc54cd --- /dev/null +++ b/tools/web_fixture.py @@ -0,0 +1,34 @@ +"""web_fixture.py PARDES OUT: the web shell's touch fixture as a dump of this +build: one pane on touch-fixture.zig at 100x30, theme helix. + + python3 -B tools/web_fixture.py test/web-snapshots/touch.dump +""" +import os, sys, time, shutil, struct, fcntl, termios +from pathlib import Path +sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'test')) +import fs # noqa: E402 + +binary, out = sys.argv[1], sys.argv[2] +root = Path('/tmp/pardes-web-fixture') # fixed: the dump names it +shutil.rmtree(root, ignore_errors=True) +root.mkdir(parents=True, mode=0o700) +shutil.copy(Path(__file__).resolve().parent.parent / 'test' / 'web-snapshots' / 'touch-fixture.zig', root / 'touch-fixture.zig') +# Set at startup, which announces nothing: a fixture with no message on +# its pane. +(root / 'config' / 'pardes').mkdir(parents=True) +(root / 'config' / 'pardes' / 'init').write_text('Theme helix\nVerbose off\n') +handles = {} +with fs.session(binary, root, 'web', tty=True, file=root / 'touch-fixture.zig', inherited={'PARDES_DAEMON': 'off'}, terminal=handles) as (client, address): + fcntl.ioctl(handles['input_fd'], termios.TIOCSWINSZ, struct.pack('HHHH', 30, 100, 0, 0)) + deadline = time.monotonic() + 10 + while client.screen()['cols'] != 100: + assert time.monotonic() < deadline + time.sleep(.01) + client.write('/ctl', b'Dump\n') + while b'\ndump ' not in client.read('/log'): + assert time.monotonic() < deadline + time.sleep(.01) + print(client.read('/index').decode()) + shutil.copy(root / 'web.dump.zon', out) +shutil.rmtree(root, ignore_errors=True) +print(os.path.getsize(out), 'bytes') -- cgit v1.3 From b7dec1c3da59f23298eaccb2a5185ccf7a50a87a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 18:27:14 -0300 Subject: Stream: a search run on the loop (a 9P write's Grep or Find, which answers whether it found anything) is a tape site, `search`, its batches what the replay takes, as it takes a worker's (Event.search): its file reads were no site, so a replica of a session a script greps in read no file and joined again (`a syscall that is no tape site, made replaying`, a terminal client live, 2 of 2 before, none after); test/cross_replay.py's session greps and finds again Co-Authored-By: Claude Opus 5.5 --- src/Stream.zig | 25 +++++++++++++++++-------- src/tape.zig | 3 +++ test/cross_replay.py | 12 ++++++------ 3 files changed, 26 insertions(+), 14 deletions(-) (limited to 'src/Stream.zig') diff --git a/src/Stream.zig b/src/Stream.zig index 665241df..263a6628 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -708,19 +708,28 @@ pub fn runHere(p: *Pardes, gen: u32) void { }; defer job.deinit(); job.waits = false; - var got: Collect = .{ .gpa = p.gpa }; - defer got.batches.deinit(p.gpa); - { - pardes.turn.yield(); - defer pardes.turn.back(); - work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); - } - for (got.batches.items) |*b| { + // Its reads a site of the tape's (a 9P write's Grep or Find runs here): + // a replica takes the batches the session got, as it takes a worker's + // (Event.search), and reads no file. + const batches = pardes.tape.site(.search, p.gpa, searchHere, .{ p.gpa, job }); + defer p.gpa.free(batches); + for (batches) |*b| { receive(p, b); b.deinit(p.gpa); } } +/// The whole search, its batches gpa's. +fn searchHere(gpa: std.mem.Allocator, job: *Job) []Batch { + var got: Collect = .{ .gpa = gpa }; + work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + return got.batches.toOwnedSlice(gpa) catch { + for (got.batches.items) |b| b.deinit(gpa); + got.batches.deinit(gpa); + return &.{}; + }; +} + const Collect = struct { gpa: std.mem.Allocator, batches: std.ArrayList(Batch) = .empty, diff --git a/src/tape.zig b/src/tape.zig index e446c06a..9152bcb0 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -94,6 +94,8 @@ pub const Site = enum(u16) { kind, probe, preview, + /// A search run on the loop (Stream.runHere: a 9P write's Grep or Find). + search, // Read in place (`value`). clock, budget, @@ -132,6 +134,7 @@ pub const results = .{ anyerror![]u8, anyerror!void, terminal.Told, ?pardes.ctlfs.pty.Printed, @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?, + []pardes.Stream.Batch, // What only the authority holds (`authority`): a closed pane's record, // Recent's entry for a closed jump, the closed panes' names. ?@import("Store.zig").Record, ?@import("recent.zig").Entry, diff --git a/test/cross_replay.py b/test/cross_replay.py index 6c5a1e4d..9ce7d152 100644 --- a/test/cross_replay.py +++ b/test/cross_replay.py @@ -17,11 +17,11 @@ The session meets the host's failures, whose errors cross by name: a Look of a fifo (a read the host refuses) and of a directory, a stat and a read under /os over 9P, a Save into a file as if it were a directory and a Restore of a dump that is not there (errors the host reports, anyerrors). -And it holds what a digest must name the same in every build: a -+LocationsConfig, whose origin is a Builtin, and a file pane renamed (H19: -no Linux client could join a FreeBSD session so). ponytail: no Grep or -Find, which a 9P write runs on the loop, its reads no tape site (a replay -of it panics, by any build). +It greps and finds, as a script does, which a 9P write runs on the loop: +its reads are a tape site (a replica of a session a script greps in read +no file and diverged). And it holds what a digest must name the same in +every build: a +LocationsConfig, whose origin is a Builtin, and a file +pane renamed (H19: no Linux client could join a FreeBSD session so). """ import os import shutil @@ -75,7 +75,7 @@ def record(binary, out): started.append(int(status['pid'])) pane = next(int(row.split()[0]) for row in c.read('/index').splitlines() if row.split()[1] == b'text') for line in (f'Look {root / "fifo"}', f'Look {root / "dir"}', f'Save {root / "a.txt" / "x"}', - f'Restore {root / "nothing.dump"}', 'LocationsConfig'): + f'Restore {root / "nothing.dump"}', 'Grep hello', 'Find txt', 'LocationsConfig'): try: c.write(f'/pane/{pane}/exec', line.encode() + b'\n') except Exception: -- cgit v1.3 From 9bd34ae2afdb05713f312e7e58dc3d516ea84ebe Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 19:37:42 -0300 Subject: Query: a +Search's rows recoloured are made again (locations.cloneRows, Query.save's copy), never written in place: the rows are a blob (File.State location_rows), whose word a digest keeps by address and a replica's rollback copy pins, so a release session kept the word of their colours before a paint landed (recolor's @memcpy); an edit moved them into dropped_marks with the stale word, a replica that hashed them at another time diverged there (fixer 1's soak: `pane 5.file.dropped_marks`), and a client loading the session's dump hashed the colours it carried and loaded "as another session"; dropped_marks stays in the digest, as an undo back to its text reads it; Stream's test, which a Debug build fails before (`the 82-byte blob ... is not what its kept word was: written in place`) Co-Authored-By: Claude Opus 5.5 --- src/Query.zig | 41 ++++++++++++++----------------- src/Stream.zig | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/locations.zig | 24 ++++++++++++++++++ 3 files changed, 115 insertions(+), 23 deletions(-) (limited to 'src/Stream.zig') diff --git a/src/Query.zig b/src/Query.zig index 2f417a68..dfe23046 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -372,24 +372,8 @@ fn save(p: *Pardes, rp: *Pane, q: *Query) !Saved { const f = &rp.file.?; const content = try p.gpa.dupe(u8, f.content); errdefer p.gpa.free(content); - const rows = try p.gpa.alloc(locations.Row, f.location_rows.len); - var made: usize = 0; - errdefer { - for (rows[0..made]) |r| { - p.gpa.free(r.path); - p.gpa.free(r.colors); - } - p.gpa.free(rows); - } - for (f.location_rows, 0..) |r, i| { - rows[i] = r; - rows[i].path = try p.gpa.dupe(u8, r.path); - rows[i].colors = p.gpa.dupe(u8, r.colors) catch |err| { - p.gpa.free(rows[i].path); - return err; - }; - made += 1; - } + const rows = try locations.cloneRows(p.gpa, f.location_rows); + errdefer locations.freeRows(p.gpa, rows); const asked = try p.gpa.dupe(u8, q.asked); errdefer p.gpa.free(asked); return .{ .content = content, .rows = rows, .asked = asked, .source = q.source, .items = try p.gpa.dupe(Item, q.items.items), .current = q.current, .total = q.total, .k = q.k }; @@ -865,18 +849,29 @@ pub fn recolor(p: *Pardes) void { const f = &rp.file.?; var sh: Shades = .{}; var lines = std.mem.splitScalar(u8, f.content, '\n'); - var changed = false; - for (f.location_rows) |row| { + // The rows are a blob (File.State): a digest keeps its word for + // them and a rollback copy pins them, so they are made again, never + // written. Written in place, a session's digest kept the word of + // their colours before; a client joining hashed them after, and + // loaded "as another session" (and so did a replica that hashed them + // at another time). + var fresh: ?[]locations.Row = null; + for (f.location_rows, 0..) |row, i| { const text = lines.next() orelse break; if (row.kind != .match or row.location_end == 0 or row.at.line == 0 or row.colors.len == 0) continue; if (std.mem.indexOfNone(u8, row.colors, &.{0}) != null) continue; const code = text[@min(row.code_start, text.len)..]; const got = sh.of(p, row.path, row.at.line, code[0..@min(code.len, row.colors.len)], row.source_column); if (got.len == 0) continue; - @memcpy(row.colors[0..got.len], got); - changed = true; + const rows = fresh orelse locations.cloneRows(p.gpa, f.location_rows) catch break; + fresh = rows; + @memcpy(rows[i].colors[0..got.len], got); + } + if (fresh) |rows| { + locations.freeRows(p.gpa, f.location_rows); + f.location_rows = rows; + dropHighlights(p, f); } - if (changed) dropHighlights(p, f); } } diff --git a/src/Stream.zig b/src/Stream.zig index 263a6628..69dc311f 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -1647,6 +1647,79 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work Stats.print("other frames", idle.items); } +test "Stream: a Grep's rows recoloured are made again, never written, and a dump of them dropped by an edit loads to its digest" { + if (comptime !filesystem.platform_has_fs) return; + if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + // Rows enough that a digest keeps a word for them (walk's blob_min). + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..80) |i| try text.print(testing.allocator, "pub const value_{d}: u32 = {d};\n", .{ i, i }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text.items }); + var root_buf: [4096]u8 = undefined; + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + var shell: TestShell = .init(); + shell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + panes.Query.open(p, 0, .grep, "value", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const f = &p.panes[qid].?.file.?; + const coloured = struct { + fn count(rows: []const @import("locations.zig").Row) usize { + var n: usize = 0; + for (rows) |row| n += @intFromBool(row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + return n; + } + }.count; + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + // The rows, plain, digested (their word kept) before their colours land. + while (now() < until and f.location_rows.len < 80) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expect(f.location_rows.len >= 80 and coloured(f.location_rows) == 0); + _ = pardes.walk.digest(Pardes, p); + const plain = f.location_rows.ptr; + while (now() < until and (coloured(f.location_rows) < 80 or p.stream.rows_paint != null)) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 80), coloured(f.location_rows)); + // Made again: a Debug build's digest finds no blob written in place + // (the plain rows' kept word is theirs), and the rows are new ones. + _ = pardes.walk.digest(Pardes, p); + try testing.expect(f.location_rows.ptr != plain); + // An edit drops them with the text they were for (dropped_marks): a + // join's load of the session is it, digest for digest. + panes.File.setContent(p, f, try p.gpa.dupe(u8, "value\n")); + try testing.expect(f.dropped_marks != null); + const bytes = try pardes.dump.dumpBytes(p); + defer p.gpa.free(bytes); + const q = try pardes.dump.load(testing.allocator, p.opts, bytes, null); + defer q.deinit(); + try testing.expectEqual(pardes.walk.digest(panes.File.State, f), pardes.walk.digest(panes.File.State, &q.panes[qid].?.file.?)); +} + test "Stream: a Grep's rows are coloured as their file's language colours them, the file painted on a worker, the match tint on top" { if (comptime !filesystem.platform_has_fs) return; if (!syntax.enabled or !syntax.readyApart("c.zig")) return; diff --git a/src/locations.zig b/src/locations.zig index 40f22f85..d0904607 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -36,6 +36,30 @@ pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { freeRaw(gpa, rows); } +/// A copy of `rows` and all they own, gpa's: rows are a blob (File.State +/// `location_rows`), made again rather than written. +pub fn cloneRows(gpa: std.mem.Allocator, rows: []const Row) ![]Row { + const out = try gpa.alloc(Row, rows.len); + var made: usize = 0; + errdefer { + for (out[0..made]) |r| { + gpa.free(r.path); + gpa.free(r.colors); + } + gpa.free(out); + } + for (rows, out) |r, *o| { + o.* = r; + o.path = try gpa.dupe(u8, r.path); + o.colors = gpa.dupe(u8, r.colors) catch |err| { + gpa.free(o.path); + return err; + }; + made += 1; + } + return out; +} + /// A list of rows being built, given up: each row's own bytes, then the list. pub fn freeRowList(gpa: std.mem.Allocator, rows: *std.ArrayList(Row)) void { for (rows.items) |row| { -- cgit v1.3