From ed6581dbf287d8a2fb1c439a88ce7380ee754f75 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:50:55 -0300 Subject: Bound a scripted run's memory sessions; check a script records the same twice A scripted run from fresh state records in memory, and a very long one grew without end. It stops at 256 MiB (ten hours or more of play at 60 Hz; a write the memory database cannot make, out of memory included, is the same): once, a warning, and no more frames, while what it holds still reads (marks, clips, past sessions). test-same plays test/past.script twice into two databases and compares them value by value with test/same_sessions.zig, but for a session's started and ended (the wall clock): every frame, keyframe, stretch and mark follows from the script alone. A third run from fresh state leaves its state file and no database. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 30 +++++++++++++++++++++++ src/host/Sessions.zig | 51 +++++++++++++++++++++++++++++++++++--- test/same_sessions.zig | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 test/same_sessions.zig diff --git a/build.zig b/build.zig index d8da703..9b99597 100644 --- a/build.zig +++ b/build.zig @@ -409,6 +409,36 @@ pub fn build(b: *Build) void { test_step.dependOn(&moved.step); b.step("test-import", "Export a session from one database, import it into another, replay it").dependOn(&moved.step); + // A script run twice records the same database but for the wall clock's + // times: the log and the keyframes follow from the script alone. From + // fresh state it records in memory: no database at all. + const same_sessions = b.addExecutable(.{ .name = "same_sessions", .root_module = b.createModule(.{ + .root_source_file = b.path("test/same_sessions.zig"), + .target = target, + .optimize = optimize, + .imports = &.{.{ .name = "Sessions", .module = b.createModule(.{ .root_source_file = b.path("src/host/Sessions.zig"), .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "xitdb", .module = xitdb } } }) }}, + }) }); + const twice = b.addSystemCommand(&.{ "sh", "-c", + \\set -e + \\mkdir -p "$3"; d=$(realpath "$3"); rm -f "$d"/* + \\"$0" --headless --script "$1" --state "$d/a.state" "$4" + \\"$0" --headless --script "$1" --state "$d/b.state" "$4" + \\"$2" "$d/a.xitdb" "$d/b.xitdb" + \\"$0" --headless --fresh --script "$1" --state "$d/c.state" "$4" + \\test -e "$d/c.state"; test ! -e "$d/c.xitdb" + \\rm -f "$d"/* + }); + twice.setName("a script recorded twice"); + twice.addArtifactArg(host_exe); + twice.addFileArg(b.path("test/past.script")); + twice.addArtifactArg(same_sessions); + _ = twice.addOutputDirectoryArg("twice"); + twice.addArtifactArg(recorded); + twice.has_side_effects = true; + twice.expectExitCode(0); + test_step.dependOn(&twice.step); + b.step("test-same", "A script run twice records the same database but for times").dependOn(&twice.step); + // Where files go. Without --state: the XDG data directory, state and // sessions. In .sketchbook/ (`zig build run`'s): a .gitignore, and an // older build's state moved in, once, never over a file there. diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig index 32b660b..751961d 100644 --- a/src/host/Sessions.zig +++ b/src/host/Sessions.zig @@ -49,8 +49,11 @@ pub const commit_ns: u64 = std.time.ns_per_s - std.time.ns_per_ms; pub const periodic_ns: u64 = 60 * std.time.ns_per_s; /// Mark and clip names: no spaces, at most this long. pub const max_name = 32; +/// A memory database (a scripted run's) stops recording at this size: ten +/// hours or more of play at 60 Hz. What it holds stays readable. +pub const memory_max: u64 = 256 << 20; -const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end }; +pub const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end }; fn key(k: K) u64 { return @intFromEnum(k); } @@ -96,6 +99,8 @@ recording: ?u64 = null, /// State may have changed other than by a logged live frame (a replay ran): /// the next live frame starts with a keyframe. away: bool = false, +/// A memory database reached `memory_max`: nothing more is logged. +full: bool = false, /// The console line that last changed State, for the next keyframe. line: [256]u8 = undefined, line_len: usize = 0, @@ -113,7 +118,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, want: Mode, path: []const u8, fi s.buffer = buffer; switch (want) { .off => unreachable, - .memory => s.db = .{ .memory = try Memory.init(std.Io.failing, .{ .buffer = buffer, .file_id = 0 }) }, + .memory => s.db = .{ .memory = try Memory.init(std.Io.failing, .{ .buffer = buffer, .file_id = 0, .max_size = memory_max }) }, .file => { s.path = try gpa.dupe(u8, path); const cwd = std.Io.Dir.cwd(); @@ -298,7 +303,7 @@ pub fn frame(s: *Sessions, one: []abi.Event, state: []const u8, why: ?Why) !void e.time = @max(e.time, s.last_time); s.last_time = e.time; } - if (s.db == .off) return; + if (s.db == .off or s.full) return; const tick = one[one.len - 1].time; if (s.batch.items.len > 0 and tick -| s.batch_time >= commit_ns) try s.commit(.none); if (why) |w| { @@ -394,7 +399,17 @@ fn commitSynced(s: *Sessions, extra: Extra) !void { pub fn commit(s: *Sessions, extra: Extra) !void { switch (s.db) { .off => return, - inline .memory, .file => |*db| try db.runContext(Commit(@TypeOf(db.*)){ .s = s, .extra = extra }), + inline .memory, .file => |*db| db.runContext(Commit(@TypeOf(db.*)){ .s = s, .extra = extra }) catch |err| { + // A memory database that cannot grow (`memory_max`, or no + // memory left) stops recording, once. + if (s.db == .memory and !s.full) { + s.full = true; + s.batch.clearRetainingCapacity(); + s.dropQueued(); + std.log.warn("sessions: the memory database cannot grow ({t}; {d} MiB at most): no more frames recorded", .{ err, memory_max >> 20 }); + } + return err; + }, } if (s.batch.items.len > 0) { s.batches += 1; @@ -1024,3 +1039,31 @@ test "a compacted session database is smaller and holds every session and frame" try again.events(0, 0, 0, 600, &back); try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items), std.mem.sliceAsBytes(back.items)); } + +test "a memory session stops recording at its bound, and what it holds still reads" { + const gpa = std.testing.allocator; + var s = try open(gpa, std.testing.io, .memory, "", .{ .build = 7, .source = "", .layout = "a layout" }, 0); + defer s.deinit(); + s.db.memory.core.max_size = 64 << 10; + var live: std.ArrayList(abi.Event) = .empty; + defer live.deinit(gpa); + var state: u64 = 0; + var failed: usize = 0; + for (0..60 * 60) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + s.frame(&one, std.mem.asBytes(&state), if (f == 0) .start else null) catch |err| { + try std.testing.expectEqual(error.WriteFailed, err); + failed += 1; + }; + try live.appendSlice(gpa, &one); + state += 1; + } + try std.testing.expectEqual(@as(usize, 1), failed); + const n = (try s.summary(s.session)).frames; + try std.testing.expect(n >= 60 and n < 60 * 60); + var back: std.ArrayList(abi.Event) = .empty; + defer back.deinit(gpa); + try s.events(s.session, 0, 0, n, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[0..n]), std.mem.sliceAsBytes(back.items)); +} diff --git a/test/same_sessions.zig b/test/same_sessions.zig new file mode 100644 index 0000000..af13be0 --- /dev/null +++ b/test/same_sessions.zig @@ -0,0 +1,66 @@ +//! test-same (build.zig): `same_sessions A B` exits 0 when two sessions +//! databases hold the same values apart from the wall clock's (a session's +//! `started` and `ended`), and otherwise names the first that differs. +const std = @import("std"); +const Sessions = @import("Sessions"); +const DB = Sessions.Disk; +const Cursor = DB.Cursor(.read_only); + +pub fn main(init: std.process.Init) !void { + const io = init.io; + const gpa = init.arena.allocator(); + var args = init.minimal.args.iterate(); + _ = args.skip(); + var dbs: [2]DB = undefined; + var buffers: [2]std.Io.Writer.Allocating = undefined; + for (&dbs, &buffers) |*db, *buffer| { + const file = try std.Io.Dir.cwd().openFile(io, args.next() orelse return error.Usage, .{}); + buffer.* = .init(gpa); + db.* = try DB.init(io, .{ .file = file, .buffer = buffer, .fsync = false }); + } + if (!try same(gpa, dbs[0].rootCursor().readOnly(), dbs[1].rootCursor().readOnly(), "root")) std.process.exit(1); +} + +fn same(gpa: std.mem.Allocator, a: Cursor, b: Cursor, at: []const u8) anyerror!bool { + const tag = (try a.slot()).tag; + if (tag != (try b.slot()).tag) return differ(at, "kinds"); + switch (tag) { + .uint => if (try a.readUint() != try b.readUint()) return differ(at, "numbers"), + .bytes, .short_bytes => { + if (!std.mem.eql(u8, try a.readBytesAlloc(gpa, null), try b.readBytesAlloc(gpa, null))) return differ(at, "bytes"); + }, + .array_list => { + if (try a.count() != try b.count()) return differ(at, "lengths"); + var ia = try a.iterator(); + var ib = try b.iterator(); + var i: usize = 0; + while (try ia.next()) |ca| : (i += 1) { + if (!try same(gpa, ca, (try ib.next()).?, try std.fmt.allocPrint(gpa, "{s}[{d}]", .{ at, i }))) return false; + } + }, + .hash_map => { + const mb = try DB.HashMap(.read_only).init(b); + var n: usize = 0; + var ia = try a.iterator(); + while (try ia.next()) |kv_cursor| { + const kv = try kv_cursor.readKeyValuePair(); + n += 1; + const name = if (std.enums.fromInt(Sessions.K, kv.hash)) |k| @tagName(k) else "?"; + const there = try std.fmt.allocPrint(gpa, "{s}.{s}", .{ at, name }); + const cb = try mb.getCursor(kv.hash) orelse return differ(there, "presence"); + if (std.mem.eql(u8, name, "started") or std.mem.eql(u8, name, "ended")) continue; + if (!try same(gpa, kv.value_cursor, cb, there)) return false; + } + var ib = try b.iterator(); + while (try ib.next()) |_| n -%= 1; + if (n != 0) return differ(at, "fields"); + }, + else => return differ(at, "kinds (unknown)"), + } + return true; +} + +fn differ(at: []const u8, what: []const u8) bool { + std.debug.print("same_sessions: the databases' {s} differ at {s}\n", .{ what, at }); + return false; +} -- cgit v1.3