summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-25 14:10:34 -0300
committerGabriel Schneider <[email protected]>2026-11-25 14:32:10 -0300
commit5b6552d1ddfdb966318dd1c5af5ea9d5c353a8a6 (patch)
tree1a34cedb0600f32777c45c4829ff9f4d1ed1d4fc /core
parente6c384833912598d65c509f0d3a7b713fab0a8eb (diff)
downloadpardes-5b6552d1ddfdb966318dd1c5af5ea9d5c353a8a6.tar.gz
pardes-5b6552d1ddfdb966318dd1c5af5ea9d5c353a8a6.zip
TagState.loaded: a view's tag state a dump gives is held to what its own state reads, and only that, so a damaged dump in describe-key's, Help's or a level's state is at rest instead of killing the session at its first layout, frame or key, and a session's own state loads as it is (R's optional robustness item, the pre-install diff read): describe-key found has its root, not the register's, its keys a leaf of the tree or an addon's SPC word, not found and missed at once, and with no root nothing typed or the one key struck; Help's and describe-key's keys and a struck key are cells keyName names; Help's scope is what `?` was struck under (SPC's keys to a group, no more than SPC's path holds; else none); a picker's cursor is in its own rows (All's are found again at each read); the view's SPC keys (`current`'s path for every level) are cells to a group, else the view is out of SPC too; THE CAUSE: loaded() checked describe-key only for `found` with no keys, so a dump left in its state reached `s.scope_root.?` (Band.zig:119, :172; keys.zig:598), `level[at]` on an empty level (Band.zig:182, and :216 for a level unrolled on SPC keys to nothing), bindings.keyName past its table (bindings.zig:428, from TagState.zig :653 `s.scope[0]`, :725 and :768 `s.miss`, chord and keyCells) and keys.helpBack's copy of Help's scope into SPC's 4-key path; and A2's checks ran on every state's fields whatever the state: a picker's cursor was held to Help's hit count, and describe-key's `found` (left as it was under Help and at rest) to its keys, so a session where a picker's cursor had moved with Help never run, or where describe-key had found a leaf before Help or rest, loaded at rest: the loaded core's digest was not the session's, the digest a join's client checks its load against (replica.zig `r.loaded(j.digest)`), and a Restore or an Upgrade dropped the picker; each state's fields are read only under it now, and the tree a load holds keys to is every builtin's (bindings.nodeAt and the registry's words, not bindings.level, which drops the live view's frontend's others), so every process holds every view alike; RUN (TEST FIRST): TagState.zig "a dump holding describe-key's state, or Help's, or a level's, damaged where a draw or a key indexes it, loads to rest and draws and takes keys" (14 damages through byteDump and dump.load, each drawn and given Backspace, Down, Enter, Backspace): fails on the base at TagState.zig:1496 (the state not at rest), and with that check taken out 10 of the 12 cases run panic on the base: `attempt to use null value` (found, no root), `index out of bounds: index 0, len 0` (found on `SPC z z`; Help's scope `z z` and Backspace; SPC unrolled on `z z`), `integer overflow` (a struck key 0x01 with no root; Help's scope key 0x01; a struck `miss` 0x05; SPC's key 0x01), `index out of bounds: index 95, len 95` (a struck key 0x7f under `g`), `index out of bounds: index 6, len 4` (Help's scope six keys, Backspace); the two that do not panic (found on `SPC t`, a group; on `g z`, nothing) draw another child's doc; and "a tag state a session made loads as it was, its digest the session's: ..." fails on the base at TagState.zig:1525 (`expected .pick, found .rest`); both pass now; the dump fuzz seeds describe-key's state too (core/fuzz.zig: a second join dump, SPC h k `g d` found, 2048 seeds by default, the test 38 s with it); a user could have hit the panics only through a damaged dump (a join or a restore: the user's rule, a session never dies of an input), and the digest's difference on any load of a session where a view's picker cursor had moved with Help never opened, or where describe-key had found a leaf before Help or rest; core-test 1099/1270 (171 skipped: the base's 1097 + these two), shell-test 1318/1321 (3 skipped), process-test 0 of 147 failed (3 skipped), the dump fuzz at 40,000 join seeds on each of the two dumps (Help's, describe-key's) and 1,779 image seeds clean (81,785 loads, the three pinned seeds among them); load 3 to 7
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core')
-rw-r--r--core/TagState.zig247
-rw-r--r--core/fuzz.zig10
2 files changed, 246 insertions, 11 deletions
diff --git a/core/TagState.zig b/core/TagState.zig
index 761eec48..5dedf62f 100644
--- a/core/TagState.zig
+++ b/core/TagState.zig
@@ -268,23 +268,71 @@ pub fn wake(p: *const Pardes) ?u64 {
return soonest;
}
-/// Each view's tag state as a dump gave it, held to what it indexes: Help's
-/// hits in the tree and the registry, its rows' cursor in them, the field's
-/// caret in its text, describe-key's found leaf or struck key in its keys.
-/// One that is not (a damaged dump: the fuzz's cut and flipped bytes) is
-/// at rest, its memory of leaves kept (dump.zig afterLoad).
+/// Each view's tag state as a dump gave it, held to what its state reads
+/// (dump.zig afterLoad); one that is not (a damaged dump: the fuzz's cut
+/// and flipped bytes) is at rest, its memory of leaves kept. Each state's
+/// own fields alone: what another state left in it (describe-key's `found`
+/// under Help, a picker's cursor past Help's rows) is as the session has
+/// it, and so is its digest. Every state: the field's caret in its text, a
+/// struck key a cell (keyName's table). Help: its hits leaves of the tree
+/// or SPC words of the registry, its cursor in them, its scope what `?` was
+/// struck under (SPC's keys to a group, as many as SPC's path holds: its
+/// Backspace unrolls them, keys.helpBack; else none). describe-key: its
+/// keys cells; found, a root's keys to a leaf, whose level Band.of
+/// replays; with no root, nothing typed or the one key struck. A picker:
+/// its cursor in its rows (Pick.rowAt; All's are found again at each
+/// read). And the view's SPC keys, every level's path (`current`): cells
+/// to a group, else the view is out of SPC too.
pub fn loaded(p: *Pardes) void {
for (&p.views) |*slot| if (slot.*) |*v| {
const s = &v.tag_state;
- var held = s.field.caret <= s.field.len and s.nhits <= max_hits and s.rows.at < @max(1, s.nhits) and !(s.found and s.scope_n == 0) and !(s.missed and s.scope_root == null and s.scope_n == 0);
- // `:`'s hits index its own rows (the themes, the settings: Command.zig,
- // which holds each where it reads it); the rest the tree's leaves.
- const command = s.kind == .prompt and s.prompt.op == .command;
- for (s.hits[0..@min(s.nhits, max_hits)]) |at| held = held and (command or at < bindings.flat.len + p.kinds.nwords);
+ const keys = s.scope[0..s.scope_n];
+ var held = s.field.caret <= s.field.len and s.nhits <= max_hits and (s.miss == 0 or cells(&.{s.miss}));
+ switch (s.kind) {
+ .help => {
+ for (s.hits[0..@min(s.nhits, max_hits)]) |at| held = held and (at < bindings.flat.len or at - bindings.flat.len < p.kinds.nwords and p.kinds.words[at - bindings.flat.len].leader.len > 0);
+ held = held and s.rows.at < @max(1, s.nhits) and
+ if (s.scope_root == .leader) keys.len <= v.leader_keys.len and cells(keys) and reaches(p, .leader, keys, false) else keys.len == 0;
+ },
+ .describe => held = held and cells(keys) and !(s.found and s.missed) and if (s.scope_root) |r|
+ r != .register and (!s.found or keys.len > 0 and reaches(p, r, keys, true))
+ else
+ !s.found and keys.len == @intFromBool(s.missed),
+ .pick => if (@import("Pick.zig").byId(p, s.pick)) |pk| {
+ held = held and (pk.source == .all or s.rows.at < @max(1, @import("Pick.zig").count(p, pk)));
+ },
+ .rest, .prompt, .rename => {},
+ }
+ const spc = v.leader_keys[0..v.leader_n];
+ if (v.prefix == .leader and !(cells(spc) and reaches(p, .leader, spc, false))) {
+ v.prefix = .none;
+ v.leader_n = 0;
+ held = false;
+ }
if (!held) s.* = .{ .used = s.used };
};
}
+/// Whether each of `keys` is a cell keyName names.
+fn cells(keys: []const u8) bool {
+ for (keys) |k| if (k < ' ' or k > '~') return false;
+ return true;
+}
+
+/// Whether `path` under `root` is a group (`leaf` false) or a leaf of the
+/// tree or of the registry's SPC words. Every builtin's, whichever view's
+/// frontend took it (bindings.level drops the live view's others): a load
+/// holds each view the same in every process.
+fn reaches(p: *const Pardes, root: Root, path: []const u8, leaf: bool) bool {
+ if (bindings.nodeAt(root, path)) |n| if (if (leaf) n.leaf != null else n.children.len > 0) return true;
+ if (root != .leader) return false;
+ for (p.kinds.words[0..p.kinds.nwords]) |*w| {
+ const keys = w.leader.slice();
+ if (if (leaf) std.mem.eql(u8, keys, path) else keys.len > path.len and std.mem.startsWith(u8, keys, path)) return true;
+ }
+ return false;
+}
+
/// Asking (SPEC §3.2, §10.2): a question waiting on the pane, its tag at
/// rest, in place of the tag's own text: what it asks, `▸`, each answer's
/// key and word, and how to give one (`Esc later` while SPC e has given it
@@ -1364,3 +1412,182 @@ test "a click that gives the keyboard to another pane ends a level as Esc does;
click(p, rt.x + config.GUTTER + 1, rt.y + pardes.BOX_H);
try std.testing.expect(p.view().tag_state.kind == .rest and hostOf(p) == null);
}
+
+test "a dump holding describe-key's state, or Help's, or a level's, damaged where a draw or a key indexes it, loads to rest and draws and takes keys" {
+ const Key = pardes.Key;
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 40 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\ntwo\nthree\n");
+ p.clock.now_ns = 5 * std.time.ns_per_s;
+ p.clock.stepped_ns = p.clock.now_ns;
+ const typed = struct {
+ fn keys(q: *Pardes, cps: []const u8) void {
+ for (cps) |c| q.update(.{ .key = .{ .cp = c } });
+ }
+ }.keys;
+ // Each case: the state a session made (describe-key's `g d` found,
+ // Help under `SPC t`, SPC's level rolled), then a field of it as a cut
+ // or flipped dump gives it.
+ const Case = struct { keys: []const u8, damage: *const fn (v: *View) void };
+ const cases = [_]Case{
+ // Found with no root (Band.takes, Band.of: `scope_root.?`).
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope_root = null;
+ }
+ }.f },
+ // Found on keys no leaf of the tree is: `SPC z z` (its level
+ // empty, Band.of's `level[at]`), `SPC t` (a group), `g z` (none).
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope_root = .leader;
+ v.tag_state.scope[0..2].* = "zz".*;
+ v.tag_state.scope_n = 2;
+ }
+ }.f },
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope_root = .leader;
+ v.tag_state.scope[0] = 't';
+ }
+ }.f },
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0] = 'z';
+ }
+ }.f },
+ // Found and missed at once; a register's root, which describe-key
+ // never takes.
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.missed = true;
+ }
+ }.f },
+ .{ .keys = " hkgd", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope_root = .register;
+ }
+ }.f },
+ // A struck key past keyName's table, with no root (`s.scope[0]`)
+ // and under one (bindings.chord).
+ .{ .keys = " hkq", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0] = 0x01;
+ }
+ }.f },
+ .{ .keys = " hkgq", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0] = 0x7f;
+ }
+ }.f },
+ // Help's scope: more keys than SPC's path holds (Backspace copies
+ // them into it: keys.helpBack), keys to no group (Backspace unrolls
+ // their level), a key past keyName's table (its key cells).
+ .{ .keys = " t?", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0..6].* = "tttttt".*;
+ v.tag_state.scope_n = 6;
+ }
+ }.f },
+ .{ .keys = " t?", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0..2].* = "zz".*;
+ v.tag_state.scope_n = 2;
+ }
+ }.f },
+ .{ .keys = " t?", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.scope[0] = 0x01;
+ }
+ }.f },
+ // SPC's level rolled and shown: a struck key past keyName's table
+ // (`s.miss`); unrolled on SPC keys to no group (Band.of's
+ // `level[at]`); SPC's keys past the table (their cells, the chord).
+ .{ .keys = " ", .damage = struct {
+ fn f(v: *View) void {
+ v.tag_state.due_ns = 1;
+ v.tag_state.miss = 0x05;
+ }
+ }.f },
+ .{ .keys = " ", .damage = struct {
+ fn f(v: *View) void {
+ v.leader_keys[0..2].* = "zz".*;
+ v.leader_n = 2;
+ v.tag_state.unrolled = true;
+ }
+ }.f },
+ .{ .keys = " ", .damage = struct {
+ fn f(v: *View) void {
+ v.leader_keys[0] = 0x01;
+ v.leader_n = 1;
+ v.tag_state.due_ns = 1;
+ }
+ }.f },
+ };
+ for (cases) |case| {
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ typed(p, case.keys);
+ try std.testing.expect(p.view().tag_state.kind != .rest or p.view().prefix == .leader);
+ case.damage(p.view());
+ const bytes = try pardes.dump.byteDump(p);
+ defer gpa.free(bytes);
+ // The session itself at rest again: the damage is the dump's.
+ p.view().tag_state = .{};
+ p.view().prefix = .none;
+ p.view().leader_n = 0;
+ var notes: std.ArrayList(u8) = .empty;
+ defer notes.deinit(gpa);
+ const q = try pardes.dump.load(gpa, .{ .cols = 120, .rows = 40 }, bytes, &notes);
+ defer q.deinit();
+ const s = &q.view().tag_state;
+ try std.testing.expect(s.kind == .rest and s.root == null and !s.unrolled and s.miss == 0);
+ // SPC's own keys kept only where they reach a group: none here.
+ try std.testing.expect(q.view().prefix != .leader or q.view().leader_n == 0);
+ // Drawn, then keys a state reads (Enter, Backspace, Down), drawn.
+ q.clock.now_ns = 10 * std.time.ns_per_s;
+ var frame = std.heap.ArenaAllocator.init(gpa);
+ defer frame.deinit();
+ q.sync();
+ _ = try q.render(frame.allocator());
+ for ([_]u21{ Key.backspace, Key.down, Key.enter, Key.backspace }) |cp| {
+ q.update(.{ .key = .{ .cp = cp } });
+ q.sync();
+ _ = try q.render(frame.allocator());
+ }
+ }
+}
+
+test "a tag state a session made loads as it was, its digest the session's: Help after describe-key found a leaf, a picker's cursor past Help's rows" {
+ const Key = pardes.Key;
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 120, .rows = 40 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\ntwo\nthree\n");
+ const same = struct {
+ fn digest(q: *Pardes) !void {
+ const bytes = try pardes.dump.byteDump(q);
+ defer std.testing.allocator.free(bytes);
+ const r = try pardes.dump.load(std.testing.allocator, .{ .cols = 120, .rows = 40 }, bytes, null);
+ defer r.deinit();
+ try std.testing.expectEqual(q.view().tag_state.kind, r.view().tag_state.kind);
+ try std.testing.expectEqual(q.view().tag_state.rows.at, r.view().tag_state.rows.at);
+ try std.testing.expectEqual(pardes.tape.digestOf(q), pardes.tape.digestOf(r));
+ }
+ }.digest;
+ // A picker of three rows, its cursor on the third (Help never opened:
+ // no hits), then rolled up.
+ pardes.Pick.open(p, 0, .list, "Refs", "", "a\nb\nc\n");
+ for (0..2) |_| p.update(.{ .key = .{ .cp = Key.down } });
+ try std.testing.expect(p.view().tag_state.kind == .pick and p.view().tag_state.rows.at == 2);
+ try same(p);
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try same(p);
+ // describe-key's `g d` found, then Help over every root: `found` is
+ // describe-key's alone, left as it was.
+ for (" hkgd") |c| p.update(.{ .key = .{ .cp = c } });
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ for (" ?") |c| p.update(.{ .key = .{ .cp = c } });
+ try std.testing.expect(p.view().tag_state.kind == .help and p.view().tag_state.found);
+ try same(p);
+}
diff --git a/core/fuzz.zig b/core/fuzz.zig
index 091eca63..56ded66b 100644
--- a/core/fuzz.zig
+++ b/core/fuzz.zig
@@ -803,6 +803,14 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan
defer gpa.free(bytes);
const image = try pardes.dump.dumpBytes(p);
defer gpa.free(image);
+ // And one with describe-key's state (SPC h k, `g d` found: a root, its
+ // keys, the leaf whose level the band replays), Help's fields as Help
+ // left them.
+ p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ for (" hkgd") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
+ try testing.expect(p.view().tag_state.kind == .describe and p.view().tag_state.found);
+ const described = try pardes.dump.byteDump(p);
+ defer gpa.free(described);
// Seconds a road, each core used (`use`). The image's past 1779 seeds
// reach xitdb's own reader (a slot's tag past its kinds: a `switch` on
// it), a dependency's.
@@ -816,7 +824,7 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan
// `live` (the core read in place, its own `live` undefined), a live
// message's `src` length 0xAAAA when the digest walked it.
try loadSeed(bytes, .join, 6356);
- for ([_][]const u8{ bytes, image }, [_]Road{ .join, .restore }, [_]u64{ 2048, 1024 }) |whole, road, n| {
+ for ([_][]const u8{ bytes, described, image }, [_]Road{ .join, .join, .restore }, [_]u64{ 2048, 2048, 1024 }) |whole, road, n| {
try loadSeed(whole, road, 0);
for (1..n + 1) |seed| try loadSeed(whole, road, seed);
}