Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

rlm: binds keep the whole MCP result (ADR 0238)

justrachcb18e9bec882
diff --git a/docs/adr/0238-rlm-binds-keep-the-whole-mcp-result.md b/docs/adr/0238-rlm-binds-keep-the-whole-mcp-result.md
new file mode 100644
index 00000000..bd4bb17b
--- /dev/null
+++ b/docs/adr/0238-rlm-binds-keep-the-whole-mcp-result.md
@@ -0,0 +1,51 @@
+# 0238. rlm binds keep the whole MCP result
+
+Status: proposed 2026-10-02
+
+## Context
+
+ADR 0029 slims large MCP list results in `exec.zig`: rows keep only
+`id`/`identifier`/`title`/`name`, and a comment list becomes
+`{n, latest_author}`. ADR 0225 kept the full payload behind a handle for a
+direct call, but an rlm host call still bound the plain cut, so a script
+could never reach a dropped field. The rule told the model to call the tool
+directly instead.
+
+That cut fits a report of ids, titles and comment counts. Tasks that need
+any other field, such as rolling issues up by priority or filtering them by
+estimate, could not stay inside rlm. The model left the script, called the
+tool directly, then paged the handle with `read_tool_result`, whose 16 KB
+slices each end with a `[a..b of N bytes]` marker. Joining the slices
+broke the JSON, and the model spent more calls repairing it. A harness
+whose code mode hands the script the whole result finished the same tasks
+in fewer calls.
+
+Slimming exists to keep large payloads out of the conversation. A bind is
+not in the conversation: only `print()` output is.
+
+## Decision
+
+- An MCP call from inside an rlm script (`ToolCtx.rlm_host`) binds the
+ tool's whole result. `exec.zig` no longer slims it.
+- `print()` still shows the slim view (`rlm.maybeSlim`). An `each()` bind,
+ one whole result per item, prints each item's own cut, the view the
+ per-item slim used to bind.
+- `project(x, field)` reads any field of a row. Over an `each()` bind of
+ comment lists, `n` and `latest_author` read the fold, so scripts written
+ against the printed view keep working.
+- `write_file("f.json", x)` saves the whole result for a shell script.
+- Inside rlm, `read_tool_result("tr_N")` with no range binds the whole
+ stored result (its one positional argument is the handle), and a direct
+ call's slim marker says so, so a model that called the tool directly can
+ still bring every field into a script.
+- The slim rule on load results says so: in rlm a bind keeps every field.
+- The `each()` "needs a JSON array" error echoes at most 200 bytes of its
+ argument, so a whole result never lands in the conversation by mistake.
+
+## Consequences
+
+Binds hold more bytes in memory for the rest of the session. Nothing new
+reaches the model unless a script prints it, and printing applies the same
+cut as before. A direct call is unchanged: slimmed, with the full result
+behind a handle. Revisit if a model starts printing whole binds that slim
+cannot cut.
diff --git a/docs/adr/README.md b/docs/adr/README.md
index a9b948c3..98265dc7 100644
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@ -264,6 +264,7 @@ record only when you need the evidence or the edge cases.
| [0235](0235-mimo-thinks-only-at-high-effort.md) | MiMo thinks only at `high` and above: `minimal`/`low`/`medium` (graff's default) send thinking Off on the Chat and Responses wires; On, a worker pin, or Jev's `high` turn it on. Replaces ADR 0192's "positive values mean On" below high. |
| [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. |
| [0237](0237-deepseek-thinks-at-its-low-level-by-default.md) | DeepSeek's default effort (`medium`) sends `reasoning_effort: low` with thinking enabled; `/effort low` still turns thinking off (ADR 0054), and `high` and above are unchanged. |
+| [0238](0238-rlm-binds-keep-the-whole-mcp-result.md) | An MCP call inside an rlm script binds the whole result; `print()` shows the slim view (each item of an `each()` bind cut on its own), `project(x, field)` reads any field (`n`/`latest_author` through a comment list's fold), and `write_file` saves the whole result; inside rlm, `read_tool_result("tr_N")` with no range binds a stored result whole. |
## When to write one
diff --git a/src/exec.zig b/src/exec.zig
index 7e4d320c..314a9518 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -189,8 +189,10 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {
if (!r.is_error) {
const shapes = @import("mcp_shapes.zig");
shapes.remember(ctx, call.name, r.text);
- const keep_in: ?@import("tool_handle.zig").Target = if (ctx.rlm_host) null else .{ .io = io, .dir = .cwd() };
- return .{ .text = shapes.takeSlimKept(gpa, keep_in, r.text), .is_error = false };
+ // ADR 0238: an rlm bind keeps every field. rlm slims only what it
+ // prints, so a script can still project or save a dropped field.
+ if (ctx.rlm_host) return .{ .text = r.text, .is_error = false };
+ return .{ .text = shapes.takeSlimKept(gpa, .{ .io = io, .dir = .cwd() }, r.text), .is_error = false };
}
return .{ .text = r.text, .is_error = r.is_error };
}
diff --git a/src/mcp_shapes.zig b/src/mcp_shapes.zig
index 5dd59355..4376e347 100644
--- a/src/mcp_shapes.zig
+++ b/src/mcp_shapes.zig
@@ -295,7 +295,8 @@ pub const slim_min_bytes: usize = 800;
const identity_keys = [_][]const u8{ "id", "identifier", "title", "name" };
/// Learnt projection: identity keys on issue-like rows; comments fold to
-/// `{n, latest_author}`. Values of `description`/`body` never survive.
+/// `{n, latest_author}`. Values of `description`/`body` never survive. An
+/// each() bind (one whole result per item, ADR 0238) is cut item by item.
/// Returns null when the payload is small, not a JSON array of objects, or
/// has nothing to cut. Caller owns a non-null result.
pub fn slim(alloc: Allocator, payload: []const u8) ?[]u8 {
@@ -304,11 +305,59 @@ pub fn slim(alloc: Allocator, payload: []const u8) ?[]u8 {
const parsed = std.json.parseFromSlice(Value, alloc, trimmed, .{}) catch return null;
defer parsed.deinit();
const items = arrayItems(parsed.value) orelse return null;
+ if (items.len == 0) return null;
+ if (items[0] == .array or wrapsRows(items[0])) return slimEach(alloc, items);
+ if (items[0] != .object) return null;
+ return slimRows(alloc, items);
+}
+
+/// An each() item that is a whole `{"comments": [...]}`-style result, not a row.
+fn wrapsRows(v: Value) bool {
+ if (v != .object or hasIdentity(v.object) or looksLikeComments(v.object)) return false;
+ return arrayItems(v) != null;
+}
+
+fn slimRows(alloc: Allocator, items: []const Value) ?[]u8 {
if (items.len == 0 or items[0] != .object) return null;
if (looksLikeComments(items[0].object)) return slimComments(alloc, items);
return slimIdentity(alloc, items);
}
+/// ADR 0238: print() of an each() bind shows each item's own cut, the view
+/// the per-item slim used to bind. Null when no item had anything to cut.
+fn slimEach(alloc: Allocator, items: []const Value) ?[]u8 {
+ var aw: Io.Writer.Allocating = .init(alloc);
+ defer aw.deinit();
+ var cut_any = false;
+ aw.writer.writeByte('[') catch return null;
+ for (items, 0..) |item, i| {
+ if (i > 0) aw.writer.writeByte(',') catch return null;
+ if (slimRows(alloc, arrayItems(item) orelse &[_]Value{})) |cut| {
+ defer alloc.free(cut);
+ aw.writer.writeAll(cut) catch return null;
+ cut_any = true;
+ continue;
+ }
+ var s: std.json.Stringify = .{ .writer = &aw.writer };
+ s.write(item) catch return null;
+ }
+ aw.writer.writeByte(']') catch return null;
+ if (!cut_any) return null;
+ return aw.toOwnedSlice() catch null;
+}
+
+/// ADR 0238: project(x, "n" | "latest_author") over an each() bind reads a
+/// comment list through its fold, so the printed view's fields stay
+/// projectable. Null for any other field or a list that is not comments.
+pub fn foldField(item: Value, field: []const u8) ?Value {
+ const items = arrayItems(item) orelse return null;
+ if (items.len > 0 and (items[0] != .object or !looksLikeComments(items[0].object))) return null;
+ if (std.mem.eql(u8, field, "n")) return .{ .integer = @intCast(items.len) };
+ if (!std.mem.eql(u8, field, "latest_author")) return null;
+ if (items.len == 0) return .null;
+ return .{ .string = authorName(items[latestIndex(items)]) orelse "" };
+}
+
/// Remember the fat payload, then replace it with the learnt cut when one
/// exists. `text` is owned by `gpa`.
pub fn takeSlim(gpa: Allocator, text: []u8) []u8 {
@@ -319,8 +368,8 @@ pub fn takeSlim(gpa: Allocator, text: []u8) []u8 {
/// ADR 0225: `takeSlim`, lossless. With `keep_in` the full payload is kept as
/// a tool-result handle that the slim result names, so a dropped field is one
-/// read_tool_result away. Null (an rlm bind, which each/project/write_file
-/// parse as JSON) or a failed write returns the plain cut.
+/// read_tool_result away. Null or a failed write returns the plain cut. An
+/// rlm bind never comes here: it keeps the whole result (ADR 0238).
pub fn takeSlimKept(gpa: Allocator, keep_in: ?tool_handle.Target, text: []u8) []u8 {
const cut = slim(gpa, text) orelse return text;
defer gpa.free(text);
@@ -328,7 +377,7 @@ pub fn takeSlimKept(gpa: Allocator, keep_in: ?tool_handle.Target, text: []u8) []
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const path = tool_handle.keep(arena_state.allocator(), target, text) orelse return cut;
- const out = std.fmt.allocPrint(gpa, "{s}\n[slimmed from {d} bytes; the full result is handle {s} (read_tool_result reads any dropped field)]", .{ cut, text.len, tool_handle.idOf(path) }) catch return cut;
+ const out = std.fmt.allocPrint(gpa, "{s}\n[slimmed from {d} bytes; the full result is handle {s} (read_tool_result reads any dropped field; in rlm, x = read_tool_result(\"{s}\") binds it whole for project() or write_file())]", .{ cut, text.len, tool_handle.idOf(path), tool_handle.idOf(path) }) catch return cut;
gpa.free(cut);
return out;
}
@@ -347,7 +396,7 @@ fn looksLikeComments(obj: std.json.ObjectMap) bool {
return obj.get("body") != null and obj.get("author") != null;
}
-fn slimComments(alloc: Allocator, items: []const Value) ?[]u8 {
+fn latestIndex(items: []const Value) usize {
var latest_i: usize = items.len - 1;
var latest_at: []const u8 = "";
for (items, 0..) |item, i| {
@@ -359,7 +408,11 @@ fn slimComments(alloc: Allocator, items: []const Value) ?[]u8 {
latest_i = i;
}
}
- const name = authorName(items[latest_i]);
+ return latest_i;
+}
+
+fn slimComments(alloc: Allocator, items: []const Value) ?[]u8 {
+ const name = authorName(items[latestIndex(items)]);
var aw: Io.Writer.Allocating = .init(alloc);
var s: std.json.Stringify = .{ .writer = &aw.writer };
s.beginObject() catch {
@@ -466,8 +519,8 @@ pub fn annotate(gpa: Allocator, arena: Allocator, io: Io, cwd: ?[]const u8, text
/// ADR 0225: what `slim` (exec.zig) does to every large list result, said
/// where the model first meets the tools. Unsaid, the model wrote code for
/// the full rows, failed on the first missing field, and spent calls finding
-/// the real shape.
-pub const slim_rule = "\nLarge list results from these tools come back slimmed: rows keep only id/identifier/title/name, and a comment list becomes {\"n\": count, \"latest_author\": name}. Code against those fields. A direct call's result names a handle holding the full result; rlm binds stay slimmed.";
+/// the real shape. ADR 0238: rlm binds keep every field; only print() slims.
+pub const slim_rule = "\nLarge list results from these tools are shown slimmed: rows keep only id/identifier/title/name, and a comment list becomes {\"n\": count, \"latest_author\": name}. A direct call's result names a handle holding the full result. In rlm a bind keeps every field: print() shows the slim view, project(x, field) reads any field, and write_file(\"f.json\", x) saves the whole result for a script.";
pub fn lookup(io: Io, name: []const u8) ?[]const u8 {
store.mu.lockUncancelable(io);
@@ -490,92 +543,3 @@ test "shutdown frees a filled cache and is a no-op when empty (#1196)" {
try std.testing.expect(store.gpa == null);
shutdown(io);
}
-
-test "infer strips values and keeps keys plus broad types" {
- const gpa = std.testing.allocator;
- var arena_state = std.heap.ArenaAllocator.init(gpa);
- defer arena_state.deinit();
- const a = arena_state.allocator();
- const shape = try infer(a,
- \\[{"id":"ISS-1","title":"Login","n":3,"ok":true,"meta":{"x":1},"tags":["a"]}]
- );
- try std.testing.expect(std.mem.indexOf(u8, shape, "ISS-1") == null);
- try std.testing.expect(std.mem.indexOf(u8, shape, "Login") == null);
- try std.testing.expect(std.mem.indexOf(u8, shape, "\"id\"") != null);
- try std.testing.expect(std.mem.indexOf(u8, shape, "string") != null);
- try std.testing.expect(std.mem.indexOf(u8, shape, "number") != null);
- try std.testing.expect(std.mem.indexOf(u8, shape, "bool") != null);
-}
-
-test "remember merges keys; annotate splices shapes; prefix text is untouched" {
- const gpa = std.testing.allocator;
- const io = std.testing.io;
- var tmp = std.testing.tmpDir(.{});
- defer tmp.cleanup();
- var path_buf: [std.fs.max_path_bytes]u8 = undefined;
- const n = try tmp.dir.realPath(io, &path_buf);
- const dir = path_buf[0..n];
- reset(gpa, io);
- defer reset(gpa, io);
- var dummy_client: std.http.Client = .{ .allocator = gpa, .io = io };
- defer dummy_client.deinit();
- const ctx: ToolCtx = .{
- .gpa = gpa,
- .io = io,
- .client = &dummy_client,
- .provider = undefined,
- .registry = null,
- .from_sub = false,
- .approvals = null,
- .tracer = null,
- .agent_cwd = dir,
- };
- remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"A\",\"title\":\"t\"}]");
- remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"B\",\"state\":\"open\"}]");
- const hit = lookup(io, "mcp__linear__list_issues") orelse return error.MissingShape;
- try std.testing.expect(std.mem.indexOf(u8, hit, "\"id\"") != null);
- try std.testing.expect(std.mem.indexOf(u8, hit, "\"title\"") != null);
- try std.testing.expect(std.mem.indexOf(u8, hit, "\"state\"") != null);
- try std.testing.expect(std.mem.indexOf(u8, hit, "\"A\"") == null);
- var arena_state = std.heap.ArenaAllocator.init(gpa);
- defer arena_state.deinit();
- const annotated = try annotate(gpa, arena_state.allocator(), io, dir, "1 tool schema(s) below\nmcp__linear__list_issues");
- try std.testing.expect(std.mem.indexOf(u8, annotated, "return_shapes") != null);
- try std.testing.expect(std.mem.indexOf(u8, annotated, "mcp__linear__list_issues") != null);
- try std.testing.expect(std.mem.indexOf(u8, annotated, "muscle:") == null);
- try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "return_shapes") == null);
- try std.testing.expect(std.mem.indexOf(u8, @import("rlm.zig").tool_desc, "muscle:") == null);
-}
-
-test "annotate writes a muscle playbook once two MCP shapes are stored" {
- const gpa = std.testing.allocator;
- const io = std.testing.io;
- var tmp = std.testing.tmpDir(.{});
- defer tmp.cleanup();
- var path_buf: [std.fs.max_path_bytes]u8 = undefined;
- const n = try tmp.dir.realPath(io, &path_buf);
- const dir = path_buf[0..n];
- reset(gpa, io);
- defer reset(gpa, io);
- var dummy_client: std.http.Client = .{ .allocator = gpa, .io = io };
- defer dummy_client.deinit();
- const ctx: ToolCtx = .{
- .gpa = gpa,
- .io = io,
- .client = &dummy_client,
- .provider = undefined,
- .registry = null,
- .from_sub = false,
- .approvals = null,
- .tracer = null,
- .agent_cwd = dir,
- };
- remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"A\"}]");
- remember(ctx, "mcp__linear__list_comments", "[{\"body\":\"b\",\"author\":\"ada\"}]");
- var arena_state = std.heap.ArenaAllocator.init(gpa);
- defer arena_state.deinit();
- const annotated = try annotate(gpa, arena_state.allocator(), io, dir, "2 tool schema(s)");
- try std.testing.expect(std.mem.indexOf(u8, annotated, "muscle:") != null);
- try std.testing.expect(std.mem.indexOf(u8, annotated, "each(") != null);
- try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "muscle:") == null);
-}
diff --git a/src/mcp_shapes_tests.zig b/src/mcp_shapes_tests.zig
index 3428dafe..cf5e318f 100644
--- a/src/mcp_shapes_tests.zig
+++ b/src/mcp_shapes_tests.zig
@@ -18,7 +18,7 @@ test "annotate states the slim rule on every load result, stored shapes or not"
try std.testing.expect(std.mem.startsWith(u8, annotated, "1 tool schema(s) below"));
try std.testing.expect(std.mem.indexOf(u8, annotated, "latest_author") != null);
try std.testing.expect(std.mem.indexOf(u8, annotated, "return_shapes") == null); // nothing stored yet
- try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "come back slimmed") == null); // never the prefix
+ try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "shown slimmed") == null); // never the prefix
}
test "slim drops description/body; comments fold to n and latest_author" {
@@ -48,7 +48,7 @@ test "slim drops description/body; comments fold to n and latest_author" {
try std.testing.expect(std.mem.indexOf(u8, folded, pad_s) == null);
}
-test "a direct call's slim result names a handle holding the full payload; an rlm bind stays plain JSON" {
+test "a direct call's slim result names a handle holding the full payload; with no handle target the cut stays plain JSON" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
@@ -76,3 +76,112 @@ test "a direct call's slim result names a handle holding the full payload; an rl
defer parsed.deinit();
try std.testing.expect(std.mem.indexOf(u8, bind, "handle") == null);
}
+
+test "infer strips values and keeps keys plus broad types" {
+ const gpa = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const shape = try shapes.infer(a,
+ \\[{"id":"ISS-1","title":"Login","n":3,"ok":true,"meta":{"x":1},"tags":["a"]}]
+ );
+ try std.testing.expect(std.mem.indexOf(u8, shape, "ISS-1") == null);
+ try std.testing.expect(std.mem.indexOf(u8, shape, "Login") == null);
+ try std.testing.expect(std.mem.indexOf(u8, shape, "\"id\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, shape, "string") != null);
+ try std.testing.expect(std.mem.indexOf(u8, shape, "number") != null);
+ try std.testing.expect(std.mem.indexOf(u8, shape, "bool") != null);
+}
+
+test "remember merges keys; annotate splices shapes; prefix text is untouched" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var path_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const n = try tmp.dir.realPath(io, &path_buf);
+ const dir = path_buf[0..n];
+ shapes.reset(gpa, io);
+ defer shapes.reset(gpa, io);
+ var dummy_client: std.http.Client = .{ .allocator = gpa, .io = io };
+ defer dummy_client.deinit();
+ const ctx: @import("tools.zig").ToolCtx = .{
+ .gpa = gpa,
+ .io = io,
+ .client = &dummy_client,
+ .provider = undefined,
+ .registry = null,
+ .from_sub = false,
+ .approvals = null,
+ .tracer = null,
+ .agent_cwd = dir,
+ };
+ shapes.remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"A\",\"title\":\"t\"}]");
+ shapes.remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"B\",\"state\":\"open\"}]");
+ const hit = shapes.lookup(io, "mcp__linear__list_issues") orelse return error.MissingShape;
+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"id\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"title\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"state\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"A\"") == null);
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const annotated = try shapes.annotate(gpa, arena_state.allocator(), io, dir, "1 tool schema(s) below\nmcp__linear__list_issues");
+ try std.testing.expect(std.mem.indexOf(u8, annotated, "return_shapes") != null);
+ try std.testing.expect(std.mem.indexOf(u8, annotated, "mcp__linear__list_issues") != null);
+ try std.testing.expect(std.mem.indexOf(u8, annotated, "muscle:") == null);
+ try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "return_shapes") == null);
+ try std.testing.expect(std.mem.indexOf(u8, @import("rlm.zig").tool_desc, "muscle:") == null);
+}
+
+test "annotate writes a muscle playbook once two MCP shapes are stored" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var path_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const n = try tmp.dir.realPath(io, &path_buf);
+ const dir = path_buf[0..n];
+ shapes.reset(gpa, io);
+ defer shapes.reset(gpa, io);
+ var dummy_client: std.http.Client = .{ .allocator = gpa, .io = io };
+ defer dummy_client.deinit();
+ const ctx: @import("tools.zig").ToolCtx = .{
+ .gpa = gpa,
+ .io = io,
+ .client = &dummy_client,
+ .provider = undefined,
+ .registry = null,
+ .from_sub = false,
+ .approvals = null,
+ .tracer = null,
+ .agent_cwd = dir,
+ };
+ shapes.remember(ctx, "mcp__linear__list_issues", "[{\"id\":\"A\"}]");
+ shapes.remember(ctx, "mcp__linear__list_comments", "[{\"body\":\"b\",\"author\":\"ada\"}]");
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const annotated = try shapes.annotate(gpa, arena_state.allocator(), io, dir, "2 tool schema(s)");
+ try std.testing.expect(std.mem.indexOf(u8, annotated, "muscle:") != null);
+ try std.testing.expect(std.mem.indexOf(u8, annotated, "each(") != null);
+ try std.testing.expect(std.mem.indexOf(u8, @import("mcp_schema_gate.zig").tool_desc, "muscle:") == null);
+}
+
+test "print of an each() bind cuts every item; a bind that slim cannot cut prints whole (ADR 0238)" {
+ const gpa = std.testing.allocator;
+ const pad: [400]u8 = @splat('x');
+ const pad_s: []const u8 = &pad;
+ const each_bind = try std.fmt.allocPrint(gpa, "[[{{\"body\":\"{s}\",\"author\":{{\"name\":\"ada\"}},\"createdAt\":\"2026-08-11\"}},{{\"body\":\"{s}\",\"author\":{{\"name\":\"bev\"}},\"createdAt\":\"2026-08-12\"}}],[{{\"body\":\"{s}\",\"author\":{{\"name\":\"cam\"}},\"createdAt\":\"2026-08-13\"}}]]", .{ pad_s, pad_s, pad_s });
+ defer gpa.free(each_bind);
+ const shown = shapes.slim(gpa, each_bind) orelse return error.ExpectedEachSlim;
+ defer gpa.free(shown);
+ try std.testing.expectEqualStrings("[{\"n\":2,\"latest_author\":\"bev\"},{\"n\":1,\"latest_author\":\"cam\"}]", shown);
+ const plain = try std.fmt.allocPrint(gpa, "[[1,2,3],[\"{s}\",\"{s}\"]]", .{ pad_s, pad_s });
+ defer gpa.free(plain);
+ try std.testing.expect(shapes.slim(gpa, plain) == null);
+}
+
+test "the slim rule says an rlm bind keeps every field (ADR 0238)" {
+ try std.testing.expect(std.mem.indexOf(u8, shapes.slim_rule, "keeps every field") != null);
+ try std.testing.expect(std.mem.indexOf(u8, shapes.slim_rule, "binds stay slimmed") == null);
+ try std.testing.expect(std.mem.indexOf(u8, shapes.slim_rule, "write_file") != null);
+}
diff --git a/src/result_read.zig b/src/result_read.zig
index 5ae07e26..516cf173 100644
--- a/src/result_read.zig
+++ b/src/result_read.zig
@@ -116,6 +116,9 @@ pub fn exec(ctx: tools.ToolCtx, call: tools.ToolCall) !tools.ToolOutput {
break :blk search(arena, stored, q) catch
return .{ .text = try gpa.dupe(u8, "read_tool_result: query must not be empty"), .is_error = true };
} else blk: {
+ // ADR 0238: an rlm bind is not in the conversation, so a read with no
+ // range binds the whole stored result; print() still shows the slim view.
+ if (ctx.rlm_host and tools.intField(call.input, "offset") == null and tools.intField(call.input, "limit") == null) break :blk stored;
const off = clampOffset(tools.intField(call.input, "offset"), stored.len);
const lim = clampLimit(tools.intField(call.input, "limit"));
break :blk try sliceText(arena, stored, off, lim);
diff --git a/src/rlm_mcp.zig b/src/rlm_mcp.zig
index 8ce25f10..447d9f6b 100644
--- a/src/rlm_mcp.zig
+++ b/src/rlm_mcp.zig
@@ -176,7 +176,7 @@ pub fn evalEach(
const tool = stripQuotes(parsed.tool);
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 } };
+ return .{ .fail = .{ .text = try std.fmt.allocPrint(ctx.gpa, "rlm: each() needs a JSON array (got {s})", .{arr_text[0..@min(arr_text.len, 200)]}), .is_error = true } };
};
var out: std.ArrayList(u8) = .empty;
try out.append(arena, '[');
diff --git a/src/rlm_reduce.zig b/src/rlm_reduce.zig
index 7823050d..e0982cb9 100644
--- a/src/rlm_reduce.zig
+++ b/src/rlm_reduce.zig
@@ -11,6 +11,7 @@ const Allocator = std.mem.Allocator;
const spec_ptc = @import("spec_ptc.zig");
const rlm_spec = @import("rlm_spec.zig");
const rlm_mcp = @import("rlm_mcp.zig");
+const mcp_shapes = @import("mcp_shapes.zig");
pub const StmtHit = rlm_mcp.StmtHit;
@@ -101,8 +102,13 @@ fn jsonArray(arena: Allocator, text: []const u8) ![]const Value {
}
fn fieldValue(arena: Allocator, item: Value, field: []const u8) ![]const u8 {
- if (item != .object) return error.Miss;
- const v = item.object.get(field) orelse return error.Miss;
+ 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,
+ };
var aw: std.Io.Writer.Allocating = .init(arena);
var s: std.json.Stringify = .{ .writer = &aw.writer };
try s.write(v);
@@ -121,6 +127,21 @@ test "len() counts a JSON array bind" {
try std.testing.expectEqualStrings("3", bind_out.items[0].text);
}
+test "project() reads a field the printed view drops, and n/latest_author off an each() bind (ADR 0238)" {
+ const gpa = std.testing.allocator;
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const binds = [_]rlm_spec.Binding{
+ .{ .name = "issues", .text = "[{\"id\":\"ISS-1\",\"priority\":2,\"estimate\":3},{\"id\":\"ISS-2\",\"priority\":1,\"estimate\":4}]" },
+ .{ .name = "comments", .text = "[[{\"body\":\"a\",\"author\":{\"name\":\"ada\"},\"createdAt\":\"2026-08-11\"},{\"body\":\"b\",\"author\":{\"name\":\"bev\"},\"createdAt\":\"2026-08-12\"}],[]]" },
+ };
+ try 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));
+}
+
test "project() extracts one field; print(len()) resolves" {
const gpa = std.testing.allocator;
var arena_state = std.heap.ArenaAllocator.init(gpa);
diff --git a/src/spec_ptc.zig b/src/spec_ptc.zig
index 7dd17afe..c858c332 100644
--- a/src/spec_ptc.zig
+++ b/src/spec_ptc.zig
@@ -373,6 +373,7 @@ fn hostField(tool: []const u8) []const u8 {
if (std.mem.eql(u8, tool, "bash") or std.mem.eql(u8, tool, "shell")) return "command";
if (std.mem.eql(u8, tool, "webfetch")) return "url";
if (std.mem.eql(u8, tool, "subagent")) return "prompt";
+ if (std.mem.eql(u8, tool, "read_tool_result")) return "handle";
return "arg";
}
@@ -590,4 +591,6 @@ test "extractCallBound: a bare bound name passes its text; write_file takes path
// argument is `command`, as the tool description tells scripts to use it.
const sh = (try extractCallBound(a, "r = shell(\"python3 x.py\")", &binds)).?;
try std.testing.expectEqualStrings("{\"command\":\"python3 x.py\"}", sh.args_json);
+ // read_tool_result's one positional argument is its handle (ADR 0238).
+ try std.testing.expectEqualStrings("{\"handle\":\"tr_0\"}", (try extractCallBound(a, "h = read_tool_result(\"tr_0\")", &binds)).?.args_json);
}
diff --git a/src/tools.zig b/src/tools.zig
index 351a97cb..314d20a7 100644
--- a/src/tools.zig
+++ b/src/tools.zig
@@ -125,7 +125,7 @@ pub const ToolCtx = struct {
registry: ?*mcp.Registry,
mcp_context: ?@import("mcp_turn_context.zig").Snapshot = null,
from_sub: bool,
- rlm_host: bool = false, // ADR 0225: a host call from inside an rlm script (its MCP result stays plain slim JSON)
+ rlm_host: bool = false, // ADR 0238: a host call from inside an rlm script (its MCP result binds whole; rlm slims what it prints)
has_eval: bool = false, // the root's --eval loop: escalation's strongest verifier
approvals: ?*Approvals,
plan_read_owner: ?*const anyopaque = null,