From b2f7c605c279b2dea0c25180730c3d2e7b4b7d45 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:31:18 -0300 Subject: Make snapshots named marks and recordings named clips of sessions The state file keeps live state only (SKETCH03, one slot). save NAME is a keyframe and a mark in the sessions database, synced to disk; load, diff, snapshot and expect-snapshot take mark names, through the keyframe's own layout migrated to today's, compared over the leaves migration kept. A file from before (SKETCH01/02) has its used snapshot slots imported as marks "1".."7" at open, then drops them; a run that records nothing leaves them alone. record NAME, stop and play NAME make and play clips: a frame range of a session and its start keyframe, nothing overwritten, nothing invalidated by a later save, no capacity. The 64 MiB .inputs file, its --fresh memset and Timeline's Bank are gone; numbers still work as names, so every existing script runs unchanged. Event times in a clip are the session's, not restarted at record. reload.script also checks that a field a reload drops is still in the keyframe taken before it (expect-key). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/Console.zig | 220 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 160 insertions(+), 60 deletions(-) (limited to 'src/host/Console.zig') diff --git a/src/host/Console.zig b/src/host/Console.zig index 82911ad..88e4864 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -1,20 +1,20 @@ //! The developer console: a line editor, a short log, and the host commands -//! (snapshots, recordings, replay, field inspection). Lines the host does not -//! know go to the sketch's `command`. +//! (marks, clips, replay, sessions, field inspection). Lines the host does +//! not know go to the sketch's `command`. const Console = @This(); const std = @import("std"); const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); const memory = @import("memory.zig"); -const timeline = @import("timeline.zig"); +const Sessions = @import("Sessions.zig"); pub const panel_height = 144; pub const Key = enum { left, right, home, end, backspace, delete, clear, tab, submit }; const Line = struct { bytes: [118]u8 = @splat(0), len: usize = 0 }; const host_commands = [_][]const u8{ - "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", + "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", "marks", "clips", "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", "overlay timing", "sessions", "timing on", "timing off", "timing frame", "timing work", "timing reset", "timing stats", }; @@ -24,11 +24,34 @@ buffer: [96]u8 = @splat(0), len: usize = 0, cursor: usize = 0, lines: [5]Line = @splat(.{}), -snapshot: usize = 1, -recording: usize = 0, +/// The mark `save`, `load`, `diff` and `record` use when given none. +snapshot: Name = .of("1"), +/// The clip `record`, `play`, `seek` and `step` use when given none. +recording: Name = .of("0"), overlay_memory: bool = false, overlay_sound: bool = false, +pub const Name = struct { + bytes: [Sessions.max_name]u8 = undefined, + len: usize = 0, + + pub fn of(text_: []const u8) Name { + var n: Name = .{ .len = text_.len }; + @memcpy(n.bytes[0..text_.len], text_); + return n; + } + pub fn get(n: *const Name) []const u8 { + return n.bytes[0..n.len]; + } +}; + +/// A mark or clip name: a word of at most `Sessions.max_name` bytes. +fn nameOr(arg: ?[]const u8, default: *const Name) ![]const u8 { + const n = arg orelse return default.get(); + if (n.len > Sessions.max_name) return error.NameTooLong; + return n; +} + pub fn text(self: *const Console) []const u8 { return self.buffer[0..self.len]; } @@ -134,8 +157,8 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const rest = words.rest(); if (eq(command, "help")) { - self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N (frames) | seek start|end|"); - self.log("diff [N] | reset | get | set | overlay memory|sound | quit"); + self.log("save|load|diff [MARK] snapshot MARK marks | record|play [CLIP] recording CLIP clips | stop | pause"); + self.log("step N (frames) | seek start|end| | sessions | reset | get|set [] | overlay memory|sound | quit"); self.log("timing [on|off|frame|work|reset|stats] | overlay timing"); if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; @@ -172,72 +195,96 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const path = arg orelse return error.ExpectedField; const leaf = host.state.layout.find(path) orelse return error.UnknownField; if (rest.len == 0) return error.ExpectedValue; - h.stop(now); + try stop(host); try fields.set(leaf, host.live(), std.mem.trim(u8, rest, " ")); self.print("{s} = {s}", .{ path, std.mem.trim(u8, rest, " ") }); } else if (eq(command, "quit") or eq(command, "exit")) { - h.stop(now); + try stop(host); host.quit = true; } else if (eq(command, "reset")) { - h.stop(now); + try stop(host); host.resetState(); self.log("Live state reset. Snapshots are kept."); - } else if (eq(command, "snapshot")) { - self.snapshot = try slot(arg orelse return error.ExpectedSlot, 1, memory.slot_count - 1); - } else if (eq(command, "recording")) { - self.recording = try slot(arg orelse return error.ExpectedSlot, 0, timeline.recording_count - 1); - } else if (eq(command, "save") or eq(command, "load")) { - const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; - if (eq(command, "load") and !h.bank.hasSnapshot(index)) return error.EmptySnapshot; - h.stop(now); - if (eq(command, "save")) { - h.bank.save(index); - self.print("Saved snapshot {d}. Recordings from its old contents are invalidated.", .{index}); - } else { - try h.restore(index); - self.print("Loaded snapshot {d}.", .{index}); - } - } else if (eq(command, "record") or eq(command, "play")) { - const index = if (arg) |a| try slot(a, 0, timeline.recording_count - 1) else self.recording; - if (eq(command, "play")) _ = try h.bank.get(index); - h.stop(now); - if (eq(command, "record")) try h.startRecording(index, self.snapshot, now, host.build) else try h.play(index, now); - self.recording = index; - self.print("{s} recording {d} from snapshot {d}.", .{ if (eq(command, "record")) "Started" else "Playing", index, h.bank.recording(index).snapshot }); - if (eq(command, "play")) self.warnBuild(host, h.bank.recording(index)); - } else if (eq(command, "sessions")) { - if (arg != null) return error.UnexpectedArgument; - try self.listSessions(host); + } else if (eq(command, "snapshot") or eq(command, "recording")) { + const n = try nameOr(arg orelse return error.ExpectedName, &self.snapshot); + (if (eq(command, "snapshot")) &self.snapshot else &self.recording).* = .of(n); + } else if (eq(command, "save")) { + const n = try nameOr(arg, &self.snapshot); + try stop(host); + _ = try host.sessions.mark(n, .mark, host.live()); + self.print("Saved snapshot {s}.", .{n}); + } else if (eq(command, "load")) { + const n = try nameOr(arg, &self.snapshot); + const key = try loadMark(host, n); + defer key.free(host.gpa); + try stop(host); + try h.restore(key); + self.print("Loaded snapshot {s}.", .{n}); + } else if (eq(command, "record")) { + const n = try nameOr(arg, &self.recording); + const from = self.snapshot.get(); + try stop(host); + // An unused mark captures the current state; an existing one is + // restored, so several clips can branch from exactly those bytes. + const k = if (try host.sessions.findMark(from) == null) try host.sessions.mark(from, .mark, host.live()) else blk: { + const key = try loadMark(host, from); + defer key.free(host.gpa); + try h.restore(key); + break :blk try host.sessions.keyframe(.jump, host.live()); + }; + // The input held now is restored first in the clip (Host.restoredInput). + h.restored = true; + try host.sessions.startClip(n, k); + h.startRecording(); + self.recording = .of(n); + self.print("Started recording {s} from snapshot {s}.", .{ n, from }); + } else if (eq(command, "play")) { + const n = try nameOr(arg, &self.recording); + try stop(host); + try loadClip(host, n); + try h.play(now); + self.recording = .of(n); + self.print("Playing recording {s}.", .{n}); + self.warnBuild(host); } else if (eq(command, "stop")) { - h.stop(now); + try stop(host); } else if (eq(command, "pause")) { if (h.mode != .playing and h.mode != .paused) return error.NotReplaying; h.pause(now); } else if (eq(command, "step") or eq(command, "seek")) { const value = arg orelse return error.ExpectedPosition; - _ = try h.bank.get(if (h.mode == .live or h.mode == .recording) self.recording else h.selected); - if (h.mode == .recording) h.stop(now); - if (h.mode == .live) try h.play(self.recording, now); - const r = try h.bank.get(h.selected); - if (h.mode == .playing and h.cursor == 0) self.warnBuild(host, r); + if (h.mode == .live or h.mode == .recording) { + try stop(host); + try loadClip(host, self.recording.get()); + try h.play(now); + self.warnBuild(host); + } if (eq(command, "step")) return h.step(std.fmt.parseInt(i64, value, 10) catch return error.InvalidPosition); if (eq(value, "start")) return h.seekCount(0); - if (eq(value, "end")) return h.seekCount(r.count); + if (eq(value, "end")) return h.seekCount(h.events.items.len); const ms = std.fmt.parseInt(u64, value, 10) catch return error.InvalidPosition; return h.seekTime(ms *| std.time.ns_per_ms); } else if (eq(command, "diff")) { - const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; - if (!h.bank.hasSnapshot(index)) return error.EmptySnapshot; - const diff = h.bank.compare(0, index); + const n = try nameOr(arg, &self.snapshot); + var arena: std.heap.ArenaAllocator = .init(host.gpa); + defer arena.deinit(); + const saved = try markState(host, arena.allocator(), n); + const diff = memory.compareKept(saved.layout, host.state.layout, host.live(), saved.bytes); if (diff.first) |first| { var buf: [96]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); first.leaf.name(first.offset, &w) catch {}; - self.print("Live vs snapshot {d}: {d} differing values, first {s}", .{ index, diff.count, w.buffered() }); - } else self.print("Live vs snapshot {d}: same", .{index}); + self.print("Live vs snapshot {s}: {d} differing values, first {s}", .{ n, diff.count, w.buffered() }); + } else self.print("Live vs snapshot {s}: same", .{n}); + } else if (eq(command, "sessions")) { + if (arg != null) return error.UnexpectedArgument; + try self.listSessions(host); + } else if (eq(command, "marks") or eq(command, "clips")) { + if (arg != null) return error.UnexpectedArgument; + try self.listNamed(host, eq(command, "marks")); } else { // Sketch commands edit state outside the recorded input. - h.stop(now); + try stop(host); switch (host.forward(self, line, now)) { .unknown => return error.UnknownCommand, .failed => return error.CommandFailed, @@ -254,8 +301,8 @@ pub fn draw(self: *const Console, host: *Host, out: *std.ArrayList(abi.Command), const width: f32 = @floatFromInt(host.canvas[0]); const h = &host.timeline; try out.append(arena, .{ .rect = .{ .dst = .{ .x = 0, .y = top, .w = width, .h = panel_height }, .color = .{ .r = 24, .g = 28, .b = 28 } } }); - const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {d} | recording {d} (active {d}) | event {d} | {d}ms | rec {t}", .{ - host.name, h.mode, self.snapshot, self.recording, h.selected, h.cursor, h.position / std.time.ns_per_ms, host.sessions.mode(), + const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {s} | recording {s} | event {d} | {d}ms | rec {t}", .{ + host.name, h.mode, self.snapshot.get(), self.recording.get(), h.cursor, h.position / std.time.ns_per_ms, host.sessions.mode(), }); try out.append(arena, .{ .text = .{ .x = 12, .y = top + 8, .scale = 1, .color = hint, .bytes = status } }); for (&self.lines, 0..) |*line, i| { @@ -291,20 +338,73 @@ fn listSessions(self: *Console, host: *Host) !void { } } -/// A replay of a recording made by other code may diverge: say so. -fn warnBuild(self: *Console, host: *const Host, r: *const timeline.Recording) void { - if (r.build() != 0 and host.build != 0 and r.build() != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +/// Ends a clip being recorded or played: live from here. +fn stop(host: *Host) !void { + if (host.timeline.mode == .recording) try host.sessions.stopClip(); + host.timeline.stop(); +} + +/// The keyframe of the newest mark `n`, or of mark number K for "#K". +pub fn loadMark(host: *Host, n: []const u8) !Sessions.Loaded { + const s = &host.sessions; + const found = if (n.len > 1 and n[0] == '#') + try s.markAt(std.fmt.parseInt(u64, n[1..], 10) catch return error.InvalidName) + else + try s.findMark(n) orelse return error.EmptySnapshot; + return s.load(found.session, found.key); +} + +/// Mark `n`'s State in today's layout (`arena`'s), and the layout it was +/// taken in: compare them over the leaves migration kept (`memory.compareKept`). +pub fn markState(host: *Host, arena: std.mem.Allocator, n: []const u8) !struct { bytes: []u8, layout: memory.Layout } { + const key = try loadMark(host, n); + defer key.free(host.gpa); + const bytes = try arena.alignedAlloc(u8, .fromByteUnits(memory.page), host.state.layout.size); + const layout = try memory.restore(arena, key.layout, key.state, host.state.layout, bytes, host.timeline.defaults); + return .{ .bytes = bytes, .layout = layout }; +} + +/// Makes clip `n` the one the timeline plays: its keyframe and frames. +fn loadClip(host: *Host, n: []const u8) !void { + const s = &host.sessions; + const clip = try s.findClip(n) orelse return error.EmptyRecording; + const key = try s.load(clip.session, clip.key); + errdefer key.free(host.gpa); + var events: std.ArrayList(abi.Event) = .empty; + errdefer events.deinit(host.gpa); + // A clip a crash left open ends where its session does. + const end = if (clip.end != 0) clip.end else (try s.summary(clip.session)).frames; + try s.events(clip.session, key.batch, clip.start, end, &events); + host.timeline.load(key, events); +} + +/// A replay of a clip made by other code may diverge: say so. +fn warnBuild(self: *Console, host: *const Host) void { + const key = host.timeline.key orelse return; + if (key.build != 0 and host.build != 0 and key.build != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +} + +/// The last marks or clips: index, name, session and frame. +fn listNamed(self: *Console, host: *Host, marks: bool) !void { + const s = &host.sessions; + const n = try s.count(if (marks) .marks else .clips); + if (n == 0) return self.log(if (marks) "No marks: save NAME makes one." else "No clips: record NAME makes one."); + var i = n -| 5; + while (i < n) : (i += 1) { + const e = if (marks) try s.markAt(i) else try s.clipAt(i); + const key = try s.load(e.session, e.key); + defer key.free(host.gpa); + const label = e.name(); + if (marks) { + self.print("#{d} {s}: session {d} frame {d}", .{ i, label, e.session, key.frame }); + } else self.print("#{d} {s}: session {d} frames {d}..{d}", .{ i, label, e.session, e.start, e.end }); + } } fn eq(a: []const u8, b: []const u8) bool { return std.mem.eql(u8, a, b); } -fn slot(value: []const u8, min: usize, max: usize) !usize { - const index = std.fmt.parseInt(usize, value, 10) catch return error.InvalidSlot; - if (index < min or index > max) return error.InvalidSlot; - return index; -} test "line editing, completion and submission" { var c: Console = .{}; -- cgit v1.3