diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:24:29 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 57e254470e11ab40b6bae31b2cbf62aebca27ce4 (patch) | |
| tree | c1ab036650033c896069fa35a3d6279fe004fb32 /src/host/Host.zig | |
| parent | 8ce27cfdbab6d576db36403afbcef9659cb6dbdc (diff) | |
| download | sketchbook-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 'src/host/Host.zig')
| -rw-r--r-- | src/host/Host.zig | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index 70d6285..e579c87 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -20,6 +20,7 @@ const Presentation = @import("Presentation.zig"); const timing_marker = @import("timing_marker.zig"); const Control = @import("control.zig").Control; const Library = @import("library.zig").Library; +const Sessions = @import("Sessions.zig"); const Watch = @import("library.zig").Watch; pub const Options = struct { @@ -27,8 +28,11 @@ pub const Options = struct { library: ?[]const u8 = null, /// Local 9P control socket, opt-in. ninep: ?[]const u8 = null, - /// State file; recordings go next to it with an `.inputs` suffix. + /// State file; the sessions database goes next to it, `<name>.xitdb` + /// for `<name>.state`. state: []const u8 = "sketch.state", + /// Playing a script: from fresh state, sessions go to memory only. + script: bool = false, /// Offscreen video, silent audio, no vsync: for scripts and CI. headless: bool = false, /// Forget the state file's contents: scripts start from a known place. @@ -76,6 +80,8 @@ store: memory.StoreFile, made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, recordings: []align(memory.page) u8, timeline: Timeline, +/// This run's session and the ones before it (Sessions.zig). +sessions: Sessions, console: Console = .{}, timing: Timing = .{}, timing_atlas: overlay.TimingAtlas = .{}, @@ -90,6 +96,8 @@ build: u64 = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, name: []const u8 = "", +/// The sketch's `Info.source`, for the session's stretches. +source: []const u8 = "", completions: []const []const u8 = &.{}, /// Frame lifetime: sketch scratch and this frame's commands. frame_arena: std.heap.ArenaAllocator, @@ -187,6 +195,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .store = store, .recordings = recordings, .timeline = undefined, + .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, .library = library, .info_arena = .init(gpa), @@ -213,6 +222,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std @memset(self.recordings, 0); self.resetState(); } + self.openSessions(info.record); if (!options.headless and options.export_to == null) { self.presentation = Presentation.init(gpa, self.window) catch null; if (options.timing_marker) self.print("presentation tracing: {s}", .{if (self.presentation) |p| p.status else "disabled_or_allocation_failed"}); @@ -220,6 +230,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std } pub fn deinit(self: *Host) void { + self.sessions.close(self.live(), wallClock(self.io)); if (self.control) |control| control.deinit(); self.dialog.deinit(); if (self.watch) |w| w.deinit(); @@ -326,8 +337,11 @@ pub fn frame(self: *Host, now: u64) !void { self.last_now = now; const h = &self.timeline; switch (h.mode) { - .playing => self.update(h.tick(now)), - .paused => {}, + .playing => { + self.sessions.away = true; + self.update(h.tick(now)); + }, + .paused => self.sessions.away = true, .live, .recording => { // One frame: what was routed since the last one, then its tick. // Recorded first, so `update` sees the times a replay will. @@ -335,6 +349,9 @@ pub fn frame(self: *Host, now: u64) !void { // An export's tick is 1/fps itself, not whole nanoseconds of it. if (self.options.export_to != null and dt != 0) tick.x = 1 / @as(f32, @floatFromInt(self.options.fps)); try self.pending.append(self.gpa, tick); + const why: ?Sessions.Why = if (self.sessions.frames == 0) .start else if (self.reloaded) .after_reload else if (self.jumped or self.sessions.away) .jump else null; + self.sessions.away = false; + self.sessions.frame(self.pending.items, self.live(), why) catch |err| self.print("session: {t}", .{err}); h.capture(self.pending.items) catch |err| self.print("Recording stopped: {t}", .{err}); self.update(self.pending.items); }, @@ -642,7 +659,10 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { try self.restoredInput(now); return err; }; - if (self.timeline.restored or movesState(line)) self.jumped = true; + if (self.timeline.restored or movesState(line)) { + self.jumped = true; + self.sessions.noteLine(line); + } try self.restoredInput(now); if (self.timeline.mode == .paused) self.update(replay); } @@ -652,7 +672,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { fn movesState(line: []const u8) bool { var words = std.mem.tokenizeAny(u8, line, " \t"); const verb = words.next() orelse return false; - const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit" }; + const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions" }; for (still) |w| if (std.mem.eql(u8, verb, w)) return false; return true; } @@ -687,11 +707,14 @@ pub fn reload(self: *Host) !void { pub fn load(self: *Host, path: []const u8) !void { var library = try Library.openFresh(self.io, path, scratchDir(self.environ), self.reloads + 1); errdefer library.close(); + // The State in the layout it is leaving, before migration drops any of it. + if (self.sessions.mode() != .off) _ = self.sessions.keyframe(.before_reload, self.live()) catch |err| self.print("session: {t}", .{err}); try self.adopt(library.sketch); if (self.library) |*old| old.close(); self.library = library; self.reloads += 1; self.build = fileId(self.io, path); + self.sessions.stretch(.{ .build = self.build, .source = self.source, .layout = self.state.layoutBytes() }) catch |err| self.print("session: {t}", .{err}); self.print("reload {d}: {s}", .{ self.reloads, path }); } @@ -705,6 +728,7 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); + self.source = try arena.dupe(u8, info.source); const completions = try arena.alloc([]const u8, info.commands.len); for (completions, info.commands) |*to, from| to.* = try arena.dupe(u8, from); self.completions = completions; @@ -869,6 +893,33 @@ fn consoleKey(e: abi.Event) ?Console.Key { }; } +/// Starts this run's session (Sessions.zig): in a file beside the state +/// file when the build records, in memory for a script from fresh state, +/// nowhere for an export. A database that cannot be opened is said, and +/// the run goes on unrecorded. +fn openSessions(self: *Host, record: bool) void { + const o = self.options; + const mode: Sessions.Mode = if (o.export_to != null) .off else if (o.script and o.fresh) .memory else if (record) .file else .off; + var buf: [std.fs.max_path_bytes]u8 = undefined; + const stem = if (std.mem.endsWith(u8, o.state, ".state")) o.state[0 .. o.state.len - ".state".len] else o.state; + const path = std.fmt.bufPrint(&buf, "{s}.xitdb", .{stem}) catch "sketch.xitdb"; + const first: Sessions.Stretch = .{ .build = self.build, .source = self.source, .layout = self.state.layoutBytes() }; + self.sessions = Sessions.open(self.gpa, self.io, mode, path, first, wallClock(self.io)) catch |err| blk: { + self.print("not recording: {s}: {t}", .{ path, err }); + break :blk .{ .gpa = self.gpa, .io = self.io }; + }; + if (o.headless) return; + switch (self.sessions.mode()) { + .file => self.print("recording to {s}", .{path}), + .memory => {}, + .off => if (o.export_to == null) self.print("not recording (release build: .record = true collects)", .{}), + } +} + +fn wallClock(io: std.Io) u64 { + return @intCast(@max(0, std.Io.Timestamp.now(io, .real).nanoseconds)); +} + /// Identifies the file at `path` without reading it (a Debug library is tens /// of megabytes): a hash of its inode, size and modification time; 0 when /// there is none. A build replaces the file, so each build gets a new one. |
