summaryrefslogtreecommitdiff
path: root/src/host/Host.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 23:35:54 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitc3b9938e9cb381bc7378fdca9073520ff1711ad6 (patch)
tree33f707ef2075bc26083a3c21d34a41b4a0e9c7ac /src/host/Host.zig
parentb2f7c605c279b2dea0c25180730c3d2e7b4b7d45 (diff)
downloadsketchbook-c3b9938e9cb381bc7378fdca9073520ff1711ad6.tar.gz
sketchbook-c3b9938e9cb381bc7378fdca9073520ff1711ad6.zip
Play past sessions back, re-synced at their keyframes
play session S [at SECONDS] restores the session's keyframe before that time (a binary search over its keyframes' times), replays its frames up to it and leaves it paused there; without a time it plays from the start. At each later keyframe the replay reaches, the State is compared with it over the leaves migration kept: a keyframe taken of what update made (periodic, mark, before a reload, exit) that differs is reported once, "diverged before frame F: N values, first <field>", and every keyframe is restored, so a replay across a jump the log does not hold (set, load, a sketch command) goes on from the State that followed it. A session built from another source or build says so. The console's status line names the session, frame and time, the mark, the clip and where it records. test-past records a session, replays it by the same build (the mark's State exactly, frame 120's two seconds in, no divergence), then by a build with an edited fixture (one divergence, before the mark). 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.zig32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 1fe1db2..1c9d65a 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -373,10 +373,42 @@ pub fn update(self: *Host, events: []const abi.Event) void {
self.started = false;
const ctx = self.context();
self.sketch.update(&ctx, self.live().ptr);
+ if (self.timeline.mode == .playing or self.timeline.mode == .paused) self.resync();
}
self.input.events = &.{};
}
+/// After a replayed frame of a past session: at each keyframe it reaches,
+/// live state is compared with the keyframe (over the leaves migration
+/// kept), any difference said, and the keyframe restored.
+fn resync(self: *Host) void {
+ const h = &self.timeline;
+ h.fed += 1;
+ const session = h.session orelse return;
+ const reached = h.key.?.frame + h.fed;
+ while (h.next_check < h.checks.items.len and h.checks.items[h.next_check].frame <= reached) {
+ const check = h.checks.items[h.next_check];
+ h.next_check += 1;
+ if (check.frame < reached) continue;
+ const key = self.sessions.load(session, check.key) catch |err| return self.print("session {d} keyframe {d}: {t}", .{ session, check.key, err });
+ defer key.free(self.gpa);
+ var arena: std.heap.ArenaAllocator = .init(self.gpa);
+ defer arena.deinit();
+ const saved = arena.allocator().alignedAlloc(u8, .fromByteUnits(memory.page), self.state.layout.size) catch return;
+ const from = memory.restore(arena.allocator(), key.layout, key.state, self.state.layout, saved, self.sketch.defaults) catch |err| return self.print("session {d} keyframe {d}: {t}", .{ session, check.key, err });
+ if (check.observed) {
+ const diff = memory.compareKept(from, self.state.layout, self.live(), saved);
+ 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("diverged before frame {d}: {d} values, first {s}", .{ reached, diff.count, w.buffered() });
+ }
+ }
+ @memcpy(self.live(), saved);
+ }
+}
+
/// After live state was restored from a snapshot: the pointer, buttons and
/// keys tracked so far belong to other bytes, so they start over. A new
/// recording then begins with events that bring back what is held now, so