From 41bdeb1b3fadc266823c43086de61913be8e60f6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:57:33 -0300 Subject: Dial by name, and fs in every sketch's binary, answering with the sketch closed A run a person watches (not headless, a script or an export) posts its tree as $XDG_RUNTIME_DIR/9p/sketchbook/, the sketch's Info.name; --9p NAME posts under another name, --9p /path listens at a socket path as before. A name a live run holds is refused, a stale one replaced; a default post that fails says so and the run goes on without one, an explicit --9p that fails ends it as before. (cloud9's post() takes names without a slash, so control.zig makes the sketchbook/ directory and binds there itself, with post.probe deciding live or stale.) `fs VERB [PATH [WORDS]]`, after the options, in the generic host (`sketchbook LIBRARY fs ...`) and in every executable built with sketchbook (`drat-meout fs read state/scene`), is cloud9.verbs.run on an Fsys: the running sketch's tree when one is posted (or at --9p), else the same tree served in this process over a socketpair, on a thread (Control.serveOffline, the same respond with `offline` set), from the save's newest State (--state's save, else the default one; read only: Sessions.openRead, no session started, restored into today's layout), else from the State's declared defaults (init is not run). README, commands, ctl's read and state/ answer; a write and status, log, screen.png and the traces answer in words, "drat-meout is not running: this is its save's newest State, frame 61 of ". No window, no SDL. sketchbook.init's modules gain cloud9, so an app's own tests can dial its executable with cloud9.Fsys. Tests: a new step, test-fs (in test): the replay fixture posted by name under a private runtime directory, driven with its own binary's fs: ls, a state write and read, a refused value in words, a live click, a screenshot read, help of state/dots and ctl, quit and the name gone; then closed: a state file read from the defaults, status refused in words. test-fs, test-control, test-paths, test-setup, test-sessions: 56/56 steps, 27/27 tests. drat-meout's copy builds unchanged and answers fs with the game closed: 94 leaves, 16,522 files, 17,608 values, 10,408 directories. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/root.zig | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'src/host/root.zig') diff --git a/src/host/root.zig b/src/host/root.zig index 44512b2..77fd14e 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -9,6 +9,10 @@ pub const memory = @import("memory.zig"); pub const fields = @import("fields.zig"); pub const script = @import("script.zig"); pub const timeline = @import("timeline.zig"); +const c9 = @import("cloud9"); +const control = @import("control.zig"); +const Sessions = @import("Sessions.zig"); +const Library = @import("library.zig").Library; /// Entry point for both the generic host (`linked == null`) and apps that /// link their sketch in. @@ -24,6 +28,12 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var args = init.minimal.args.iterate(); _ = args.skip(); while (args.next()) |arg| { + if (std.mem.eql(u8, arg, "fs")) { + var words: std.ArrayList([]const u8) = .empty; + defer words.deinit(gpa); + while (args.next()) |word| try words.append(gpa, word); + std.process.exit(try fs(init, linked, options, words.items)); + } if (!std.mem.startsWith(u8, arg, "-")) { options.library = arg; continue; @@ -127,6 +137,74 @@ const usage = blk: { break :blk text; }; +/// `fs VERB [PATH [WORDS]]`: cloud9's verbs on the sketch's tree. The +/// running sketch's, posted as sketchbook/ (or at --9p); with none, +/// the same tree served here from the save's newest State (--state's, or +/// the default save), else from the State's defaults: README, commands, +/// state/ answer, and the rest say the sketch is not running. No window. +fn fs(init: std.process.Init, linked: ?abi.Sketch, options: Host.Options, words: []const []const u8) !u8 { + const gpa = init.gpa; + const io = init.io; + const env = init.environ_map; + var in_buf: [4096]u8 = undefined; + var out_buf: [4096]u8 = undefined; + var err_buf: [1024]u8 = undefined; + var in = std.Io.File.stdin().reader(io, &in_buf); + var out = std.Io.File.stdout().writer(io, &out_buf); + var err = std.Io.File.stderr().writer(io, &err_buf); + const user = env.get("USER") orelse "sketchbook"; + var library: ?Library = null; + defer if (library) |*l| l.close(); + const sketch = linked orelse blk: { + library = try Library.openFresh(io, options.library orelse fatal("fs needs the sketch library first: sketchbook LIBRARY fs ...", .{}), Host.scratchDir(env), 0); + break :blk library.?.sketch; + }; + const info = sketch.info(); + const no_env = [_:null]?[*:0]const u8{}; + var f: c9.Fsys = undefined; + var path_buf: [c9.post.sun_path_len]u8 = undefined; + const where = options.ninep orelse info.name; + const path = if (std.mem.startsWith(u8, where, "/")) where else control.registered(env, where, &path_buf) catch ""; + if (f.dial(io, &no_env, path, user)) |_| { + defer f.close(); + return c9.verbs.run(&f, words, &in.interface, &out.interface, &err.interface); + } else |_| {} + + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const layout = try memory.Layout.copy(arena.allocator(), info.state); + const bytes = try arena.allocator().alignedAlloc(u8, .fromByteUnits(memory.page), layout.size); + @memset(bytes, 0); + sketch.defaults(.state, bytes.ptr); + var from = try std.fmt.allocPrint(arena.allocator(), "{s} is not running: this is its State's defaults (no save)", .{info.name}); + var data_buf: [std.fs.max_path_bytes]u8 = undefined; + const save = if (options.state.len > 0) + try std.fmt.allocPrint(arena.allocator(), "{s}.xitdb", .{Host.stemOf(options.state)}) + else + try std.fmt.allocPrint(arena.allocator(), "{s}/{s}/{s}.xitdb", .{ env.get("XDG_DATA_HOME") orelse try std.fmt.bufPrint(&data_buf, "{s}/.local/share", .{env.get("HOME") orelse "."}), info.name, info.name }); + if (Sessions.openRead(gpa, io, save)) |opened| { + var s = opened; + defer s.deinit(); + var events: std.ArrayList(abi.Event) = .empty; + defer events.deinit(gpa); + if (try s.present(&events)) |key| { + defer key.free(gpa); + _ = try memory.restore(arena.allocator(), key.layout, key.state, layout, bytes, sketch.defaults); + from = try std.fmt.allocPrint(arena.allocator(), "{s} is not running: this is its save's newest State, frame {d} of {s}", .{ info.name, key.frame, save }); + } + } else |_| {} + const commands = try arena.allocator().alloc([]const u8, info.commands.len); + for (commands, info.commands) |*to, word| to.* = word; + var server: control.Control = .{ .allocator = gpa, .path = undefined, .listener = -1, .offline = .{ .layout = layout, .bytes = bytes, .completions = commands, .from = from } }; + var fds: [2]i32 = undefined; + if (std.c.socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) return error.SocketPair; + const thread = try std.Thread.spawn(.{}, control.Control.serveOffline, .{ &server, fds[0] }); + defer thread.join(); + try f.init(io, .{ .socket = .{ .handle = fds[1], .address = undefined } }, user); + defer f.close(); + return c9.verbs.run(&f, words, &in.interface, &out.interface, &err.interface); +} + /// A save in a directory named `.sketchbook` (`zig build run`'s): the /// directory is made, with a `.gitignore` of `*` (the save grows, and a jj /// snapshot would commit it), and the state file an older build kept beside -- cgit v1.3