From 3e5ad32c529c4c566989ef8201159100a4a3e32c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:52:03 -0300 Subject: The State as files: state/ in the 9P tree, a directory per struct and array state/ serves the running sketch's State nested: a directory per struct and per array, a file per value, state/tracker/songs/3/bpm. A read is the value as get shows it, and a newline; a write is the console's set with the file's path (control.zig builds `set tracker.songs[3].bpm 90` and runs it through runLine, so it stops a replay, logs, marks a jump and notes the line like any set). Numbers, bools and enums are written; optionals, unions and the like read as `` and refuse writes. An array of scalars (a vector's lanes, talk.text) is one file of its values, blank-separated; an array of arrays is a directory of such rows (grid/1 for grid[1] of a [2][3]i16). So a file is one value or one row, the run of values one set names, and stays the size of a row however large the array; and paths keep the State's own shape. An array of structs is a directory of indices: drat-meout's patterns, 8x16x16x4 of {pitch, velocity}, are state/tracker/songs//patterns//// pitch, reached by walking, listed a dimension at a time. state_files.zig does the walking from a (Layout, bytes) alone: lookup, readdir, read, the set path and a README line per file, so the save's newest State and a sketch's defaults can be served the same way. A node is the layout's first leaf under it, its depth and the element's offset, with bit 62 set (bit 63 stays the sketch's). A node a reload made stale is refused, "StateLayoutChangedWalkAgain". fields.setRow sets a row from its words, all or none, and the console's set takes it: `set talk.text 104 105 0`. The engine keeps names up to 64 bytes (from 28), for long field names. Tests: test-control (now also "the State as files" and "a row of scalars"): 40/40 steps, 9/9 tests. Live, headless, the replay fixture over plan9port 9p: ls state, read and write dots/buffer/3/kind and the position vector, a bad value refused as InvalidValue. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/control.zig | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) (limited to 'src/host/control.zig') diff --git a/src/host/control.zig b/src/host/control.zig index 33b186b..1a8da92 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -4,17 +4,18 @@ const c9 = @import("cloud9"); const c = @import("c"); const Host = @import("Host.zig"); const Presentation = @import("Presentation.zig"); +const state_files = @import("state_files.zig"); const fs = c9.fs; const transport = c9.transport; const Backend = struct { pub const Req = fs.Req; pub const Reply = fs.Reply; }; -const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4 }); +const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4, .name_capacity = 64 }); const msize = 16384; /// The files, a row each: what lookup, readdir and getattr answer. A /// file's node is its number here. -const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace" }; +const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace", state = @bitCast(state_files.root) }; const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const u8 }).init(.{ .@"/" = .{ .mode = 0o700, .doc = "" }, .ctl = .{ .mode = 0o600, .doc = "write console lines, a line each; screenshot captures the next frame, profile-reset clears profile" }, @@ -25,6 +26,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const .@"profile-trace" = .{ .doc = "with --profile-gpu: label,sample,ns, the samples kept, as CSV" }, .@"timing-trace" = .{ .doc = "index,frame_ns,work_ns,outside_work_ns, the frames kept, as CSV" }, .@"presentation-trace" = .{ .doc = "on Wayland: presentation feedback per frame, as CSV" }, + .state = .{ .mode = 0o700, .doc = "the sketch's State: a directory per struct and array, a file per value; a read is the value, a write what set does (a jump); an array of numbers is one file of its values, blank-separated" }, }); /// The verbs `ctl` takes beside the console's lines. @@ -148,6 +150,7 @@ pub const Control = struct { } fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(host, req, reply, buffer); const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile; switch (req.op) { .lookup => { @@ -188,8 +191,8 @@ pub const Control = struct { for (std.enums.values(File)[1..], 0..) |f, index| { if (index < req.off) continue; const name = @tagName(f); - std.mem.writeInt(u64, buffer[used..][0..8], index + 2, .little); - buffer[used + 8] = 0; + std.mem.writeInt(u64, buffer[used..][0..8], @backingInt(f), .little); + buffer[used + 8] = @intFromBool(f == .state); buffer[used + 9] = @intCast(name.len); @memcpy(buffer[used + 10 ..][0..name.len], name); used += 10 + name.len; @@ -225,7 +228,7 @@ pub const Control = struct { } break :blk writer.buffered(); }, - .@"/" => return error.NotFile, + .@"/", .state => return error.NotFile, }; return window(bytes, req.off, req.size); }, @@ -305,7 +308,7 @@ pub const Control = struct { fn attr(self: *Control, node: u64) fs.Attr { const file: File = @fromBackingInt(@intCast(node)); - return .{ .name = @tagName(file), .node = node, .dir = file == .@"/", .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 }; + return .{ .name = @tagName(file), .node = node, .dir = file == .@"/" or file == .state, .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 }; } pub fn capture(self: *Control, surface: *c.SDL_Surface) void { @@ -329,6 +332,51 @@ pub const Control = struct { } }; +/// Under `state/`: the live State as files (state_files.zig), a write +/// run as the console's `set`. +fn stateRespond(host: *Host, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + const layout = host.state.layout; + const node: state_files.Node = @bitCast(req.node); + if (!state_files.valid(layout, node)) return error.StateLayoutChangedWalkAgain; + const dir = state_files.isDir(layout, node); + const mode: u16 = if (dir) 0o700 else if (state_files.writable(layout, node)) 0o600 else 0o400; + switch (req.op) { + .lookup => { + if (!dir) return error.NotDirectory; + const child = state_files.lookup(layout, node, req.data) orelse return error.UnknownField; + const child_dir = state_files.isDir(layout, child); + reply.attr = .{ .node = @bitCast(child), .dir = child_dir, .mode = if (child_dir) 0o700 else if (state_files.writable(layout, child)) 0o600 else 0o400 }; + }, + .getattr => reply.attr = .{ .node = req.node, .dir = dir, .mode = mode }, + .setattr => { + if (mode != 0o600 or !req.truncate) return error.ReadOnly; + reply.attr = .{ .node = req.node, .mode = mode }; + }, + .open, .release => {}, + .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, host.live(), node, &w) catch return error.ValueTooLong; + return window(w.buffered(), req.off, req.size); + }, + .write => { + if (mode != 0o600) return error.ReadOnly; + var w: std.Io.Writer = .fixed(buffer); + w.writeAll("set ") catch unreachable; + state_files.path(layout, node, &w) catch return error.ValueTooLong; + w.writeByte(' ') catch return error.ValueTooLong; + for (std.mem.trim(u8, req.data, " \t\r\n")) |byte| w.writeByte(if (byte == '\n') ' ' else byte) catch return error.ValueTooLong; + try host.runLine(w.buffered(), c.SDL_GetTicksNS()); + reply.written = @intCast(req.data.len); + }, + } + return &.{}; +} + fn window(bytes: []const u8, offset: u64, count: u32) []const u8 { if (offset >= bytes.len) return &.{}; const start: usize = @intCast(offset); -- cgit v1.3