graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(cli): graff cube — one-command cloud graff (sandbox + serve + preview)
Rach Pradhan0266070f7a1e
diff --git a/src/main.zig b/src/main.zigindex 23cf5cc5..2f94244f 100644--- a/src/main.zig+++ b/src/main.zig@@ -5510,6 +5510,8 @@ const usage_text =\\ graff worktree merge <name> squash-land worktree-<name> onto the current branch + clean up\\ graff sandboxes list your gateway sandboxes (what's burning credits)\\ graff sandboxes stop <id> spin a sandbox down (stops it + settles the meter)+ \\ graff cube new spin up a cloud graff (sandbox + serve + preview URL)+ \\ graff cube [status|stop] inspect the running cube or spin it down\\ graff --schema print the machine-readable interface (SDK codegen)\\ graff serve HTTP/NDJSON bridge over the --json protocol\\ (--host/--port/--token; sessions are --json children)@@ -5878,7 +5880,7 @@ pub fn main(init: std.process.Init) !void {const is_subcommand = positionals.items.len > 0 and(std.mem.eql(u8, positionals.items[0], "login") or std.mem.eql(u8, positionals.items[0], "key") or std.mem.eql(u8, positionals.items[0], "mcp") orstd.mem.eql(u8, positionals.items[0], "serve") or std.mem.eql(u8, positionals.items[0], "update") or std.mem.eql(u8, positionals.items[0], "title") or std.mem.eql(u8, positionals.items[0], "repl") or- std.mem.eql(u8, positionals.items[0], "worktree") or std.mem.eql(u8, positionals.items[0], "sandboxes"));+ std.mem.eql(u8, positionals.items[0], "worktree") or std.mem.eql(u8, positionals.items[0], "sandboxes") or std.mem.eql(u8, positionals.items[0], "cube"));var oneshot_prompt: ?[]const u8 = null;if (!is_subcommand and positionals.items.len > 0) {oneshot_prompt = try std.mem.join(arena, " ", positionals.items);@@ -5968,6 +5970,18 @@ pub fn main(init: std.process.Init) !void {return;}+ // `graff cube [new|status|stop]`: a personal cloud graff — a gateway+ // sandbox running `graff serve` behind a Daytona preview URL. This is the+ // broker the iOS app mirrors; any serve client can attach with the+ // printed base + token.+ if (positionals.items.len > 0 and std.mem.eql(u8, positionals.items[0], "cube")) {+ const cg_key = init.environ_map.get("CODEGRAFF_API_KEY") orelse+ (if (homeEnv(init.environ_map)) |home| loadCodegraffKey(io, arena, home) else null) orelse+ std.process.fatal("cube: no codegraff key — run `graff login` first", .{});+ try cubeCommand(io, gpa, arena, cg_key, positionals.items[1..]);+ return;+ }+// `--schema`: print the machine-readable interface and exit. No keys,// network, or MCP — so it works anywhere (CI codegen calls this).if (schema_flag) {@@ -10208,18 +10222,21 @@ fn loadCodegraffKey(io: Io, arena: Allocator, home: []const u8) ?[]const u8 {return null;}-// `graff sandboxes [stop <id>]` — the account's gateway sandboxes: the same-// ones the PR bot, the e2e cube probe, and the iOS app create. List shows-// state + spec + labels so you can see what's burning credits; stop spins one-// down via the gateway, which settles its billing meter.-fn gatewayJson(io: Io, gpa: Allocator, arena: Allocator, method: std.http.Method, url: []const u8, key: []const u8) !Value {+// Gateway REST helpers shared by `graff sandboxes` and `graff cube`.+// gatewayFetch returns status + raw body without judging; gatewayJson is the+// strict variant — non-2xx is fatal with the gateway's error message. A null+// payload on POST sends "{}" (the gateway rejects empty bodies).+const GatewayResponse = struct { code: u16, body: []const u8 };++fn gatewayFetch(io: Io, gpa: Allocator, arena: Allocator, method: std.http.Method, url: []const u8, key: []const u8, payload: ?[]const u8) !GatewayResponse {var client: std.http.Client = .{ .allocator = gpa, .io = io };defer client.deinit();var aw: Io.Writer.Allocating = .init(arena);+ const default_payload: ?[]const u8 = if (method == .POST) "{}" else null;const res = try client.fetch(.{.location = .{ .url = url },.method = method,- .payload = if (method == .POST) "{}" else null,+ .payload = payload orelse default_payload,.response_writer = &aw.writer,.headers = .{.content_type = .{ .override = "application/json" },@@ -10227,21 +10244,29 @@ fn gatewayJson(io: Io, gpa: Allocator, arena: Allocator, method: std.http.Method.user_agent = .{ .override = "simple-harness/" ++ harness_version },},});- const body = aw.writer.buffered();- const code = @intFromEnum(res.status);- if (code < 200 or code >= 300) {- var msg: []const u8 = body;- if (std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always })) |v| {+ return .{ .code = @intFromEnum(res.status), .body = aw.writer.buffered() };+}++fn gatewayJson(io: Io, gpa: Allocator, arena: Allocator, method: std.http.Method, url: []const u8, key: []const u8, payload: ?[]const u8) !Value {+ const r = try gatewayFetch(io, gpa, arena, method, url, key, payload);+ if (r.code < 200 or r.code >= 300) {+ var msg: []const u8 = r.body;+ if (std.json.parseFromSliceLeaky(Value, arena, r.body, .{ .allocate = .alloc_always })) |v| {if (v == .object) if (v.object.get("error")) |e| if (e == .object)if (e.object.get("message")) |m| if (m == .string) {msg = m.string;};} else |_| {}- std.process.fatal("sandboxes: gateway HTTP {d}: {s}", .{ code, msg[0..@min(msg.len, 200)] });+ std.process.fatal("gateway: HTTP {d}: {s}", .{ r.code, msg[0..@min(msg.len, 200)] });}- return std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always });+ return std.json.parseFromSliceLeaky(Value, arena, r.body, .{ .allocate = .alloc_always });}+// `graff sandboxes [stop <id>]` — the account's gateway sandboxes: the same+// ones the PR bot, the e2e cube probe, and the iOS app create. List shows+// state + spec + labels so you can see what's burning credits; stop spins one+// down via the gateway, which settles its billing meter.+fn sandboxesCommand(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, args: []const []const u8) !void {var obuf: [4096]u8 = undefined;var ow = Io.File.stdout().writer(io, &obuf);@@ -10249,7 +10274,7 @@ fn sandboxesCommand(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, aif (args.len >= 2 and std.mem.eql(u8, args[0], "stop")) {const url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}/stop", .{args[1]});- const resp = try gatewayJson(io, gpa, arena, .POST, url, key);+ const resp = try gatewayJson(io, gpa, arena, .POST, url, key, null);const state = if (resp == .object) (strFieldObj(resp.object, "state") orelse "stopped") else "stopped";const cost_micro = if (resp == .object) intFieldObj(resp.object, "costMicro", 0) else 0;const usd = @as(f64, @floatFromInt(cost_micro)) / 1e6;@@ -10260,7 +10285,7 @@ fn sandboxesCommand(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, aif (args.len != 0 and !std.mem.eql(u8, args[0], "list"))std.process.fatal("usage: graff sandboxes [stop <id>]", .{});- const resp = try gatewayJson(io, gpa, arena, .GET, codegraff_device_base ++ "/v1/sandboxes", key);+ const resp = try gatewayJson(io, gpa, arena, .GET, codegraff_device_base ++ "/v1/sandboxes", key, null);if (resp != .array) std.process.fatal("sandboxes: unexpected gateway response (not a list)", .{});if (resp.array.items.len == 0) {try out.writeAll("no sandboxes — nothing is burning credits\n");@@ -10288,6 +10313,254 @@ fn sandboxesCommand(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, atry out.flush();}+// ---- graff cube: a personal cloud graff (sandbox + serve + preview) ----+// `cube new` productizes scripts/e2e-cube-serve.py: create a gateway sandbox,+// install graff in it, start `graff serve` behind a fresh token, mint a+// Daytona preview URL, and save the connection to .graff/cube-state.json so+// any serve client (the iOS app, curl, another machine) can attach with the+// printed base + token. `cube status` re-checks it; `cube stop` spins the+// sandbox down and settles the meter. This is the broker the iOS app mirrors.+const cube_state_file = ".graff/cube-state.json";+const cube_port: u16 = 8787;++fn cubeExec(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, sandbox_id: []const u8, command: []const u8, timeout_s: i64, asynch: bool) !Value {+ var obj: std.json.ObjectMap = .empty;+ try obj.put(arena, "command", .{ .string = command });+ try obj.put(arena, "timeoutSeconds", .{ .integer = timeout_s });+ if (asynch) try obj.put(arena, "async", .{ .bool = true });+ var aw: Io.Writer.Allocating = .init(arena);+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ try s.write(Value{ .object = obj });+ const url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}/exec", .{sandbox_id});+ return gatewayJson(io, gpa, arena, .POST, url, key, aw.writer.buffered());+}++fn cubeReadState(io: Io, arena: Allocator) ?std.json.ObjectMap {+ const data = Io.Dir.cwd().readFileAlloc(io, cube_state_file, arena, .limited(64 * 1024)) catch return null;+ const v = std.json.parseFromSliceLeaky(Value, arena, data, .{ .allocate = .alloc_always }) catch return null;+ if (v != .object) return null;+ return v.object;+}++fn cubePrintAttach(out: *Io.Writer, st: std.json.ObjectMap) !void {+ const url = strFieldObj(st, "preview_url") orelse return;+ const tok = strFieldObj(st, "serve_token") orelse return;+ try out.print(" attach any serve client:\n GRAFF_SERVE_BASE={s}\n GRAFF_SERVE_TOKEN={s}\n", .{ url, tok });+ if (strFieldObj(st, "preview_token")) |pt|+ try out.print(" x-daytona-preview-token: {s}\n", .{pt});+}++// Reach serve from THIS machine through the preview proxy — any HTTP status+// proves the pipe (serve 404s a bare GET /); only a transport error means no.+fn cubePipeProbe(io: Io, gpa: Allocator, arena: Allocator, base: []const u8, serve_token: []const u8, preview_token: ?[]const u8) u16 {+ var client: std.http.Client = .{ .allocator = gpa, .io = io };+ defer client.deinit();+ var aw: Io.Writer.Allocating = .init(arena);+ var extra: [1]std.http.Header = undefined;+ var n: usize = 0;+ if (preview_token) |pt| {+ extra[n] = .{ .name = "x-daytona-preview-token", .value = pt };+ n += 1;+ }+ const res = client.fetch(.{+ .location = .{ .url = std.fmt.allocPrint(arena, "{s}/", .{base}) catch return 0 },+ .method = .GET,+ .response_writer = &aw.writer,+ .headers = .{+ .authorization = .{ .override = std.fmt.allocPrint(arena, "Bearer {s}", .{serve_token}) catch return 0 },+ .user_agent = .{ .override = "simple-harness/" ++ harness_version },+ },+ .extra_headers = extra[0..n],+ }) catch return 0;+ return @intFromEnum(res.status);+}++fn cubeNew(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, out: *Io.Writer, minutes: i64) !void {+ // A cube that is still running is reused, not duplicated — `cube stop`+ // first for a fresh one. Stale state (stopped/deleted sandbox) is replaced.+ if (cubeReadState(io, arena)) |st| {+ if (strFieldObj(st, "sandbox_id")) |old_id| {+ const info_url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}", .{old_id});+ if (gatewayFetch(io, gpa, arena, .GET, info_url, key, null)) |r| {+ if (r.code >= 200 and r.code < 300) {+ if (std.json.parseFromSliceLeaky(Value, arena, r.body, .{ .allocate = .alloc_always })) |info| {+ if (info == .object and std.mem.eql(u8, strFieldObj(info.object, "state") orelse "", "started")) {+ try out.print("cube already running: {s}\n", .{old_id});+ try cubePrintAttach(out, st);+ try out.flush();+ return;+ }+ } else |_| {}+ }+ } else |_| {}+ }+ }++ try out.writeAll("spinning up a cube …\n");+ try out.flush();+ const create_body = try std.fmt.allocPrint(arena, "{{\"autoStopMinutes\":{d},\"labels\":{{\"purpose\":\"cube\"}}}}", .{minutes});+ const created = try gatewayJson(io, gpa, arena, .POST, codegraff_device_base ++ "/v1/sandboxes", key, create_body);+ if (created != .object) std.process.fatal("cube: unexpected create response", .{});+ const id = strFieldObj(created.object, "id") orelse std.process.fatal("cube: create returned no id", .{});+ try out.print(" sandbox {s}\n", .{id});+ try out.flush();++ var state: []const u8 = strFieldObj(created.object, "state") orelse "";+ var waits: usize = 0;+ while (!std.mem.eql(u8, state, "started") and waits < 60) : (waits += 1) {+ io.sleep(Io.Duration.fromSeconds(2), .awake) catch {};+ const info_url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}", .{id});+ const info = try gatewayJson(io, gpa, arena, .GET, info_url, key, null);+ if (info == .object) state = strFieldObj(info.object, "state") orelse "";+ }+ if (!std.mem.eql(u8, state, "started"))+ std.process.fatal("cube: sandbox never reached started (last state '{s}')", .{state});++ try out.writeAll(" installing graff …\n");+ try out.flush();+ const inst = try cubeExec(io, gpa, arena, key, id, "curl -fsSL https://raw.githubusercontent.com/justrach/codegraff/main/install.sh | bash", 120, false);+ if (inst != .object or intFieldObj(inst.object, "exitCode", 1) != 0) {+ const r = if (inst == .object) (strFieldObj(inst.object, "result") orelse "") else "";+ std.process.fatal("cube: graff install failed: {s}", .{r[(r.len -| 300)..]});+ }++ var tok_bytes: [24]u8 = undefined;+ io.random(&tok_bytes);+ var tok_buf: [32]u8 = undefined;+ const serve_token = std.base64.url_safe_no_pad.Encoder.encode(&tok_buf, &tok_bytes);++ try out.writeAll(" starting graff serve …\n");+ try out.flush();+ const launch = try std.fmt.allocPrint(arena, "CODEGRAFF_API_KEY={s} exec $HOME/bin/graff serve --host 0.0.0.0 --port {d} --token {s}", .{ key, cube_port, serve_token });+ const started = try cubeExec(io, gpa, arena, key, id, launch, 60, true);+ const exec_id = if (started == .object) (strFieldObj(started.object, "execId") orelse "") else "";++ const probe_cmd = try std.fmt.allocPrint(arena, "curl -s -o /dev/null -w '%{{http_code}}' http://127.0.0.1:{d}/", .{cube_port});+ var up = false;+ var tries: usize = 0;+ while (tries < 20) : (tries += 1) {+ const p: ?Value = cubeExec(io, gpa, arena, key, id, probe_cmd, 15, false) catch null;+ if (p) |pv| if (pv == .object) {+ const code_s = std.mem.trim(u8, strFieldObj(pv.object, "result") orelse "", " \t\r\n'");+ if (code_s.len > 0 and !std.mem.eql(u8, code_s, "000")) {+ up = true;+ break;+ }+ };+ io.sleep(Io.Duration.fromSeconds(1), .awake) catch {};+ }+ if (!up) {+ const logcmd = try std.fmt.allocPrint(arena, "cat /tmp/cg-exec/{s}.out 2>/dev/null | tail -5", .{exec_id});+ const lg: ?Value = cubeExec(io, gpa, arena, key, id, logcmd, 15, false) catch null;+ const tail = if (lg) |l| (if (l == .object) (strFieldObj(l.object, "result") orelse "") else "") else "";+ std.process.fatal("cube: serve never came up; log tail: {s}", .{tail});+ }++ const pv_url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}/ports/{d}/preview", .{ id, cube_port });+ const pv = try gatewayJson(io, gpa, arena, .GET, pv_url, key, null);+ if (pv != .object) std.process.fatal("cube: unexpected preview response", .{});+ const purl_raw = strFieldObj(pv.object, "url") orelse std.process.fatal("cube: preview returned no url", .{});+ const purl = std.mem.trimEnd(u8, purl_raw, "/");+ const ptok = strFieldObj(pv.object, "token");++ const pipe = cubePipeProbe(io, gpa, arena, purl, serve_token, ptok);+ if (pipe == 0) {+ try out.print(" {s}!{s} preview minted but unreachable from here — check network\n", .{ style.yellow, style.reset });+ } else {+ try out.print(" pipe ok (HTTP {d} through the preview proxy)\n", .{pipe});+ }++ Io.Dir.cwd().createDirPath(io, ".graff") catch {};+ var st: std.json.ObjectMap = .empty;+ try st.put(arena, "sandbox_id", .{ .string = id });+ try st.put(arena, "preview_url", .{ .string = purl });+ if (ptok) |p| try st.put(arena, "preview_token", .{ .string = p });+ try st.put(arena, "serve_token", .{ .string = try arena.dupe(u8, serve_token) });+ try st.put(arena, "exec_id", .{ .string = exec_id });+ try st.put(arena, "port", .{ .integer = cube_port });+ var aw: Io.Writer.Allocating = .init(arena);+ var sfy: std.json.Stringify = .{ .writer = &aw.writer };+ try sfy.write(Value{ .object = st });+ const f = try Io.Dir.cwd().createFile(io, cube_state_file, .{});+ defer f.close(io);+ var wbuf: [1024]u8 = undefined;+ var fw = f.writer(io, &wbuf);+ try fw.interface.writeAll(aw.writer.buffered());+ try fw.interface.flush();++ try out.print("{s}✓{s} cube ready\n", .{ style.green, style.reset });+ try cubePrintAttach(out, st);+ try out.print(" state → {s}\n stop → graff cube stop\n", .{cube_state_file});+ try out.flush();+}++fn cubeCommand(io: Io, gpa: Allocator, arena: Allocator, key: []const u8, args: []const []const u8) !void {+ var obuf: [4096]u8 = undefined;+ var ow = Io.File.stdout().writer(io, &obuf);+ const out = &ow.interface;++ const sub = if (args.len > 0) args[0] else "status";+ if (std.mem.eql(u8, sub, "new")) {+ var minutes: i64 = 30;+ var i: usize = 1;+ while (i < args.len) : (i += 1) {+ if (std.mem.eql(u8, args[i], "--minutes") and i + 1 < args.len) {+ i += 1;+ minutes = std.fmt.parseInt(i64, args[i], 10) catch std.process.fatal("cube: --minutes needs a number", .{});+ } else std.process.fatal("usage: graff cube new [--minutes N]", .{});+ }+ try cubeNew(io, gpa, arena, key, out, minutes);+ return;+ }+ if (std.mem.eql(u8, sub, "stop")) {+ const st = cubeReadState(io, arena) orelse std.process.fatal("cube: nothing to stop — no {s}", .{cube_state_file});+ const id = strFieldObj(st, "sandbox_id") orelse std.process.fatal("cube: state file has no sandbox_id", .{});+ const url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}/stop", .{id});+ const resp = try gatewayJson(io, gpa, arena, .POST, url, key, null);+ const state = if (resp == .object) (strFieldObj(resp.object, "state") orelse "stopped") else "stopped";+ const cost_micro = if (resp == .object) intFieldObj(resp.object, "costMicro", 0) else 0;+ Io.Dir.cwd().deleteFile(io, cube_state_file) catch {};+ try out.print("{s}✓{s} cube {s} → {s} (meter settled ${d:.4})\n", .{ style.green, style.reset, id, state, @as(f64, @floatFromInt(cost_micro)) / 1e6 });+ try out.flush();+ return;+ }+ if (!std.mem.eql(u8, sub, "status"))+ std.process.fatal("usage: graff cube [new [--minutes N]|status|stop]", .{});++ const st = cubeReadState(io, arena) orelse {+ try out.writeAll("no cube — `graff cube new` spins one up\n");+ try out.flush();+ return;+ };+ const id = strFieldObj(st, "sandbox_id") orelse std.process.fatal("cube: state file has no sandbox_id", .{});+ const info_url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}", .{id});+ var state: []const u8 = "unknown";+ if (gatewayFetch(io, gpa, arena, .GET, info_url, key, null)) |r| {+ if (r.code >= 200 and r.code < 300) {+ if (std.json.parseFromSliceLeaky(Value, arena, r.body, .{ .allocate = .alloc_always })) |info| {+ if (info == .object) state = strFieldObj(info.object, "state") orelse "unknown";+ } else |_| {}+ } else state = "gone";+ } else |_| {}+ const scolor = if (std.mem.eql(u8, state, "started")) style.green else style.dim;+ try out.print("cube {s}: {s}{s}{s}\n", .{ id, scolor, state, style.reset });+ const meter_url = try std.fmt.allocPrint(arena, codegraff_device_base ++ "/v1/sandboxes/{s}/meter", .{id});+ if (gatewayFetch(io, gpa, arena, .GET, meter_url, key, null)) |r| {+ if (r.code >= 200 and r.code < 300) {+ if (std.json.parseFromSliceLeaky(Value, arena, r.body, .{ .allocate = .alloc_always })) |m| {+ if (m == .object) {+ const up_s = intFieldObj(m.object, "current_uptime_seconds", 0);+ const billed = @as(f64, @floatFromInt(intFieldObj(m.object, "billed_so_far_micro_usd", 0))) / 1e6;+ const unbilled = @as(f64, @floatFromInt(intFieldObj(m.object, "unbilled_cost_micro_usd", 0))) / 1e6;+ try out.print(" uptime {d}s · billed ${d:.4} · unbilled ${d:.4}\n", .{ up_s, billed, unbilled });+ }+ } else |_| {}+ }+ } else |_| {}+ if (std.mem.eql(u8, state, "started")) try cubePrintAttach(out, st);+ try out.flush();+}+// Safe API-key store. On macOS, keys live in the login Keychain (service// "simple-harness", account=provider id) via the `security` CLI — never on// disk in plaintext. Elsewhere they fall back to a 0600 file