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/fs.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/fs.zig')
| -rw-r--r-- | src/fs.zig | 143 |
1 files changed, 80 insertions, 63 deletions
@@ -171,11 +171,7 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { const path = osPath(p, req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .release) return .{ .tag = req.tag }; const io = std.Io.Threaded.global_single_threaded.io(); - const stat = stat: { - pardes.turn.yield(); - defer pardes.turn.back(); - break :stat statPath(io, path, .{}) catch return Reply.fail(req.tag, E.NOENT); - }; + const stat = pardes.tape.site(.os_stat, null, statPath, .{ io, path, std.Io.Dir.StatFileOptions{} }) catch return Reply.fail(req.tag, E.NOENT); const attr: Reply.Attr = .{ .name = if (req.node == os_root) "os" else std.fs.path.basename(path), .node = req.node, .dir = stat.kind == .directory, .size = stat.size, .mode = if (stat.kind == .directory) 0o755 else 0o644, .mtime = std.math.cast(u32, stat.mtime.toSeconds()) orelse 0 }; switch (req.op) { .getattr => return .{ .tag = req.tag, .attr = attr }, @@ -199,32 +195,10 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { // Listed into a buffer of this request's own first; the shared // reply buffer is another request's to use while the turn is out. var listing: [16 * 1024]u8 = undefined; - var fixed: std.heap.FixedBufferAllocator = .init(&listing); - var listed: std.ArrayList(u8) = .empty; - { - pardes.turn.yield(); - defer pardes.turn.back(); - var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return Reply.fail(req.tag, E.NOTDIR); - defer dir.close(io); - var it = dir.iterate(); - var skip = req.off; - while (it.next(io) catch return Reply.fail(req.tag, E.IO)) |entry| { - var buf: [4096]u8 = undefined; - const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; - var normalized_buf: [4096]u8 = undefined; - const normalized = resolveOs(joined, &normalized_buf) orelse continue; - const child = dir.statFile(io, entry.name, .{}) catch continue; - if (skip > 0) { - skip -= 1; - continue; - } - const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); - tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); - if (listed.items.len >= @max(req.size, 512)) break; - } - } + const listed = pardes.tape.site(.os_listing, null, osListing, .{ io, path, req.off, req.size, &listing }) catch |err| + return Reply.fail(req.tag, if (err == error.NotDir) E.NOTDIR else E.IO); const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, listed.items) catch return Reply.fail(req.tag, E.NOMEM); + out.appendSlice(p.gpa, listed) catch return Reply.fail(req.tag, E.NOMEM); return .{ .tag = req.tag, .payload = .{ .staged = @intCast(out.items.len) } }; }, .read, .write, .setattr => { @@ -237,37 +211,70 @@ pub fn osHandle(p: *pardes.Pardes, req: Req) Reply { var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return Reply.fail(req.tag, E.NOENT); var buf: [ninep_io.msize]u8 = undefined; // a read never asks for more than a frame - const got = io: { - pardes.turn.yield(); - defer pardes.turn.back(); - const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); - if (fd < 0) return Reply.fail(req.tag, E.PERM); - defer _ = libc.close(fd); - if (req.op == .setattr) { - if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero"); - var truncated = attr; - truncated.size = 0; - return .{ .tag = req.tag, .attr = truncated }; - } - if (req.off > std.math.maxInt(i64)) return tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold"); - if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return Reply.fail(req.tag, E.IO); - if (req.op == .write) { - const written = libc.write(fd, req.data.ptr, req.data.len); - if (written < 0) return Reply.fail(req.tag, E.IO); - return .{ .tag = req.tag, .written = @intCast(written) }; - } - const got = libc.read(fd, &buf, @min(req.size, buf.len)); - if (got < 0) return Reply.fail(req.tag, E.IO); - break :io @as(usize, @intCast(got)); + const got = switch (pardes.tape.site(.os_rw, null, osReadWrite, .{ path_z, req, attr, &buf })) { + .reply => |reply| return reply, + .read => |bytes| bytes, }; const out = p.fs.stage(p.gpa); - out.appendSlice(p.gpa, buf[0..got]) catch return Reply.fail(req.tag, E.NOMEM); - return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got) } }; + out.appendSlice(p.gpa, got) catch return Reply.fail(req.tag, E.NOMEM); + return .{ .tag = req.tag, .payload = .{ .staged = @intCast(got.len) } }; }, else => return Reply.fail(req.tag, E.PERM), } } +/// A directory's listing under /os, the entries from `off` until `size` +/// bytes of them are staged, into `listing`: the syscalls of a readdir. +fn osListing(io: std.Io, path: []const u8, off: u64, size: u32, listing: *[16 * 1024]u8) error{ NotDir, Io }![]const u8 { + var fixed: std.heap.FixedBufferAllocator = .init(listing); + var listed: std.ArrayList(u8) = .empty; + var dir = std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return error.NotDir; + defer dir.close(io); + var it = dir.iterate(); + var skip = off; + while (it.next(io) catch return error.Io) |entry| { + var buf: [4096]u8 = undefined; + const joined = std.fmt.bufPrint(&buf, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), entry.name }) catch continue; + var normalized_buf: [4096]u8 = undefined; + const normalized = resolveOs(joined, &normalized_buf) orelse continue; + const child = dir.statFile(io, entry.name, .{}) catch continue; + if (skip > 0) { + skip -= 1; + continue; + } + const node = if (std.mem.eql(u8, normalized.path, "/")) os_root else os_node | (std.hash.Wyhash.hash(0, normalized.path) & (os_node - 1)); + tree.stageDirent(&listed, fixed.allocator(), node, child.kind == .directory, entry.name); + if (listed.items.len >= @max(size, 512)) break; + } + return listed.items; +} + +const OsReadWrite = union(enum) { reply: Reply, read: []const u8 }; + +/// The syscalls of an /os read, write or truncate: the reply, or the bytes +/// read into `buf`. +fn osReadWrite(path_z: [:0]const u8, req: Req, attr: Reply.Attr, buf: []u8) OsReadWrite { + const fd = libc.open(path_z, .{ .ACCMODE = if (req.op == .read) .RDONLY else .WRONLY, .NONBLOCK = true, .CLOEXEC = true }); + if (fd < 0) return .{ .reply = Reply.fail(req.tag, E.PERM) }; + defer _ = libc.close(fd); + if (req.op == .setattr) { + if (!req.truncate or req.off != 0 or libc.ftruncate(fd, 0) != 0) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid truncate: only to zero") }; + var truncated = attr; + truncated.size = 0; + return .{ .reply = .{ .tag = req.tag, .attr = truncated } }; + } + if (req.off > std.math.maxInt(i64)) return .{ .reply = tree.failText(req.tag, E.INVAL, "invalid offset: past what a file may hold") }; + if (libc.lseek(fd, @intCast(req.off), libc.SEEK.SET) < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + if (req.op == .write) { + const written = libc.write(fd, req.data.ptr, req.data.len); + if (written < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .reply = .{ .tag = req.tag, .written = @intCast(written) } }; + } + const got = libc.read(fd, buf.ptr, @min(req.size, buf.len)); + if (got < 0) return .{ .reply = Reply.fail(req.tag, E.IO) }; + return .{ .read = buf[0..@intCast(got)] }; +} + pub fn validMountName(name: []const u8) bool { if (name.len == 0 or name.len > name_capacity or std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false; if (std.mem.eql(u8, name, "os") or std.mem.eql(u8, name, "self")) return false; @@ -687,8 +694,10 @@ test "a path with a part over 255 bytes is NameTooLong, never a panic" { pub fn makeDirs(dir: []const u8) void { if (comptime !platform_has_fs) return; if (dir.len == 0) return; - pardes.turn.yield(); - defer pardes.turn.back(); + pardes.tape.site(.make_dirs, null, makeDirsHost, .{dir}); +} + +fn makeDirsHost(dir: []const u8) void { _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {}; } @@ -798,6 +807,12 @@ pub fn dotRelative(word: []const u8) bool { /// `~` to expand, or no home for it. pub fn expandHome(path: []const u8, out: *[4096]u8) []const u8 { if (comptime !pardes.hosted) return path; + if (path.len == 0 or path[0] != '~') return path; + // Home as the session that recorded found it (src/tape.zig). + return pardes.tape.value(.env, expandHomeHost, .{ path, out }); +} + +fn expandHomeHost(path: []const u8, out: *[4096]u8) []const u8 { return expandHomeFrom(path, out, std.c.getenv("HOME")); } @@ -846,8 +861,10 @@ pub fn resolveOs(path: []const u8, out: *[4096]u8) ?Resolved { if (comptime !platform_has_fs) return null; var z: [4096]u8 = undefined; const path_z = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return null; - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.resolve, null, resolveHost, .{ path_z, out }); +} + +fn resolveHost(path_z: [:0]const u8, out: *[4096]u8) ?Resolved { const resolved = realpath(path_z, out) orelse return null; return .{ .path = std.mem.span(resolved), .dir = isDir(resolved) }; } @@ -904,9 +921,7 @@ pub fn readLimit(p: *pardes.Pardes, path: []const u8, max_bytes: usize) ![]u8 { if (!std.mem.eql(u8, name, mount.name)) continue; // A peer answers on its own time, and the peer may be this very // editor: the turn goes out with the request. - pardes.turn.yield(); - defer pardes.turn.back(); - return ninep_io.Client.readLimit(p.gpa, mount.dial, remote_path, path, limit); + return pardes.tape.site(.read_mount, p.gpa, ninep_io.Client.readLimit, .{ p.gpa, mount.dial, remote_path, path, limit }); } return error.FileNotFound; } @@ -1233,8 +1248,10 @@ fn readFileLimit(gpa: std.mem.Allocator, path: []const u8, limit: usize) ![]u8 { const path_z = std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{native}, 0) catch return error.PathTooLong; // The path may be a mount this editor serves: the turn goes out with the // syscalls, and the bytes come back into memory of the caller's own. - pardes.turn.yield(); - defer pardes.turn.back(); + return pardes.tape.site(.read_file, gpa, readHost, .{ gpa, path_z, limit }); +} + +fn readHost(gpa: std.mem.Allocator, path_z: [:0]const u8, limit: usize) ![]u8 { const fd = libc.open(path_z, .{ .ACCMODE = .RDONLY, .NONBLOCK = true }); if (fd < 0) return switch (libc.errno(fd)) { .ACCES, .PERM => error.PermissionDenied, |
