graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
rlm: a projection or len() statement reports why it failed (#1536)
justrach7f985c1fb3af
diff --git a/src/rlm.zig b/src/rlm.zigindex 1a4a9af9..5c621766 100644--- a/src/rlm.zig+++ b/src/rlm.zig@@ -372,7 +372,10 @@ fn renderPrint(ctx: ToolCtx, arena: Allocator, inner: []const u8, binds: []constfn renderPrintPart(ctx: ToolCtx, arena: Allocator, inner: []const u8, binds: []const Binding, claimed: *std.StringHashMap(ToolOutput), status: *?ToolOutput) ![]const u8 {const t = std.mem.trim(u8, inner, " \t");if (t.len >= 2 and (t[0] == '"' or t[0] == '\'') and t[t.len - 1] == t[0]) return t[1 .. t.len - 1];- if (rlm_reduce.evalExpr(arena, t, binds)) |text| return maybeSlim(arena, text) else |_| {}+ if (rlm_reduce.evalExpr(arena, t, binds)) |text| return maybeSlim(arena, text) else |err| if (err != error.Miss) {+ status.* = .{ .text = try rlm_reduce.failText(ctx.gpa, t, err), .is_error = true };+ return "";+ }var i = binds.len;while (i > 0) {i -= 1;diff --git a/src/rlm_order_tests.zig b/src/rlm_order_tests.zigindex 4c9f1fb9..59a895b3 100644--- a/src/rlm_order_tests.zig+++ b/src/rlm_order_tests.zig@@ -301,3 +301,19 @@ test "rlm destructive git in a yolo subagent hits the subagent block (#1292)" {try std.testing.expect(std.mem.indexOf(u8, results[0].text, "destructive git is blocked for subagents") != null);try std.testing.expectError(error.FileNotFound, f.tmp.dir.openFile(io, "marker", .{}));}++test "rlm projection assignments take comments, keywords and sparse fields, then print (#1536)" {+ var f = try Fixture.init();+ defer f.deinit();+ try f.tmp.dir.writeFile(io, .{ .sub_path = "items.json", .data = "[{\"id\":1,\"name\":\"a\"},{\"id\":2}]" });+ const out = try f.run("items = read_file(\"items.json\")\nids = project(items, \"id\") # plain ids\nnames = project(x=items, field=\"name\")\nn = len(items) # count\nprint(ids, names, n)");+ defer gpa.free(out.text);+ try std.testing.expect(!out.is_error);+ try std.testing.expectEqualStrings("[1,2]\n[\"a\",null]\n2", out.text);++ const bad = try f.run("t = read_file(\"target.txt\")\nv = project(t, \"id\")\nprint(v)");+ defer gpa.free(bad.text);+ try std.testing.expect(bad.is_error);+ try std.testing.expect(std.mem.indexOf(u8, bad.text, "unsupported statement") == null);+ try std.testing.expect(std.mem.indexOf(u8, bad.text, "not a JSON array") != null);+}diff --git a/src/rlm_reduce.zig b/src/rlm_reduce.zigindex e0982cb9..3900e1e3 100644--- a/src/rlm_reduce.zig+++ b/src/rlm_reduce.zig@@ -22,20 +22,21 @@ pub fn evalStmt(binds: []const rlm_spec.Binding,bind_out: *std.ArrayList(rlm_spec.Binding),) !StmtHit {- const trimmed = std.mem.trim(u8, stmt, " \t");+ // #1536: a trailing `# comment` is stripped like it is for a host call.+ const trimmed = std.mem.trim(u8, spec_ptc.stripComment(stmt), " \t");var rest = trimmed;var assign: ?[]const u8 = null;if (std.mem.indexOfScalar(u8, trimmed, '=')) |eq| {const lhs = std.mem.trim(u8, trimmed[0..eq], " \t");const rhs = std.mem.trim(u8, trimmed[eq + 1 ..], " \t");- if (lhs.len > 0 and (std.mem.startsWith(u8, rhs, "len(") or std.mem.startsWith(u8, rhs, "project("))) {+ if (lhs.len > 0 and isReducer(rhs)) {assign = lhs;rest = rhs;}}const text = evalExpr(arena, rest, binds) catch |err| switch (err) {error.Miss => return .miss,- else => return .{ .fail = try std.fmt.allocPrint(gpa, "rlm: {s} failed", .{rest}) },+ else => return .{ .fail = try failText(gpa, rest, err) },};if (assign) |nm| try bind_out.append(arena, .{.name = try arena.dupe(u8, nm),@@ -44,32 +45,75 @@ pub fn evalStmt(return .ok;}-/// Resolve `len(x)` / `project(x, "field")` for `print(...)`.-pub fn evalExpr(arena: Allocator, expr: []const u8, binds: []const rlm_spec.Binding) ![]const u8 {+/// `len(...)` or `project(...)`, whatever is inside. Once a statement has+/// this shape it is a reducer: a failure says why, it never falls through+/// to "unsupported statement" (#1536).+fn isReducer(expr: []const u8) bool {const t = std.mem.trim(u8, expr, " \t");- if (std.mem.startsWith(u8, t, "len(") and t[t.len - 1] == ')') {- const inner = std.mem.trim(u8, t["len(".len .. t.len - 1], " \t");- const raw = bindText(binds, inner) orelse inner;- const items = try jsonArray(arena, raw);- return try std.fmt.allocPrint(arena, "{d}", .{items.len});- }- if (std.mem.startsWith(u8, t, "project(") and t[t.len - 1] == ')') {- const inner = t["project(".len .. t.len - 1];- const parts = try spec_ptc.splitTopLevel(arena, inner, ',');- if (parts.len != 2) return error.Miss;- const raw = bindText(binds, parts[0]) orelse std.mem.trim(u8, parts[0], " \t");- const field = stripQuotes(parts[1]);- const items = try jsonArray(arena, raw);- var out: std.ArrayList(u8) = .empty;- try out.append(arena, '[');- for (items, 0..) |item, i| {- if (i > 0) try out.append(arena, ',');- try out.appendSlice(arena, try fieldValue(arena, item, field));- }- try out.append(arena, ']');- return out.toOwnedSlice(arena);+ if (t.len == 0 or t[t.len - 1] != ')') return false;+ return std.mem.startsWith(u8, t, "len(") or std.mem.startsWith(u8, t, "project(");+}++pub const Error = error{ Miss, BadArgs, NotBound, NotArray, NoField } || Allocator.Error || std.Io.Writer.Error;++/// Resolve `len(x)` / `project(x, "field")` for `print(...)`. error.Miss+/// means the text is not a reducer; any other error is the reducer failing.+pub fn evalExpr(arena: Allocator, expr: []const u8, binds: []const rlm_spec.Binding) Error![]const u8 {+ const t = std.mem.trim(u8, spec_ptc.stripComment(expr), " \t");+ if (!isReducer(t)) return error.Miss;+ const is_len = std.mem.startsWith(u8, t, "len(");+ const inner = t[(if (is_len) "len(".len else "project(".len) .. t.len - 1];+ const parts = spec_ptc.splitTopLevel(arena, inner, ',') catch return error.BadArgs;+ if (parts.len != @as(usize, if (is_len) 1 else 2)) return error.BadArgs;+ const items = try jsonArray(arena, try argValue(binds, parts[0]));+ if (is_len) return try std.fmt.allocPrint(arena, "{d}", .{items.len});+ const field = stripQuotes(dropKeyword(parts[1]));+ if (field.len == 0) return error.BadArgs;+ var out: std.ArrayList(u8) = .empty;+ var found = items.len == 0;+ try out.append(arena, '[');+ for (items, 0..) |item, i| {+ if (i > 0) try out.append(arena, ',');+ const v = try fieldValue(arena, item, field);+ if (v != null) found = true;+ try out.appendSlice(arena, v orelse "null");}- return error.Miss;+ // A field some rows lack reads null; one no row has is a typo.+ if (!found) return error.NoField;+ try out.append(arena, ']');+ return out.toOwnedSlice(arena);+}++/// The first argument: a bound name's text, or a JSON literal. `x=issues`+/// spells the same argument as a keyword, as host calls may.+fn argValue(binds: []const rlm_spec.Binding, arg: []const u8) Error![]const u8 {+ const t = dropKeyword(arg);+ if (bindText(binds, t)) |text| return text;+ if (t.len > 0 and (t[0] == '[' or t[0] == '{')) return t;+ return error.NotBound;+}++/// `field="id"` → `"id"`; a bare value is returned as is.+fn dropKeyword(arg: []const u8) []const u8 {+ const t = std.mem.trim(u8, arg, " \t");+ var i: usize = 0;+ while (i < t.len and (std.ascii.isAlphanumeric(t[i]) or t[i] == '_')) i += 1;+ if (i == 0 or i == t.len or std.ascii.isDigit(t[0])) return t;+ const rest = std.mem.trimStart(u8, t[i..], " \t");+ if (rest.len < 2 or rest[0] != '=' or rest[1] == '=') return t;+ return std.mem.trim(u8, rest[1..], " \t");+}++/// What went wrong, in terms of the documented call.+pub fn failText(gpa: Allocator, expr: []const u8, err: Error) ![]u8 {+ const why = switch (err) {+ error.BadArgs => "takes len(x) or project(x, \"field\"), x a bound name or a JSON literal",+ error.NotBound => "its first argument is not a bound name; bind the result first (x = tool(...))",+ error.NotArray => "its first argument is not a JSON array (or an object holding one); write_file it and use shell to parse other text",+ error.NoField => "no item has that field; print(x) lists the fields a row has",+ else => "failed",+ };+ return std.fmt.allocPrint(gpa, "rlm: {s}: {s}", .{ std.mem.trim(u8, expr, " \t"), why });}fn bindText(binds: []const rlm_spec.Binding, name: []const u8) ?[]const u8 {@@ -88,9 +132,9 @@ fn stripQuotes(s: []const u8) []const u8 {return t;}-fn jsonArray(arena: Allocator, text: []const u8) ![]const Value {+fn jsonArray(arena: Allocator, text: []const u8) Error![]const Value {const trimmed = std.mem.trim(u8, text, " \t\r\n");- const parsed = std.json.parseFromSlice(Value, arena, trimmed, .{}) catch return error.Miss;+ const parsed = std.json.parseFromSlice(Value, arena, trimmed, .{}) catch return error.NotArray;if (parsed.value == .array) return parsed.value.array.items;if (parsed.value == .object) {var it = parsed.value.object.iterator();@@ -98,21 +142,22 @@ fn jsonArray(arena: Allocator, text: []const u8) ![]const Value {if (e.value_ptr.* == .array) return e.value_ptr.array.items;}}- return error.Miss;+ return error.NotArray;}-fn fieldValue(arena: Allocator, item: Value, field: []const u8) ![]const u8 {+/// The field's JSON text, or null when this item does not carry it.+fn fieldValue(arena: Allocator, item: Value, field: []const u8) Error!?[]const u8 {const v = switch (item) {// ADR 0238: an each() item is the tool's whole result; a comment// list answers through its printed fold, {n, latest_author}.- .object => |obj| obj.get(field) orelse mcp_shapes.foldField(item, field) orelse return error.Miss,- .array => mcp_shapes.foldField(item, field) orelse return error.Miss,- else => return error.Miss,+ .object => |obj| obj.get(field) orelse mcp_shapes.foldField(item, field) orelse return null,+ .array => mcp_shapes.foldField(item, field) orelse return null,+ else => return null,};var aw: std.Io.Writer.Allocating = .init(arena);var s: std.json.Stringify = .{ .writer = &aw.writer };try s.write(v);- return aw.toOwnedSlice();+ return try aw.toOwnedSlice();}test "len() counts a JSON array bind" {@@ -139,7 +184,7 @@ test "project() reads a field the printed view drops, and n/latest_author off antry std.testing.expectEqualStrings("[2,1]", try evalExpr(arena, "project(issues, \"priority\")", &binds));try std.testing.expectEqualStrings("[2,0]", try evalExpr(arena, "project(comments, \"n\")", &binds));try std.testing.expectEqualStrings("[\"bev\",null]", try evalExpr(arena, "project(comments, \"latest_author\")", &binds));- try std.testing.expectError(error.Miss, evalExpr(arena, "project(comments, \"body\")", &binds));+ try std.testing.expectError(error.NoField, evalExpr(arena, "project(comments, \"body\")", &binds));}test "project() extracts one field; print(len()) resolves" {diff --git a/src/spec_ptc.zig b/src/spec_ptc.zigindex c858c332..bffff419 100644--- a/src/spec_ptc.zig+++ b/src/spec_ptc.zig@@ -127,7 +127,7 @@ fn printInner(stmt: []const u8) ?[]const u8 {return t["print(".len .. t.len - 1];}-fn stripComment(stmt: []const u8) []const u8 {+pub fn stripComment(stmt: []const u8) []const u8 {var i: usize = 0;var in_str: ?u8 = null;var esc = false;