From 174e031985db910b4907ba324d6fd12c67898a5a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 8 Nov 2026 23:49:04 -0300 Subject: tests: monkey and replay are modes of the T1 runner and snapshot.zig goes: behaviour-test runs test/monkey9p.zig bare (its smoke), `-- --monkey ` drives an in-process session with test/monkey.zig's input (keys, leader bursts, the mouse, tiny resizes, over /kbdin, /mousein and Session.resize) hunting panics and hangs, `-- --replay` records each seed's sessions as a tape in memory and replays it (tape.replayBytes, replayMain's loop: three replicas and the dump), and `process-test -- --replay` records and replays the tty cases' real sessions as `zig build replay` did (with the addons it lacked, so images passes); the snap, snap-driver-test, monkey, monkey-test (it no longer compiled), replay and monkey-session steps, pardes-snap and build/snap.zig (now build/process.zig) go; checked: a counter outside the core bumped at the 40th update diverges every T1 seed and 89 of 103 old tapes, one bumped on terminal output diverges no T1 seed but 69 tty cases and 70 old tapes (why the tty replay stays), a panic on a window under 5x5 is found by the T1 monkey (seeds 1 8 17 20) and the old one (seed 13) Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/monkey9p.zig | 162 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 146 insertions(+), 16 deletions(-) (limited to 'test/monkey9p.zig') diff --git a/test/monkey9p.zig b/test/monkey9p.zig index 39a1a187..c9dd5083 100644 --- a/test/monkey9p.zig +++ b/test/monkey9p.zig @@ -4,8 +4,19 @@ //! /log said meanwhile. It was test/monkey9p.py, which drove a real //! `pardes --detach` over its socket. //! -//! zig build monkey-session -Dplatform=tui -- [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [-v] -//! zig build monkey-session-smoke -Dplatform=tui (part of behaviour-test) +//! zig build behaviour-test -Dplatform=tui (a short fixed-seed run) +//! zig build behaviour-test -Dplatform=tui -- [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [-v] +//! zig build behaviour-test -Dplatform=tui -- --monkey [--steps M] +//! zig build behaviour-test -Dplatform=tui -- --replay [--monkey ] +//! +//! The T1 runner's modes: this, the rules over random 9P requests; with +//! `--monkey`, random input (keys, the mouse, resizes) hunting panics and +//! hangs, as test/monkey.zig's random snapshot scripts did through a real +//! pty; with `--replay`, those seeds (20 unless named) each recording the +//! tape of what its sessions consumed and replaying it, as `PARDES_REPLAY` +//! replays a real session's: into two replicas at different addresses and +//! one that never draws, every digest checked, and each session's dump +//! loaded to its digest and dumped to its bytes again (src/tape.zig). //! //! A step's op is drawn from the seed alone: the panes, columns and held //! files it names are tokens (`{p3}`: /index's 4th serial, mod its length, @@ -45,6 +56,9 @@ const write_max = msize - 23; const room = msize - 24; /// A step that has not finished in this long is a hang. const hang_ms = 30_000; +/// What the runs are (see the top): the rules, the input, or the input +/// with every session's tape replayed. +var run_mode: enum { rules, input, replay } = .rules; const fixtures = [_][2][]const u8{ .{ "alpha.txt", "alpha beta gamma\nsecond line\n\nthird after a blank\n" }, @@ -71,12 +85,11 @@ const words = [_][]const u8{ }; const shell_lines = [_][]const u8{ "true", "false", "echo hi", "echo m9p", "sleep 1", "ls", "cat nonexist", "printf abc", "exit 3", "seq 1 2000", "yes | head -n 3000", "date", "pwd", "env | wc" }; const builtins = [_][]const u8{ - "Look", "Exec", "Kill", "Dump", "Msg", "NextColor", "Themes", "DumpThemes", "TreeContext", "PdfFit", "PdfTint", "PdfSections", "Petscii", - "Palette", "Ascii", "Save", "New", "Newcol", "Del", "Filter", "Mode", "Togglettymode", "Edit", "Undo", "Redo", "Collapse", - "Delcol", "DelAbove", "DelBelow", "Term", "Repl", "Joincol", "Config", "LocationsConfig", "Messages", "Mini", "Find", "Grep", "Left", - "Down", "Up", "Right", "Back", "Forward", "Last", "Recent", "Jumplist", "Wrap", "Debug", - "Verbose", "InactiveDim", "DumpDir", "Theme", "Get", "Put", "Undo", "Redo", "Zerox", "Snarf", "Paste", "Cut", - "Sort", "Font", "Local", + "Look", "Exec", "Kill", "Dump", "Msg", "NextColor", "Themes", "DumpThemes", "TreeContext", "PdfFit", "PdfTint", "PdfSections", "Petscii", + "Palette", "Ascii", "Save", "New", "Newcol", "Del", "Filter", "Mode", "Togglettymode", "Edit", "Undo", "Redo", "Collapse", + "Delcol", "DelAbove", "DelBelow", "Term", "Repl", "Joincol", "Config", "LocationsConfig", "Messages", "Mini", "Find", "Grep", "Left", + "Down", "Up", "Right", "Back", "Forward", "Last", "Recent", "Jumplist", "Wrap", "Debug", "Verbose", "InactiveDim", "DumpDir", + "Theme", "Get", "Put", "Undo", "Redo", "Zerox", "Snarf", "Paste", "Cut", "Sort", "Font", "Local", }; const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" }; const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; @@ -364,8 +377,75 @@ const Op = union(enum) { /// Down to the empty window (every pane closed but `keep`, the columns /// too), sometimes rebuilt with a new pane. clear: struct { keep: ?usize, cols: bool, rebuild: bool }, + /// The input modes' (genInput): runes at /kbdin, lines at /mousein, a + /// word on the screen clicked with the middle button (a workspace + /// word run), the window resized to `[cols, rows]`. + keys: []const u8, + mouse: []const u8, + word: []const u8, + resize: [2]u16, }; +// --------------------------------------------------------------------------- +// The tty monkey's input (--monkey): it was test/monkey.zig, random +// snapshot scripts through a real pty hunting panics. Keys and leader +// bursts, typing (sometimes far past a prompt's band), the mouse's presses, +// drags, wheel and hover, and windows as tiny as ordinary ones, at the +// fixtures' panes (bad UTF-8, wide glyphs). A hit is a panic or a hang, +// which the parent reports and shrinks as any failure; the rules are not +// checked between its steps. + +/// Workspace words, run with a middle click: new columns make narrow +/// panes, Find and Grep open prompts. +const monkey_words = [_][]const u8{ "Newcol", "Find", "Grep", "Joincol", "Help", "Dump", "NextColor", "Debug" }; +const leader_keys = "abcdefghijklmnopqrstuvwxyz0123456789$^"; +const typed = "abcdefghijklmnopqrstuvwxyz0123456789 -_=+,:;()"; +/// Enter, Tab, Backspace, the arrows, Page Down, Home and End as kbdin's +/// runes (docs/typ/reference.typ, Other files). +const named_keys = [_][]const u8{ "\n", "\t", "\x08", "\u{f00e}", "\u{f800}", "\u{f011}", "\u{f012}", "\u{f013}", "\u{f00d}", "\u{f018}" }; + +fn genInput(r: Random, a: Allocator, size: *[2]u16) Op { + const cols = size[0]; + const rows = size[1]; + switch (below(r, 12)) { + 0, 1 => if (cols >= 70 and rows >= 6) return .{ .word = pick(r, &monkey_words) }, + 2, 3 => { + const n = range(r, 1, if (below(r, 4) == 0) 201 else 13); + const text = a.alloc(u8, n) catch oom(); + for (text) |*c| c.* = typed[below(r, typed.len)]; + return .{ .keys = text }; + }, + 4 => return .{ .keys = pick(r, &named_keys) }, + 5 => return .{ .keys = "\x1b" }, + 6 => { + var chord: std.ArrayList(u8) = .empty; + chord.append(a, ' ') catch oom(); + for (0..range(r, 1, 4)) |_| chord.append(a, leader_keys[below(r, leader_keys.len)]) catch oom(); + return .{ .keys = chord.items }; + }, + 7, 8 => { + const button: u8 = pick(r, &[_]u8{ 1, 2, 4 }); + var lines: std.ArrayList(u8) = .empty; + lines.print(a, "m {d} {d} {d}\n", .{ below(r, cols), below(r, rows), button }) catch oom(); + if (r.boolean()) lines.print(a, "m {d} {d} {d}\n", .{ below(r, cols), below(r, rows), button }) catch oom(); + lines.print(a, "m {d} {d} 0\n", .{ below(r, cols), below(r, rows) }) catch oom(); + return .{ .mouse = lines.items }; + }, + 9 => { + const x = below(r, cols); + const y = below(r, rows); + return .{ .mouse = print(a, "m {d} {d} {d}\nm {d} {d} 0\n", .{ x, y, @as(u8, if (r.boolean()) 8 else 16), x, y }) }; + }, + 10 => return .{ .mouse = print(a, "m {d} {d} 0\n", .{ below(r, cols), below(r, rows) }) }, + else => { + size[1] = @intCast(if (r.boolean()) range(r, 1, 7) else range(r, 7, 41)); + size[0] = @intCast(if (r.boolean()) range(r, 1, 21) else range(r, 21, 141)); + return .{ .resize = size.* }; + }, + } + return .{ .keys = "" }; +} + fn seq(a: Allocator, ops: []const Op) Op { return .{ .seq = a.dupe(Op, ops) catch oom() }; } @@ -522,11 +602,22 @@ const Ctx = struct { step: usize = 0, fail_kind: []const u8 = "", fail_why: []const u8 = "", + /// Sessions whose tape did not replay (--replay). + diverged: usize = 0, fn s(c: *Ctx) *Session { return &c.sess.?; } + /// The tape of the session just ended replayed (--replay): a + /// divergence is a failure at the step it ended. + fn replayed(c: *Ctx, at_step: usize) void { + if (run_mode != .replay or Session.replayTape(c.gpa)) return; + c.diverged += 1; + std.debug.print("[seed {d} step {d}] FAIL replay: a replica diverged\n", .{ c.seed, at_step }); + std.debug.print("mky fail {d} replay:diverged\n", .{at_step -| 1}); + } + fn fail(c: *Ctx, a: Allocator, kind: []const u8, comptime f: []const u8, args: anytype) error{Fail} { c.fail_kind = kind; c.fail_why = print(a, f, args); @@ -543,7 +634,10 @@ const Ctx = struct { /// close does not end it), the session's own first pane removed, and /// half the time a follower of the log. fn fresh(c: *Ctx, at_step: usize) !void { - if (c.sess) |*old| old.deinit(); + if (c.sess) |*old| { + old.deinit(); + c.replayed(at_step); + } c.sess = null; _ = c.life.reset(.free_all); c.serials = .empty; @@ -556,7 +650,10 @@ const Ctx = struct { const la = c.life.allocator(); for (fixtures) |fx| try putFile(la, c.root, fx[0], fx[1]); removeFile(la, c.root, "fresh.txt"); - c.sess = try Session.initFile(c.gpa, fixtures[0][1]); + // The input modes start as `pardes` does, everything after on the + // tape: a file set in place before the first input is not. + if (run_mode == .replay) try Session.recordTape(c.gpa); + c.sess = try if (run_mode == .rules) Session.initFile(c.gpa, fixtures[0][1]) else Session.initTerm(c.gpa); const s_ = c.s(); const first = try serialsOf(la, (try readAll(c, la, "/index")) orelse ""); for (fixtures[0..3]) |fx| try s_.write("/look", print(la, "{s}/{s}\n", .{ c.root, fx[0] })); @@ -939,6 +1036,11 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void { .host => |o| if (o.data) |data| try putFile(a, c.root, o.name, data) else removeFile(a, c.root, o.name), .seq => |ops| for (ops) |sub| try exec(c, a, res, sub), .clear => unreachable, // resolved into a seq + // A refusal is an answer; a panic or a hang is what is hunted. + .keys => |typed_keys| _ = c.s().refused("/kbdin", typed_keys) catch {}, + .mouse => |lines| _ = c.s().refused("/mousein", lines) catch {}, + .word => |w| c.s().click(w, 0, 2) catch {}, + .resize => |size| c.s().resize(size[0], size[1]), } } @@ -1323,6 +1425,7 @@ fn runStep(c: *Ctx, a: Allocator, raw: Op) Fail!void { }; // Closing the session's last pane quits it (docs/typ/reference.typ). if (c.s().ended()) return error.Ended; + if (run_mode != .rules) return; checks(c, a, &res) catch |err| switch (err) { error.Fail => return error.Fail, else => return c.fail(a, "checker", "after {s}: {t}", .{ what, err }), @@ -1392,6 +1495,8 @@ fn fuzz(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: var step_arena: std.heap.ArenaAllocator = .init(gpa); var found: std.StringArrayHashMapUnmanaged(struct { n: usize, first: usize }) = .empty; try c.fresh(0); + // The window's size as the input's resizes leave it. + var size: [2]u16 = .{ 80, 24 }; const t0 = nowMs(); var ended: usize = 0; var ran: usize = 0; @@ -1399,7 +1504,7 @@ fn fuzz(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: _ = step_arena.reset(.retain_capacity); const a = step_arena.allocator(); c.step = step; - const op = gen(r, a); + const op = if (run_mode == .rules) gen(r, a) else genInput(r, a, &size); if (only) |list| if (std.mem.indexOfScalar(usize, list, step) == null) continue; std.debug.print("mky step {d}\n", .{step}); ran += 1; @@ -1425,8 +1530,11 @@ fn fuzz(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: const dt = @max(1, nowMs() - t0); std.debug.print("[seed {d}] {d} steps in {d} ms ({d}/s), {d} clean quit(s)\n", .{ seed, ran, dt, @divTrunc(@as(i64, @intCast(ran)) * 1000, dt), ended }); for (found.keys(), found.values()) |sig, v| std.debug.print(" {d}x {s} (first at step {d})\n", .{ v.n, sig[0..@min(sig.len, 200)], v.first }); - if (c.sess) |*s| s.deinit(); - return if (found.count() > 0) 1 else 0; + if (c.sess) |*s| { + s.deinit(); + c.replayed(steps); + } + return if (found.count() > 0 or c.diverged > 0) 1 else 0; } fn nowMs() i64 { @@ -1455,6 +1563,7 @@ fn spawn(gpa: Allocator, seed: u64, steps: usize, only: ?[]const usize, verbose: var argv: std.ArrayList(?[*:0]const u8) = .empty; for ([_][]const u8{ "pardes-monkey-session", "--seed", print(a, "{d}", .{seed}), "--steps", print(a, "{d}", .{steps}) }) |arg| try argv.append(a, try a.dupeSentinel(u8, arg, 0)); if (verbose) try argv.append(a, "-v"); + if (run_mode != .rules) try argv.append(a, if (run_mode == .input) "--input-mode" else "--replay"); if (only) |list| { var text: std.ArrayList(u8) = .empty; for (list, 0..) |n, i| try text.print(a, "{s}{d}", .{ if (i > 0) "," else "", n }); @@ -1607,11 +1716,15 @@ fn shrink(gpa: Allocator, seed: u64, f: Found, budget: usize) !void { const small = try ddmin(gpa, body, &trial, Trial.fails); var text: std.ArrayList(u8) = .empty; for (small, 0..) |n, i| try text.print(gpa, "{s}{d}", .{ if (i > 0) "," else "", n }); - std.debug.print(" shrunk {d} -> {d} ops: zig build monkey-session -Dplatform=tui -- --seed {d} --steps {d} --only {s} -v\n", .{ body.len, small.len, seed, f.step + 1, text.items }); + std.debug.print(" shrunk {d} -> {d} ops: zig build behaviour-test -Dplatform=tui -- {s}--seed {d} --steps {d} --only {s} -v\n", .{ body.len, small.len, switch (run_mode) { + .rules => "", + .input => "--monkey 1 ", + .replay => "--replay ", + }, seed, f.step + 1, text.items }); } fn usage() noreturn { - std.debug.print("usage: pardes-monkey-session [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [--shrink-budget N] [--smoke] [-v]\n", .{}); + std.debug.print("usage: pardes-monkey-session [--seed N]... [--steps M] [--only i,j,k] [--no-shrink] [--shrink-budget N] [--monkey ] [--replay] [-v]\n", .{}); std.process.exit(2); } @@ -1623,8 +1736,11 @@ pub fn main(init: std.process.Init) !void { var only: ?[]const usize = null; var no_shrink = false; var budget: usize = 300; - var smoke = false; + // Bare, as behaviour-test runs it: a short fixed-seed run. + var smoke = args.len == 1; var verbose = false; + // --monkey's count of seeds, 1 to N, when none is named. + var monkey: usize = 0; var i: usize = 1; while (i < args.len) : (i += 1) { const arg = args[i]; @@ -1648,6 +1764,14 @@ pub fn main(init: std.process.Init) !void { no_shrink = true; } else if (std.mem.eql(u8, arg, "--smoke")) { smoke = true; + } else if (std.mem.eql(u8, arg, "--monkey")) { + if (run_mode == .rules) run_mode = .input; + monkey = std.fmt.parseInt(usize, value, 10) catch usage(); + i += 1; + } else if (std.mem.eql(u8, arg, "--input-mode")) { + run_mode = .input; + } else if (std.mem.eql(u8, arg, "--replay")) { + run_mode = .replay; } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--verbose")) { verbose = true; } else usage(); @@ -1671,6 +1795,11 @@ pub fn main(init: std.process.Init) !void { steps = steps orelse 800; no_shrink = true; } + // The input's seeds are 70 steps each, as the tty monkey's scripts were. + if (run_mode != .rules) { + if (seeds.items.len == 0) for (1..if (monkey > 0) monkey + 1 else 21) |n| try seeds.append(gpa, n); + steps = steps orelse 70; + } if (seeds.items.len == 0) try seeds.append(gpa, 1); var total: usize = 0; for (seeds.items) |seed| { @@ -1680,5 +1809,6 @@ pub fn main(init: std.process.Init) !void { var seen: std.StringHashMapUnmanaged(void) = .empty; for (found) |f| if (!(try seen.getOrPut(gpa, f.sig)).found_existing) try shrink(gpa, seed, f, budget); } + if (run_mode != .rules) std.debug.print("{s}: {d} seeds of {d} steps, {d} failure(s)\n", .{ @tagName(run_mode), seeds.items.len, steps.?, total }); if (total > 0) std.process.exit(1); } -- cgit v1.3