summaryrefslogtreecommitdiff
path: root/test/snapshot.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 11:14:38 -0300
committerGabriel Schneider <[email protected]>2026-10-29 15:57:34 -0300
commita4b85cb7689c111c9f06891164e765a1b49f63c8 (patch)
tree7204b7978312e398ece4312c0ced0776c3296f68 /test/snapshot.zig
parent044da0f75ab5fb5be0213e512741860d95dad8e5 (diff)
downloadpardes-a4b85cb7689c111c9f06891164e765a1b49f63c8.tar.gz
pardes-a4b85cb7689c111c9f06891164e765a1b49f63c8.zip
replay gate: pardes-snap --tape and pardes-monkey --tape record every session's tape and fail a script whose tapes do not replay; `zig build replay`
pardes-snap --tape=DIR sets PARDES_RECORD for each script (each session, a nested pardes's too, writes DIR/<stem>-<pid>.tape) and, once the script has run and its goldens compared, replays every tape with the same build (PARDES_REPLAY): a divergence, or no tape at all, fails the script as DIVERGED with the replay's words. pardes-monkey --tape=DIR passes it on and counts a divergence as a hit. `zig build replay -Dplatform=tty [-Dreplay-seeds=N]` runs every snapshot script, then N monkey seeds of 70 steps (default 20), tapes under .zig-cache/tapes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/snapshot.zig')
-rw-r--r--test/snapshot.zig43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/snapshot.zig b/test/snapshot.zig
index 2d8a8664..255f55bf 100644
--- a/test/snapshot.zig
+++ b/test/snapshot.zig
@@ -86,6 +86,9 @@ var careful = false;
/// (--lspmock=); null leaves the protocol client fully disabled for the run
var lspmock_bin: ?[:0]const u8 = null;
var record_dir: ?[]const u8 = null;
+/// --tape=DIR: each session records its tape there (PARDES_RECORD, src/tape.zig)
+/// and the script fails unless every tape replays without diverging.
+var tape_dir: ?[]const u8 = null;
const gpa = std.heap.page_allocator;
pub fn main(init: std.process.Init) !void {
@@ -123,6 +126,8 @@ pub fn main(init: std.process.Init) !void {
lspmock = try absPath(arena, orig_cwd, a["--lspmock=".len..]);
} else if (std.mem.startsWith(u8, a, "--record=")) {
record_dir = try absPath(arena, orig_cwd, a["--record=".len..]);
+ } else if (std.mem.startsWith(u8, a, "--tape=")) {
+ tape_dir = try absPath(arena, orig_cwd, a["--tape=".len..]);
} else if (std.mem.startsWith(u8, a, "--jobs=")) {
jobs = @max(1, try std.fmt.parseInt(usize, a["--jobs=".len..], 10));
} else if (exe == null) {
@@ -148,6 +153,7 @@ pub fn main(init: std.process.Init) !void {
std.mem.eql(u8, a, "--trace-stable") or
std.mem.startsWith(u8, a, "--lspmock=") or
std.mem.startsWith(u8, a, "--record=") or
+ std.mem.startsWith(u8, a, "--tape=") or
std.mem.startsWith(u8, a, "--jobs=")) continue;
if (!saw_exe) {
saw_exe = true;
@@ -179,6 +185,7 @@ pub fn main(init: std.process.Init) !void {
if (update) fatal("--record and --update are separate modes", .{});
try mkdir(arena, dir, true);
}
+ if (tape_dir) |dir| try mkdir(arena, dir, true);
try mkdir(arena, SNAP_BASE, true);
// one run at a time (this is the flock that used to be typed by hand)
@@ -463,9 +470,15 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8
const started = eh.nowMs();
const out = runScript(arena, exe_z, script_path, stem) catch |e| {
std.debug.print("FAIL {s}: script error {s} ({d}ms)\n", .{ stem, @errorName(e), eh.nowMs() - started });
+ // What the session did before the script gave up replays all the same.
+ if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why });
return 1;
};
const took = eh.nowMs() - started;
+ if (tape_dir != null) if (replayTapes(arena, exe_z, stem)) |why| {
+ std.debug.print("DIVERGED {s}: {s}\n", .{ stem, why });
+ return 1;
+ };
if (record_dir) |dir| {
const path = std.fmt.allocPrintSentinel(arena, "{s}/{s}.snapshot", .{ dir, stem }, 0) catch return 1;
eh.writeFile(path, out) catch return 1;
@@ -495,6 +508,30 @@ fn runOne(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const u8
return 1;
}
+/// Every tape the script's sessions wrote, replayed by the same build
+/// (PARDES_REPLAY): why the first that diverged did, or null.
+fn replayTapes(arena: std.mem.Allocator, exe_z: [:0]const u8, stem: []const u8) ?[]const u8 {
+ const dir = tape_dir.?;
+ const io = std.Io.Threaded.global_single_threaded.io();
+ var d = std.Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }) catch return "the tape directory cannot be read";
+ defer d.close(io);
+ var it = d.iterate();
+ var tapes: usize = 0;
+ while (it.next(io) catch return "the tape directory cannot be listed") |entry| {
+ if (!std.mem.startsWith(u8, entry.name, stem) or !std.mem.endsWith(u8, entry.name, ".tape")) continue;
+ const pid = entry.name[stem.len..][0 .. entry.name.len - stem.len - ".tape".len];
+ if (pid.len < 2 or pid[0] != '-' or std.mem.indexOfNone(u8, pid[1..], "0123456789") != null) continue;
+ tapes += 1;
+ const cmd = std.fmt.allocPrintSentinel(arena, "PARDES_RECORD= PARDES_REPLAY='{s}/{s}' timeout 300 '{s}' > '{s}/{s}.replay' 2>&1", .{ dir, entry.name, exe_z, dir, entry.name }, 0) catch return "out of memory";
+ if (system(cmd) != 0) {
+ const why_path = std.fmt.allocPrint(arena, "{s}/{s}.replay", .{ dir, entry.name }) catch return "out of memory";
+ return readFileAlloc(arena, why_path) catch "the replay failed and said nothing";
+ }
+ }
+ if (tapes == 0) return "no session wrote a tape";
+ return null;
+}
+
fn reportPath(arena: std.mem.Allocator, script_path: []const u8) ![]const u8 {
return std.fmt.allocPrint(arena, "{s}/{s}.report", .{ SNAP_BASE, std.fs.path.stem(script_path) });
}
@@ -603,6 +640,12 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const
// together. Scripts name it `$DUMP`.
const dump_path = try std.fmt.allocPrint(arena, "{s}/dump.zon", .{base});
_ = setenv("PARDES_DUMP", try arena.dupeSentinel(u8, dump_path, 0), 1);
+ // Each session the script starts (and each pardes run inside one)
+ // writes its own tape, <stem>-<pid>.tape.
+ if (tape_dir) |dir| {
+ _ = system(try std.fmt.allocPrintSentinel(arena, "rm -f '{s}/{s}-'[0-9]*.tape '{s}/{s}-'[0-9]*.tape.replay", .{ dir, stem, dir, stem }, 0));
+ _ = setenv("PARDES_RECORD", try std.fmt.allocPrintSentinel(arena, "{s}/{s}-", .{ dir, stem }, 0), 1);
+ }
if (chdirRc(try arena.dupeSentinel(u8, work, 0)) < 0) return error.ChdirFailed;
const chunks = try arena.alloc([]const u8, countCaptures(src));