graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
rlm takes JSON literals, and each() maps plain values
justrachef4da7a7d018
diff --git a/docs/adr/0236-rlm-takes-json-literals-and-each-maps-plain-values.md b/docs/adr/0236-rlm-takes-json-literals-and-each-maps-plain-values.mdnew file mode 100644index 00000000..7fb2515f--- /dev/null+++ b/docs/adr/0236-rlm-takes-json-literals-and-each-maps-plain-values.md@@ -0,0 +1,48 @@+# 0236. rlm takes JSON literals, and each() maps plain values++Status: accepted 2026-10-02++## Context++Models write `rlm` scripts the way they write JSON and Python. With thinking+off they did it often enough to cost real time on MCP tasks:++- `ids = ["ISS-1", "ISS-2"]`, `parts = {"ids": ids, "titles": titles}` and+ `write_file("comments.json", [c1, c2])` were refused. Only string, number and+ boolean literals were arguments, and a statement could not bind a literal.+- `each(["ISS-1", "ISS-2"], tool, "id")` was refused as an unsupported+ statement: the splitter nested only parentheses, so the array's commas cut+ it into many arguments.+- `ids = project(issues, "id")` gives a list of plain ids, and+ `each(ids, tool, "id")` failed with `item missing field id`, because each()+ only read fields out of objects. One run repeated that refused call until+ the task ran out of time.+- `each(arr, tool)` with two arguments was refused.+- The refusal said only `unsupported statement`, so the next try was a guess.++## Decision++- A JSON array or object literal is an argument and can be bound:+ `name = [...]`, `name = {...}`, `write_file("r.json", {"ids": ids})`. A bound+ name inside a literal stands for its value: JSON goes in as JSON, other+ text as a JSON string. `True`, `False`, `None` and single-quoted strings are+ read the Python way. `name = "text"`, `name = 3` and `name = other` bind too+ (`rlm_literal.zig`). For `write_file`, the file holds the literal's JSON text.+- The statement and argument splitters nest `[...]` and `{...}` as well as+ `(...)`.+- `each(arr, tool[, field])`: `arr` may be a literal; a plain item (a string+ id, a number) is passed as is; `field` picks the value out of object items+ and defaults to an MCP tool's first parameter. A missing field names the+ fields the items do have.+- An unsupported statement lists the forms that work. Nested calls, indexing+ and operators are still refused: `rlm` stays a call-only script.++## Consequences++Scripts that used to fail on a literal now run. A literal holding a name that+was never bound is still refused. The tool description stays inside its+600-byte budget, so the literal forms are spelled out in the `code` argument's+description and in the refusal.++Revisit if traces show models leaning on literals to fake computation that+belongs in the shell.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex c6f9d084..d4499e86 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -257,6 +257,7 @@ record only when you need the evidence or the edge cases.| [0228](0228-read-only-child-calls-declared-read-only-mcp-tools.md) | An MCP tool is a read when its server declares `readOnlyHint: true` (`mcp.Tool.read_only`). A read-only (informational) child may call those and inherits only those from its root; undeclared MCP tools stay refused. || [0229](0229-every-chatgpt-login-is-chatgpt-new.md) | The Codex client's login no longer works, so every ChatGPT login name (`chatgpt`, `codex`, `openai`, `gpt`, `oai`) and the model picker run the `chatgpt-new` sign-in, and `chatgpt-new` precedes `codex` in the provider table. The `codex` provider stays for an existing Codex CLI `auth.json`. Replaces ADR 0221's "Codex first". || [0231](0231-no-round-trip-the-model-does-not-need.md) | No call the task did not ask for: a named small MCP server (and subagents, when asked for) loads with the first request, with the slim-shape note; `write_file` refuses to replace an unread file unless `replace: true` and reports created/replaced and JSON validity; inherited child MCP tools are never deferred; a piped bracketed paste is one prompt; unwatched (`-p`, piped) sessions get no `ask_user` and no low-effort reasoning summaries. |+| [0236](0236-rlm-takes-json-literals-and-each-maps-plain-values.md) | `rlm` takes JSON array/object literals as arguments and binds (`name = [...]`, bound names allowed inside); its splitters nest `[]`/`{}`; `each(arr, tool[, field])` passes plain items as is, picks `field` from objects (default: an MCP tool's first parameter) and names the fields it found; an unsupported statement lists the forms that work. |## When to write onediff --git a/src/rlm.zig b/src/rlm.zigindex 31db65f2..f2677701 100644--- a/src/rlm.zig+++ b/src/rlm.zig@@ -25,12 +25,12 @@ const mcp_shapes = @import("mcp_shapes.zig");const read_miss = @import("read_miss.zig");pub const tool_name = "rlm";-pub const tool_desc = "Tools as a call-only script, not Python: name = tool(args), args being literals or bound names (a bound name passes its text); no expressions, indexing or comprehensions. To compute over a result, write_file(\"r.json\", name), then shell. Leading read_file/codedb/sleep_ms/llm_query calls overlap; others run in order, stopping on error/cancel/pending. Binds persist. subagent(\"task\") is sidecar-only (critical-path steps stay local). Loaded MCP names are host functions after load_tool_schemas; each(arr, tool, field) maps a JSON array; len(x)/project(x, field) slim it. print() is the answer.";+pub const tool_desc = "Tools as a call-only script, not Python: name = tool(args), args being literals or bound names (a bound name passes its text); no expressions, indexing or comprehensions. To compute over a result, write_file(\"r.json\", name), then shell. Leading read_file/codedb/sleep_ms/llm_query calls overlap; others run in order, stopping on error/cancel/pending. Binds persist. subagent(\"task\") is sidecar-only (critical-path steps stay local). Loaded MCP names are host functions after load_tool_schemas; each(arr, tool, field?) maps a JSON array; len(x)/project(x, field) slim it. print() is the answer.";/// --lean catalog desc: same contract, no REPL essay. maybeAppend is after/// compactLeanSpecs, so this is the one-shot wire text.pub const lean_tool_desc = "Batch independent read_file/codedb here. print(read_file(\"p\")) returns the file — do not catalog-read it again. Then edit_file/write_file/shell as catalog tools. Leading reads overlap; other calls run in order, stopping on error/cancel/pending. Binds persist. Loaded MCP names are host functions after load_tool_schemas.";pub const tool_schema =- \\{"type": "object", "properties": {"code": {"type": "string", "description": "Call-only script, not Python. One statement per line: name = read_file(\"path\") / codedb(\"command\") / bash(\"cmd\") / sleep_ms(ms) / llm_query(\"prompt\") / subagent(\"task\") / loaded mcp__server__tool() or tools.server.tool(); write_file(\"path\", name) saves a bound result; each(arr, tool, field) maps a JSON array; len(x) and project(x, field) slim it; print(name, ...) is the result. Arguments are string, number or boolean literals or bound names. Assignments persist across rlm calls."}}, "required": ["code"]}+ \\{"type": "object", "properties": {"code": {"type": "string", "description": "Call-only script, not Python. One statement per line: name = read_file(\"path\") / codedb(\"command\") / bash(\"cmd\") / sleep_ms(ms) / llm_query(\"prompt\") / subagent(\"task\") / loaded mcp__server__tool() or tools.server.tool(); write_file(\"path\", name) saves a bound result; each(arr, tool[, field]) calls tool per item; len(x) and project(x, field) slim it; name = [..] / {..} / \"text\" binds a literal; print(name, ...) is the result. Arguments are string, number, boolean or JSON array/object literals, or bound names. Assignments persist across rlm calls."}}, "required": ["code"]};/// Process-global: on by default. `--old` / `--no-rlm` turn it off; `--rlm`@@ -324,9 +324,17 @@ fn evalStmt(try printed.appendSlice(ctx.gpa, text);return null;}- return .{ .text = try std.fmt.allocPrint(ctx.gpa, "rlm: unsupported statement: {s}", .{stmt}), .is_error = true };+ // ADR 0236: `name = [..]`, `{..}`, a string, a number or another name.+ if (try @import("rlm_literal.zig").assign(arena, stmt, binds)) |b| {+ try bind_out.append(arena, b);+ return null;+ }+ return .{ .text = try std.fmt.allocPrint(ctx.gpa, "rlm: unsupported statement: {s}\n{s}", .{ stmt, statement_forms }), .is_error = true };}+/// What an unsupported statement could have been (ADR 0236).+const statement_forms = "A statement is one of: name = tool(args) · name = each(arr, tool[, field]) · name = len(x) or project(x, field) · name = a literal ([...], {...}, \"text\", 3) or another name · write_file(\"path\", name) · print(name, ...). Nested calls, indexing and operators are not supported: bind each value first.";+fn assignName(stmt: []const u8) ?[]const u8 {const eq = std.mem.indexOfScalar(u8, stmt, '=') orelse return null;const lhs = std.mem.trim(u8, stmt[0..eq], " \t");diff --git a/src/rlm_literal.zig b/src/rlm_literal.zignew file mode 100644index 00000000..85835180--- /dev/null+++ b/src/rlm_literal.zig@@ -0,0 +1,170 @@+//! JSON literals in rlm scripts (ADR 0236): `ids = ["ISS-1", "ISS-2"]`,+//! `each(["ISS-1", "ISS-2"], tool, "id")`, `write_file("r.json", {"ids": ids})`.+//! A bound name inside a literal stands for its value: a bound result that is+//! JSON goes in as JSON, any other text as a JSON string. Python spellings of+//! the constants (True, False, None) and single-quoted strings are accepted.++const std = @import("std");+const Allocator = std.mem.Allocator;+const Value = std.json.Value;+const Bound = @import("spec_ptc.zig").Bound;++/// One past the bracket that closes the literal opening at `src[start]`+/// (`[` or `{`), skipping strings; null when it never closes.+pub fn scan(src: []const u8, start: usize) ?usize {+ var depth: usize = 0;+ var i = start;+ while (i < src.len) : (i += 1) {+ switch (src[i]) {+ '"', '\'' => i = (stringEnd(src, i) orelse return null) - 1,+ '[', '{' => depth += 1,+ ']', '}' => {+ if (depth == 0) return null;+ depth -= 1;+ if (depth == 0) return i + 1;+ },+ else => {},+ }+ }+ return null;+}++/// The literal as canonical JSON text, bound names replaced by their values.+pub fn render(arena: Allocator, literal: []const u8, binds: []const Bound) ![]const u8 {+ var out: std.ArrayList(u8) = .empty;+ var i: usize = 0;+ while (i < literal.len) {+ const c = literal[i];+ if (c == '"' or c == '\'') {+ const end = stringEnd(literal, i) orelse return error.Unterminated;+ if (c == '"') try out.appendSlice(arena, literal[i..end]) else try appendString(arena, &out, literal[i + 1 .. end - 1]);+ i = end;+ continue;+ }+ if (std.ascii.isAlphabetic(c) or c == '_') {+ var j = i + 1;+ while (j < literal.len and (std.ascii.isAlphanumeric(literal[j]) or literal[j] == '_')) j += 1;+ const word = literal[i..j];+ if (constant(word)) |json| try out.appendSlice(arena, json) else if (lookup(binds, word)) |text| try appendValue(arena, &out, text) else return error.UnknownName;+ i = j;+ continue;+ }+ try out.append(arena, c);+ i += 1;+ }+ const parsed = try std.json.parseFromSliceLeaky(Value, arena, out.items, .{});+ return stringify(arena, parsed);+}++/// `name = <literal>`: a JSON array or object (bound names allowed inside), a+/// string, a number, a constant, or another bound name. A string binds its text; anything else+/// binds canonical JSON. Null when the statement is not this shape.+pub fn assign(arena: Allocator, stmt: []const u8, binds: []const Bound) !?Bound {+ const t = std.mem.trim(u8, stmt, " \t");+ const eq = std.mem.indexOfScalar(u8, t, '=') orelse return null;+ const name = std.mem.trim(u8, t[0..eq], " \t");+ if (name.len == 0 or !(std.ascii.isAlphabetic(name[0]) or name[0] == '_')) return null;+ for (name) |ch| if (!std.ascii.isAlphanumeric(ch) and ch != '_') return null;+ const rhs = std.mem.trim(u8, t[eq + 1 ..], " \t");+ if (rhs.len == 0 or rhs[0] == '=') return null;+ const text = switch (rhs[0]) {+ '[', '{' => blk: {+ if ((scan(rhs, 0) orelse return null) != rhs.len) return null;+ break :blk render(arena, rhs, binds) catch return null;+ },+ '"', '\'' => blk: {+ if ((stringEnd(rhs, 0) orelse return null) != rhs.len) return null;+ const json = if (rhs[0] == '"') rhs else try quoted(arena, rhs[1 .. rhs.len - 1]);+ const v = std.json.parseFromSliceLeaky(Value, arena, json, .{}) catch return null;+ break :blk v.string;+ },+ else => blk: {+ if (constant(rhs)) |json| break :blk json;+ if (lookup(binds, rhs)) |bound| break :blk bound; // an alias of a bound name+ _ = std.fmt.parseFloat(f64, rhs) catch return null;+ break :blk rhs;+ },+ };+ return .{ .name = try arena.dupe(u8, name), .text = text };+}++fn stringEnd(src: []const u8, start: usize) ?usize {+ const q = src[start];+ var i = start + 1;+ while (i < src.len) : (i += 1) {+ if (src[i] == '\\') {+ i += 1;+ continue;+ }+ if (src[i] == q) return i + 1;+ }+ return null;+}++fn constant(word: []const u8) ?[]const u8 {+ if (std.mem.eql(u8, word, "true") or std.mem.eql(u8, word, "True")) return "true";+ if (std.mem.eql(u8, word, "false") or std.mem.eql(u8, word, "False")) return "false";+ if (std.mem.eql(u8, word, "null") or std.mem.eql(u8, word, "None")) return "null";+ return null;+}++fn lookup(binds: []const Bound, name: []const u8) ?[]const u8 {+ var i = binds.len;+ while (i > 0) {+ i -= 1;+ if (std.mem.eql(u8, binds[i].name, name)) return binds[i].text;+ }+ return null;+}++fn appendValue(arena: Allocator, out: *std.ArrayList(u8), text: []const u8) !void {+ const trimmed = std.mem.trim(u8, text, " \t\r\n");+ if (std.json.parseFromSliceLeaky(Value, arena, trimmed, .{})) |_| {+ try out.appendSlice(arena, trimmed);+ } else |_| try appendString(arena, out, text);+}++fn appendString(arena: Allocator, out: *std.ArrayList(u8), raw: []const u8) !void {+ try out.appendSlice(arena, try quoted(arena, raw));+}++fn quoted(arena: Allocator, raw: []const u8) ![]const u8 {+ return stringify(arena, Value{ .string = raw });+}++fn stringify(arena: Allocator, v: Value) ![]const u8 {+ var aw: std.Io.Writer.Allocating = .init(arena);+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ try s.write(v);+ return aw.toOwnedSlice();+}++test "render: JSON literals with bound names, Python constants and single quotes" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const binds = [_]Bound{ .{ .name = "ids", .text = "[\"ISS-1\",\"ISS-2\"]" }, .{ .name = "note", .text = "plain text" } };+ try std.testing.expectEqualStrings("[\"ISS-1\",\"ISS-2\"]", try render(a, "[\"ISS-1\", \"ISS-2\"]", &.{}));+ try std.testing.expectEqualStrings("{\"ids\":[\"ISS-1\",\"ISS-2\"],\"note\":\"plain text\",\"ok\":true,\"x\":null}", try render(a, "{\"ids\": ids, \"note\": note, \"ok\": True, \"x\": None}", &binds));+ try std.testing.expectEqualStrings("[\"a\",[\"ISS-1\",\"ISS-2\"]]", try render(a, "['a', ids]", &binds));+ try std.testing.expectError(error.UnknownName, render(a, "[missing]", &binds));+ try std.testing.expectEqual(@as(?usize, 11), scan("[\"a]\", [1]] tail", 0));+ try std.testing.expect(scan("[1, 2", 0) == null);+}++test "assign: literals bind; calls and expressions do not" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const binds = [_]Bound{.{ .name = "c1", .text = "{\"n\":2}" }};+ try std.testing.expectEqualStrings("[\"ISS-1\",\"ISS-2\"]", (try assign(a, "ids = [\"ISS-1\", \"ISS-2\"]", &.{})).?.text);+ try std.testing.expectEqualStrings("[{\"n\":2}]", (try assign(a, "all = [c1]", &binds)).?.text);+ try std.testing.expectEqualStrings("[]", (try assign(a, "a = []", &.{})).?.text);+ try std.testing.expectEqualStrings("{\"issue_count\": 8", (try assign(a, "rows = \"{\\\"issue_count\\\": 8\"", &.{})).?.text);+ try std.testing.expectEqualStrings("hi", (try assign(a, "s = 'hi'", &.{})).?.text);+ try std.testing.expectEqualStrings("3", (try assign(a, "n = 3", &.{})).?.text);+ try std.testing.expectEqualStrings("{\"n\":2}", (try assign(a, "first = c1", &binds)).?.text);+ try std.testing.expect((try assign(a, "x = read_file(\"a\")", &.{})) == null);+ try std.testing.expect((try assign(a, "line = ids[0] + \"|\"", &.{})) == null);+ try std.testing.expect((try assign(a, "x == 3", &.{})) == null);+}diff --git a/src/rlm_literal_tests.zig b/src/rlm_literal_tests.zignew file mode 100644index 00000000..9d767ac7--- /dev/null+++ b/src/rlm_literal_tests.zig@@ -0,0 +1,74 @@+//! ADR 0236 end to end: JSON literals bind and pass as arguments, each()+//! takes plain items and a literal array, and refusals say what would work.++const std = @import("std");+const rlm = @import("rlm.zig");+const spec = @import("rlm_spec.zig");+const tools = @import("tools.zig");+const Fixture = @import("rlm_order_tests.zig").Fixture;+const gpa = std.testing.allocator;++const EchoHost = struct {+ fn run(ctx: tools.ToolCtx, call: @import("spec_ptc.zig").Call) tools.ToolOutput {+ return .{ .text = ctx.gpa.dupe(u8, call.args_json) catch unreachable };+ }+};++fn eachOut(f: *Fixture, arena: std.mem.Allocator, stmt: []const u8, binds: []const spec.Binding) !?[]const u8 {+ var outputs: std.ArrayList(spec.Binding) = .empty;+ const hit = try @import("rlm_mcp.zig").evalEach(f.ctx(), arena, stmt, binds, &outputs, EchoHost.run);+ switch (hit) {+ .ok => return if (outputs.items.len == 1) outputs.items[0].text else null,+ .fail => |e| {+ defer gpa.free(e.text);+ return error.EachFailed;+ },+ .miss => return null,+ }+}++test "ADR 0236: each() maps a literal array and plain ids, with or without a field" {+ var f = try Fixture.init();+ defer f.deinit();+ var arena = std.heap.ArenaAllocator.init(gpa);+ defer arena.deinit();+ const a = arena.allocator();+ const literal = (try eachOut(&f, a, "out = each([\"ISS-1\", \"ISS-2\"], \"read_file\", \"id\")", &.{})).?;+ try std.testing.expect(std.mem.indexOf(u8, literal, "ISS-1") != null and std.mem.indexOf(u8, literal, "ISS-2") != null);+ const ids = [_]spec.Binding{.{ .name = "ids", .text = "[\"ISS-1\",\"ISS-2\"]" }};+ const two_arg = (try eachOut(&f, a, "out = each(ids, \"read_file\")", &ids)).?;+ try std.testing.expect(std.mem.indexOf(u8, two_arg, "ISS-2") != null);+ const objects = [_]spec.Binding{.{ .name = "issues", .text = "[{\"id\":\"ISS-1\",\"title\":\"t\"}]" }};+ try std.testing.expect(std.mem.indexOf(u8, (try eachOut(&f, a, "out = each(issues, \"read_file\", \"id\")", &objects)).?, "ISS-1") != null);+}++test "ADR 0236: a missing field names the fields the items do have" {+ var f = try Fixture.init();+ defer f.deinit();+ var arena = std.heap.ArenaAllocator.init(gpa);+ defer arena.deinit();+ const binds = [_]spec.Binding{.{ .name = "issues", .text = "[{\"key\":\"ISS-1\",\"title\":\"t\"}]" }};+ var outputs: std.ArrayList(spec.Binding) = .empty;+ const hit = try @import("rlm_mcp.zig").evalEach(f.ctx(), arena.allocator(), "out = each(issues, \"read_file\", \"id\")", &binds, &outputs, EchoHost.run);+ try std.testing.expect(hit == .fail);+ defer gpa.free(hit.fail.text);+ try std.testing.expect(std.mem.indexOf(u8, hit.fail.text, "its fields: key, title") != null);+}++test "ADR 0236: a script binds literals and writes one built from bound names" {+ var f = try Fixture.init();+ defer f.deinit();+ const out = try f.run("ids = [\"a\", \"b\"]\nw = write_file(\"ids.json\", {\"ids\": ids, \"ok\": True})\nr = read_file(\"ids.json\")\nprint(r)");+ defer gpa.free(out.text);+ try std.testing.expect(!out.is_error);+ try std.testing.expect(std.mem.indexOf(u8, out.text, "{\"ids\":[\"a\",\"b\"],\"ok\":true}") != null);+}++test "ADR 0236: an unsupported statement lists the forms that work" {+ var f = try Fixture.init();+ defer f.deinit();+ const out = try f.run("ids = [\"a\"]\nline = ids[0] + \"|\"");+ defer gpa.free(out.text);+ try std.testing.expect(out.is_error);+ try std.testing.expect(std.mem.indexOf(u8, out.text, "A statement is one of") != null);+}diff --git a/src/rlm_mcp.zig b/src/rlm_mcp.zigindex fffb3058..8ce25f10 100644--- a/src/rlm_mcp.zig+++ b/src/rlm_mcp.zig@@ -156,6 +156,9 @@ pub const HostHit = union(enum) { miss, ok, fail: ToolOutput };/// `each(arr, tool, field)` — map a JSON array through one host tool./// Smallest control flow that makes `for issue in list_issues(): list_comments(id)` honest.+/// ADR 0236: `arr` may be a JSON literal; a plain item (a string id, a+/// number) is passed as is; `field` picks the value out of object items and+/// defaults to an MCP tool's first parameter.pub fn evalEach(ctx: ToolCtx,arena: Allocator,@@ -165,9 +168,13 @@ pub fn evalEach(run: *const fn (ToolCtx, spec_ptc.Call) ToolOutput,) !HostHit {const parsed = parseEach(arena, stmt) orelse return .miss;- const arr_text = resolveBind(binds, parsed.arr) orelse parsed.arr;+ const arr_src = std.mem.trim(u8, parsed.arr, " \t");+ const arr_text = resolveBind(binds, arr_src) orelse literal: {+ if (arr_src.len == 0 or (arr_src[0] != '[' and arr_src[0] != '{')) break :literal arr_src;+ break :literal @import("rlm_literal.zig").render(arena, arr_src, binds) catch arr_src;+ };const tool = stripQuotes(parsed.tool);- const field = stripQuotes(parsed.field);+ const field: ?[]const u8 = if (parsed.field) |f| stripQuotes(f) else if (ctx.registry) |reg| firstField(reg, tool) else null;const items = jsonArray(arena, arr_text) catch {return .{ .fail = .{ .text = try std.fmt.allocPrint(ctx.gpa, "rlm: each() needs a JSON array (got {s})", .{arr_text}), .is_error = true } };};@@ -176,7 +183,7 @@ pub fn evalEach(for (items, 0..) |item, i| {if (i > 0) try out.append(arena, ',');const val = fieldValue(arena, item, field) catch {- return .{ .fail = .{ .text = try std.fmt.allocPrint(ctx.gpa, "rlm: each() item missing field {s}", .{field}), .is_error = true } };+ return .{ .fail = .{ .text = try missingField(ctx.gpa, arena, item, field), .is_error = true } };};const args = try std.fmt.allocPrint(arena, "{{\"arg\":{s}}}", .{val});const call: spec_ptc.Call = .{ .name = tool, .args_json = args };@@ -196,7 +203,7 @@ pub fn evalEach(return .ok;}-const Each = struct { assign: ?[]const u8, arr: []const u8, tool: []const u8, field: []const u8 };+const Each = struct { assign: ?[]const u8, arr: []const u8, tool: []const u8, field: ?[]const u8 };fn parseEach(arena: Allocator, stmt: []const u8) ?Each {const trimmed = std.mem.trim(u8, stmt, " \t");@@ -212,8 +219,8 @@ fn parseEach(arena: Allocator, stmt: []const u8) ?Each {if (!std.mem.startsWith(u8, rest, "each(") or rest[rest.len - 1] != ')') return null;const inner = rest["each(".len .. rest.len - 1];const parts = spec_ptc.splitTopLevel(arena, inner, ',') catch return null;- if (parts.len != 3) return null;- return .{ .assign = assign, .arr = parts[0], .tool = parts[1], .field = parts[2] };+ if (parts.len != 2 and parts.len != 3) return null;+ return .{ .assign = assign, .arr = parts[0], .tool = parts[1], .field = if (parts.len == 3) parts[2] else null };}fn resolveBind(binds: []const rlm_spec.Binding, name: []const u8) ?[]const u8 {@@ -245,15 +252,29 @@ fn jsonArray(arena: Allocator, text: []const u8) ![]const Value {return error.NotArray;}-fn fieldValue(arena: Allocator, item: Value, field: []const u8) ![]const u8 {- if (item != .object) return error.NotObject;- const v = item.object.get(field) orelse return error.Missing;+/// A plain item is the value itself; an object item gives up `field`.+fn fieldValue(arena: Allocator, item: Value, field: ?[]const u8) ![]const u8 {+ const v = if (item == .object) item.object.get(field orelse return error.NoField) orelse return error.Missing else item;var aw: std.Io.Writer.Allocating = .init(arena);var s: std.json.Stringify = .{ .writer = &aw.writer };try s.write(v);return aw.toOwnedSlice();}+/// Name the fields an object item does have, so the next try can pick one.+fn missingField(gpa: Allocator, arena: Allocator, item: Value, field: ?[]const u8) ![]u8 {+ var keys: std.ArrayList(u8) = .empty;+ if (item == .object) {+ var it = item.object.iterator();+ while (it.next()) |e| {+ if (keys.items.len > 0) try keys.appendSlice(arena, ", ");+ try keys.appendSlice(arena, e.key_ptr.*);+ }+ }+ if (field) |f| return std.fmt.allocPrint(gpa, "rlm: each() item has no field {s} (its fields: {s}); pass the one that holds the value, e.g. each(arr, tool, \"id\")", .{ f, keys.items });+ return std.fmt.allocPrint(gpa, "rlm: each() over objects needs the field to pass (its fields: {s}), e.g. each(arr, tool, \"id\")", .{keys.items});+}+fn jsonOrString(arena: Allocator, text: []const u8) []const u8 {const trimmed = std.mem.trim(u8, text, " \t\r\n");if (std.json.parseFromSlice(Value, arena, trimmed, .{})) |_| {diff --git a/src/rlm_order_tests.zig b/src/rlm_order_tests.zigindex 068f3aba..28754cc2 100644--- a/src/rlm_order_tests.zig+++ b/src/rlm_order_tests.zig@@ -5,11 +5,11 @@ const tools = @import("tools.zig");const gpa = std.testing.allocator;const io = std.testing.io;-const Fixture = struct {+pub const Fixture = struct { // shared with rlm_literal_tests.zigtmp: std.testing.TmpDir,client: std.http.Client,cwd: []u8,- fn init() !Fixture {+ pub fn init() !Fixture {var tmp = std.testing.tmpDir(.{});errdefer tmp.cleanup();try tmp.dir.writeFile(io, .{ .sub_path = "target.txt", .data = "old" });@@ -17,10 +17,10 @@ const Fixture = struct {const n = try tmp.dir.realPath(io, &buf);return .{ .tmp = tmp, .client = .{ .allocator = gpa, .io = io }, .cwd = try gpa.dupe(u8, buf[0..n]) };}- fn ctx(self: *Fixture) tools.ToolCtx {+ pub fn ctx(self: *Fixture) tools.ToolCtx {return .{ .gpa = gpa, .io = io, .client = &self.client, .provider = undefined, .registry = null, .from_sub = false, .approvals = null, .tracer = null, .agent_cwd = self.cwd };}- fn deinit(self: *Fixture) void {+ pub fn deinit(self: *Fixture) void {@import("jobs.zig").jobsReap(gpa, io);@import("job_notify.zig").resetForTest(io);rlm.resetLive(gpa, io);@@ -28,7 +28,7 @@ const Fixture = struct {gpa.free(self.cwd);self.tmp.cleanup();}- fn run(self: *Fixture, code: []const u8) !tools.ToolOutput {+ pub fn run(self: *Fixture, code: []const u8) !tools.ToolOutput {return rlm.runScript(self.ctx(), code);}};diff --git a/src/spec_ptc.zig b/src/spec_ptc.zigindex ddb27198..e745fb5e 100644--- a/src/spec_ptc.zig+++ b/src/spec_ptc.zig@@ -203,6 +203,7 @@ const Value = union(enum) {string: []const u8,int: i64,bool: bool,+ json: []const u8, // ADR 0236: a JSON array/object literal, canonical text};fn parseArgs(arena: Allocator, tool: []const u8, inner: []const u8, binds: []const Bound) !?[]const u8 {@@ -246,6 +247,13 @@ fn parseValue(arena: Allocator, src: []const u8, i: *usize, binds: []const Boundif (c == '-' or (c >= '0' and c <= '9')) return parseInt(src, i);if (takeWord(src, i, "true")) return .{ .bool = true };if (takeWord(src, i, "false")) return .{ .bool = false };+ if (c == '[' or c == '{') {+ const literal = @import("rlm_literal.zig");+ const end = literal.scan(src, i.*) orelse return null;+ const json = literal.render(arena, src[i.*..end], binds) catch return null;+ i.* = end;+ return .{ .json = json };+ }if (!identStart(c)) return null;var end = i.* + 1;while (end < src.len and identCont(src[end])) end += 1;@@ -331,7 +339,11 @@ fn canonicalize(arena: Allocator, tool: []const u8, args: []const Arg) ![]const// how a script saves a bound result for the shell to compute over.if (positional and args.len == 2 and std.mem.eql(u8, tool, "write_file")) {try putValue(arena, &obj, field, args[0].value);- try putValue(arena, &obj, "content", args[1].value);+ // The file holds the literal's JSON text.+ try putValue(arena, &obj, "content", switch (args[1].value) {+ .json => |j| .{ .string = j },+ else => |v| v,+ });} else if (args.len == 1 and args[0].key == null) {try putValue(arena, &obj, field, args[0].value);} else {@@ -387,11 +399,11 @@ pub fn splitStatements(arena: Allocator, src: []const u8) ![][]const u8 {in_str = c;continue;}- if (c == '(') {+ if (c == '(' or c == '[' or c == '{') {depth += 1;continue;}- if (c == ')' and depth > 0) {+ if ((c == ')' or c == ']' or c == '}') and depth > 0) {depth -= 1;continue;}@@ -432,11 +444,11 @@ pub fn splitTopLevel(arena: Allocator, src: []const u8, sep: u8) ![][]const u8 {in_str = c;continue;}- if (c == '(') {+ if (c == '(' or c == '[' or c == '{') {depth += 1;continue;}- if (c == ')' and depth > 0) {+ if ((c == ')' or c == ']' or c == '}') and depth > 0) {depth -= 1;continue;}@@ -456,6 +468,7 @@ fn putValue(arena: Allocator, obj: *std.json.ObjectMap, key: []const u8, v: Valu.string => |s| .{ .string = s },.int => |n| .{ .integer = n },.bool => |b| .{ .bool = b },+ .json => |j| try std.json.parseFromSliceLeaky(std.json.Value, arena, j, .{}),};try obj.put(arena, key, jv);}diff --git a/src/test_hooks.zig b/src/test_hooks.zigindex 45585fbd..555326d4 100644--- a/src/test_hooks.zig+++ b/src/test_hooks.zig@@ -404,4 +404,6 @@ test {_ = @import("output_elide.zig"); // #1271: over-cap output keeps head and tail_ = @import("graff_dir.zig"); // #1273: .graff/.gitignore keeps run state out of git_ = @import("additional_tools.zig"); // ADR 0221: loaded tools as additional_tools items on the ChatGPT plan route+ _ = @import("rlm_literal.zig"); // ADR 0236: JSON literals in rlm scripts+ _ = @import("rlm_literal_tests.zig"); // ADR 0236: each() over plain items, literal binds and args}