diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 14:44:08 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 15:36:57 -0300 |
| commit | a86e213e3e8053906df5ed4a37821c41a4afd932 (patch) | |
| tree | 9e22bf5bbe7b988757d10f80ac2348f8e5da27cf /core/jj.zig | |
| parent | 8152cf383c8296be29e80175b57e40f8d41049bf (diff) | |
| download | pardes-a86e213e3e8053906df5ed4a37821c41a4afd932.tar.gz pardes-a86e213e3e8053906df5ed4a37821c41a4afd932.zip | |
layers: `Changes` does not hold the loop (P43), in Stream.zig's model: the word records what is asked (`jj.Ask`: the pane by serial, the file, the revision annotated, the revset; `Stream.changes`, one at a time, one asked meanwhile in `changes_next`) and emits the `search` effect with a generation of its own (`jj.gen_bit`); the shell's worker (`jj.start`, through Stream.start) runs jj's two commands (the log with each change's diff of the file, the annotation) and posts their raw answers as ONE input (`Event.search`, its new `jj` field naming the pane: the log in `rows`, the annotation in `styles`, a refusal with `end` failed), which the step takes (`jj.receive`): the last asked is parsed (`jj.parse`: pins, provenance, hunks, nest) and pinned, an earlier one's or a closed pane's dropped, then the one asked meanwhile goes; a replica replays that input and never runs jj; a host with no workers (a test) runs both as sites on the loop, a host whose worker cannot start hands the answer in as an input; while it runs the pane says `Changes: jj…` and what it showed stays until the answer lands; round 3's `may_wait` row for `Changes` is gone (the far-client walk: nothing stops there now); tests through the replica's surface with a worker host: the word returns before jj answers and says so, the answer lands as one input and a client replaying it ends equal; a second Changes before the first's answer pins only the second's; the pane closed before the answer: nothing pinned, nothing kept (no leak); measured (ReleaseSafe, `Changes xpxluunz..pvtvmpzx` on core/replica.zig, 16 changes, 8,328 lines, 112 hunks, jj's answers taken by hand): the step that takes the answer holds the loop 2.3-2.8 ms, where the word held it 588-603 ms (jj's two runs, 253 and 349 ms here, are the worker's now); core-test 899/1059 (160 skipped), shell-test 1101/1104 (3 skipped)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/jj.zig')
| -rw-r--r-- | core/jj.zig | 364 |
1 files changed, 319 insertions, 45 deletions
diff --git a/core/jj.zig b/core/jj.zig index b4d119e8..5f86dbe6 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -46,7 +46,7 @@ fn dirOf(path: []const u8) []const u8 { /// The name of `path` as a fileset of that one file in its directory, /// taken literally whatever it is called. -fn fileset(arena: std.mem.Allocator, path: []const u8) ![]const u8 { +fn fileset(arena: std.mem.Allocator, path: []const u8) ![]u8 { var set: std.ArrayList(u8) = .empty; try set.appendSlice(arena, "file:\""); for (std.Io.Dir.path.basename(path)) |c| { @@ -54,7 +54,7 @@ fn fileset(arena: std.mem.Allocator, path: []const u8) ![]const u8 { try set.append(arena, c); } try set.append(arena, '"'); - return set.items; + return set.toOwnedSlice(arena); } /// jj with `args`, run in `dir`: what it printed, or its refusal. The @@ -198,16 +198,17 @@ pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look } // ---- `Changes [revset]` (PARDES.md: pinned changes as lanes): what jj -// answers for it, kept as flat arrays in the pane ---- +// answers for it, kept as flat arrays, the session's ---- -/// What jj answered for a pane's `Changes`: replicated state, the session's -/// (`Pardes.changes`, by the pane's serial: a client never runs jj; one that -/// joins late has it in the dump), its revset with it, drawn by each view that shows them (File.Layers.lanes: -/// a view holds that bit, not the revset; one set a pane, so a view's -/// `Changes other` replaces what every view shows). Two jj runs, each a -/// site of the step's tape: the changes with their diffs of the file -/// (`jj log -p --git`, the diffs read with diff.zig's walk), and who wrote -/// each line of the shown revision (`jj file annotate`). +/// What jj answered for a pane's `Changes`: replicated state, the +/// session's (`Pardes.changes`, by the pane's serial: a client never runs +/// jj; one that joins late has it in the dump), its revset with it, drawn +/// by each view that shows them (File.Layers.lanes: a view holds that, not +/// the revset; one set a pane, so a view's `Changes other` replaces what +/// every view shows). From two jj runs off the loop (`Ask`): the changes +/// with their diffs of the file (`jj log -p --git`, the diffs read with +/// diff.zig's walk), and who wrote each line of the shown revision (`jj +/// file annotate`). pub const Changes = struct { pub const replica = .{ .len = .{.{ "revset", "revset_len" }} }; /// The pane's (its serial). @@ -329,17 +330,208 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { .{ .path = f.path, .rev = "@" } else return p.reportFailure(c.id, "Changes: only a file's pane takes it"); + pane.layers.lanes = cells; + ask(p, c.id, pane.serial, shown.path, shown.rev, revset); +} + +// ---- jj off the loop (Stream.zig's model) ---- + +/// A `Changes` asked of jj: the pane (by serial), the file it shows and the +/// revision whose lines are annotated, the revset. Its two runs go on a +/// worker where the host has one (the `search` effect: Stream.start), their +/// raw answers back as one input (`Event.search`, its `jj` set) stamped +/// with its generation, which the step takes (`receive`): a replica replays +/// that input and never runs jj. With no worker (a test), on the loop, each +/// run a site of the step's tape. +pub const Ask = struct { + gpa: std.mem.Allocator, + /// What its answer is made with: the shell's, which frees a batch. + out: std.mem.Allocator, + gen: u32, + serial: u32, + path: []u8, + rev: []u8, + revset: []u8, + + fn make(gpa: std.mem.Allocator, gen: u32, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) error{OutOfMemory}!*Ask { + const a = try gpa.create(Ask); + errdefer gpa.destroy(a); + const owned = try std.mem.concat(gpa, u8, &.{ path, rev, revset }); + a.* = .{ .gpa = gpa, .out = gpa, .gen = gen, .serial = serial, .path = owned[0..path.len], .rev = owned[path.len..][0..rev.len], .revset = owned[path.len + rev.len ..] }; + return a; + } + + pub fn deinit(a: *Ask) void { + a.gpa.free(a.path.ptr[0 .. a.path.len + a.rev.len + a.revset.len]); + a.gpa.destroy(a); + } +}; + +/// The `Changes` asked and its worker (`Stream.changes`), one at a time: +/// one asked while it runs waits (`Stream.changes_next`), and only the last +/// asked is pinned. +pub const Task = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + ask: *Ask, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; + +/// A `Changes` generation has this bit (a search's and a parse's count +/// from 0; a paint's has the top one). +pub const gen_bit: u32 = 1 << 29; + +fn liveGen(s: *const pardes.Stream) u32 { + return gen_bit | (s.changes_count & (gen_bit - 1)); +} + +/// Ask jj of `revset` for pane `serial` (said at pane `id` while it runs): +/// what the pane shows stays until the answer lands. +fn ask(p: *Pardes, id: usize, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) void { + const s = &p.stream; + s.changes_count +%= 1; + const a = Ask.make(p.gpa, liveGen(s), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); + p.setStatus(.at(id), "Changes: jj\u{2026}"); + if (s.changes != null) { + if (s.changes_next) |old| old.deinit(); + s.changes_next = a; + return; + } + go(p, a); +} + +fn go(p: *Pardes, a: *Ask) void { + p.stream.changes = .{ .ask = a }; + if (p.tape.has(p, .search)) return p.emit(.{ .search = .{ .gen = a.gen } }); + runHere(p, a.gen, false); +} + +/// The host's `search` effect for a `Changes` generation (Stream.start): +/// its runs on a worker that sends to `sink`. +pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { + const t = if (p.stream.changes) |*t| t else return; + if (t.ask.gen != gen or t.future != null) return; + t.ask.out = gpa; + // No worker to be had: here, its answer an input. + t.future = io.concurrent(work, .{ t.ask, ctx, sink }) catch return runHere(p, gen, true); + t.io = io; +} + +fn work(a: *Ask, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { + const b = answer(null, a.out, a); + if (!sink(ctx, b)) b.deinit(a.out); +} + +/// With no worker: the runs here, each a site (a replica replays them). +/// `in_call`: in the host's `search` entry, the answer handed in as an +/// input, as a worker's is. +pub fn runHere(p: *Pardes, gen: u32, comptime in_call: bool) void { + const t = p.stream.changes orelse return; + if (t.ask.gen != gen or t.future != null) return; + const b = answer(p, p.gpa, t.ask); + defer b.deinit(p.gpa); + if (in_call) p.update(.{ .search = b }) else receive(p, &b, t.ask.serial); +} + +/// jj's two answers for `a`, raw, in `gpa`: through `p`'s tape when given +/// (on the loop), else run here (a worker). +fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batch { + const failed = struct { + fn f(g: std.mem.Allocator, at: *const Ask, why: []u8) pardes.Stream.Batch { + _ = g; + return .{ .gen = at.gen, .jj = at.serial, .rows = why, .end = .failed }; + } + }.f; + const set = fileset(gpa, a.path) catch return failed(gpa, a, refusal(gpa, "out of memory").refused); + defer gpa.free(set); + // The changes, oldest first, each with its diff of the file: one run. + const query = std.fmt.allocPrint(gpa, "({s}) & files({s})", .{ a.revset, set }) catch return failed(gpa, a, refusal(gpa, "out of memory").refused); + defer gpa.free(query); + const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", log_template, "--git", "--context=0", "--", set }; + const logged = switch (runJj(p, gpa, dirOf(a.path), &log_args)) { + .refused => |why| return failed(gpa, a, why), + .bytes => |bytes| bytes, + }; + var it = std.mem.splitScalar(u8, logged, '\n'); + const any = while (it.next()) |l| { + if (pinOf(l) != null) break true; + } else false; + if (!any) return .{ .gen = a.gen, .jj = a.serial, .rows = logged, .end = .done }; + // Who wrote each line: a path, not a fileset (annotate takes one file + // as it is named). + const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(a.path) }; + return switch (runJj(p, gpa, dirOf(a.path), &annotate_args)) { + .refused => |why| { + gpa.free(logged); + return failed(gpa, a, why); + }, + .bytes => |annotated| .{ .gen = a.gen, .jj = a.serial, .rows = logged, .styles = annotated, .end = .done }, + }; +} + +fn runJj(p: ?*Pardes, gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) Shown { + return if (p) |q| q.tape.site(.jj, gpa, runHost, .{ gpa, dir, args }) else runHost(gpa, dir, args); +} + +/// A `Changes` answer (`Event.search`, its `jj` set), borrowed: the last +/// asked's is parsed and pinned on its pane; an earlier one's, or a gone +/// pane's, dropped. Then the one asked meanwhile goes. +pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { + const s = &p.stream; + var revset: [Changes.revset_max]u8 = undefined; + var revset_len: usize = 0; + if (s.changes) |t| if (t.ask.gen == b.gen) { + s.changes = null; + if (t.future) |f| pardes.Stream.join(f, t.io.?, .wait); + revset_len = t.ask.revset.len; + @memcpy(revset[0..revset_len], t.ask.revset); + t.ask.deinit(); + }; + defer if (s.changes == null) if (s.changes_next) |next| { + s.changes_next = null; + go(p, next); + }; + if (b.gen != liveGen(s) or revset_len == 0) return; + const id = p.paneBySerial(serial) orelse return; + pinAnswer(p, id, b, revset[0..revset_len]); +} + +/// The session's end: the worker canceled, the asks let go. +pub fn cancel(s: *pardes.Stream) void { + if (s.changes) |t| { + s.changes = null; + if (t.future) |f| pardes.Stream.join(f, t.io.?, .cancel); + t.ask.deinit(); + } + if (s.changes_next) |a| { + s.changes_next = null; + a.deinit(); + } +} + +/// jj's answer `b` for `revset` parsed and pinned on pane `id`, or its +/// refusal said there. +fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []const u8) void { + const pane = p.panes[id].?; var line: pardes.Kind.Buf(512) = .{}; - var got = read(p, shown.path, shown.rev, revset, f.content) catch |err| return p.reportFailure(c.id, line.print("Changes: {s}", .{@errorName(err)})); + const f = if (pane.file) |*file| file else return; + // Nothing pinned yet: no gutter for it. + defer if (answered(p, pane.serial) == null) { + pane.layers.lanes = 0; + }; + if (b.end == .failed) return p.reportFailure(id, line.print("Changes: {s}", .{b.rows[0..@min(b.rows.len, 480)]})); + var got = parse(p.gpa, p.scratch.allocator(), b.rows, b.styles, f.content) catch |err| return p.reportFailure(id, line.print("Changes: {s}", .{@errorName(err)})); switch (got) { .refused => |why| { defer p.gpa.free(why); - return p.reportFailure(c.id, line.print("Changes: {s}", .{why[0..@min(why.len, 480)]})); + return p.reportFailure(id, line.print("Changes: {s}", .{why[0..@min(why.len, 480)]})); }, .read => |*changes| { if (changes.pins.len == 0) { changes.deinit(p.gpa); - return p.reportFailure(c.id, line.print("Changes: no change of `{s}` touches {s}", .{ revset, std.Io.Dir.path.basename(shown.path) })); + const shown = if (of(f)) |r| r.path else f.path; + return p.reportFailure(id, line.print("Changes: no change of `{s}` touches {s}", .{ revset, std.Io.Dir.path.basename(shown) })); } @memcpy(changes.revset[0..revset.len], revset); changes.revset_len = @intCast(revset.len); @@ -349,34 +541,22 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { kept.* = changes.*; } else p.changes.append(p.gpa, changes.*) catch { changes.deinit(p.gpa); - return p.reportFailure(c.id, "Changes: out of memory"); + return p.reportFailure(id, "Changes: out of memory"); }; - pane.layers.lanes = cells; + p.setStatus(.at(id), ""); }, } } const Read = union(enum) { refused: []u8, read: Changes }; -/// What jj says of `revset`'s changes in `path` (absolute) at `rev`, whose -/// text is `text` (what the pane shows: a line is matched to the line its -/// change added by its text). -fn read(p: *Pardes, path: []const u8, rev: []const u8, revset: []const u8, text: []const u8) error{OutOfMemory}!Read { - const gpa = p.gpa; - const arena = p.scratch.allocator(); - const set = try fileset(arena, path); - const dir = dirOf(path); +/// jj's answers parsed: `logged` (the pins, each with its diff of the +/// file) and `annotated` (who wrote each line of `text`, what the pane +/// shows: a line is matched to the line its change added by its text). +fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, annotated: []const u8, text: []const u8) error{OutOfMemory}!Read { var out: Changes = .{ .serial = 0 }; var kept = false; defer if (!kept) out.deinit(gpa); - // The changes, oldest first, each with its diff of the file: one run. - const query = try std.fmt.allocPrint(arena, "({s}) & files({s})", .{ revset, set }); - const log_args = [_][]const u8{ "log", "--no-graph", "--reversed", "-r", query, "-T", log_template, "--git", "--context=0", "--", set }; - const logged = switch (p.tape.site(.jj, gpa, runHost, .{ gpa, dir, @as([]const []const u8, &log_args) })) { - .refused => |why| return .{ .refused = why }, - .bytes => |b| b, - }; - defer gpa.free(logged); var pins: std.ArrayList(Pin) = .empty; defer pins.deinit(gpa); // Where each pin's diff starts in `logged`, and the end. @@ -393,12 +573,6 @@ fn read(p: *Pardes, path: []const u8, rev: []const u8, revset: []const u8, text: kept = true; return .{ .read = out }; } - // Who wrote each line. - const annotated = switch (annotate(p, path, rev)) { - .refused => |why| return .{ .refused = why }, - .bytes => |b| b, - }; - defer gpa.free(annotated); out.lines = try provenance(gpa, annotated, out.pins); // Each change's hunks, and which of its lines it added whole. var hunks: std.ArrayList(Hunk) = .empty; @@ -434,13 +608,6 @@ fn pinOf(l: []const u8) ?Pin { return pin; } -/// Who last wrote each line of `path` (absolute) at `rev`: a change id -/// (`change_id.short(8)`) a line, as `jj file annotate` says it. -pub fn annotate(p: *Pardes, path: []const u8, rev: []const u8) Shown { - // A path, not a fileset (annotate takes one file as it is named). - return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "annotate", "-r", rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(path) }) }); -} - /// Which pin wrote each line: a value a line, from `annotated` /// (`annotate`'s answer); `whole` is the hunks' to say. The caller's, in /// `gpa`. @@ -894,6 +1061,113 @@ test "a client replays Changes from what the session's jj answered and ends equa try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); } +/// A session whose `Changes` runs on a worker (Stream.start), as a shell's +/// does, its answer posted to an inbox, with a client of it. +const WorkerJj = struct { + const Inbox = @import("dev").host_io.Inbox; + const R = pardes.replica; + /// The host's: where its worker posts, and the core it starts for. + const Shell = struct { q: Inbox, core: ?*Pardes = null }; + shell: *Shell, + a: *R.Authority, + r: *R.Replica, + /// The revision `@-` of the repository's `a.zig`, open. + id: usize, + serial: u32, + + fn search(ctx: ?*anyopaque, gen: u32) void { + const shell: *Shell = @ptrCast(@alignCast(ctx.?)); + pardes.Stream.start(testing.io, testing.allocator, shell.core.?, gen, &shell.q, Inbox.pushSearch); + } + const vtable: pardes.Host.VTable = .{ .search = search }; + + fn init(repo: *const Repo) !WorkerJj { + const shell = try testing.allocator.create(Shell); + shell.* = .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; + const a = try R.Authority.init(testing.allocator, .{ .cols = 80, .rows = 24 }, .{ .vtable = &vtable, .ctx = shell }, false); + errdefer a.deinit(); + shell.core = a.core; + const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0), false); + var line: [4200]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}@@-", .{repo.file}) })); + const id = a.core.view().active; + return .{ .shell = shell, .a = a, .r = r, .id = id, .serial = a.core.panes[id].?.serial }; + } + + fn deinit(w: *WorkerJj) void { + w.r.deinit(); + w.a.deinit(); + w.shell.q.deinit(); + testing.allocator.destroy(w.shell); + } + + fn word(w: *WorkerJj, text: []const u8) !void { + w.r.confirm(try w.a.local(.{ .command = text })); + w.r.settle(); + } + + /// Until no `Changes` is asked or running (five seconds at most): each + /// answer the session's input, as its loop hands it, and the client's. + fn land(w: *WorkerJj) !void { + var waited: u32 = 0; + while (w.a.core.stream.changes != null or w.a.core.stream.changes_next != null) : (waited += 1) { + if (waited == 5000) return error.Timeout; + var batch: Inbox.Batch = .{}; + w.shell.q.takeInto(&batch); + for (batch.slice()) |m| switch (m) { + .search => |b| { + defer b.deinit(testing.allocator); + w.r.confirm(try w.a.local(.{ .search = b })); + w.r.settle(); + }, + else => m.deinit(testing.allocator, testing.allocator), + }; + testing.io.sleep(.fromMilliseconds(1), .awake) catch {}; + } + try testing.expect(w.r.diverged == null); + try testing.expectEqual(pardes.tape.digestOf(w.a.core), pardes.tape.digestOf(w.r.core())); + } +}; + +test "Changes asks jj on a worker: the word says so and returns, its answer lands as one input and pins; a client replays that input and ends equal" { + var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + var w = try WorkerJj.init(&repo); + defer w.deinit(); + try w.word("Changes @--::@-"); + // Asked, not answered: the word did not wait for jj. + try testing.expect(answered(w.a.core, w.serial) == null); + try testing.expect(std.mem.indexOf(u8, told(w.a.core, w.id), "Changes: jj") != null); + try w.land(); + try testing.expectEqual(@as(usize, 2), answered(w.a.core, w.serial).?.pins.len); + try testing.expectEqualSlices(Line, answered(w.a.core, w.serial).?.lines, answered(w.r.core(), w.serial).?.lines); +} + +test "a second Changes before the first's answer: only the second's pins" { + var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + var w = try WorkerJj.init(&repo); + defer w.deinit(); + try w.word("Changes @--::@-"); + try w.word("Changes @-"); + try w.land(); + const ch = answered(w.a.core, w.serial).?; + try testing.expectEqualStrings("@-", ch.pinned()); + try testing.expectEqual(@as(usize, 1), ch.pins.len); +} + +test "the pane closed before its Changes answer: nothing pinned, nothing kept" { + var repo = try Repo.init(testing.allocator) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + var w = try WorkerJj.init(&repo); + defer w.deinit(); + try w.word("Changes @--::@-"); + try w.word("Del"); + try testing.expect(w.a.core.panes[w.id] == null or w.a.core.panes[w.id].?.serial != w.serial); + try w.land(); + try testing.expectEqual(@as(usize, 0), w.a.core.changes.items.len); +} + test "Changes draws a lane a pinned change after the line number, nested ones a level to the right on their rows only, and a line one added whole green across it; bare again, the gutter as it was" { const gpa = testing.allocator; // `fn` wrote the function; `mid` rewrote its lines 3-4 as 3-6 (two |
