From 987210dbff8b830203cf5f52fa0cfc9b2974a3a2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 21:01:07 -0300 Subject: Kind: a draw's token is this process's count, which no rollback takes back, never its slot's: the token api.draw gives was the slot's `draws` (core/Kind.zig, the `.draw` entry of the api), which the rollback copy holds with the slot, so a frame drawn under a speculation that is rolled back asked token T, the copy put the count back, and the next frame's draw, of the state the replay came to, was given T again; both answers come to the kind's job_done as T, in the order its workers finish (`deliver` matches a job by core, pane, serial, gen and ops, not by token), and a kind with one draw outstanding takes the first and drops the second: it showed the discarded state's picture while asking nothing more, until its state moved again; the count is now `drawn`, beside `ready` (this process's draws), on root.zig's allowed_vars with why, and the slot's field and its kept_out entry are gone; the test `a far client's frame drawn under a speculation that rolls back asks a picture whose token the next draw is never given again: ...` (end of core/replica.zig: a plain kind drawing its count from its paint, a frontend's workers held so the discarded picture lands first) failed on the base with `expected 3, found 1` (the picture of 1 shown, its count 3) and passes; a user could not have hit it with the kinds in the tree (image draws in its open, from the file's bytes, the same picture whichever answer comes, and ignores the token; the pdf kind is not plain, so a client runs none of its code), only with a plain kind that draws from its paint and keeps its last token; gates on the seven together (the stack's top, load 18 to 50 beside them): tui core-test 855/999 (144 skipped), shell-test 1035/1038, pane-test 183/184, term-test 15/15, kitty-test 45/45, behaviour-test 275/275 and --replay 20 seeds 0 failures, process-test 3 of 144 (lspcomplete and lspcompletemoved, the base's, and headersweep-restored, which passes alone), gui unit-test 1633/1637 with the base's BaselineHarnessMismatch; replicas --clients 5 and --seeds 8: diverged 0, unequal 0, deltas wrong 0, quiet checks differed 0, unlisted stops 0, corpus 22 of 22, no leak report; the walk over every word and key passes; frame and perf gate against tree1 (farfaults): frames 77/77 byte-identical in Debug, unit-test 1633/1637, perfab corpus2x -0.087% (noise 0.092%, bar 0.4%, pictures identical), perfops 97 counts within 0.1% (largest +0.0329%) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Kind.zig | 17 ++++----- core/replica.zig | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/root.zig | 4 +-- 3 files changed, 115 insertions(+), 10 deletions(-) diff --git a/core/Kind.zig b/core/Kind.zig index 7641c2d1..81997afb 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -136,7 +136,7 @@ pub const Slot = struct { // jobs' inputs, its file's hash, a placeholder's record), and what // a frame makes (the thumb, the pointer, whether one was drawn: a // view's, never a step's). - .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "used", "draws" }, + .local = .{ "ops", "st", "gpa", "arena", "plain", "file", "opened", "reloads", "jobs", "record", "thumb_at", "thumb_span", "pointer", "painted", "over", "used" }, }; /// Its kind's code here; null in a placeholder (`live` false: its kind /// not in the registry, its record kept for the day it comes) or a @@ -198,11 +198,6 @@ pub const Slot = struct { /// whether a frame was painted since its last tick. pointer: u8 = 0, painted: bool = false, - /// The last of its draws' tokens (api.draw), each its own process's: a - /// draw is a frame's, and how many frames each process paints differs, - /// so it is in no digest; the copy holds it with the slot. A kind - /// keeping one in its state (PA_PLAIN) is caught at that call's digest. - draws: u32 = 0, /// Each view's facts (a hash of its pa_view) as `view` was last told /// them, 0 for none: a change tells it again. views: [views.max]u64 = @splat(0), @@ -1220,8 +1215,8 @@ pub const api: c.pa_api = .{ heap.free(copy); return 0; }; - cl.slot.draws = (cl.slot.draws +% 1) & ~c.PA_DRAWN; - const token = c.PA_DRAWN | cl.slot.draws; + drawn = (drawn +% 1) & ~c.PA_DRAWN; + const token = c.PA_DRAWN | drawn; j.* = .{ .gpa = heap, .pane = @intCast(cl.id), .serial = pane.serial, .token = token, .gen = cl.slot.gen, .ops = ops, .in = copy, .arena = .init(heap), .draw = p }; Addon.holdJob(ops, true); if (drawer) |d| if (d.go(d.ctx, j)) return token; @@ -1250,6 +1245,12 @@ pub const api: c.pa_api = .{ pub var drawer: ?struct { ctx: ?*anyopaque, go: *const fn (ctx: ?*anyopaque, j: *host_io.KindJob) bool } = null; /// Draws done, each kept for its pane's next paint (the loop's alone). var ready: std.ArrayListUnmanaged(*host_io.KindJob) = .empty; +/// The last token a draw was given: a draw is a frame's, this process's, in +/// no digest and no copy (a kind keeping one in its PA_PLAIN state is +/// caught at that call's digest). A rollback takes a slot back, never this, +/// so no draw is given the token a discarded speculation's frame was, whose +/// answer may come after. +var drawn: u32 = 0; /// A draw done on a worker, back on the loop: kept for its pane's next /// paint, a frame owed. One of another core's (a client joined again diff --git a/core/replica.zig b/core/replica.zig index 5b4afbb6..42aae524 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -5975,3 +5975,107 @@ test "a far client's drag in a kind's pane it grabbed, the pane since too short try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); try agrees(a, r); } + +// A draw's token is its slot's count of draws, which the rollback copy +// holds with the slot: a frame drawn under a speculation asks token T, the +// rollback takes the count back, and the next frame's draw (of the state +// the replay came to) is given T again. Its answer and the discarded one's +// both come to job_done as T, in the order the workers finish. +test "a far client's frame drawn under a speculation that rolls back asks a picture whose token the next draw is never given again: the kind keeps the picture of the state it shows" { + // A plain kind whose picture is a draw of its count (api.draw from its + // paint, when the picture asked for is of another count), its answer kept + // for the token it asked last, once: a kind with one draw outstanding. + // What it shows is this process's (a kind's own memory, never its block). + const Drawn = struct { + const pa = pardes.Kind.c; + var want: u32 = 0; + var asked: ?u32 = null; + var shown: ?u32 = null; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + const n: *u32 = @ptrCast(@alignCast(pardes.Kind.api.alloc.?(4))); + n.* = 0; + return n; + } + fn paint(st: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void { + const n: *const u32 = @ptrCast(@alignCast(st.?)); + if (asked == n.*) return; + asked = n.*; + want = pardes.Kind.api.draw.?(n, 4); + } + fn press(st: ?*anyopaque, _: [*c]const pa.pa_key) callconv(.c) c_int { + const n: *u32 = @ptrCast(@alignCast(st.?)); + n.* = n.* * 2 + 1; + return 1; + } + fn jobRun(in: [*c]const u8, n: u32, out: [*c]pa.pa_out) callconv(.c) u32 { + const o: [*]u8 = @ptrCast(out.*.alloc.?(out, n) orelse return 0); + @memcpy(o[0..n], in[0..n]); + return n; + } + fn jobDone(_: ?*anyopaque, token: u32, out: [*c]const u8, n: u32) callconv(.c) void { + if (token != want or n != 4) return; + want = 0; + shown = std.mem.readInt(u32, out[0..4], .little); + } + const words = [_]pa.pa_word{std.mem.zeroInit(pa.pa_word, .{ .name = "Drawn", .opens = 1, .doc = "a picture of a count" })}; + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "drawn".* ++ @as([11]u8, @splat(0)), .version = 1, .exts = "", .flags = pa.PA_PLAIN, .words = &words, .nwords = 1 }), + .open = open, + .paint = paint, + .key = press, + .job_run = jobRun, + .job_done = jobDone, + }); + }; + + // A frontend's workers for draws, run when the test says, each landing + // when it says (host_io.KindJob: run on a worker, landed on the loop). + const HeldDraws = struct { + var held: [4]*@import("dev").host_io.KindJob = undefined; + var n: usize = 0; + fn go(_: ?*anyopaque, j: *@import("dev").host_io.KindJob) bool { + j.run(); + held[n] = j; + n += 1; + return true; + } + }; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const drawn = pardes.Kind.loadedOf(&Drawn.ops, "/x/drawn.so", @splat(9), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &drawn } }); + _ = try a.local(.{ .command = "Drawn" }); + const kid = a.core.view().active; + const r = try joinedFresh(a); + defer r.deinit(); + try runsHere(r, kid, &Drawn.ops); + Drawn.want = 0; + Drawn.asked = null; + Drawn.shown = null; + HeldDraws.n = 0; + const was = pardes.Kind.drawer; + pardes.Kind.drawer = .{ .ctx = null, .go = HeldDraws.go }; + defer pardes.Kind.drawer = was; + const none: pardes.views.Present = .{ .ctx = null, .f = null }; + // Its own key: 0 -> 1, drawn (a picture of 1 asked). + const msg = try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'x', .text = "x" } })); + defer gpa.free(msg); + _ = r.draw(none); + try std.testing.expectEqual(@as(?u32, 1), Drawn.asked); + // The session's own key first: rolled back, its own applied again on + // top (0 -> 1 -> 3), drawn (a picture of 3 asked). + r.confirm(try a.own(.{ .key = .{ .cp = 'x', .text = "x" } })); + r.settle(); + _ = r.draw(none); + try std.testing.expectEqual(@as(?u32, 3), Drawn.asked); + try std.testing.expectEqual(@as(usize, 2), HeldDraws.n); + // The discarded picture's worker done first, then the other's; a frame. + for (HeldDraws.held[0..2]) |j| pardes.Kind.landDraw(r.core(), j); + _ = r.draw(none); + try std.testing.expectEqual(@as(?u32, 3), Drawn.shown); + r.confirm(try a.take(msg)); + r.settle(); + try agrees(a, r); +} diff --git a/core/root.zig b/core/root.zig index f55d72c3..ef3d1614 100644 --- a/core/root.zig +++ b/core/root.zig @@ -458,7 +458,7 @@ const allowed_vars = [_]struct { []const u8, []const u8, []const u8 }{ .{ "Kind.zig", "current", "the kind call in progress, put back at its end (threadlocal)" }, .{ "Kind.zig", "breach", "Debug: the first call over budget, for the addon test tool" }, .{ "Kind.zig", "drawer heap seed_base", "set once by the host at start" }, - .{ "Kind.zig", "ready", "this process's draws done, each keyed by its core, pane and serial" }, + .{ "Kind.zig", "ready drawn", "this process's draws: those done, each keyed by its core, pane and serial, and the last token given, which no rollback takes back" }, .{ "Names.zig", "dp_rows dp_trace", "the scorer's scratch, written before it is read in one call" }, .{ "Turn.zig", "yield_depth", "this thread's nesting of yields (threadlocal)" }, .{ "c_heap.zig", "allocators allocators_len allocators_lock last", "the C heap's registry of allocators and its lookup's memo" }, @@ -750,7 +750,7 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "File.zig", "tables", "the whole text's, read at once by whoever asks first (Tables.ensure): no reading under way that a frame could move" }, .{ "Text.zig", "room", "a buffer's capacity" }, .{ "Kind.zig", "ops st gpa arena plain file opened reloads jobs record", "the kind's code and memory here: a plain kind's block is in the copy, and its state hash is checked at every call (Kind.Call.end)" }, - .{ "Kind.zig", "thumb_at thumb_span pointer painted over used draws", "a frame's: written by a paint; `painted` is read by a tick of a kind that is not plain, whose calls are the authority's alone" }, + .{ "Kind.zig", "thumb_at thumb_span pointer painted over used", "a frame's: written by a paint; `painted` is read by a tick of a kind that is not plain, whose calls are the authority's alone" }, .{ "Places.zig", "ticks ticks_gen re", "the rail's ticks, made again when the places move (`gen`), and the compiled pattern, of the pattern" }, .{ "Plumb.zig", "parsed parsed_arena", "the rules parsed, of their text" }, .{ "Stream.zig", "parse_ns paint paint_retired paint_count ahead ahead_for painted_ahead painted_on_loop ahead_seen ahead_ask", "the workers' paints and timings: how a text looks, and numbers" }, -- cgit v1.3