From 5c306e3dce3535dee1f6670076d537e05abff0d1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:02:04 -0300 Subject: state/: a value is read as it was at the open A read of a State file formatted the value at each Tread and sent the window at its offset; a value that changed between two reads (a float a frame moves) could change length, and the second read sent the tail of the new text after the head of the old: `1.526633` then `5`, which a shell loop awaiting a number read as "1\n4". Now the open formats the value once into one of the connection's snapshots, as the traces and README are, and reads window it; release frees it. A write is unchanged. Found driving drat-meout from a shell loop, `until [ "$(drat-meout fs read state/street/time | cut -d. -f1)" -ge 3 ]`. Tests: test-control and test-fs, 51/51 steps, 11/11 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/control.zig | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/host/control.zig b/src/host/control.zig index 75fb524..943316c 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -199,7 +199,7 @@ pub const Control = struct { fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { // Nodes from 2^63 are the sketch's: none is served but sketch/. if (req.node > @backingInt(File.sketch)) return error.UnknownFile; - if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(self, host, req, reply, buffer); + if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(self, host, conn, req, reply, buffer); const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile; if (self.offline != null and (req.op == .write or (req.op == .open or req.op == .read) and file != .README and file != .commands and file != .ctl and file != .@"/" and file != .sketch)) return error.SketchNotRunning; switch (req.op) { @@ -426,7 +426,7 @@ fn verbLine(w: *std.Io.Writer, indent: []const u8, name: []const u8, args: []con /// Under `state/`: the live State as files (state_files.zig), a write /// run as the console's `set`. -fn stateRespond(self: *Control, host: *Host, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { +fn stateRespond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { const layout = if (self.offline) |o| o.layout else host.state.layout; const node: state_files.Node = @bitCast(req.node); if (!state_files.valid(layout, node)) return error.StateLayoutChangedWalkAgain; @@ -444,16 +444,29 @@ fn stateRespond(self: *Control, host: *Host, req: fs.Req, reply: *fs.Reply, buff if (mode != 0o600 or !req.truncate) return error.ReadOnly; reply.attr = .{ .node = req.node, .mode = mode }; }, - .open, .release => {}, + // A value is read as it was at the open: it changes every frame, + // and its length with it. + .open => if (!dir) for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { + var text: std.Io.Writer.Allocating = .init(self.allocator); + defer text.deinit(); + try state_files.read(layout, if (self.offline) |o| o.bytes else host.live(), node, &text.writer); + slot.* = try text.toOwnedSlice(); + reply.handle = @intCast(handle); + break; + } else return error.TooManySnapshots, + .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) { + const slot = &conn.snapshots[req.handle - 1]; + if (slot.*) |bytes| self.allocator.free(bytes); + slot.* = null; + }, .readdir => { if (!dir) return error.NotDirectory; return buffer[0..state_files.readdir(layout, node, req.off, buffer)]; }, .read => { if (dir) return error.NotFile; - var w: std.Io.Writer = .fixed(buffer); - state_files.read(layout, if (self.offline) |o| o.bytes else host.live(), node, &w) catch return error.ValueTooLong; - return window(w.buffered(), req.off, req.size); + const bytes = if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle; + return window(bytes, req.off, req.size); }, .write => { if (mode != 0o600) return error.ReadOnly; -- cgit v1.3