summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 23:24:29 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit57e254470e11ab40b6bae31b2cbf62aebca27ce4 (patch)
treec1ab036650033c896069fa35a3d6279fe004fb32 /build.zig
parent8ce27cfdbab6d576db36403afbcef9659cb6dbdc (diff)
downloadsketchbook-57e254470e11ab40b6bae31b2cbf62aebca27ce4.tar.gz
sketchbook-57e254470e11ab40b6bae31b2cbf62aebca27ce4.zip
Record every run's frames to a sessions database in xitdb
Live play was recorded only after someone typed record N, into 16 fixed slots. Now every run is a session in one xitdb database per sketch, beside its state file (<name>.xitdb): an append-only log of the frames live update was fed, one commit a second of session time, and keyframes of the State at start, every minute, on jumps, before and after reloads and at exit. A kill -9 loses at most the uncommitted second (test-kill). Whether a build records travels in the sketch: App.Options.record, null meaning Debug, reaching the host as Info.record. Scripted runs from fresh state record in memory, exports not at all. Each stretch of a session keeps its build, layout and Info.source: the repository revision (one jj or git call per build, build_info), zig version, backend and mode. Sessions.zig takes plain data (bytes, events, strings, a path, an Io), so a second host can drive it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig77
1 files changed, 70 insertions, 7 deletions
diff --git a/build.zig b/build.zig
index 5f464ab..b642a09 100644
--- a/build.zig
+++ b/build.zig
@@ -56,6 +56,8 @@ pub fn build(b: *Build) void {
_ = b.addModule("gpu_scene", .{ .root_source_file = b.path("src/gpu/scene.zig"), .imports = &.{.{ .name = "abi", .module = abi }} });
// Embedders provide their existing SDL translation and device.
if (b.option(bool, "gpu-only", "export GPU modules without building the sketch host") orelse false) return;
+ // The host's session database (src/host/Sessions.zig).
+ const xitdb = (b.lazyDependency("xitdb", .{}) orelse return).module("xitdb");
// Geometry and noise, shared by the sketch library and `vector`.
const math = b.addModule("math", .{ .root_source_file = b.path("src/math/root.zig") });
const sketch_imports: []const Build.Module.Import = &.{ .{ .name = "abi", .module = abi }, .{ .name = "math", .module = math } };
@@ -82,12 +84,13 @@ pub fn build(b: *Build) void {
const host = b.addModule("host", .{
.root_source_file = b.path("src/host/root.zig"),
.target = target,
- .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 } },
+ .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 }, .{ .name = "xitdb", .module = xitdb } },
});
b.step("test-control", "Test 9P control validation and screenshot snapshots").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{ "control writes", "9P reads" } })).step);
b.step("test-timing", "Test display clock, recorded ticks, timing history and console controls").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{ "timing", "presentation", "frames are whole", "a tick's dt" } })).step);
+ b.step("test-sessions", "Test the sessions database: frames, keyframes, marks, clips").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{"session"} })).step);
b.step("test-presentation", "Test presentation feedback, pacing and trace snapshots").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{"presentation"} })).step);
const setup_tests = b.step("test-setup", "Test resource setup lifecycle and arguments");
@@ -176,7 +179,7 @@ pub fn build(b: *Build) void {
b.createModule(.{ .root_source_file = b.path("src/abi.zig"), .target = target, .optimize = optimize }),
b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }),
b.createModule(.{ .root_source_file = b.path("src/math/root.zig"), .target = target, .optimize = optimize }),
- b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 } } }),
+ b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 }, .{ .name = "xitdb", .module = xitdb } } }),
}) |module| test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step);
// The noise tests again on wasm32 as the web builds it, under Node's
@@ -328,6 +331,30 @@ pub fn build(b: *Build) void {
reload.expectExitCode(0);
test_step.dependOn(&reload.step);
+ // A session survives a kill -9 up to its last commit: 255 frames
+ // played, four commits of 60 made, the fifth lost with the process.
+ // Its sketch records in every mode, so the test does in release builds too.
+ const recorded = sb.app(.{ .name = "replay", .root_source_file = b.path("test/replay.zig"), .record = true }).addLibrary();
+ const killed = b.addSystemCommand(&.{ "sh", "-c",
+ \\set -e
+ \\db="${3%.state}.xitdb"
+ \\rm -f "$3" "$3.store" "$db"
+ \\if "$0" --headless --script "$1" --state "$3" "$4"; then echo "killed run exited"; exit 1; else test $? = 137; fi
+ \\"$0" --headless --script "$2" --state "$3" "$4"
+ \\test "$(stat -c %s "$db")" -lt 1048576
+ \\rm -f "$3" "$3.store" "$db"
+ });
+ killed.setName("a session survives kill -9");
+ killed.addArtifactArg(host_exe);
+ killed.addFileArg(b.path("test/kill.script"));
+ killed.addFileArg(b.path("test/killed.script"));
+ _ = killed.addOutputFileArg("kill.state");
+ killed.addArtifactArg(recorded);
+ killed.has_side_effects = true;
+ killed.expectExitCode(0);
+ test_step.dependOn(&killed.step);
+ b.step("test-kill", "A session survives kill -9 up to its last commit").dependOn(&killed.step);
+
// Recorded input replays to the same state: played, seeked, stepped,
// and after a live pointer move (core host behaviour, not an API).
const replay = b.addRunArtifact(host_exe);
@@ -549,6 +576,9 @@ const Shared = struct {
openssl: ?Build.LazyPath = null,
/// A self-signed certificate and its key, for `addServe`.
certificate: ?Certificate = null,
+ /// `source`, the revision the build's sketches are made from (see
+ /// `buildInfo`).
+ build_info: ?*Build.Module = null,
const Certificate = struct { cert: Build.LazyPath, key: Build.LazyPath };
const Recipe = union(enum) { glsl: Sketchbook.ShaderOptions, zig: Sketchbook.ZigShaderOptions };
@@ -977,6 +1007,30 @@ pub const Sketchbook = struct {
return b.createModule(.{ .root_source_file = dir.path(b, "typst.zig") });
}
+ /// The module `build_info`: `source`, the revision of the repository
+ /// the build runs in, read once per build.
+ // ponytail: one `jj log -r @` (else git, else nothing) per build, on the
+ // critical path of every sketch compile: 10-20 ms, a `snapshot working
+ // copy` op in the user's op log per build, and `sh` only. Upgrade, once
+ // incremental builds make 10 ms matter: the development host asks the
+ // repository at load time instead, off the compile path. Never fails a
+ // build: any error is "no source".
+ fn buildInfo(sb: Sketchbook) *Build.Module {
+ if (sb.shared.build_info) |module| return module;
+ const b = sb.b;
+ const run = b.addSystemCommand(&.{ "sh", "-c",
+ \\r=$(jj log --no-graph -r @ -T 'commit_id ++ " " ++ change_id' 2>/dev/null) && r="jj:$r" ||
+ \\ { r=$(git rev-parse HEAD 2>/dev/null) && { git diff --quiet HEAD 2>/dev/null || r="$r+dirty"; } && r="git:$r"; } ||
+ \\ r=""
+ \\printf 'pub const source = "%s";\n' "$r"
+ });
+ run.setCwd(b.path("."));
+ run.setName("source revision");
+ run.has_side_effects = true;
+ sb.shared.build_info = b.createModule(.{ .root_source_file = run.captureStdOut(.{ .basename = "build_info.zig" }) });
+ return sb.shared.build_info.?;
+ }
+
pub fn app(sb: Sketchbook, options: App.Options) App {
const imports = sb.b.allocator.alloc(Build.Module.Import, options.imports.len + 1) catch @panic("OOM");
imports[0] = .{ .name = "sketch", .module = sb.modules.sketch };
@@ -986,6 +1040,7 @@ pub const Sketchbook = struct {
.name = options.name,
.root_source_file = options.root_source_file,
.imports = imports,
+ .record = options.record,
// No target: every artifact compiles it for its own.
.module = sb.b.createModule(.{ .root_source_file = options.root_source_file, .imports = imports }),
};
@@ -999,6 +1054,7 @@ pub const App = struct {
imports: []const Build.Module.Import,
/// The app's root, importing `sketch` and the app's own imports.
module: *Build.Module,
+ record: ?bool,
pub const Options = struct {
name: []const u8,
@@ -1006,16 +1062,23 @@ pub const App = struct {
/// Imports for the app's root module, besides `sketch`: for example
/// `sb.modules.vector`, or shaders from `addSpirvModule`.
imports: []const Build.Module.Import = &.{},
+ /// Record every interactive run's sessions to a file beside its
+ /// state (src/host/Sessions.zig). Null: in Debug builds only. A
+ /// playtest build of a release opts in with one line,
+ /// `.record = b.option(bool, "record", "collect sessions in a release build")`.
+ record: ?bool = null,
};
- /// A root module around the app: `source` sees `app`, `sketch` and `host`.
+ /// A root module around the app: `source` sees `app`, `sketch` and `host`,
+ /// and `{made}` in it becomes the app's `sketch.Build`.
fn wrap(app: App, source: []const u8, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Module {
const b = app.sb.b;
+ const made = b.fmt(".{{ .source = @import(\"build_info\").source, .record = {} }}", .{app.record orelse (optimize == .debug)});
const root = b.createModule(.{
- .root_source_file = b.addWriteFiles().add("root.zig", source),
+ .root_source_file = b.addWriteFiles().add("root.zig", std.mem.replaceOwned(u8, b.allocator, source, "{made}", made) catch @panic("OOM")),
.target = target,
.optimize = optimize,
- .imports = &.{ .{ .name = "app", .module = app.module }, .{ .name = "sketch", .module = app.sb.modules.sketch } },
+ .imports = &.{ .{ .name = "app", .module = app.module }, .{ .name = "sketch", .module = app.sb.modules.sketch }, .{ .name = "build_info", .module = app.sb.buildInfo() } },
});
if (std.mem.indexOf(u8, source, "\"host\"") != null) root.addImport("host", app.sb.modules.host);
return root;
@@ -1026,7 +1089,7 @@ pub const App = struct {
return app.sb.b.addLibrary(.{
.name = app.name,
.linkage = .dynamic,
- .root_module = app.wrap("comptime { @import(\"sketch\").exportSketch(@import(\"app\")); }", app.sb.target, app.sb.optimize),
+ .root_module = app.wrap("comptime { @import(\"sketch\").exportSketch(@import(\"app\"), {made}); }", app.sb.target, app.sb.optimize),
});
}
@@ -1048,7 +1111,7 @@ pub const App = struct {
.root_module = app.wrap(
\\const std = @import("std");
\\pub fn main(init: std.process.Init) !void {
- \\ return @import("host").main(init, @import("sketch").sketch(@import("app")));
+ \\ return @import("host").main(init, @import("sketch").sketch(@import("app"), {made}));
\\}
, app.sb.target, optimize),
});